:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(ducklake): replace PRAGMA table_info in duckgres verification (#58064)
Eric Duong05f82f86bc7b
diff --git a/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py b/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.pyindex 89776b0a0ecb..4aa8f7a57e68 100644--- a/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py+++ b/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py@@ -57,6 +57,9 @@ DATA_IMPORTS_DUCKLAKE_WORKFLOW_PREFIX = "data_imports"class _VerificationCursor(typing.Protocol):+ @property+ def description(self) -> typing.Sequence[object] | None: ...+def fetchone(self) -> tuple[object, ...] | None: ...def fetchall(self) -> list[tuple[object, ...]]: ...@@ -766,11 +769,11 @@ def _fetch_delta_schema(conn: _VerificationConnection, source_uri: str, *, parameter_placeholder: str = "?") -> list[tuple[str, str]]:"""Fetch schema from a Delta table."""- rows = conn.execute(- f"DESCRIBE SELECT * FROM delta_scan({parameter_placeholder}) LIMIT 0",+ cursor = conn.execute(+ f"SELECT * FROM delta_scan({parameter_placeholder}) LIMIT 0",[source_uri],- ).fetchall()- return [(str(row[0]), str(row[1])) for row in rows]+ )+ return _schema_from_cursor_description(cursor)def _get_column_type_from_schema(schema: list[tuple[str, str]], column_name: str) -> str | None:@@ -784,8 +787,24 @@ def _get_column_type_from_schema(schema: list[tuple[str, str]], column_name: strdef _fetch_schema(conn: _VerificationConnection, table_name: str) -> list[tuple[str, str]]:"""Fetch schema from a DuckLake table."""- rows = conn.execute(f"PRAGMA table_info('{table_name}')").fetchall()- return [(str(row[1]), str(row[2])) for row in rows]+ cursor = conn.execute(f"SELECT * FROM {table_name} LIMIT 0")+ return _schema_from_cursor_description(cursor)+++def _schema_from_cursor_description(cursor: _VerificationCursor) -> list[tuple[str, str]]:+ description = cursor.description+ if not description:+ raise ValueError("Schema query did not return column metadata")++ schema: list[tuple[str, str]] = []+ for column in description:+ name = getattr(column, "name", None)+ type_code = getattr(column, "type_code", None)+ if name is None and isinstance(column, tuple):+ name = column[0] if len(column) > 0 else None+ type_code = column[1] if len(column) > 1 else None+ schema.append((str(name or ""), str(type_code or "")))+ return schemadef _diff_schema(source_schema: list[tuple[str, str]], ducklake_schema: list[tuple[str, str]]) -> list[str]:diff --git a/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py b/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.pyindex 5177f12a0c14..d15f666d89a3 100644--- a/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py+++ b/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py@@ -1,5 +1,6 @@import uuidimport datetime as dt+from collections.abc import Sequenceimport pytestfrom unittest.mock import MagicMock@@ -32,6 +33,33 @@ from products.data_warehouse.backend.models.external_data_source import Externalfrom products.data_warehouse.backend.models.table import DataWarehouseTable+class _FakeColumn:+ def __init__(self, name: str, type_code: int) -> None:+ self.name = name+ self.type_code = type_code+++class _FakeVerificationCursor:+ def __init__(self, description: Sequence[object] | None) -> None:+ self.description = description++ def fetchone(self) -> tuple[object, ...] | None:+ raise AssertionError("schema fetching should use cursor.description")++ def fetchall(self) -> list[tuple[object, ...]]:+ raise AssertionError("schema fetching should use cursor.description")+++class _FakeVerificationConnection:+ def __init__(self, description: Sequence[object] | None) -> None:+ self.description = description+ self.calls: list[tuple[str, Sequence[object] | None]] = []++ def execute(self, query: str, params: Sequence[object] | None = None) -> _FakeVerificationCursor:+ self.calls.append((query, params))+ return _FakeVerificationCursor(self.description)++@pytest.mark.asyncioasync def test_data_imports_ducklake_copy_inputs_round_trip_serialization():schema_id = uuid.uuid4()@@ -814,6 +842,51 @@ def test_verify_data_imports_ducklake_copy_activity_tolerance_comparison(monkeypassert results[1].passed is False+_DUCKGRES_CURSOR_DESCRIPTION = [_FakeColumn("id", 20), _FakeColumn("name", 25)]+_DUCKGRES_EXPECTED_SCHEMA = [("id", "20"), ("name", "25")]+_DUCKDB_CURSOR_DESCRIPTION = [+ ("id", "BIGINT", None, None, None, None, None),+ ("name", "VARCHAR", None, None, None, None, None),+]+_DUCKDB_EXPECTED_SCHEMA = [("id", "BIGINT"), ("name", "VARCHAR")]+++ "description, expected_schema",+ [+ pytest.param(_DUCKGRES_CURSOR_DESCRIPTION, _DUCKGRES_EXPECTED_SCHEMA, id="duckgres_psycopg_columns"),+ pytest.param(_DUCKDB_CURSOR_DESCRIPTION, _DUCKDB_EXPECTED_SCHEMA, id="duckdb_dbapi_tuples"),+ ],+)+def test_fetch_delta_schema_uses_select_metadata(description, expected_schema):+ conn = _FakeVerificationConnection(description)++ result = ducklake_module._fetch_delta_schema(conn, "s3://bucket/staged/customers", parameter_placeholder="%s")++ assert result == expected_schema+ assert conn.calls == [+ ("SELECT * FROM delta_scan(%s) LIMIT 0", ["s3://bucket/staged/customers"]),+ ]+++ "description, expected_schema",+ [+ pytest.param(_DUCKGRES_CURSOR_DESCRIPTION, _DUCKGRES_EXPECTED_SCHEMA, id="duckgres_psycopg_columns"),+ pytest.param(_DUCKDB_CURSOR_DESCRIPTION, _DUCKDB_EXPECTED_SCHEMA, id="duckdb_dbapi_tuples"),+ ],+)+def test_fetch_schema_uses_select_metadata(description, expected_schema):+ conn = _FakeVerificationConnection(description)++ result = ducklake_module._fetch_schema(conn, "posthog_data_imports_team_1.postgres_customers")++ assert result == expected_schema+ assert conn.calls == [+ ("SELECT * FROM posthog_data_imports_team_1.postgres_customers LIMIT 0", None),+ ]++def test_ducklake_copy_data_imports_workflow_parse_inputs():schema_id = uuid.uuid4()json_input = f"""{{