fix: read SQL text with escape strings, Unicode and quoted names

RA
3d ago
+145-37

Issue for this PR

Closes #1852

Type of change

  • Bug fix

What does this PR do?

Follow-ups from the reviews of #1754 and #1760: how the column answer and dbt_lineage read a model's SQL text.

  • Postgres escape strings (E'it\'s') end at an unescaped quote, so the SELECT items no longer shift.
  • An alias without AS is read after a column that looks like an operator (t.mod id) and after a complete interval literal (interval '1 day' id). An interval field (interval '1' day) is still part of the literal.
  • A quoted name unescapes only its own doubled quote, and names in any script are names.
  • On Postgres and Redshift, a cast followed by collate keeps the column's name. The cast reader stops at 64 levels of nesting and reads anything deeper as no cast.
  • One word list now backs the three guards (operand, end-of-expression, type), so they can't drift apart.

Nothing is reported unaffected, as before.

How did you verify your code works?

Each shape has a test that fails on main, and the allé case right after SELECT is a guard that passes there too. The demo project's answer is unchanged: 11 carry it, 28 may carry it, of 39.

Cost side effects

None (local manifest only).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR
  • I checked for cost side effects (model calls, queries or cloud resources that grow with user data) and described them, or wrote "none"

🤖 Generated with Claude Code

https://claude.ai/code/session_01BXxzvqLZuTJdDJ1Ku2ZMDY


Summary by cubic

Fixes how column answers and dbt_lineage read a model's SQL text so Postgres escape strings (E'it\'s') end at unescaped quotes, aliases without AS are read after operator-like and isnull/notnull names (t.mod id, x isnull flag) and complete interval literals (interval '1 day' id), and quoted names unescape only their own doubled quotes. A name in any script is read whole, so 𐐨from doesn't end a SELECT list and é_1 isn't mistaken for the engine's _1 alias. castColumn stops at 64 levels of nesting and keeps the column name when a cast is followed by collate. Closes #1852.

Summary by CodeRabbit

  • Bug Fixes
    • Improved SQL handling for PostgreSQL escape strings, quoted identifiers, and Unicode names.
    • More accurately identifies aliases, interval expressions, and column names in casts with COLLATE clauses.
    • Preserved column detection in deeply nested expressions, with parsing limited to 64 levels.
    • Improved recognition of SELECT-list boundaries and lineage for Unicode identifiers, including names adjacent to SQL keywords.