Skip to content

Conversation

@geooo109
Copy link
Collaborator

@geooo109 geooo109 commented Nov 20, 2025

Fixes #6358

This PR fixes the qualification of GENERATE_SERIES and table as column.

GENERATE_SERIES

SELECT * FROM GENERATE_SERIES(1, 2);
 generate_series 
-----------------
               1
               2
(2 rows)

In postgres GENERATE_SERIES creates a column generate_series, thus in order to safely qualify this we have the two following cases:

  1. Without alias: we just create an alias on optimization time and use the default generate_series column:
SELECT generate_series FROM GENERATE_SERIES(1,2); => SELECT generate_series FROM GENERATE_SERIES(1, 2) AS _q_0(generate_series);
  1. With alias: we wrap it with a generated alias from the optimizer:
SELECT g FROM GENERATE_SERIES(1,2) AS g; => SELECT g FROM GENERATE_SERIES(1, 2) AS _q_0(g);

Table projection
postgres supports:

SELECT t FROM t;

The type of t is record, which is an abstract type. We treat this case as the bigquerry table-column reference.

@geooo109 geooo109 changed the title fix(optimizer): postgres qualify GENERATE_SERIES and table projection fix(optimizer): postgres qualify GENERATE_SERIES and table as column reference Nov 20, 2025
elif not table_alias.args.get("columns"):
original_alias_name = table_alias.alias_or_name
table_alias.set("this", exp.to_identifier(next_alias_name()))
table_alias.set("columns", [exp.to_identifier(original_alias_name)])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel great about this change.

I think there are other dialects besides postgres, and also other functions besides generate_series, that are "special", in the sense that they generate a single column that you can project directly. Manually handling these doesn't really scale, so we should find a cleaner way to support this case.

Since we now iterate over all tables in L161, perhaps we can handle these cases there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, I had the same intuition when I was implementing it, will make it more generic and re-push.

@georgesittas georgesittas marked this pull request as draft November 20, 2025 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using generate_series in postgres with alias

4 participants