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 by column position #110

Closed
alamb opened this issue Apr 26, 2021 · 3 comments · Fixed by #519
Closed

Grouping by column position #110

alamb opened this issue Apr 26, 2021 · 3 comments · Fixed by #519
Labels
datafusion Changes in the datafusion crate

Comments

@alamb
Copy link
Contributor

alamb commented Apr 26, 2021

Note: migrated from original JIRA: https://issues.apache.org/jira/browse/ARROW-10374

It would be great to have the support of grouping by column position instead of grouping by exact expression. For example:

SELECT state, COUNT(*) FROM customers GROUP BY 1

For example, for a query like

> select database_name, storage, sum(estimated_bytes) from chunks group by database_name, storage;
+-----------------------------------+---------------------+----------------------+
| database_name                     | storage             | SUM(estimated_bytes) |
+-----------------------------------+---------------------+----------------------+
| 844910ece80be8bc_cac95fa59126cd01 | OpenMutableBuffer   | 109737               |
| 844910ece80be8bc_05d1e95653672000 | OpenMutableBuffer   | 2337719              |
| 844910ece80be8bc_7be09b71c487d5d3 | ClosedMutableBuffer | 799682176            |
+-----------------------------------+---------------------+----------------------+

It can be expressed in the same way using numbers to refer to other items in the select list.

However, this does not work today in DataFusion:

> select database_name, storage, sum(estimated_bytes) from chunks group by 1, 2;
Plan("Projection references non-aggregate values")
@alamb alamb added the datafusion Changes in the datafusion crate label Apr 26, 2021
@alamb
Copy link
Contributor Author

alamb commented Apr 26, 2021

Comment from Jorge Leitão(jorgecarleitao) @ 2020-10-23T06:03:45.335+0000:

Could you describe what is the use-case? E.g. in which situation does that improve readability over the having the column name, or in which case does the column name not work?

Comment from Pavel Tiunov(paveltiunov) @ 2020-10-23T06:41:53.205+0000:

Hey [~jorgecarleitao]! Thanks for the quick turnaround. The use case is usually to support grouping by complex expressions. For example
{code:java}
SELECT date_trunc(convert_tz(`page_views__time_hour`, 'America/Los_Angeles'), 'week') `page_views__time_week`, sum(`page_views__count`) `page_views__count` FROM page_views GROUP BY date_trunc(convert_tz(`page_views__time_hour`, 'America/Los_Angeles'), 'week') LIMIT 10000
{code}
becomes
{code:java}
SELECT date_trunc(convert_tz(`page_views__time_hour`, 'America/Los_Angeles'), 'week') `page_views__time_week`, sum(`page_views__count`) `page_views__count` FROM page_views GROUP BY 1 LIMIT 10000
{code}
Calculated columns can be pretty complex and it usually just bloats up the SQL significantly. I believe it's beneficial to have this feature as most of the modern query engines have it.

Comment from Jorge Leitão(jorgecarleitao) @ 2020-10-23T08:00:39.544+0000:

Thanks for the example, [~paveltiunov] . makes sense

Meanwhile, I think that you can use <{{expression> AS name}} and {{GROUP BY name}} .

 

@jychen7 jychen7 mentioned this issue Apr 28, 2021
@jychen7
Copy link
Contributor

jychen7 commented Apr 28, 2021

I was trying to fix it, then find out actually expression AS name, GROUP BY name will raise error
so I create #215 and try to fix #215 first

@alamb
Copy link
Contributor Author

alamb commented Apr 28, 2021

Thanks @jychen7

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

Successfully merging a pull request may close this issue.

2 participants