Skip to content

Commit a29f238

Browse files
fix: Handle type-mapping with SQLConnector.jsonschema_to_sql
1 parent f8d7fcc commit a29f238

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

Diff for: pyproject.toml

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ target-version = "py39"
4040

4141
[tool.ruff.lint]
4242
ignore = [
43-
"ANN101", # missing-type-self
44-
"ANN102", # missing-type-cls
4543
"ANN201",
4644
"TD",
4745
"D",

Diff for: target_snowflake/connector.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from cryptography.hazmat.primitives import serialization
1414
from singer_sdk import typing as th
1515
from singer_sdk.connectors import SQLConnector
16-
from singer_sdk.connectors.sql import FullyQualifiedName
16+
from singer_sdk.connectors.sql import FullyQualifiedName, JSONSchemaToSQL
1717
from singer_sdk.exceptions import ConfigValidationError
1818
from snowflake.sqlalchemy import URL
1919
from snowflake.sqlalchemy.base import SnowflakeIdentifierPreparer
@@ -27,6 +27,7 @@
2727

2828
from sqlalchemy.engine import Engine
2929

30+
# TODO: Remove this when when JSON schema to SQL is stable
3031
SNOWFLAKE_MAX_STRING_LENGTH = 16777216
3132

3233

@@ -89,6 +90,8 @@ class SnowflakeConnector(SQLConnector):
8990
allow_merge_upsert: bool = False # Whether MERGE UPSERT is supported.
9091
allow_temp_tables: bool = True # Whether temp tables are supported.
9192

93+
max_varchar_length = 16_777_216
94+
9295
def __init__(self, *args: Any, **kwargs: Any) -> None:
9396
self.table_cache: dict = {}
9497
self.schema_cache: dict = {}
@@ -317,6 +320,16 @@ def _conform_max_length(jsonschema_type): # noqa: ANN205, ANN001
317320
jsonschema_type["maxLength"] = SNOWFLAKE_MAX_STRING_LENGTH
318321
return jsonschema_type
319322

323+
@cached_property
324+
def jsonschema_to_sql(self) -> JSONSchemaToSQL:
325+
to_sql = super().jsonschema_to_sql
326+
to_sql.register_type_handler("integer", NUMBER)
327+
to_sql.register_type_handler("object", VARIANT)
328+
to_sql.register_type_handler("array", VARIANT)
329+
to_sql.register_type_handler("number", sct.DOUBLE)
330+
to_sql.register_format_handler("date-time", TIMESTAMP_NTZ)
331+
return to_sql
332+
320333
def to_sql_type(self, jsonschema_type: dict) -> sqlalchemy.types.TypeEngine:
321334
"""Return a JSON Schema representation of the provided type.
322335
@@ -336,23 +349,14 @@ def to_sql_type(self, jsonschema_type: dict) -> sqlalchemy.types.TypeEngine:
336349
maxlength = jsonschema_type.get("maxLength", SNOWFLAKE_MAX_STRING_LENGTH)
337350
# define type maps
338351
string_submaps = [
339-
TypeMap(eq, TIMESTAMP_NTZ(), "date-time"),
340352
TypeMap(contains, sqlalchemy.types.TIME(), "time"),
341353
TypeMap(eq, sqlalchemy.types.DATE(), "date"),
342354
TypeMap(eq, sqlalchemy.types.VARCHAR(maxlength), None),
343355
]
344-
type_maps = [
345-
TypeMap(th._jsonschema_type_check, NUMBER(), ("integer",)), # noqa: SLF001
346-
TypeMap(th._jsonschema_type_check, VARIANT(), ("object",)), # noqa: SLF001
347-
TypeMap(th._jsonschema_type_check, VARIANT(), ("array",)), # noqa: SLF001
348-
TypeMap(th._jsonschema_type_check, sct.DOUBLE(), ("number",)), # noqa: SLF001
349-
]
350356
# apply type maps
351357
if th._jsonschema_type_check(jsonschema_type, ("string",)): # noqa: SLF001
352358
datelike_type = th.get_datelike_property_type(jsonschema_type)
353359
target_type = evaluate_typemaps(string_submaps, datelike_type, target_type)
354-
else:
355-
target_type = evaluate_typemaps(type_maps, jsonschema_type, target_type)
356360

357361
return cast(sqlalchemy.types.TypeEngine, target_type)
358362

0 commit comments

Comments
 (0)