[
https://issues.apache.org/jira/browse/CALCITE-4702?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17441540#comment-17441540
]
Soumyakanti Das commented on CALCITE-4702:
------------------------------------------
I have added a ruleĀ {{GroupByConstantAddJoinRule}}, which converts a query like:
{code}
select avg(sal)
from emp
group by true;
{code}
to:
{code}
select avg(sal)
from emp, (select true x) dummy
group by dummy.x;
{code}
The rule matches an {{Aggregate}} on top of a {{Project}}, and adds a {{Join}}
of {{TableScan}} and {{Values}} below the {{Project}}. The plan changes from:
{code}
LogicalProject(EXPR$0=[$1])
LogicalAggregate(group=[{0}], EXPR$0=[AVG($1)])
LogicalProject($f0=[true], SAL=[$5])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}
to:
{code}
LogicalProject(EXPR$0=[$1])
LogicalAggregate(group=[{0}], EXPR$0=[AVG($1)])
LogicalProject(T=[$9], SAL=[$5])
LogicalJoin(condition=[true], joinType=[inner])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalValues(tuples=[[{ true }]])
{code}
I have also added a couple of tests to {{RelOptRulesTest.java}} and
{{JdbcTest.java}}. Please provide some feedback!
> Error when executing query with GROUP BY constant via JDBC adapter
> ------------------------------------------------------------------
>
> Key: CALCITE-4702
> URL: https://issues.apache.org/jira/browse/CALCITE-4702
> Project: Calcite
> Issue Type: Bug
> Components: core, jdbc-adapter
> Affects Versions: 1.27.0
> Reporter: Stamatis Zampetakis
> Assignee: Soumyakanti Das
> Priority: Major
> Labels: pull-request-available
> Time Spent: 0.5h
> Remaining Estimate: 0h
>
> The following functionally equivalent SQL queries are accepted by Calcite and
> produce a valid plan
> {noformat}
> select avg(salary) from employee group by true
> select avg(salary) from employee group by 'a'
> {noformat}
> but they may fail if they are executed via the JDBC adapter since not all
> DBMS allow grouping by constants expressions. Moreover, what works for one
> may not work for the other.
> +Examples+
> The {{GROUP BY TRUE}} query works in Postgres, and MySQL but fails in
> Redshift with the following exception:
> {noformat}
> com.amazon.redshift.util.RedshiftException: ERROR: non-integer constant in
> GROUP BY
> {noformat}
> The {{GROUP BY 'a'}} query works in MySQL but fails in Postgres with the
> following exception:
> {noformat}
> ERROR: non-integer constant in GROUP BY
> {noformat}
> +Edit:+
> The {{GROUP BY}} constant is similar to {{GROUP BY ()}} "nothing" but as
> shown in the discussion below they are not equivalent. There is a nice [blog
> post|https://blog.jooq.org/2018/05/25/how-to-group-by-nothing-in-sql/]
> listing some limitations of various DBMS when it comes to {{GROUP BY ()}}.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)