Fix SQL span summaries for multi-line queries and CTEs #2958
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 410 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.360
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2958"
general
weight: 0.35
chrikrah
Summary
Fixes two issues in
logfire-sdk/logfire/_internal/db_statement_summary.py:1. Multi-line or space-padded CTE queries matched the inner
SELECTinstead of the outerSELECT:After collapsing whitespace,
WITH active_users AS (\n SELECT ...becameWITH active_users AS ( SELECT ....SELECT_CTE_RE = re.compile(rf'WITH (.+?) SELECT (.+?) FROM ({TABLE_RE})')then matched theSELECT ... FROM ...inside the first CTE body instead of the mainSELECTafter the CTE definitions — producing the wrong table name, wrong columns, wrong CTE count, and wrongWHERE …indicator.2. Single newlines were not collapsed:
re.sub(r'\s{2,}', ' ', db_statement)left single\n/\tcharacters intact, so queries formatted with a single newline between clauses (e.g.SELECT ...\nFROM table) failedSELECT_REand fell back tofallback().Replaced
re.sub(r'\s{2,}', ' ', ...)withre.sub(r'\s+', ' ', ...)and replacedSELECT_CTE_REwith a linear-timematch_cte_select()helper using the same quote/parenthesis depth tracking asoutside_parentheses()(which also avoids quadratic regex backtracking inSELECT_CTE_RE).Minimal runnable reproducer
Checklist context (
CONTRIBUTING.md)db_statement_summary.py; affects any multi-line CTE or unindented multi-line query over 80 chars.db_statement_summary.py, easy to verify and merge as-is.db_statement(and removesSELECT_CTE_RE's two-lazy-group backtracking).Verification
ruff checkandruff format --checkpassed on both touched files.pytest tests/test_db_statement_summary.py -vpassed (33 passed, including 3 new tests covering multi-line CTEs, spaces inside CTE parentheses, and unindented multi-lineSELECT).<!-- This is an auto-generated description by cubic. -->
<a href="https://cubic.dev/pr/pydantic/logfire/pull/2958?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->