fix(ol_dbt_cli): bound broken-column-macro collapse to prevent cross-CTE overrun#2442
Open
blarghmatey wants to merge 2 commits into
Open
fix(ol_dbt_cli): bound broken-column-macro collapse to prevent cross-CTE overrun#2442blarghmatey wants to merge 2 commits into
blarghmatey wants to merge 2 commits into
Conversation
…CTE overrun sql_parser.py's Jinja-macro-tail collapse used an unbounded lazy loop to find the next `) as alias` after a placeholder. A bare (unaliased) macro call repeated in a GROUP BY list has no real terminal, so the search ran past the GROUP BY, the CTE's closing paren, and the outer SELECT keyword before seizing on an unrelated downstream alias -- corrupting the parsed SQL and producing false yaml_sql_sync/broken_ref_columns errors on marts__combined_program_enrollment_detail (which blocked this PR's CI after rebasing onto the new dbt PR CI gate). Bound the continuation to 6 lines, which still covers the two existing orphaned-tail regression tests, and make the leading ')' before 'as' optional so a plain multi-line macro+alias (no stray tokens) resolves on the very next line instead of searching further.
🔎 ol-dbt impact — column-level blast radius✅ No column-level downstream impact detected for the changed models. Posted by |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a false-positive in ol-dbt validate’s SQL parsing where broken-column macro placeholder collapsing could scan unboundedly forward and incorrectly swallow unrelated columns (even across CTE boundaries). This improves reliability of the new dbt PR CI gate by preventing validator-induced parse corruption.
Changes:
- Bound the broken-column placeholder “continuation line” regex to a small fixed window to prevent cross-CTE overruns.
- Adjusted the terminal alias pattern so plain
{{ macro(...) }}followed byas aliason the next line terminates immediately. - Added a regression test covering the “plain multiline alias” case that previously could trigger runaway collapsing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/ol_dbt_cli/ol_dbt_cli/lib/sql_parser.py | Bounds the broken-column collapse regex and broadens the terminal alias match to prevent over-consuming SQL. |
| src/ol_dbt_cli/tests/test_sql_parser.py | Adds regression coverage ensuring plain multiline macro aliases don’t cause later columns to be swallowed. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The new test's CTE was named 'combined' and its own body did 'from combined', an invalid recursive self-reference without WITH RECURSIVE. Rename the CTE's underlying source to raw_source so the test SQL is valid while still exercising the same placeholder-collapsing behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What are the relevant tickets?
N/A — found while triaging
dbt PR CIfailures (added in #2430) on two rebased PRs (#2416, #2400).Description (What does it do?)
Fixes a false-positive bug in
ol-dbt validate's SQL parser (src/ol_dbt_cli/ol_dbt_cli/lib/sql_parser.py) that was blocking otherwise-correct PRs from passing the newdbt PR CIgate._collapse_broken_column_expressionshandles the case where a Jinja macro call like{{ some_macro(...) }}splits a SQL column expression across the}}boundary, leaving orphaned tokens before the real) as alias. It found the real alias with an unbounded lazy regex search forward through the file.That's fine when the macro call actually gets an alias nearby. But a macro call can also appear unaliased, e.g. repeated bare in a
GROUP BYlist:The bare
GROUP BYoccurrence has no real terminal) as aliasnearby, so the unbounded search kept scanning — straight through the rest of theGROUP BYlist, the CTE's closing paren, and the outerselectkeyword — until it landed on the next unrelatedas aliasseveral columns and one CTE away (as capstone_indicator). It then collapsed that entire span into a single bogus placeholder, deleting a dozen real columns from the parsed output.This exact pattern hit
marts__combined_program_enrollment_detail.sqlin production and produced falseyaml_sql_sync/broken_ref_columnserrors on two unrelated PRs (#2416, #2400) — the columnsuser_hashed_id,program_completion_days,capstone_indicator, andunique_courses_taken_in_programare all present in the actual compiled SQL; the validator just couldn't see them because the corrupted intermediate SQL no longer contained them.Fix:
)beforeasoptional in the terminal pattern, so a plain macro call followed byas aliasalone on the very next line (no stray tokens — nothing to actually collapse) terminates immediately instead of needing a paren that was never going to appear.Added a regression test (
test_broken_column_regex_stops_at_plain_multiline_alias) reproducing the bare-GROUP BY-placeholder shape.How can this be tested?
Also verified end-to-end against the real model that triggered this:
Before the fix: 2 false
ERRORs onmarts__combined_program_enrollment_detail/program_enrollment_with_user_report. After: those 2 errors are gone, and a full-repovalidatediff shows zero newly-introduced findings (3 unrelated pre-existing errors elsewhere are unchanged).Additional Context
This was found and fixed while rebasing several open PRs onto
mainto pick up the new lightweight CI (#2430). The identical fix was cherry-picked directly onto the two affected branches (#2416, #2400) to unblock them immediately; once this PR merges, those branches will be rebased ontomainto drop the duplicate cherry-picked commit in favor of this one.