You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@drill.apache.org by "Aman Sinha (JIRA)" <ji...@apache.org> on 2015/06/05 01:36:38 UTC

[jira] [Comment Edited] (DRILL-3211) Assert in a query with window function and group by clause

    [ https://issues.apache.org/jira/browse/DRILL-3211?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14573801#comment-14573801 ] 

Aman Sinha edited comment on DRILL-3211 at 6/4/15 11:36 PM:
------------------------------------------------------------

If a query has both window aggregate as well as a group-by,  then the group-by must include all columns needed by the window function. For instance,  your query would fail in Postgres: 
{code}
postgres=# select sum(a1) over (partition by b1)  from tt1 group by b1;
ERROR:  column "tt1.a1" must appear in the GROUP BY clause or be used in an aggregate function
{code}

Adding a1 to the group-by makes it succeed in **both** Postgres and Drill.  Can you please verify ? 

Note that the behavior for the following nested aggregate function is different: 
{code}
select avg(sum(a1)) over (partition by b1) from tt1 group by b1;
{code}

Here, it is OK to not have a1 in the group-by clause because it is being used in a regular aggregate function sum(a1).  The rule is to use a1 in either group-by or aggregate function.  In the first query, a1 is used in a window function, not an aggregate function.  

That said, we (calcite) should not be throwing an Assertion error.  We should produce the appropriate error message.  


was (Author: amansinha100):
If a query has both window aggregate as well as a group-by,  then the group-by must include all columns needed by the window function.  In the special case of a window aggregate containing a nested aggregate function, I think some system like Postgres may handle it by implicitly adding
the column to group-by.
For instance,  your query would fail in Postgres: 
{code}
postgres=# select sum(a1) over (partition by b1)  from tt1 group by b1;
ERROR:  column "tt1.a1" must appear in the GROUP BY clause or be used in an aggregate function
{code}

Adding a1 to the group-by makes it succeed in **both** Postgres and Drill.  Can you please verify ? 

Note that the behavior for the following nested aggregate function is different: 
{code}
select avg(sum(a1)) over (partition by b1) from tt1 group by b1;
{code}

Here, it is OK to not have a1 in the group-by clause because it is being used in a regular aggregate function sum(a1).  The rule is to use a1 in either group-by or aggregate function.  In the first query, a1 is used in a window function, not an aggregate function.  

That said, we (calcite) should not be throwing an Assertion error.  We should produce the appropriate error message.  

> Assert in a query with window function and group by clause 
> -----------------------------------------------------------
>
>                 Key: DRILL-3211
>                 URL: https://issues.apache.org/jira/browse/DRILL-3211
>             Project: Apache Drill
>          Issue Type: Bug
>          Components: Query Planning & Optimization
>    Affects Versions: 1.0.0
>            Reporter: Victoria Markman
>            Assignee: Jinfeng Ni
>            Priority: Blocker
>              Labels: window_function
>
> {code}
> 0: jdbc:drill:schema=dfs> select sum(a1) over (partition by b1)  from t1 group by b1;
> Error: SYSTEM ERROR: java.lang.AssertionError: Internal error: while converting SUM(`t1`.`a1`)
> [Error Id: 21872cfa-6f09-4e92-aee6-5dd8698cf9e7 on atsqa4-133.qa.lab:31010] (state=,code=0)
> {code}
> drillbit.log
> {code}
> Caused by: java.lang.AssertionError: Internal error: while converting SUM(`t1`.`a1`)
>         at org.apache.calcite.util.Util.newInternal(Util.java:790) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.ReflectiveConvertletTable$2.convertCall(ReflectiveConvertletTable.java:152) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlNodeToRexConverterImpl.convertCall(SqlNodeToRexConverterImpl.java:60) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.convertOver(SqlToRelConverter.java:1762) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.access$1000(SqlToRelConverter.java:180) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.convertExpression(SqlToRelConverter.java:3937) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl(SqlToRelConverter.java:2521) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.convertAgg(SqlToRelConverter.java:2342) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(SqlToRelConverter.java:604) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(SqlToRelConverter.java:564) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(SqlToRelConverter.java:2741) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(SqlToRelConverter.java:522) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.prepare.PlannerImpl.convert(PlannerImpl.java:198) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.drill.exec.planner.sql.handlers.DefaultSqlHandler.convertToRel(DefaultSqlHandler.java:246) ~[drill-java-exec-1.0.0-mapr-r1-rebuffed.jar:1.0.0-mapr-r1]
>         at org.apache.drill.exec.planner.sql.handlers.DefaultSqlHandler.getPlan(DefaultSqlHandler.java:182) ~[drill-java-exec-1.0.0-mapr-r1-rebuffed.jar:1.0.0-mapr-r1]
>         at org.apache.drill.exec.planner.sql.DrillSqlWorker.getPlan(DrillSqlWorker.java:177) ~[drill-java-exec-1.0.0-mapr-r1-rebuffed.jar:1.0.0-mapr-r1]
>         at org.apache.drill.exec.work.foreman.Foreman.runSQL(Foreman.java:902) [drill-java-exec-1.0.0-mapr-r1-rebuffed.jar:1.0.0-mapr-r1]
>         at org.apache.drill.exec.work.foreman.Foreman.run(Foreman.java:240) [drill-java-exec-1.0.0-mapr-r1-rebuffed.jar:1.0.0-mapr-r1]
>         ... 3 common frames omitted
> Caused by: java.lang.reflect.InvocationTargetException: null
>         at sun.reflect.GeneratedMethodAccessor120.invoke(Unknown Source) ~[na:na]
>         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_71]
>         at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_71]
>         at org.apache.calcite.sql2rel.ReflectiveConvertletTable$2.convertCall(ReflectiveConvertletTable.java:142) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         ... 19 common frames omitted
> Caused by: java.lang.AssertionError: null
>         at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.getRootField(SqlToRelConverter.java:3810) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.adjustInputRef(SqlToRelConverter.java:3139) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.convertIdentifier(SqlToRelConverter.java:3114) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter.access$1400(SqlToRelConverter.java:180) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.visit(SqlToRelConverter.java:4061) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.visit(SqlToRelConverter.java:3489) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql.SqlIdentifier.accept(SqlIdentifier.java:274) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.convertExpression(SqlToRelConverter.java:3944) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.StandardConvertletTable.convertExpressionList(StandardConvertletTable.java:833) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         at org.apache.calcite.sql2rel.StandardConvertletTable.convertAggregateFunction(StandardConvertletTable.java:706) ~[calcite-core-1.1.0-drill-r7.jar:1.1.0-drill-r7]
>         ... 23 common frames omitted
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)