: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(google_sheets): handle integer columns exceeding int64 range (#63775)
Tom Owers572b2a4cced8
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 d09a58e17d4b..e8df0eaed9dc 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@@ -362,6 +362,35 @@ def test_table_from_py_list_with_rescaling_decimal_data_loss_error():assert table.schema.equals(expected_schema)+def test_table_from_py_list_int_column_exceeding_int64_promoted_to_decimal():+ # Python ints are unbounded; a value past int64 overflows pyarrow's inferred int64 type with+ # "Python int too large to convert to C long". It should be promoted to decimal instead.+ over_int64 = 2**63 + 1++ table = table_from_py_list([{"column": over_int64}, {"column": 1}, {"column": None}])++ assert pa.types.is_decimal(table.schema.field("column").type)+ assert table.column("column").to_pylist() == [decimal.Decimal(over_int64), decimal.Decimal(1), None]+++def test_table_from_py_list_huge_int_column_falls_back_to_string():+ # A value too large even for decimal256 (76 digits) must not crash the sync — keep it as text.+ enormous = int("1" * 340)++ table = table_from_py_list([{"column": enormous}, {"column": 5}])++ assert table.schema.field("column").type == pa.string()+ assert table.column("column").to_pylist() == [str(enormous), "5"]+++def test_table_from_py_list_normal_int_column_stays_int64():+ # Values within int64 are unaffected by the overflow handling.+ table = table_from_py_list([{"column": 1}, {"column": 2}, {"column": None}])++ assert table.schema.field("column").type == pa.int64()+ assert table.column("column").to_pylist() == [1, 2, None]++@pytest.mark.parametrize("data, schema, expected_type",[diff --git a/posthog/temporal/data_imports/pipelines/pipeline/utils.py b/posthog/temporal/data_imports/pipelines/pipeline/utils.pyindex da2fbb37284e..bd6810059dd1 100644--- a/posthog/temporal/data_imports/pipelines/pipeline/utils.py+++ b/posthog/temporal/data_imports/pipelines/pipeline/utils.py@@ -47,6 +47,11 @@ DLT_TO_PA_TYPE_MAP: dict[DEFAULT_NUMERIC_PRECISION = 38 # Delta Lake maximum precisionDEFAULT_NUMERIC_SCALE = 18 # Good default scale for decimal128, 20 int digits plus 18 decimal casesMAX_NUMERIC_SCALE = 32 # Maximum scale for Delta Lake++# pyarrow infers `int64` for Python `int` columns; values outside this range overflow with+# "OverflowError: Python int too large to convert to C long" (Python ints are unbounded).+INT64_MIN = -(2**63)+INT64_MAX = 2**63 - 1DEFAULT_PARTITION_TARGET_SIZE_IN_BYTES = 200 * 1024 * 1024 # 200 MBtype SupportedDltDataType = Literal["text", "bigint", "bool", "timestamp", "json", "double", "date", "time", "decimal"]@@ -891,6 +896,38 @@ def _process_batch(table_data: list[dict], schema: Optional[pa.Schema] = None) -if arrow_schema:arrow_schema = arrow_schema.set(field_index, arrow_schema.field(field_index).with_type(new_field_type))+ # Integer columns can hold Python ints larger than pyarrow's int64 range (e.g. a Google+ # Sheets cell with a huge number). pyarrow raises "OverflowError: Python int too large to+ # convert to C long" while inferring int64. Such a column already failed `pa.array(values)`+ # above and fell back to an object ndarray, so restrict the scan to those candidates to+ # avoid touching every normal int column. Promote to decimal when it fits, else stringify.+ if (+ py_type is int+ and unique_types_in_column == {int}+ and isinstance(columnar_table_data[field_name], np.ndarray)+ ):+ all_values = _to_list_array(columnar_table_data[field_name])+ if any(value is not None and not (INT64_MIN <= value <= INT64_MAX) for value in all_values):+ try:+ overflow_arr = _decimal_array_from_values(+ [None if value is None else decimal.Decimal(value) for value in all_values]+ )+ py_type = decimal.Decimal+ unique_types_in_column = {decimal.Decimal}+ except ValueError:+ # Too large even for decimal256 — keep it as text rather than crash the sync.+ overflow_arr = pa.array(+ [None if value is None else str(value) for value in all_values], type=pa.string()+ )+ py_type = str+ unique_types_in_column = {str}++ columnar_table_data[field_name] = overflow_arr+ if arrow_schema:+ arrow_schema = arrow_schema.set(+ field_index, arrow_schema.field(field_index).with_type(overflow_arr.type)+ )+# If one type is a list, then make everything into a listif len(unique_types_in_column) > 1 and list in unique_types_in_column:list_array = pa.array(