#8462·sqlglot

Postgres Dialect : LATERAL UNNEST drops WITH ORDINALITY alias column

Author: SoTrxCreated Sep 29, 2026Updated Sep 29, 2026

Hey,

First of all, thanks for your work !

I'm using sqlglot (version 30.20.0), and I encountered an issue when trying to convert a query using LATERAL UNNEST back and forth.

python
from sqlglot import parse_one

# This works
sql = "SELECT r.idx FROM t, UNNEST(t.a) WITH ORDINALITY AS r(v, idx)"
print(parse_one(sql, read="postgres").sql(dialect="postgres"))
# SELECT r.idx FROM t, UNNEST(t.a) WITH ORDINALITY AS r(v, idx)

# Adding lateral makes idx disappear
sql = "SELECT r.v, r.idx FROM t, LATERAL UNNEST(t.a) WITH ORDINALITY AS r(v, idx)"
print(parse_one(sql, read="postgres").sql(dialect="postgres"))
# SELECT r.v, r.idx FROM t, LATERAL UNNEST(t.a) WITH ORDINALITY AS r(v)
-- ERROR:  column r.idx does not exist

Could you look into this ?