Skip to content

Follow-up to #352: UNNEST(...) alias, alias.field still reported as a table #689

Open

Description

@illes

#352 (PR #617) fixed the simple case. Confirmed on 3.0.1:

Parser("SELECT A, B, metrics.C FROM tbl, UNNEST(metrics) as metrics").tables
# ['tbl']   ✅

Flattening a repeated field off an alias bound by an earlier UNNEST still reports it as a table:

Parser("SELECT h.value FROM `proj.ds.ga_sessions_*` t, UNNEST(t.hits) h, h.customDimensions cd").tables
# ['proj.ds.ga_sessions_*', 'h.customDimensions']   ← expected ['proj.ds.ga_sessions_*']

# tables_aliases == {'t': 'proj.ds.ga_sessions_*', 'cd': 'h.customDimensions'}   ← 'h' missing

h is bound by the UNNEST and never appears in tables_aliases, so a consumer cannot tell a table from a flattened field. This is the standard BigQuery ga_sessions shape.

Activity

  1. Yuren0312 commented on Oct 18, 2026

    @Yuren0312
    Contributor

    Thanks for the clear repro — I can confirm this on current master. The root cause is that sqlglot parses a later alias.field reference (e.g. h.customDimensions after UNNEST(t.hits) h) as an exp.Table node with db=h, so TableExtractor emits h.customDimensions as a table name and the UNNEST alias h is never tracked.

    I've opened #702 with a minimal fix: TableExtractor now collects the aliases introduced by exp.Unnest nodes (both TableAlias.this for UNNEST(x) AS a and TableAlias.columns for UNNEST(x) a) and drops any table whose catalog/db segment matches one of them. The reported query now yields tables == ['proj.ds.ga_sessions_*'] and tables_aliases == {'t': 'proj.ds.ga_sessions_*'}.

    A regression test is included; pytest test/ is 277 passed with 100% coverage, mypy and ruff check are clean. Happy to adjust if you'd prefer a different approach.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions