Skip to content

Allow parentheses in GROUP BY #390

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

Closed
wants to merge 10 commits into from
Closed

Conversation

yuval-illumex
Copy link
Contributor

In TrinoDB, GROUP BY Can have parentheses with multiple values.

Example Query:

SELECT orderstatus,orderpriority,COUNT(*)
FROM tpch.sf1.orders 
GROUP BY (orderstatus,orderpriority)
LIMIT 10;

@yuval-illumex yuval-illumex reopened this Dec 26, 2021
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

thank you for the contribution -- sorry for the delay in response @yuval-illumex

So I was playing around with postgres and there is a difference between col1, col2 and (col1, col2). Specifically, I think (col1, col2) is treated as some tuple type.

I think this PR effectively strips off the parenthesis

alamb=# select (t1_id, t1_name), count(*) from t1 group by (t1_id, t1_name);
  row   | count 
--------+-------
 (44,d) |     1
 (77,e) |     1
 (11,a) |     1
 (22,b) |     1
 (33,c) |     1
(5 rows)

alamb=# select t1_id, t1_name, count(*) from t1 group by t1_id, t1_name;
 t1_id | t1_name | count 
-------+---------+-------
    44 | d       |     1
    77 | e       |     1
    11 | a       |     1
    22 | b       |     1
    33 | c       |     1
(5 rows)

Turns out that mysql just rejects this syntax

mysql> select (t1_id, t1_name), count(*) from t1 group by (t1_id, t1_name);
ERROR 1241 (21000): Operand should contain 1 column(s)

I didn't catch it quite in time, but the same thing is true for distinct: #391

I don't have a trino installation around to test -- what does trino do with the syntax above?

@yuval-illumex
Copy link
Contributor Author

yuval-illumex commented Feb 6, 2022

Thanks @alamb

The following syntax works in Trino:

image

This is the default schemas and tables coming with tirno, so you can tun Trino with docker to play with using this query.

I'm more worried about GroupBy rather then Select - I think the parentheses are more common in GroupBy (and Distinct)
If this PR is a problem. I would love to find a way to make it work in Groupby/Distinct

@alamb
Copy link
Contributor

alamb commented Feb 7, 2022

Thanks @yuval-illumex -- it looks to me like in this case too Trino is treating GROUP BY (a, b) as GROUP BY {set_expr} while the change in this PR effectively ignores the parenthesis and treats GROUP BY (a, b) as GROUP BY a, b

@alamb
Copy link
Contributor

alamb commented Feb 7, 2022

@yuval-illumex
Copy link
Contributor Author

@alamb parse_tuple do works, but I could use some help.
I don't understand how it goes with parse_group_by_expr:
https://github.com/sqlparser-rs/sqlparser-rs/blob/2614576dbf140824cb659bcc7026b21bd1b82f0a/src/parser.rs#L623

It parses expressions, and I'm not sure how to identify when it should be an expression and when should it be a tuple. as the function must return an expression.

@alamb
Copy link
Contributor

alamb commented Feb 8, 2022

@alamb parse_tuple do works, but I could use some help.

Looking into it

@alamb
Copy link
Contributor

alamb commented Feb 8, 2022

@yuval-illumex this is what I had in mind (it should cover all paren cases): #414

@alamb
Copy link
Contributor

alamb commented Feb 8, 2022

Probably not needed after #414

@alamb alamb closed this Feb 8, 2022
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.

2 participants