: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): surface actionable error when postgres int column type widens (#59384)
Tom Owers33b0c39287c2
diff --git a/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py b/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.pyindex 915b25a0335b..f1652447bc83 100644--- a/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py+++ b/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py@@ -15,6 +15,7 @@ from structlog.types import FilteringBoundLoggerfrom posthog.temporal.data_imports.pipelines.pipeline.consts import PARTITION_KEYfrom posthog.temporal.data_imports.pipelines.pipeline.utils import (+ SchemaColumnTypeChangedException,_evolve_pyarrow_schema,_get_max_decimal_type,append_partition_key_to_table,@@ -493,6 +494,53 @@ def test_evolve_pyarrow_schema_decimal_does_not_widen_unnecessarily_and_can_wideassert evolved_table.schema.field("amount").type == expected_type+ "delta_type, incoming_type, overflowing_value",+ [+ (pa.int32(), pa.int64(), 6178466636), # > int32 max (2147483647)+ (pa.int16(), pa.int64(), 6178466636), # > int16 max, fits int64+ (pa.int16(), pa.int32(), 100000), # > int16 max (32767), fits int32+ ],+)+def test_evolve_pyarrow_schema_integer_overflow_raises_actionable_error(+ delta_type: pa.DataType, incoming_type: pa.DataType, overflowing_value: int+):+ """An incoming integer value that overflows the stored (narrower) Delta type raises a+ clear, actionable error instructing the user to reset and re-sync — rather than a raw+ pyarrow ArrowInvalid."""+ arrow_table = pa.table(+ {+ "id": pa.array([1, 2], type=pa.int64()),+ "val": pa.array([10, overflowing_value], type=incoming_type),+ }+ )+ delta_schema = deltalake.Schema.from_arrow(+ pa.schema([pa.field("id", pa.int64(), nullable=False), pa.field("val", delta_type, nullable=True)])+ )++ with pytest.raises(SchemaColumnTypeChangedException, match="Source column type changed"):+ _evolve_pyarrow_schema(arrow_table, delta_schema)+++def test_evolve_pyarrow_schema_integer_narrowing_within_range_is_preserved():+ """A wider incoming integer column whose values still fit the stored narrower type is+ narrowed without error (existing behaviour must not regress)."""+ arrow_table = pa.table(+ {+ "id": pa.array([1, 2], type=pa.int64()),+ "val": pa.array([10, 20], type=pa.int64()),+ }+ )+ delta_schema = deltalake.Schema.from_arrow(+ pa.schema([pa.field("id", pa.int64(), nullable=False), pa.field("val", pa.int32(), nullable=True)])+ )++ evolved_table = _evolve_pyarrow_schema(arrow_table, delta_schema)++ assert evolved_table.schema.field("val").type == pa.int32()+ assert evolved_table.column("val").to_pylist() == [10, 20]++def test_evolve_pyarrow_schema_with_struct_containing_datetime_and_decimal():"""Test that _evolve_pyarrow_schema can handle struct columns with non-JSON-serializable types."""metadata_struct_type = pa.struct(diff --git a/posthog/temporal/data_imports/pipelines/pipeline/utils.py b/posthog/temporal/data_imports/pipelines/pipeline/utils.pyindex a04faf5a9ee4..26537dc91602 100644--- a/posthog/temporal/data_imports/pipelines/pipeline/utils.py+++ b/posthog/temporal/data_imports/pipelines/pipeline/utils.py@@ -69,6 +69,18 @@ class TemporaryFileSizeExceedsLimitException(Exception):pass+class SchemaColumnTypeChangedException(Exception):+ """Raised when an incoming column can't be cast into the existing (narrower) Delta column type.++ The usual cause is the source column's type being widened upstream (e.g. Postgres+ `integer` → `bigint`) after the Delta table was already created with the narrower type.+ delta-rs cannot widen an existing column in place, so retrying is futile — the table must+ be reset and fully re-synced to adopt the new type.+ """++ pass++def normalize_column_name(column_name: str) -> str:return NamingConvention.normalize_identifier(column_name)@@ -268,10 +280,26 @@ def _evolve_pyarrow_schema(incoming_table: pa.Table, delta_schema: deltalake.Schincoming_table.schema.get_field_index(delta_field.name), delta_field.name, parsed_timestamps)else:+ try:+ casted_column = incoming_column.cast(delta_field.type).combine_chunks()+ except pa.ArrowInvalid as e:+ # A narrowing cast overflowed. The usual cause is the source column's type+ # being widened upstream (e.g. Postgres `integer` → `bigint`) after the Delta+ # column was created with the narrower type. delta-rs cannot widen an existing+ # column in place, so retrying is futile — surface an actionable error telling+ # the user to reset and fully re-sync the table.+ if pa.types.is_integer(delta_field.type) and pa.types.is_integer(incoming_column.type):+ raise SchemaColumnTypeChangedException(+ f"Source column type changed: '{delta_field.name}' has values that no longer "+ f"fit its stored type {delta_field.type} (incoming data is now "+ f"{incoming_column.type}). Reset and fully re-sync this table to adopt the new type."+ ) from e+ raise+incoming_table = incoming_table.set_column(incoming_table.schema.get_field_index(delta_field.name),delta_field.name,- incoming_column.cast(delta_field.type).combine_chunks(),+ casted_column,)incoming_column = incoming_table.column(delta_field.name)diff --git a/posthog/temporal/data_imports/sources/bigquery/source.py b/posthog/temporal/data_imports/sources/bigquery/source.pyindex aa03999a30a8..ad2bc98ca8be 100644--- a/posthog/temporal/data_imports/sources/bigquery/source.py+++ b/posthog/temporal/data_imports/sources/bigquery/source.py@@ -46,6 +46,12 @@ class BigQuerySource(SimpleSource[BigQuerySourceConfig]):return {"PermissionDenied: 403 request failed": "BigQuery permission denied. Please check that your service account has the necessary permissions.","NotFound: 404": "BigQuery dataset or table not found. Please verify your project, dataset, and table names.",+ # Raised from the shared `_evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`+ # when an integer column's source type was widened (e.g. `INT64` widened from a+ # narrower numeric type) after the destination table was created with the narrower+ # type. Delta Lake can't widen an existing column in place, so retrying won't help —+ # the table must be reset and fully re-synced to adopt the new type.+ "Source column type changed": "A column's type changed in your source database (for example an integer column was widened to bigint) and no longer fits the type we stored. We can't widen an existing column in place — please reset and fully re-sync this table to adopt the new type.",}def get_schemas(diff --git a/posthog/temporal/data_imports/sources/clickhouse/source.py b/posthog/temporal/data_imports/sources/clickhouse/source.pyindex e6b1319d50a7..c6c23f4df887 100644--- a/posthog/temporal/data_imports/sources/clickhouse/source.py+++ b/posthog/temporal/data_imports/sources/clickhouse/source.py@@ -157,6 +157,12 @@ class ClickHouseSource(SimpleSource[ClickHouseSourceConfig], SSHTunnelMixin, Val"No route to host": None,"certificate verify failed": None,"SSL: WRONG_VERSION_NUMBER": None,+ # Raised from the shared `_evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`+ # when an integer column's source type was widened (e.g. `Int32` → `Int64`) after+ # the destination table was created with the narrower type. Delta Lake can't widen+ # an existing column in place, so retrying won't help — the table must be reset and+ # fully re-synced to adopt the new type.+ "Source column type changed": "A column's type changed in your source database (for example an integer column was widened to bigint) and no longer fits the type we stored. We can't widen an existing column in place — please reset and fully re-sync this table to adopt the new type.",}def get_schemas(diff --git a/posthog/temporal/data_imports/sources/mssql/source.py b/posthog/temporal/data_imports/sources/mssql/source.pyindex 51f9402ea97d..4580ab5bde7f 100644--- a/posthog/temporal/data_imports/sources/mssql/source.py+++ b/posthog/temporal/data_imports/sources/mssql/source.py@@ -52,6 +52,12 @@ class MSSQLSource(SimpleSource[MSSQLSourceConfig], SSHTunnelMixin, ValidateDatab# Lake's decimal budget (precision > 76 or scale > 32). Fixed source-data shape —# retrying won't help."Cannot build decimal array from values": "One of your numeric columns contains values that exceed our decimal storage limits (max precision 76, max scale 32). Please constrain the column with a lower precision/scale, cast it to text in a view, or round the values at the source.",+ # Raised from the shared `_evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`+ # when an integer column's source type was widened (e.g. `INT` → `BIGINT`) after the+ # destination table was created with the narrower type. Delta Lake can't widen an+ # existing column in place, so retrying won't help — the table must be reset and+ # fully re-synced to adopt the new type.+ "Source column type changed": "A column's type changed in your source database (for example an integer column was widened to bigint) and no longer fits the type we stored. We can't widen an existing column in place — please reset and fully re-sync this table to adopt the new type.",}@propertydiff --git a/posthog/temporal/data_imports/sources/mssql/test_mssql.py b/posthog/temporal/data_imports/sources/mssql/test_mssql.pyindex 72daaa0c3af2..df9e51ad32c7 100644--- a/posthog/temporal/data_imports/sources/mssql/test_mssql.py+++ b/posthog/temporal/data_imports/sources/mssql/test_mssql.py@@ -19,3 +19,15 @@ class TestMSSQLSourceNonRetryableErrors:non_retryable = source.get_non_retryable_errors()is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())assert is_non_retryable, f"Unrepresentable decimal error should be non-retryable: {error_msg}"++ @pytest.mark.parametrize(+ "error_msg",+ [+ "Source column type changed",+ "SchemaColumnTypeChangedException: Source column type changed: 'id' has values that no longer fit",+ ],+ )+ def test_widened_integer_column_errors_are_non_retryable(self, source, error_msg):+ non_retryable = source.get_non_retryable_errors()+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())+ assert is_non_retryable, f"Widened integer column error should be non-retryable: {error_msg}"diff --git a/posthog/temporal/data_imports/sources/mysql/source.py b/posthog/temporal/data_imports/sources/mysql/source.pyindex 8edf661606d1..beed6bbfe3a1 100644--- a/posthog/temporal/data_imports/sources/mysql/source.py+++ b/posthog/temporal/data_imports/sources/mysql/source.py@@ -135,6 +135,12 @@ class MySQLSource(SimpleSource[MySQLSourceConfig], SSHTunnelMixin, ValidateDatab# decimal budget (precision > 76 or scale > 32). Fixed source-data shape — retrying# won't help."Cannot build decimal array from values": "One of your numeric columns contains values that exceed our decimal storage limits (max precision 76, max scale 32). Please constrain the column with a lower precision/scale, cast it to text in a view, or round the values at the source.",+ # Raised from the shared `_evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`+ # when an integer column's source type was widened (e.g. `INT` → `BIGINT`) after the+ # destination table was created with the narrower type. Delta Lake can't widen an+ # existing column in place, so retrying won't help — the table must be reset and+ # fully re-synced to adopt the new type.+ "Source column type changed": "A column's type changed in your source database (for example an integer column was widened to bigint) and no longer fits the type we stored. We can't widen an existing column in place — please reset and fully re-sync this table to adopt the new type.",}def get_schemas(diff --git a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.pyindex 1538cf914004..a63a6933590c 100644--- a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py+++ b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py@@ -409,3 +409,15 @@ class TestMySQLSourceNonRetryableErrors:non_retryable = source.get_non_retryable_errors()is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())assert is_non_retryable, f"Unrepresentable decimal error should be non-retryable: {error_msg}"++ @pytest.mark.parametrize(+ "error_msg",+ [+ "Source column type changed",+ "SchemaColumnTypeChangedException: Source column type changed: 'id' has values that no longer fit",+ ],+ )+ def test_widened_integer_column_errors_are_non_retryable(self, source, error_msg):+ non_retryable = source.get_non_retryable_errors()+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())+ assert is_non_retryable, f"Widened integer column error should be non-retryable: {error_msg}"diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.pyindex 1cc0ff2f1dd3..0632a658a670 100644--- a/posthog/temporal/data_imports/sources/postgres/source.py+++ b/posthog/temporal/data_imports/sources/postgres/source.py@@ -173,6 +173,11 @@ class PostgresSource(SimpleSource[PostgresSourceConfig], SSHTunnelMixin, Validat# exceeds Delta Lake's decimal budget (precision > 76 or scale > 32); retrying won't# help because the value shape is fixed in the source."Cannot build decimal array from values": "One of your numeric columns contains values that exceed our decimal storage limits (max precision 76, max scale 32). Please constrain the column with a lower precision/scale, cast it to text in a view, or round the values at the source.",+ # Raised when an integer column's source type was widened (e.g. `integer` → `bigint`)+ # after the destination table was created with the narrower type. Delta Lake can't widen+ # an existing column in place, so retrying won't help — the table must be reset and+ # fully re-synced to adopt the new type.+ "Source column type changed": "A column's type changed in your source database (for example an integer column was widened to bigint) and no longer fits the type we stored. We can't widen an existing column in place — please reset and fully re-sync this table to adopt the new type.",}def cleanup_cdc_resources_on_deletion(self, source: "ExternalDataSource") -> None:diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex d0a4a7ec41cd..22b8359a50f4 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -137,6 +137,18 @@ class TestPostgresSourceNonRetryableErrors:is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())assert is_non_retryable, f"Unrepresentable decimal error should be non-retryable: {error_msg}"+ @pytest.mark.parametrize(+ "error_msg",+ [+ "Source column type changed",+ "SchemaColumnTypeChangedException: Source column type changed: 'id' has values that no longer fit",+ ],+ )+ def test_widened_integer_column_errors_are_non_retryable(self, source, error_msg):+ non_retryable = source.get_non_retryable_errors()+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())+ assert is_non_retryable, f"Widened integer column error should be non-retryable: {error_msg}"+class TestPostgresSourceForPipelineSchemaResolution:@pytest.fixturediff --git a/posthog/temporal/data_imports/sources/redshift/source.py b/posthog/temporal/data_imports/sources/redshift/source.pyindex 4af081de1b5d..5452430e86bd 100644--- a/posthog/temporal/data_imports/sources/redshift/source.py+++ b/posthog/temporal/data_imports/sources/redshift/source.py@@ -144,6 +144,12 @@ class RedshiftSource(SimpleSource[RedshiftSourceConfig], SSHTunnelMixin, Validat"password authentication failed connection": None,"connection timeout expired": None,"Connection refused": None,+ # Raised from the shared `_evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`+ # when an integer column's source type was widened (e.g. `INTEGER` → `BIGINT`)+ # after the destination table was created with the narrower type. Delta Lake can't+ # widen an existing column in place, so retrying won't help — the table must be+ # reset and fully re-synced to adopt the new type.+ "Source column type changed": "A column's type changed in your source database (for example an integer column was widened to bigint) and no longer fits the type we stored. We can't widen an existing column in place — please reset and fully re-sync this table to adopt the new type.",}def get_schemas(diff --git a/posthog/temporal/data_imports/sources/snowflake/source.py b/posthog/temporal/data_imports/sources/snowflake/source.pyindex 4e715d10a6c7..246262bb346f 100644--- a/posthog/temporal/data_imports/sources/snowflake/source.py+++ b/posthog/temporal/data_imports/sources/snowflake/source.py@@ -181,6 +181,12 @@ class SnowflakeSource(SimpleSource[SnowflakeSourceConfig]):"MFA authentication is required": None,"invalid credentials": "Snowflake authentication failed. Please check your username, password, and account details.","authentication failed": "Snowflake authentication failed. Please check your username, password, and account details.",+ # Raised from the shared `_evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`+ # when an integer column's source type was widened (e.g. a narrower NUMBER widened+ # to a larger NUMBER/BIGINT) after the destination table was created with the+ # narrower type. Delta Lake can't widen an existing column in place, so retrying+ # won't help — the table must be reset and fully re-synced to adopt the new type.+ "Source column type changed": "A column's type changed in your source database (for example an integer column was widened to bigint) and no longer fits the type we stored. We can't widen an existing column in place — please reset and fully re-sync this table to adopt the new type.",}def get_schemas(