Skip to content

fix(sql): preserve comma joins and dblink suffixes in routine bodies (#5169) - #5173

Closed
mohitdixit02 wants to merge 1 commit into
Graphify-Labs:v8from
mohitdixit02:fix/sql-comma-joins-dblink-5169
Closed

mohitdixit02 wants to merge 1 commit into
Graphify-Labs:v8from
mohitdixit02:fix/sql-comma-joins-dblink-5169

Conversation

@mohitdixit02

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #5169.

In graphify/extractors/sql.py, SQL routine body scanning suffered from two reference extraction gaps:

  1. _BODY_READ_RX only matched table identifiers immediately preceded by FROM or JOIN. In classic multi-table comma joins (FROM hr.emp a, hr.dept d), any table following a comma was missed, losing reads_from edges for all tables after the first.
  2. _BODY_TAIL = r"(?![\w$]|\s*\.)" permitted @ immediately after table identifiers. When reading FROM hr.emp@remote_db or writing to INSERT INTO hr.emp_copy@remote_db, the regex matched up to @ and silently stripped the @dblink suffix, falsely linking the reference to a local table of the same name.

This change:

  • Iteratively consumes comma-separated table references (,\s*<ident>[\s+alias]) following a FROM clause in _routine_body_refs, stopping at SQL keywords (WHERE, GROUP BY, ORDER BY) and parenthesized function calls.
  • Disallows @ in _BODY_TAIL ((?![\w$@]|\s*\.)) and incorporates optional @dblink suffix matching in _BODY_TABLE_IDENT, _clean_ident, _norm_ident, and the AST _walk_from_refs path. Remote tables are uniquely preserved and keyed as distinct nodes.
-- Before: only hr.emp read; hr.dept dropped; @remote_db silently stripped to local table
-- After: hr.emp and hr.dept both read; hr.emp@remote_db preserves distinct remote node
CREATE OR REPLACE PROCEDURE p3c IS
BEGIN
    INSERT INTO hr.t_out SELECT a.id FROM hr.emp a, hr.dept d WHERE a.d = d.id;
    INSERT INTO hr.emp_copy SELECT * FROM hr.emp@remote_db;
END;

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

  • Read the CONTRIBUTING.md /CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

    uv run pytest tests/test_sql_tsql_procedures.py tests/test_sql_go_batches.py tests/test_sql_cross_file_tables.py tests/test_sql_comma_joins_and_dblink.py -v
    uv run ruff check graphify/extractors/sql.py tests/test_sql_comma_joins_and_dblink.py
    uv run python -m graphify update .

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @mohitdixit02. 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.10 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 12, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 2 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 4 changed function(s) (4 sampled, not proven).

Formal verification. PR-changed functions: 4/4 verified (0 proven, 4 may-equivalent, 0 distinguished) · 0 not verified.


Graphify review — findings

Adds comma-join and @dblink support to SQL routine-body extraction, so FROM hr.emp a, hr.dept d now yields a reads_from edge for every table instead of only the first. Remote references like hr.emp@remote_db keep their link suffix on read and write targets, and they stay distinct nodes from the local hr.emp. The @ handling in _clean_ident and _norm_ident splits on the first @ without regard to quoting, so a quoted or bracketed name that contains @ can get a garbled label or a mismatched lookup key.

Worth a look

  • _norm_ident splits on '@' inside quoted/bracketed parts, breaking lookup key equivalence — graphify/extractors/sql.py:464 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
  • _clean_ident splits on '@' inside bracketed/quoted identifiers, garbling labels — graphify/extractors/sql.py:443 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3176 functions depend on the 47 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract_sql() — 47 callers, 13 callees

Verification — 3176 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1229 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

7 of 560 test file(s) selected (1%) via static blast radius.

  • tests/test_extract.py — impact
  • tests/test_multilang.py — impact
  • tests/test_pg_introspect.py — impact
  • tests/test_python_member_call_language_gate.py — impact
  • tests/test_sql_comma_joins_and_dblink.py — impact, changed-test
  • tests/test_sql_go_batches.py — impact
  • tests/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.

Formal verification

No difference found (not proven): No behavior difference found in \_clean\_ident (not a proof).

The verifier ran both versions of \_clean\_ident on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

No difference found (not proven): No behavior difference found in \_norm\_ident (not a proof).

The verifier ran both versions of \_norm\_ident on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

No difference found (not proven): No behavior difference found in \_routine\_body\_refs (not a proof).

The verifier ran both versions of \_routine\_body\_refs on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

No difference found (not proven): No behavior difference found in extract\_sql (not a proof).

The verifier ran both versions of extract\_sql on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

· 1 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Landed on v8 as e605f4a via authorship-preserving cherry-pick, released in graphify 0.9.108. Thanks @mohitdixit02.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: SQL routine bodies: Oracle comma joins (FROM a x, b y) lose every table after the first, and @dblink suffixes are silently stripped

2 participants