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

Support of HAVING without GROUP BY #208

Open
dey4ss opened this issue May 9, 2022 · 0 comments
Open

Support of HAVING without GROUP BY #208

dey4ss opened this issue May 9, 2022 · 0 comments

Comments

@dey4ss
Copy link
Member

dey4ss commented May 9, 2022

While going through the Public BI Benchmark, I found multiple queries with HAVING clauses but no GROUP BY columns (e.g., [1, 2, 3]). I'd like to discuss whether we want to support such statements or not.
Related to #186.

Consider the following query [1]:

SELECT SUM(CAST("TrainsUK2_2"."Number of Records" AS BIGINT)) AS "sum:Number of Records:ok"
FROM "TrainsUK2_2" 
HAVING (COUNT(1) > 0)

According to DBFiddle [4], Postgres executes the query*, whereas we raise a syntax error due to seeing HAVING without GROUP BY columns. However, most of the HAVING criteria seem weird, as they consist of a a term always evaluating to true (see example above [1]) or false (see below [5]), assuming that the numbers passed th the aggregate functions do not refer to column IDs.

HAVING ((SUM(1) >= 30) AND (SUM(1) <= 100000))

If we want to adapt here, we could add a rule to the parser and change

opt_group : GROUP BY expr_list opt_having {
  $$ = new GroupByDescription();
  $$->columns = $3;
  $$->having = $4;
}
| /* empty */ { $$ = nullptr; };

to

opt_group : GROUP BY expr_list opt_having {
  $$ = new GroupByDescription();
  $$->columns = $3;
  $$->having = $4;
}
| HAVING expr {
  $$ = new GroupByDescription();
  $$->having = $2;
}
| /* empty */ { $$ = nullptr; };

Additionally, I noticed that the escaped column names in the example [1] do not work properly: "Number of Records" is not considered as one single identifier and "sum:Number of Records:ok" does not parse because sum is mistakenly treated as token for the SUM(...)aggregate function.

[1] https://github.com/cwida/public_bi_benchmark/blob/master/benchmark/TrainsUK2/queries/17.sql
[2] https://github.com/cwida/public_bi_benchmark/blob/master/benchmark/MLB/queries/103.sql
[3] https://github.com/cwida/public_bi_benchmark/blob/master/benchmark/Wins/queries/10.sql
[4] https://www.db-fiddle.com/
[5] https://github.com/cwida/public_bi_benchmark/blob/master/benchmark/Hatred/queries/12.sql

* SQL for reproduction:

CREATE TABLE "TrainsUK2_2"("Number of Records" smallint NOT NULL);
INSERT INTO "TrainsUK2_2" VALUES(-1);

SELECT SUM(CAST("TrainsUK2_2"."Number of Records" AS BIGINT)) AS "sum:Number of Records:ok" FROM "TrainsUK2_2" HAVING (COUNT(1) > 0);

Result:

sum:Number of Records:ok
-1
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

No branches or pull requests

1 participant