Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouping sets fails on struct columns with dot notation #22604

Open
cploonker opened this issue Jul 6, 2024 · 7 comments
Open

Grouping sets fails on struct columns with dot notation #22604

cploonker opened this issue Jul 6, 2024 · 7 comments

Comments

@cploonker
Copy link

cploonker commented Jul 6, 2024

Following query works

SELECT pr.name as name, MAX(pr.age) as age, MAX(city) as city
FROM (
  SELECT CAST(pr AS ROW(name VARCHAR, age int)) as pr, city
  FROM(
    VALUES 
      (ROW('John Doe', 30), 'New York'),
      (ROW('Jane Doe', 25), 'Los Angeles')
  ) AS t(pr, city)
)
GROUP BY pr.name

However following query fails

SELECT pr.name as name, MAX(pr.age) as age, MAX(city) as city
FROM (
  SELECT CAST(pr AS ROW(name VARCHAR, age int)) as pr, city
  FROM(
    VALUES 
      (ROW('John Doe', 30), 'New York'),
      (ROW('Jane Doe', 25), 'Los Angeles')
  ) AS t(pr, city)
)
GROUP BY GROUPING SETS (pr.name)

Error

trino error: TrinoUserError(type=USER_ERROR, name=INVALID_COLUMN_REFERENCE, message="line 11:26: GROUP BY expression must be a column reference: pr.name

Shouldn't both work?

@cploonker
Copy link
Author

Presto has the same issue: prestodb/presto#23151

@hashhar
Copy link
Member

hashhar commented Jul 11, 2024

cc: @martint

@martint
Copy link
Member

martint commented Aug 9, 2024

Per the SQL specification:

Each <grouping column reference> shall unambiguously reference a column of the table resulting from the <from clause>

The way the GROUPING SETS, ROLLUP and CUBE clauses are implemented follow the specification to the letter. Grouping by an expression (in the case of a vanilla GROUP BY) is an extension to the specification, which predates the implementation of those more advance grouping set qualifiers. We could consider adding the same extensions to those constructs, but there are some non-trivial implications for how grouping() and other operations such as non-deterministic functions would work, and that would need to be sorted out.

@cploonker
Copy link
Author

Is a struct field not considered a column?

@martint
Copy link
Member

martint commented Aug 9, 2024

No, that’s considered a “complex expression” (a field dereference expression)

@cploonker
Copy link
Author

Got it. Thanks for the explaination.
So this is a case of SQL standard coming in the way of ease of use.

@bristy
Copy link

bristy commented Nov 11, 2024

@cploonker Is there any workaround for this limitation?

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

No branches or pull requests

4 participants