-
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
sql: group by does not conform with postgres #26709
Comments
Andy says:
OTOH, the query above uses a join in the FROM clause and yet the syntax is valid. This means that:
Probably a good idea to peek at pg to determine how this is detected. |
@knz Can we get a docs description this issue as a Known Limitation for 2.1? |
""" Applications developed for PostgreSQL may exploit the fact that PostgreSQL allows a SELECT clause to name a column that is not also listed in GROUP BY in some cases, for example |
Another example from @jordanlewis:
|
PG docs:
So the column is accepted if the PK of the base table it is coming from is in the grouping list. In our case this would be the An alternative would be to use our internal FDs. These are a superset of this simple rule and we would accept more cases; however it is fragile - changing some rules could in principle change the detected FDs, so it's possible that some queries would work in a release but then stop working in a future release. |
A note -- this feature seems to be used heavily by django (alot of the django annotate/group by tools end up creating queries like this). |
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.
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.
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.
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.
@RaduBerinde, can we resolve/remove this known limitation from the 20.1 docs? https://www.cockroachlabs.com/docs/dev/known-limitations.html#truncate-does-not-behave-like-delete |
@jseldess yes, we should remove it. |
Sorry, was linking to the wrong limitation. I meant: https://www.cockroachlabs.com/docs/dev/known-limitations.html#using-columns-in-select-not-listed-in-group-by I'll remove that one. Please let me know if that's not right, @RaduBerinde. |
Yes, I figured you were referring to that one. |
org.hibernate.userguide.hql.HQLTest test_hql_group_by_example_3 fails
This should parse and execute, but instead it produces
ERROR: column "address" must appear in the GROUP BY clause or be used in an aggregate function
From the postgres docs:
The text was updated successfully, but these errors were encountered: