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

release-19.2: opt: backport two pg compatibility fixes for GROUP BY #42694

Merged

Conversation

RaduBerinde
Copy link
Member

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

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
@RaduBerinde RaduBerinde requested a review from a team as a code owner November 22, 2019 14:02
@cockroach-teamcity
Copy link
Member

This change is Reviewable

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 RaduBerinde force-pushed the backport19.2-41732-42447 branch from 52dd647 to 566be28 Compare November 23, 2019 13:33
Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 5 of 5 files at r1, 5 of 5 files at r2, 3 of 3 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @justinj)

@RaduBerinde
Copy link
Member Author

TFTR!

@RaduBerinde RaduBerinde merged commit 49c79a1 into cockroachdb:release-19.2 Nov 26, 2019
@RaduBerinde RaduBerinde deleted the backport19.2-41732-42447 branch November 26, 2019 20:30
@jordanlewis
Copy link
Member

Woot

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

Successfully merging this pull request may close these issues.

4 participants