Skip to content

: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.

fix(mysql): restrict arrow schema to columns the read returned (#85646)

Tom Owers1bd02c10ac5b
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
index 5345919a2f47..892fa2deaeb5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
@@ -42,6 +42,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arr
DEFAULT_NUMERIC_PRECISION,
DEFAULT_NUMERIC_SCALE,
build_pyarrow_decimal_type,
+ restrict_schema_to_columns,
table_from_iterator,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import open_ssh_tunnel
@@ -1512,13 +1513,19 @@ class MySQLImplementation(SQLSourceImplementation[MySQLSourceConfig, pymysql.Con
column_names = [column[0] for column in ss_cursor.description or []]
+ # The streaming read can return a strict subset of the columns discovered
+ # during setup (a column dropped at the source, or the table recreated
+ # narrower, between discovery and the read), so restrict the schema to what
+ # the query actually returned instead of failing the batch build.
+ read_schema = restrict_schema_to_columns(arrow_schema, column_names)
+
while True:
# use chunk_size to fetch rows instead of DEFAULT_CHUNK_SIZE
batch = ss_cursor.fetchmany(chunk_size)
if not batch:
break
- yield table_from_iterator((dict(zip(column_names, row)) for row in batch), arrow_schema)
+ yield table_from_iterator((dict(zip(column_names, row)) for row in batch), read_schema)
finally:
# Tear the streaming cursor down without draining the rest of
# the unbuffered result set — see `_release_streaming_cursor`.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
index dc69adae335a..702da7015621 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
@@ -905,6 +905,36 @@ class TestUnavoidableFilesortFallback:
assert any(pattern in UNAVOIDABLE_FILESORT_LOST_CONNECTION_ERROR for pattern in non_retryable.keys())
+class TestStreamingSchemaDrift:
+ """Schema discovery can find columns the streaming read no longer returns — a column
+ dropped at the source, or the table recreated narrower, between discovery and the read.
+ The batch must still build against the columns the query actually returned, instead of
+ raising pyarrow's `from_pydict` KeyError ("The passed mapping doesn't contain ... field(s)")."""
+
+ def test_read_returning_a_subset_of_discovered_columns_builds(self, build_pipeline_mocks, mocker):
+ _, _, ss_cursor = build_pipeline_mocks
+ # Discovery finds three columns...
+ wide_table = Table(
+ name="messages",
+ parents=("mydb",),
+ columns=[
+ MySQLColumn(name="id", data_type="int", column_type="int", nullable=False),
+ MySQLColumn(name="text", data_type="text", column_type="text", nullable=True),
+ MySQLColumn(name="provider", data_type="varchar", column_type="varchar", nullable=True),
+ ],
+ )
+ mocker.patch.object(MySQLImplementation, "get_table_metadata", return_value=wide_table)
+ # ...but the streaming read only returns `id`.
+ ss_cursor.description = [("id",)]
+ ss_cursor.fetchmany.side_effect = [[(1,)], []]
+
+ source = MySQLImplementation().build_pipeline(_make_config(), _make_inputs())
+ batches = list(cast(Generator, source.items()))
+
+ assert len(batches) == 1
+ assert batches[0].column_names == ["id"]
+
+
class TestIsBadPlanError:
def test_matches_error_2013(self):
assert _is_bad_plan_error(pymysql.err.OperationalError(2013, "Lost connection to MySQL server during query"))