: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(mysql): skip unquotable columns in row-size estimation (#63791)
Tom Owersefdfecbef8c7
diff --git a/posthog/temporal/data_imports/sources/mysql/mysql.py b/posthog/temporal/data_imports/sources/mysql/mysql.pyindex 84f9db8f1c96..47b011fbe10f 100644--- a/posthog/temporal/data_imports/sources/mysql/mysql.py+++ b/posthog/temporal/data_imports/sources/mysql/mysql.py@@ -677,7 +677,30 @@ class MySQLImplementation(SQLSourceImplementation[MySQLSourceConfig, pymysql.Conreturn Nonecolumns = [row[0] for row in rows]- length_sum = " + ".join(f"LENGTH(COALESCE({_IDENTIFIER_QUOTER.quote(col)}, ''))" for col in columns)+ # Column names come from the DB catalog and can legitimately contain+ # characters the identifier allowlist rejects (e.g. `:` or spaces in+ # `Ach:CompanyId`). Row-size estimation is best-effort, so skip any+ # column we can't safely quote rather than abandoning the whole+ # estimate. The allowlist stays the hard boundary for user-supplied+ # identifiers elsewhere.+ quoted_columns = []+ skipped_columns = []+ for col in columns:+ try:+ quoted_columns.append(_IDENTIFIER_QUOTER.quote(col))+ except InvalidIdentifierError:+ skipped_columns.append(col)++ if skipped_columns:+ logger.debug(+ f"fetch_average_row_size: skipping {len(skipped_columns)} unquotable column(s): {skipped_columns}."+ )++ if not quoted_columns:+ logger.debug("fetch_average_row_size: No quotable columns found.")+ return None++ length_sum = " + ".join(f"LENGTH(COALESCE({quoted}, ''))" for quoted in quoted_columns)# length_sum and inner_query are built from sanitized identifiers;# no user-supplied values are interpolated into the SQL itself.size_query = "SELECT AVG(" + length_sum + ") as avg_row_size FROM (" + inner_query + " LIMIT 1000) as t"diff --git a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.pyindex 3d4b7bd97365..a33b66276638 100644--- a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py+++ b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py@@ -380,15 +380,31 @@ class TestFetchAverageRowSize:assert "`email`" in sqlassert "LENGTH(COALESCE(`id`" in sql- def test_rejects_malformed_column_names(self, impl, cursor, logger):- # If INFORMATION_SCHEMA somehow returns a weird column name, we must- # reject it rather than splice it into SQL. The quoter raises; the- # method catches and returns None.+ def test_returns_none_when_no_columns_are_quotable(self, impl, cursor, logger):+ # A column name we can't safely quote is never spliced into SQL. When+ # it's the only column there's nothing left to estimate from, so we+ # return None rather than raising.cursor.fetchall.return_value = [("bad;col",)]cursor.fetchone.return_value = (1,)result = impl.fetch_average_row_size(cursor, "db", "t", "SELECT 1", {}, logger)assert result is None+ def test_skips_unquotable_columns_and_estimates_from_rest(self, impl, cursor, logger):+ # Real MySQL columns can contain characters the identifier allowlist+ # rejects (e.g. `:` in `Ach:CompanyId`). Row-size estimation is+ # best-effort: skip the columns we can't quote and estimate from the+ # rest instead of abandoning the whole query.+ cursor.fetchall.return_value = [("id",), ("Ach:CompanyId",), ("email",)]+ cursor.fetchone.return_value = (100,)+ result = impl.fetch_average_row_size(cursor, "db", "t", "SELECT * FROM x", {}, logger)+ assert result == 100+ second_call = cursor.execute.call_args_list[1]+ sql = second_call.args[0]+ assert "`id`" in sql+ assert "`email`" in sql+ # The unquotable column is neither quoted nor spliced in raw.+ assert "Ach:CompanyId" not in sql+def test_returns_none_on_exception(self, impl, cursor, logger):cursor.execute.side_effect = RuntimeError("boom")result = impl.fetch_average_row_size(cursor, "db", "t", "SELECT 1", {}, logger)