From 5d201fba9671729bed7b060fe036db989118de11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Fri, 25 Oct 2024 10:57:31 -0600 Subject: [PATCH] Correctly handle multiple types --- singer_sdk/connectors/sql.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/singer_sdk/connectors/sql.py b/singer_sdk/connectors/sql.py index 7fd6d4b82..eb3cd3cd6 100644 --- a/singer_sdk/connectors/sql.py +++ b/singer_sdk/connectors/sql.py @@ -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. @@ -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