graphify icon indicating copy to clipboard operation
graphify copied to clipboard

SQL: a subquery alias can become a reads_from target with tree-sitter-sql 0.3.13

Open sub4biz opened this issue 3 weeks ago • 2 comments

Summary

On clean graphify v0.9.106 with tree-sitter-sql 0.3.13, an alias of a parenthesized subquery is emitted as a table read.

Reproduction

Install the dependencies from a clean v0.9.106 checkout with python -m pip install ".[sql]" "tree-sitter-sql==0.3.13", then run:

from pathlib import Path
from tempfile import TemporaryDirectory
from graphify.extract import extract_sql
with TemporaryDirectory() as tmp:
    path = Path(tmp) / 'demo.sql'
    path.write_text('CREATE FUNCTION median(anyarray) RETURNS double precision\n'
                    'LANGUAGE sql IMMUTABLE AS $_$\n'
                    'WITH q AS (SELECT val FROM unnest($1) val),\n'
                    'cnt AS (SELECT COUNT(*) AS c FROM q)\n'
                    'SELECT AVG(val) FROM (SELECT val FROM q\n'
                    'LIMIT 2 - MOD((SELECT c FROM cnt), 2)\n'
                    'OFFSET GREATEST(CEIL((SELECT c FROM cnt) / 2.0) - 1, 0)) q2;\n'
                    '$_$;\n', encoding='utf-8')
    graph = extract_sql(path)
    nodes = {n['id']: n['label'] for n in graph['nodes']}
    print(sorted((nodes[e['source']], e['relation'], nodes[e['target']]) for e in graph['edges'] if e['relation'] != 'contains'))

Observed: [('median()', 'reads_from', 'q2')]. Expected: no edge to q2, which names a subquery rather than a table. With tree-sitter-sql 0.3.11 this example produces no such edge.

sub4biz avatar Nov 11 '26 09:11 sub4biz

Thanks for opening this issue, @sub4biz. 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 11 '26 09:11 github-actions[bot]

Fixed by #5156 (794f8aa4), released in graphify 0.9.108. Thanks @sub4biz for the report and @Yi-111-a for the fix.

safishamsi avatar Nov 12 '26 22:11 safishamsi