[Bug]: SQL routine bodies: Oracle comma joins (FROM a x, b y) lose every table after the first, and @dblink suffixes are silently stripped
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):
- Comma join (not Oracle-specific, no
@involved)._BODY_READ_RX(line 295) matches onlyFROM|JOIN <ident>. InFROM hr.emp a, hr.dept dthe second table follows a comma, so it never matches and the edge tohr.deptis lost. This old implicit-join style is ubiquitous in Oracle code, but also valid in ANSI SQL. - Database links. For
FROM hr.emp@remote_dbandINSERT 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 localhr.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 againstFROM t1, TABLE(...),(subquery), and function calls as the existinglstrip().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 theSchema.tablelabel with the@linksuffix, or store the link as a node attribute. Alternative minimal behavior that fails closed (CONTRIBUTING item 5): skip references carrying@linkrather 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)
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.
Looked into this and confirmed the root cause locally in _routine_body_refs (graphify/extractors/sql.py):
- Comma joins:
_BODY_READ_RXonly matchesFROM|JOIN <ident>. It doesn't continue consuming subsequent, <ident>tables inFROM a, b, so every table after the first is lost. @dblinktruncation:_BODY_TAIL = r"(?![\w$]|\s*\.)"allows@to follow the matched table name. The regex matcheshr.empand 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.
Fixed by #5173 (e605f4a4), released in graphify 0.9.108. Thanks @a9385208438-dev for the report and @mohitdixit02 for the fix.