Skip to content

Commit dd3cf40

Browse files
committed
Scaffold basic file structure for a new dialect
https://github.com/sqlalchemy/sqlalchemy/blob/main/README.dialects.rst Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent 6d62baa commit dd3cf40

File tree

8 files changed

+223
-2
lines changed

8 files changed

+223
-2
lines changed

poetry.lock

+140-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ thrift = "^0.13.0"
1414
pyarrow = "^5.0.0"
1515
pandas = "^1.3.0"
1616

17+
[tool.poetry.plugins."sqlalchemy.dialects"]
18+
"databricks.thrift" = "databricks.sqlalchemy:DatabricksDialect"
19+
1720
[tool.poetry.dev-dependencies]
1821
pytest = "^7.1.2"
1922
mypy = "^0.950"
2023
black = "^22.3.0"
24+
SQLAlchemy = "^1.4.39"
2125

2226
[build-system]
2327
requires = ["poetry-core>=1.0.0"]
@@ -28,4 +32,5 @@ ignore_missing_imports = "true"
2832
exclude = ['ttypes\.py$', 'TCLIService\.py$']
2933

3034
[tool.black]
31-
exclude = '/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|\.svn|_build|buck-out|build|dist|thrift_api)/'
35+
exclude = '/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|\.svn|_build|buck-out|build|dist|thrift_api)/'
36+

setup.cfg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[sqla_testing]
2+
3+
requirement_cls = databricks.sqlalchemy.requirements:Requirements
4+
profile_file = tests/sqlalchemy/profiles.txt

src/databricks/sqlalchemy/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from databricks.sqlalchemy.dialect import DatabricksDialect

src/databricks/sqlalchemy/dialect.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from databricks import sql
2+
from typing import AnyStr
3+
4+
from sqlalchemy import types
5+
from sqlalchemy import util
6+
7+
from sqlalchemy.engine import default
8+
9+
10+
class DatabricksDialect(default.DefaultDialect):
11+
12+
# Possible attributes are defined here: https://docs.sqlalchemy.org/en/14/core/internals.html#sqlalchemy.engine.Dialect
13+
name: str = "databricks"
14+
driver: str= "thrift"
15+
default_schema_name: str = "default"
16+
17+
@classmethod
18+
def dbapi(cls):
19+
return sql
20+
21+
def create_connect_args(self, url):
22+
# Expected URI format is: databricks+thrift://token:dapi***@***.cloud.databricks.com?http_path=/sql/***
23+
24+
kwargs = {
25+
"server_hostname": url.host,
26+
"access_token": url.password,
27+
"http_path": url.query.get("http_path")
28+
}
29+
30+
return [], kwargs
31+
32+
def get_table_names(self, *args, **kwargs):
33+
34+
# TODO: Implement with native driver `.tables()` call
35+
return super().get_table_names(*args, **kwargs)
36+
37+
def get_columns(self, *args, **kwargs):
38+
39+
# TODO: Implement with native driver `.columns()` call
40+
41+
return super().get_columns(*args, **kwargs)
42+
43+
def do_rollback(self, dbapi_connection):
44+
# Databricks SQL Does not support transaction
45+
pass
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Following official SQLAlchemy guide:
2+
#
3+
# https://github.com/sqlalchemy/sqlalchemy/blob/main/README.dialects.rst#dialect-layout
4+
#
5+
# The full group of requirements is available here:
6+
#
7+
# https://github.com/sqlalchemy/sqlalchemy/blob/a453256afc334acabee25ec275de555ef7287144/test/requirements.py
8+
9+
10+
from sqlalchemy.testing.requirements import SuiteRequirements
11+
from sqlalchemy.testing import exclusions
12+
13+
class Requirements(SuiteRequirements):
14+
15+
@property
16+
def two_phase_transactions(self):
17+
# Databricks SQL doesn't support transactions
18+
return exclusions.closed()

tests/sqlalchemy/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from sqlalchemy.dialects import registry
2+
import pytest
3+
4+
registry.register("databricks.thrift", "databricks.sqlalchemy", "DatabricksDialect")
5+
pytest.register_assert_rewrite("sqlalchemy.testing.assertions")
6+
7+
from sqlalchemy.testing.plugin.pytestplugin import *

tests/sqlalchemy/test_suite.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from sqlalchemy.testing.suite import *
2+

0 commit comments

Comments
 (0)