Skip to content

Commit

Permalink
Correctly handle multiple types
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Oct 25, 2024
1 parent c4bba9d commit 5d201fb
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions singer_sdk/connectors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ def register_format_handler(
""" # noqa: E501
self._format_handlers[format_name] = handler

def handle_multiple_types(self, types: t.Sequence[str]) -> sa.types.TypeEngine: # noqa: ARG002, PLR6301
"""Handle multiple types by returning a VARCHAR.
Args:
types: The list of types to handle.
Returns:
A VARCHAR type.
"""
return sa.types.VARCHAR()

def _get_type_from_schema(self, schema: dict) -> sa.types.TypeEngine | None:
"""Try to get a SQL type from a single schema object.
Expand All @@ -291,15 +302,21 @@ def _get_type_from_schema(self, schema: dict) -> sa.types.TypeEngine | None:
return format_type

# Then check regular types
if "type" in schema:
schema_type = schema["type"]
if schema_type := schema.get("type"):
if isinstance(schema_type, (list, tuple)):
# For type arrays, try each type
for t in schema_type:
if handler := self._type_mapping.get(t):
return self._invoke_handler(handler, schema)
elif schema_type in self._type_mapping:
handler = self._type_mapping[schema_type]
# Filter out null type if present
non_null_types = [t for t in schema_type if t != "null"]

# If we have multiple non-null types, use VARCHAR
if len(non_null_types) > 1:
self.handle_multiple_types(non_null_types)

# If we have exactly one non-null type, use its handler
if len(non_null_types) == 1 and non_null_types[0] in self._type_mapping:
handler = self._type_mapping[non_null_types[0]]
return self._invoke_handler(handler, schema)

elif handler := self._type_mapping.get(schema_type):
return self._invoke_handler(handler, schema)

return None
Expand Down

0 comments on commit 5d201fb

Please sign in to comment.