- Notifications
You must be signed in to change notification settings - Fork126
SQLAlchemy 2: add type compilation for all CamelCase types#238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
76 changes: 20 additions & 56 deletionssrc/databricks/sqlalchemy/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletionssrc/databricks/sqlalchemy/compiler.py
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
128 changes: 128 additions & 0 deletionssrc/databricks/sqlalchemy/test_local/test_types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import enum | ||
| import pytest | ||
| from sqlalchemy.types import ( | ||
| BigInteger, | ||
| Boolean, | ||
| Date, | ||
| DateTime, | ||
| Double, | ||
| Enum, | ||
| Float, | ||
| Integer, | ||
| Interval, | ||
| LargeBinary, | ||
| MatchType, | ||
| Numeric, | ||
| PickleType, | ||
| SchemaType, | ||
| SmallInteger, | ||
| String, | ||
| Text, | ||
| Time, | ||
| TypeEngine, | ||
| Unicode, | ||
| UnicodeText, | ||
| Uuid, | ||
| ) | ||
| from databricks.sqlalchemy import DatabricksDialect | ||
| class DatabricksDataType(enum.Enum): | ||
| """https://docs.databricks.com/en/sql/language-manual/sql-ref-datatypes.html""" | ||
| BIGINT = enum.auto() | ||
| BINARY = enum.auto() | ||
| BOOLEAN = enum.auto() | ||
| DATE = enum.auto() | ||
| DECIMAL = enum.auto() | ||
| DOUBLE = enum.auto() | ||
| FLOAT = enum.auto() | ||
| INT = enum.auto() | ||
| INTERVAL = enum.auto() | ||
| VOID = enum.auto() | ||
| SMALLINT = enum.auto() | ||
| STRING = enum.auto() | ||
| TIMESTAMP = enum.auto() | ||
| TIMESTAMP_NTZ = enum.auto() | ||
| TINYINT = enum.auto() | ||
| ARRAY = enum.auto() | ||
| MAP = enum.auto() | ||
| STRUCT = enum.auto() | ||
| # Defines the way that SQLAlchemy CamelCase types are compiled into Databricks SQL types. | ||
| # Note: I wish I could define this within the TestCamelCaseTypesCompilation class, but pytest doesn't like that. | ||
| camel_case_type_map = { | ||
| BigInteger: DatabricksDataType.BIGINT, | ||
| LargeBinary: DatabricksDataType.BINARY, | ||
| Boolean: DatabricksDataType.BOOLEAN, | ||
| Date: DatabricksDataType.DATE, | ||
| DateTime: DatabricksDataType.TIMESTAMP, | ||
| Double: DatabricksDataType.DOUBLE, | ||
| Enum: DatabricksDataType.STRING, | ||
| Float: DatabricksDataType.FLOAT, | ||
| Integer: DatabricksDataType.INT, | ||
| Interval: DatabricksDataType.TIMESTAMP, | ||
| Numeric: DatabricksDataType.DECIMAL, | ||
| PickleType: DatabricksDataType.BINARY, | ||
| SmallInteger: DatabricksDataType.SMALLINT, | ||
| String: DatabricksDataType.STRING, | ||
| Text: DatabricksDataType.STRING, | ||
| Time: DatabricksDataType.STRING, | ||
| Unicode: DatabricksDataType.STRING, | ||
| UnicodeText: DatabricksDataType.STRING, | ||
| Uuid: DatabricksDataType.STRING, | ||
| } | ||
| # Convert the dictionary into a list of tuples for use in pytest.mark.parametrize | ||
| _as_tuple_list = [(key, value) for key, value in camel_case_type_map.items()] | ||
| class CompilationTestBase: | ||
| dialect = DatabricksDialect() | ||
| def _assert_compiled_value(self, type_: TypeEngine, expected: DatabricksDataType): | ||
| """Assert that when type_ is compiled for the databricks dialect, it renders the DatabricksDataType name. | ||
| This method initialises the type_ with no arguments. | ||
| """ | ||
| compiled_result = type_().compile(dialect=self.dialect) # type: ignore | ||
| assert compiled_result == expected.name | ||
| def _assert_compiled_value_explicit(self, type_: TypeEngine, expected: str): | ||
| """Assert that when type_ is compiled for the databricks dialect, it renders the expected string. | ||
| This method expects an initialised type_ so that we can test how a TypeEngine created with arguments | ||
| is compiled. | ||
| """ | ||
| compiled_result = type_.compile(dialect=self.dialect) | ||
| assert compiled_result == expected | ||
| class TestCamelCaseTypesCompilation(CompilationTestBase): | ||
| """Per the sqlalchemy documentation[^1] here, the camel case members of sqlalchemy.types are | ||
| are expected to work across all dialects. These tests verify that the types compile into valid | ||
| Databricks SQL type strings. For example, the sqlalchemy.types.Integer() should compile as "INT". | ||
| Truly custom types like STRUCT (notice the uppercase) are not expected to work across all dialects. | ||
| We test these separately. | ||
| Note that these tests have to do with type **name** compiliation. Which is separate from actually | ||
| mapping values between Python and Databricks. | ||
| Note: SchemaType and MatchType are not tested because it's not used in table definitions | ||
| [1]: https://docs.sqlalchemy.org/en/20/core/type_basics.html#generic-camelcase-types | ||
| """ | ||
| @pytest.mark.parametrize("type_, expected", _as_tuple_list) | ||
| def test_bare_camel_case_types_compile(self, type_, expected): | ||
| self._assert_compiled_value(type_, expected) | ||
| def test_numeric_renders_as_decimal_with_precision(self): | ||
| self._assert_compiled_value_explicit(Numeric(10), "DECIMAL(10)") | ||
| def test_numeric_renders_as_decimal_with_precision_and_scale(self): | ||
| self._assert_compiled_value_explicit(Numeric(10, 2), "DECIMAL(10, 2)") |
75 changes: 75 additions & 0 deletionssrc/databricks/sqlalchemy/types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| from sqlalchemy.ext.compiler import compiles | ||
| from sqlalchemy.sql.compiler import GenericTypeCompiler | ||
| from sqlalchemy.types import ( | ||
| DateTime, | ||
| Enum, | ||
| Integer, | ||
| LargeBinary, | ||
| Numeric, | ||
| String, | ||
| Text, | ||
| Time, | ||
| Unicode, | ||
| UnicodeText, | ||
| Uuid, | ||
| ) | ||
| @compiles(Enum, "databricks") | ||
| @compiles(String, "databricks") | ||
| @compiles(Text, "databricks") | ||
| @compiles(Time, "databricks") | ||
| @compiles(Unicode, "databricks") | ||
| @compiles(UnicodeText, "databricks") | ||
| @compiles(Uuid, "databricks") | ||
| def compile_string_databricks(type_, compiler, **kw): | ||
| """ | ||
| We override the default compilation for Enum(), String(), Text(), and Time() because SQLAlchemy | ||
| defaults to incompatible / abnormal compiled names | ||
| Enum -> VARCHAR | ||
| String -> VARCHAR[LENGTH] | ||
| Text -> VARCHAR[LENGTH] | ||
| Time -> TIME | ||
| Unicode -> VARCHAR[LENGTH] | ||
| UnicodeText -> TEXT | ||
| Uuid -> CHAR[32] | ||
| But all of these types will be compiled to STRING in Databricks SQL | ||
| """ | ||
| return "STRING" | ||
| @compiles(Integer, "databricks") | ||
| def compile_integer_databricks(type_, compiler, **kw): | ||
| """ | ||
| We need to override the default Integer compilation rendering because Databricks uses "INT" instead of "INTEGER" | ||
| """ | ||
| return "INT" | ||
| @compiles(LargeBinary, "databricks") | ||
| def compile_binary_databricks(type_, compiler, **kw): | ||
| """ | ||
| We need to override the default LargeBinary compilation rendering because Databricks uses "BINARY" instead of "BLOB" | ||
| """ | ||
| return "BINARY" | ||
| @compiles(Numeric, "databricks") | ||
| def compile_numeric_databricks(type_, compiler, **kw): | ||
| """ | ||
| We need to override the default Numeric compilation rendering because Databricks uses "DECIMAL" instead of "NUMERIC" | ||
| The built-in visit_DECIMAL behaviour captures the precision and scale. Here we're just mapping calls to compile Numeric | ||
| to the SQLAlchemy Decimal() implementation | ||
| """ | ||
| return compiler.visit_DECIMAL(type_, **kw) | ||
| @compiles(DateTime, "databricks") | ||
| def compile_datetime_databricks(type_, compiler, **kw): | ||
| """ | ||
| We need to override the default DateTime compilation rendering because Databricks uses "TIMESTAMP" instead of "DATETIME" | ||
| """ | ||
| return "TIMESTAMP" |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.