-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
release-19.2: opt: backport two pg compatibility fixes for GROUP BY #42694
Merged
RaduBerinde
merged 3 commits into
cockroachdb:release-19.2
from
RaduBerinde:backport19.2-41732-42447
Nov 26, 2019
Merged
release-19.2: opt: backport two pg compatibility fixes for GROUP BY #42694
RaduBerinde
merged 3 commits into
cockroachdb:release-19.2
from
RaduBerinde:backport19.2-41732-42447
Nov 26, 2019
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This refactoring cleans up some aspects of the group-by code in the optbuilder. Currently each scope contains a set of fields (collected in the `groupby` struct). But building an aggregation involves multiple scopes and subsets of these fields are used by different scopes (e.g. `aggs` is used on the `aggOutScope`). In addition, many helper functions related to grouping are implemented by `*scope`, and you have to know which of the scopes needs to be used for each one (even worse, sometimes you need to pass around information like `groupingsLen` because it's not all in one place). This change addresses these issues. There is now a single `groupby` structure per grouping context and it is now a pointer inside the `scope`. All related functionality has been moved as methods on this structure. Release note: None
Postgres supports selecting columns that are not grouping columns if the entire PK is part of the grouping columns (see https://www.postgresql.org/docs/current/sql-select.html#SQL-GROUPBY). This commit implements this feature, motivated by the desire for compatibility with various ORMs. We implement this feature by adding these columns as grouping columns on-the-fly; in other words, we interpret `SELECT v FROM kv GROUP BY k` as having `GROUP BY k,v` (which is equivalent). Normalization rules will subsequently trim down the list of columns. Fixes cockroachdb#26709. Release note (sql change): It is now valid for SELECT and HAVING to refer to ungrouped columns in the special case when the grouped columns contain the primary key of the table containing the ungrouped column.
There is some baggage left over from SQL92 which allowed grouping by select targets by their alias. We implement the same rules used by postgres, as explained in the `buildGroupingColumns` comment. Fixes cockroachdb#28059. Release note (sql change): It is now supported to specify selection target aliases as GROUP BY columns. Note that the FROM columns take precedence over the aliases, which are only used if there is no column with that name in the current scope.
RaduBerinde
force-pushed
the
backport19.2-41732-42447
branch
from
November 23, 2019 13:33
52dd647
to
566be28
Compare
rytaft
approved these changes
Nov 26, 2019
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.
Reviewed 5 of 5 files at r1, 5 of 5 files at r2, 3 of 3 files at r3.
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @justinj)
TFTR! |
Woot |
This was referenced Dec 18, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backporting two pg compatibility fixes (needed for Django compatibility and others). One supports projecting non-grouping columns if the PK of that table is a grouping column, and one supports grouping by select list aliases.
Backport:
Please see individual PRs for details.
/cc @cockroachdb/release
/cc @awoods187