graphify icon indicating copy to clipboard operation
graphify copied to clipboard

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

Open a9385208438-dev opened this issue 2 weeks ago • 3 comments

Pre-flight checks

  • [x] I have checked the Troubleshooting section in the README

What happened?

Two related gaps in _routine_body_refs (graphify/extractors/sql.py):

  1. Comma join (not Oracle-specific, no @ involved). _BODY_READ_RX (line 295) matches only FROM|JOIN <ident>. In FROM hr.emp a, hr.dept d the second table follows a comma, so it never matches and the edge to hr.dept is lost. This old implicit-join style is ubiquitous in Oracle code, but also valid in ANSI SQL.
  2. Database links. For FROM hr.emp@remote_db and INSERT INTO hr.emp_copy@remote_db, the identifier atom stops before @ and _BODY_TAIL ((?![\w$]|\s*\.), line 294) does not reject it, so the edge goes to the local hr.emp / hr.emp_copy. A remote table is therefore indistinguishable from a local table of the same name, which can create a false link (or merge nodes) between two different databases. (Initially I suspected the table was dropped; it is actually kept with the link removed.)

Steps to reproduce

cat > t3c.sql <<'SQL'
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;
END;
/
SQL
cat > t3a.sql <<'SQL'
CREATE OR REPLACE PROCEDURE p3a IS
BEGIN
  INSERT INTO hr.emp_copy SELECT * FROM hr.emp@remote_db;
END;
/
SQL
python -c "
from pathlib import Path
from graphify.extractors.sql import extract_sql
for f in ('t3c.sql','t3a.sql'):
    r = extract_sql(Path(f)); print(f, [n['label'] for n in r['nodes']], sorted({(e['relation'], e['target']) for e in r['edges'] if e['relation']!='contains'}))"

Error output or graph output

t3c.sql ['t3c.sql', 'p3c()', 'hr.t_out', 'hr.emp'] [('reads_from', 'hr_emp'), ('writes_to', 'hr_t_out')]      # hr.dept missing
t3a.sql ['t3a.sql', 'p3a()', 'hr.emp_copy', 'hr.emp'] [('reads_from', 'hr_emp'), ('writes_to', 'hr_emp_copy')]  # points at local hr.emp, link lost

Expected: p3c reads_from both hr.emp and hr.dept; p3a reads_from a node that records it is remote (e.g. label hr.emp@remote_db, or the existing node plus a remote=remote_db attribute).

Proposed fix

  • Comma join: after a matched FROM/JOIN identifier (and its optional alias), continue consuming ,\s*<ident>[\s+alias] while the next token is a comma, stopping at a keyword (WHERE, GROUP, ORDER, ...), reusing _BODY_NON_TABLES; guard against FROM t1, TABLE(...), (subquery), and function calls as the existing lstrip().startswith("(") check does.
  • Dblink: extend the identifier atom with an optional (?:@[\w$.]+)? suffix; key the stub id including the link (so it does not collide with a local table) and keep the Schema.table label with the @link suffix, or store the link as a node attribute. Alternative minimal behavior that fails closed (CONTRIBUTING item 5): skip references carrying @link rather than misattributing them to a local table.

Graphify version

0.9.104

Operating System

Linux

Python Version

3.12

Installation Method

uv tool install (recommended)

a9385208438-dev avatar Nov 12 '26 07:11 a9385208438-dev

Thanks for opening this issue, @a9385208438-dev. A maintainer will take a look soon.

If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

github-actions[bot] avatar Nov 12 '26 07:11 github-actions[bot]

Looked into this and confirmed the root cause locally in _routine_body_refs (graphify/extractors/sql.py):

  1. Comma joins: _BODY_READ_RX only matches FROM|JOIN <ident>. It doesn't continue consuming subsequent , <ident> tables in FROM a, b, so every table after the first is lost.
  2. @dblink truncation: _BODY_TAIL = r"(?![\w$]|\s*\.)" allows @ to follow the matched table name. The regex matches hr.emp and silently drops @remote_db, falsely linking to the local table.

Local reproduction:

from graphify.extractors.sql import _routine_body_refs

# 1. Comma join loses hr.dept:
_routine_body_refs("INSERT INTO hr.t_out SELECT a.id FROM hr.emp a, hr.dept d WHERE a.d = d.id;")
# Output: [('writes_to', 'hr.t_out', 9), ('reads_from', 'hr.emp', 42)]  # hr.dept dropped

# 2. Dblink stripped:
_routine_body_refs("INSERT INTO hr.emp_copy SELECT * FROM hr.emp@remote_db;")
# Output: [('writes_to', 'hr.emp_copy', 0), ('reads_from', 'hr.emp', 33)]  # @remote_db lost

I am working on a fix for this targeting v8 and will open a PR shortly.

mohitdixit02 avatar Nov 12 '26 08:11 mohitdixit02

Fixed by #5173 (e605f4a4), released in graphify 0.9.108. Thanks @a9385208438-dev for the report and @mohitdixit02 for the fix.

safishamsi avatar Nov 12 '26 22:11 safishamsi