: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(data-warehouse): match the id primary-key fallback case-insensitively (#71064)
Tom Owers44daa06b935e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.pyindex 7bd3f3ceb6a3..f8f7083d74e2 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py@@ -251,8 +251,13 @@ def resolve_primary_keys(return schema.primary_key_columnsif resource.primary_keys:return list(resource.primary_keys)- if "id" in extract_available_column_names(schema.schema_metadata):- return ["id"]+ # Case-insensitive: engines like Snowflake uppercase unquoted identifiers, so the column+ # arrives as `ID`. Return the actual stored casing — the merge indexes batches by real name.+ id_column = next(+ (name for name in extract_available_column_names(schema.schema_metadata) if name.lower() == "id"), None+ )+ if id_column is not None:+ return [id_column]return Nonediff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.pyindex 5fb0ea6709ad..85155f8e2305 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py@@ -35,6 +35,10 @@ class TestResolvePrimaryKeys:("id_fallback_when_neither", None, None, {"columns": [{"name": "id"}, {"name": "name"}]}, ["id"]),# Nothing to fall back on -> None, so the keyless-table guardrail still fires.("none_when_no_id_and_nothing_else", None, None, {"columns": [{"name": "name"}]}, None),+ # Snowflake uppercases unquoted identifiers: the fallback must match `ID`+ # case-insensitively AND return the actual stored casing — the merge indexes batches+ # by the real column name, so a hardcoded lowercase `id` would fail it just the same.+ ("uppercase_id_matched_with_actual_casing", None, None, {"columns": [{"name": "ID"}]}, ["ID"]),])def test_precedence(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/base.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/base.pyindex b7271b12863b..53b701434373 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/base.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/base.py@@ -100,11 +100,13 @@ class SQLSource(SimpleSource[ConfigType], Generic[ConfigType]):def _default_primary_key_from_columns(self, columns: list[tuple[str, str, bool]]) -> list[str] | None:"""Fallback: use `id` when the driver didn't detect a PK but one is present.- Mirrors what every SQL source has always done just before building- a `SourceSchema`.+ Mirrors what every SQL source has always done just before building a `SourceSchema`.+ Matched case-insensitively (Snowflake uppercases unquoted identifiers) but the column's+ actual stored casing is returned, because the merge indexes batches by the real name."""- if any(col[0] == "id" for col in columns):- return ["id"]+ id_column = next((col[0] for col in columns if col[0].lower() == "id"), None)+ if id_column is not None:+ return [id_column]return Nonedef get_schemas(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/tests/test_types.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/tests/test_types.pynew file mode 100644index 000000000000..dc86cf58f9b6--- /dev/null+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/tests/test_types.py@@ -0,0 +1,21 @@+from parameterized import parameterized++from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.types import (+ resolve_detected_primary_keys,+)+++class TestResolveDetectedPrimaryKeys:+ @parameterized.expand(+ [+ # Driver-discovered keys always win over the name fallback.+ ("discovered_wins", ["pk"], [("id", "integer", False)], ["pk"]),+ ("id_fallback", None, [("name", "text", True), ("id", "integer", False)], ["id"]),+ # Snowflake uppercases unquoted identifiers: match `ID` case-insensitively and return+ # the actual stored casing — the merge indexes batches by the real column name.+ ("uppercase_id_matched_with_actual_casing", None, [("ID", "text", False)], ["ID"]),+ ("no_id_column", None, [("name", "text", True)], None),+ ]+ )+ def test_fallback(self, _name, discovered, columns, expected):+ assert resolve_detected_primary_keys(discovered, columns) == expecteddiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/types.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/types.pyindex cd360ad6e54d..993d0c1cef2e 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/types.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/sql/types.py@@ -14,14 +14,16 @@ def resolve_detected_primary_keys() -> list[str] | None:"""Pick the per-table primary key columns to advertise on a `SourceSchema`.- Returns the discovered list when present, otherwise falls back to `["id"]` if any column is- literally named `id` (the convention every SQL source we ingest from happens to follow), else- None. Used by Postgres / MySQL / MSSQL / Redshift discovery.+ Returns the discovered list when present, otherwise falls back to a column named `id` —+ matched case-insensitively, since engines like Snowflake uppercase unquoted identifiers —+ else None. The column's actual stored casing is returned, because the merge later indexes+ batches by the real column name. Used by Postgres / MySQL / MSSQL / Redshift discovery."""if discovered_pk_columns:return discovered_pk_columns- if any(column[0] == "id" for column in columns):- return ["id"]+ id_column = next((column[0] for column in columns if column[0].lower() == "id"), None)+ if id_column is not None:+ return [id_column]return None