Repository navigation
fix(sql): one routine node for partially parsed T-SQL procedure headers (#5236) - #5324
Deepjyoti-Sarmah wants to merge 5 commits into
Conversation
From tree-sitter-sql 0.3.12 a T-SQL CREATE PROCEDURE header can partially parse with a misread name: unparenthesised params absorbed or taken as the name, a ""/]] escape truncating it. Each case must yield one routine node with the full name and no doubled reads/writes. The CREATE FUNCTION "a""b" case already fails on 0.3.11.
The tree walk and the whole-file recovery scan named one declaration twice when the grammar misread a partially parsed routine header, leaving two routine nodes and two copies of their edges. A walk routine on the same line as a recovered CREATE under a different id is that misread: remove it, its edges and its name lookups, and let recovery own the node and its refs.
The grammar partially parses T-SQL AS BEGIN...END procedures from tree-sitter-sql 0.3.12, so update the recovery regex comment and the two regression test docstrings that relied on the old behaviour.
|
Thanks for the pull request, @Deepjyoti-Sarmah. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify code review 
Review completed · 260a359527d9
Note
Merge confidence: 5/5 · Nothing blocking · 2 possible issues to check · 🟠 High impact
2 possible issues not confirmed
The SQL recovery scan now uses recovered names by line to distinguish parser-created routines with misread names.
Full summary
The SQL recovery scan now uses recovered names by line to distinguish parser-created routines with misread names. It removes unmatched parser nodes and edges, then adds the recovered routine and derives its table references.
No confirmed issues. We found 2 possible issues we couldn't confirm; see below.
Not confirmed (2 possible issues)
Leads worth a look; we didn't double-check these. They don't change the score.
Not confirmed · The trigger executes edge check is skipped on tree-sitter-sql 0.3.12 and later, so that edge is never verified there Inspect source
Detailed review · not confirmed
The test asserts the executes edge only when the installed grammar is older than 0.3.12. On newer grammars the check is skipped silently, so the test passes without checking the trigger edge.
The gate if not _ts_sql_at_least((0, 3, 12)) depends only on the grammar version. The comment says the trigger is only recovered on newer grammars and that recovery emits no executes edge there, but the test never asserts what the newer path does. The gate should key on whether the walk parses the trigger, or the test should assert the expected edge set for each grammar version.
-
Not checked: whether the installed grammar version actually parses the trigger on the walk path
-
Not checked: the expected edge set for grammars at 0.3.12 and later
-
Found by the per-file sweep; not double-checked.
Not confirmed · A genuine routine sharing a CREATE line with a misread one can be deleted when the recovery regex does not match it Inspect source
Detailed review · not confirmed
The cleanup loop drops any tree-built routine on a line whose id is not among the regex matches on that line, so a real second routine on the same line can be removed.
recovered_on_line is built only from routine_matches (lines 1237-1241), but the tree-built nodes come from the walk. The comment at lines 1234-1236 says the other routine on a shared line is real, yet the filter at lines 1253-1254 removes any walk node whose id is not in that regex set. If the tree parsed a routine the regex misses, nothing recreates it, and lines 1255-1260 delete its node, edges and routine_nids entry.
-
Did not verify whether tree-sitter-sql can produce a tree routine on the same line that the regex skips
-
Did not inspect the walk code that populates structured_routine_lines and routine_nids
-
Found by the per-file sweep; not double-checked.
Code graph and static checks · 3511 functions depend on this change
Impact & health
Impact — 3511 functions depend on the 217 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
extract_sql()— 62 callers, 14 callees
Formal checks — 3511 functions this change can affect weren't formally checked in this review. Formal checks are informational only.
Gate & verification
✅ PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 1850 function(s) in the blast radius were not formally verified this run
Test selection
10 of 593 test file(s) selected (2%) via static blast radius.
tests/test_extract.py— impacttests/test_multilang.py— impact, changed-testtests/test_pg_introspect.py— impacttests/test_python_member_call_language_gate.py— impacttests/test_sql_comma_joins_and_dblink.py— impacttests/test_sql_go_batches.py— impacttests/test_sql_non_ascii_table_names.py— impacttests/test_sql_oracle_editionable.py— impacttests/test_sql_oracle_packages.py— impacttests/test_sql_tsql_procedures.py— impact
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Every flagged function
extract_sql()ingraphify/extractors/sql.py:550· 62 callers, 14 callees: fans out to 14 callees (efferent coupling); 62 callers depend on it (afferent coupling)
Recovery dropped every walk routine on a CREATE's line whose id differed from the match, so a correctly parsed neighbour lost its node and the trigger's executes edge. Drop only ids no recovery match on that line names.
|
Confirmed the P2 and fixed in 1d1d142. It was a bit wider than reported: the same-line neighbour lost its node too, not only the trigger's Recovery now collects every id the scan finds on each line first, and drops a walk routine only when no match on its line names it. The misread header is still dropped; a real routine sharing the line is kept with its edges and Added |
…gger From tree-sitter-sql 0.3.12 the trigger in this test is only recovered, and recovery emits no executes edge; v8 gives none there either. CI runs 0.3.11, so this passed there and failed locally on 0.3.12/0.3.13. The label check, which catches the dropped neighbour routine, still runs on every version.
What does this PR do?
Fixes #5236. On tree-sitter-sql 0.3.12 and later, one T-SQL
CREATE PROCEDUREcan produce two routine nodes. The second one has a broken name, and every read and write edge is attached to both.The SQL extractor names a routine in two places:
The merge only works if both places read the same name. Up to 0.3.11 the grammar had no parse for T-SQL's
AS BEGIN ... ENDbody, so the whole header was oneERRORnode and only the recovery scan named the routine. From 0.3.12 the grammar parses the header ascreate_procedure, but it reads the name wrong:object_reference[dbo].[usp_Load]+@BatchId INTdbo].[usp_Load]\n @BatchId INTdbo.usp_Load@BatchId INT()p @x INTINTINT()"dbo"."a""b""dbo"."a", then"b"as a separate ERROR node"dbo"."a"()[a]]b]bb()The recovery scan reads each of these correctly, so the file ends up with two ids for one declaration. A profiler trace (
sys.setprofileon_add_node/_add_edge) on the issue's bracketed input shows the walk adding the bad node withreads_fromandwrites_toatsql.py:733, and recovery adding the good node with the same two edges atsql.py:1239.The fix is in the recovery loop:
Because the walk's node is removed, recovery owns the routine and derives its edges from the body text, the same way it did on 0.3.11.
Why the walk's edges are dropped too
The walk's edges for a misread routine can't be trusted either.
test_procedure_write_makes_a_non_read_edge[MERGE]passes on v8 only because of the duplicate. ForCREATE PROCEDURE dbo.W @id INT AS BEGIN MERGE INTO dbo.Payments ..., the walk's node has no edges at all, and thewrites_to dbo.Paymentsedge comes from the recovery node. A fix that only corrected the walk's name (so recovery saw the id as already found and skipped its body scan) broke that test. I tried it.Three commits
The tests go in first and fail on their own. Reverting
0fb14ba7removes the fix and leaves the tests, which then fail. The last commit only corrects a code comment and two test docstrings that saidAS BEGINnever parses.Type of change
Verification & Invariants
Invariant: one declaration gives one routine node, named the way the source text spells it, with each edge once.
Merge Danger
Door: two-way. No schema, id format or cache change. Reverting
0fb14ba7restores v8 behaviour.Blast Radius: small.
CREATEand has a different id..sqlfixture undertests/is identical to v8 on both 0.3.11 and 0.3.13. I compared every node and edge.Limitations
CREATE OR ALTER. A routine whoseCREATEkeyword and grammar node start on different lines would not be matched. I haven't found such a shape.get_userintests/fixtures/sample.sql,RETURN QUERY SELECT * FROM usersgives noreads_from usersedge on v8, on either grammar. Letting recovery also scan the bodies of error-bearing routines would add that edge, but it changes output outside this issue, so it belongs in its own issue.How was this tested?
Before and after, the issue's own script on tree-sitter-sql 0.3.13:
New tests in
tests/test_multilang.py. Each asserts exactly one routine node with the full name, and that itsreads_from/writes_toedges appear once:SQL tests (
-k sql) at each commit, under both grammar versions:340268a7(no new tests)304c8321tests0fb14ba7fixe2aa55f6docsI measured three other fixes against the same tests before settling on this one:
p @x INT: the grammar readsINTwith no error near the nameMERGEwrite test (see above)get_useredge above, outside this issueFull suite on 0.3.13: 11034 passed, 71 failed. All 71 also fail on v8
340268a7in the same environment. They're Terraform, Pascal, C# and install tests, and this venv lacks the optionaltree_sitter_hcl,tree_sitter_pascalandtree_sitter_language_packgrammars. No SQL test fails.Not tested: tree-sitter-sql releases before 0.3.11, and Windows. The change adds no path handling.
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)