-
Notifications
You must be signed in to change notification settings - Fork 23
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
ADBDEV-6314: Fix unexpected output for empty grouping set #1117
base: adb-6.x-dev
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You wrote, that changes are similar to Postgres 9.5, but I did not find the same sources there. Can you give the commit, which is example of your patch? Or can you backport such commit?
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
f3d3118 introduces support in PG 9.5. Also in GPDB7.
I discussed this issue with @Stolb27. I only need to correct the parser behavior without porting. My changes only concern the ungrouped columns check. Whereas the patch in PostgreSQL makes changes to the planner and much more. |
Tests for GPDB6 is run with "optimizer_enable_table_alias=off". You do not need to set GUC in the test. gpdb/concourse/scripts/common.bash Line 92 in c69f669
|
I fixed the typo and the use of tense. I hope the meaning of "grouping expressions" and expressions (Vars) does not overlap. I will also correct the comment in the code. |
This comment was marked as resolved.
This comment was marked as resolved.
Also testcases are added
It is necessary to select common attributes among top-level groupings.
It is necessary to take into account nesting. This query also degenerates into a regular GROUP BY and works: This query does not work: With a composite key it works differently (does not work): |
I have changed the behavior. Please check. Pay attention to the tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add note about f3d3118 to the description.
Allure report https://allure.adsw.io/launch/86899 |
Modify get_com_grouping_ressortgroupref_routine. Uses list_intersection_int instead of while cycle. The function was taken from f3d3118. Also code refactoring.
Allure report https://allure.adsw.io/launch/87081 |
* after flatten_joinalias_vars. We also find out if there are grouping | ||
* expressions. | ||
* Getting ressortgrouprefs of common TargetEntrys in groupings. | ||
* TargetEntry is common if it appears in several grouping expressions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is common if it appears in several grouping expressions
* (including nesting) and in a regular GROUP BY (general case)."
Not if
"is common if it appears in all grouping expressions
* (including nesting) or in a regular GROUP BY (general case)."
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then for the case of GROUPING SETS(a)
I will point out that it degenerates into a regular GROUP BY.
{ | ||
List *result = NIL; | ||
|
||
if ( !grpcl ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra spaces: if (!grpcl)
List *result = NIL; | ||
|
||
if ( !grpcl ) | ||
return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do return NIL;
, doing return result
is better only if it does not empty.
Assert(IsIntegerList(list1)); | ||
Assert(IsIntegerList(list2)); | ||
|
||
result = NIL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List *result = NIL;
foreach(lc, tles) | ||
{ | ||
List *chunk = get_com_grouping_ressortgroupref_routine((Node *)lfirst(lc), targetList, com_refs); | ||
if(!chunk) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space
{ | ||
TargetEntry *tle = get_sortgroupclause_tle((SortGroupClause *) grpcl, targetList); | ||
result = lappend_int(result, tle->ressortgroupref); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you add return result;
here, you may remove the last else
with brackets and one tab in each line inside.
Prohibiting grouping sets with ungrouped attributes in TargetList
Problem:
Queries with grouping expressions and ungrouped attributes in TL could
produce unexpected output if the group did not have a primary key.
Example with 3 columns where "a" is a primary key:
SELECT a,b,c FROM t GROUP BY GROUPING SETS((a),(b));
Grouping "(a)" had correct output of columns "a", "b", "c". Attribute "b" in
group "(b)" is not a primary key, so it is not unique, so it can be grouped.
In this case, the output of column "c" was undefined. This also applied to
"GROUPING SETS ((a),())", ROLLUP and CUBE (detailed behavior is
described in tests with ungrouped check).
Grouping expressions were introduced in PostgreSQL 9.5 (f3d3118) and
such queries were prohibited. And this patch aligns the behavior with that.
Solution:
Improve parse-stage validation for ungrouped columns when using grouping
expressions.
Changes:
Similar to PostgreSQL, the parsing stage for queries with grouping
expressions reads common attributes for all groups. If any, they are placed in
"groupClauseCommonVars". In case of ungrouped attributes in TL, this list is
used to check for PK in groups.
Compared to the original, the "groupClauses" list uses TargetEntry rather than
an expression. Added function to get common refsortgroupref in grouping
clauses. At the beginning, the function assumes that all expressions are found
in grouping clauses. By running through the groupings, missing expressions
are excluded from the common refs list. The extraction function in the form
"list_intersection_int" taken from f3d3118.
The patch does not change the behavior of regular GROUP BY.
Co-authored-by: anarazel [email protected]