: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
chore(tests): replace sqlparse comment-strip in capture_queries with a head scanner (#69441)
Paul D'Ambra841b93cf48fa
diff --git a/posthog/test/base.py b/posthog/test/base.pyindex 038f14b858b5..f827542887a0 100644--- a/posthog/test/base.py+++ b/posthog/test/base.py@@ -1574,12 +1574,48 @@ class ClickhouseTestMixin(QueryMatchingTest):def capture_queries_startswith(self, query_prefixes: Union[str, tuple[str, ...]]):return self.capture_queries(lambda x: x.startswith(query_prefixes))+ @staticmethod+ def _strip_leading_sql_comments(sql: str) -> str:+ """Drop leading whitespace and comments (keeping any parens) so head-anchored query+ filters see the first real token. Equivalent to sqlparse.format(strip_comments=True)+ for every filter used with capture_queries — all of them match on the statement head —+ but without paying sqlparse's full-statement lexing (~100ms on large generated SQL)."""+ i, n, parens = 0, len(sql), []+ while i < n:+ char = sql[i]+ if char.isspace():+ i += 1+ elif char == "(":+ parens.append("(")+ i += 1+ elif sql.startswith("/*", i):+ end = sql.find("*/", i + 2)+ if end == -1:+ break+ i = end + 2+ elif sql.startswith("--", i):+ newline = sql.find("\n", i)+ if newline == -1:+ i = n+ break+ i = newline + 1+ else:+ break+ return "".join(parens) + sql[i:]+@contextmanagerdef capture_queries(self, query_filter: Callable[[str], bool]):+ """Capture ClickHouse queries that pass query_filter.++ The filter receives the query after stripping leading whitespace and comments (head-only+ — see _strip_leading_sql_comments). All built-in filters (capture_select_queries,+ capture_queries_startswith) are head-anchored, so this contract holds for them. Do not+ pass a filter that matches on mid-statement text; it will not see comments embedded there.+ """queries = []def execute_wrapper(original_client_execute, query, *args, **kwargs):- if query_filter(sqlparse.format(query, strip_comments=True).strip()):+ if query_filter(self._strip_leading_sql_comments(query)):queries.append(query)return original_client_execute(query, *args, **kwargs)