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

Implement aggregate operator in new planner framework #3749

Closed
leiysky opened this issue Jan 4, 2022 · 0 comments · Fixed by #5027
Closed

Implement aggregate operator in new planner framework #3749

leiysky opened this issue Jan 4, 2022 · 0 comments · Fixed by #5027
Assignees
Labels
C-feature Category: feature

Comments

@leiysky
Copy link
Contributor

leiysky commented Jan 4, 2022

Implement aggregate operator in new planner.

The GROUP BY clause is used to group rows together with specified group key. In standard SQL, the group key can only be column, but in databend we allow the group key to be scalar expression, e.g. a + 1, a + b.

With GROUP BY, we cannot reference any non-grouped columns in SELECT clause directly(select b from t group by a is not allowed), since after grouping the non-grouped columns are reduced with aggregate functions and cannot appear in result set.

Aggregate functions are the functions that takes groups as input, and produce a single accumulated value as result. Aggregated functions can only work with GROUP BY. If there is a query with aggregate function(e.g. select count(*) from t) but no group key is specified, it will be treated as an aggregate query has only one group which consists of all the input rows.

If we want to filter the aggregated result, a HAVING clause can be utilized with. You can reference aggregate functions and group keys in HAVING clause(which is pretty similar with WHERE clause).

Here's an example of aggregate:

SELECT a, sum(b + c) + 1 FROM t GROUP BY a, b + 1 HAVING b + 1 > 1;

a is one of the group keys, so it could appears in the result. Although b and c are not group keys, they can be referenced by an aggregate function sum.

b + 1 is one of the group keys, so we can use it in HAVING clause. But if we replace the b + 1 > 1 with b > 0, then the query won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants