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

expression support for grouping(a,b,c,...) with maximum of 64 parameters #44437

Closed
AilinKid opened this issue Jun 6, 2023 · 1 comment · Fixed by #44436
Closed

expression support for grouping(a,b,c,...) with maximum of 64 parameters #44437

AilinKid opened this issue Jun 6, 2023 · 1 comment · Fixed by #44436
Assignees
Labels
sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.

Comments

@AilinKid
Copy link
Contributor

AilinKid commented Jun 6, 2023

Enhancement

add one more dimension for grouping marks maintenance.

ideas come from MySQL's support for grouping(a,b,c,...) with maximum of 64 parameters.

mysql> select grouping(a,b), grouping(a), grouping(b) from t group by a, b with rollup;
+---------------+-------------+-------------+
| grouping(a,b) | grouping(a) | grouping(b) |
+---------------+-------------+-------------+
|             0 |           0 |           0 |
|             1 |           0 |           1 |
|             0 |           0 |           0 |
|             1 |           0 |           1 |
|             3 |           1 |           1 |
+---------------+-------------+-------------+
5 rows in set (0.00 sec)
mysql> select grouping(b,a), grouping(a), grouping(b) from t group by a, b with rollup;
+---------------+-------------+-------------+
| grouping(b,a) | grouping(a) | grouping(b) |
+---------------+-------------+-------------+
|             0 |           0 |           0 |
|             2 |           0 |           1 |
|             0 |           0 |           0 |
|             2 |           0 |           1 |
|             3 |           1 |           1 |
+---------------+-------------+-------------+
5 rows in set (0.00 sec)

Notice the result difference of grouping(a,b) and grouping(b,a)!

mysql reference

As seen here, if GROUPING (a,b) returns 3, it means that NULL in column “a” and NULL in column “b” for that row is produce by a ROLLUP operation. If result is 1, NULL in column “b” alone is a result of ROLLUP operation.
https://dev.mysql.com/blog-archive/mysql-8-0-grouping-function/

when evaluating the grouping function's value, the formula is:

grouping(a,b,c) = grouping(a)<<2 + grouping(b)<<1 << grouping(c)

the left shift size is exactly according to their listed position in the grouping function.
so we should maintain all the grouping marks (for every single col) down, and add one more round computation for the left shift plus if the len(groupingMarks) is greater than 1 (means multi col args in grouping function).

@AilinKid AilinKid added type/enhancement The issue or PR belongs to an enhancement. sig/planner SIG: Planner labels Jun 6, 2023
@AilinKid AilinKid self-assigned this Jun 6, 2023
@AilinKid
Copy link
Contributor Author

AilinKid commented Jun 6, 2023

pingcap/tiflash#7590 related tiflash issue

@ti-chi-bot ti-chi-bot bot closed this as completed in aedbcd0 Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant