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

Tim Armstrong commented on IMPALA-7083:
---------------------------------------

I think the issue is this logic that is invoked to rewrite group by and order 
by expressions:
{code}
  /**
   * If given expr is rewritten into an integer literal, then return the 
original expr,
   * otherwise return the rewritten expr.
   * Used for GROUP BY, ORDER BY, and HAVING where we don't want to create an 
ordinal
   * from a constant arithmetic expr, e.g. 1 * 2 =/=> 2
   */
  private Expr rewriteCheckOrdinalResult(ExprRewriter rewriter, Expr expr)
      throws AnalysisException {
    Expr rewrittenExpr = rewriter.rewrite(expr, analyzer_);
    if (rewrittenExpr.isLiteral() && rewrittenExpr.getType().isIntegerType()) {
      return expr;
    } else {
      return rewrittenExpr;
    }
  }
{code}

Given the current code, we can't safely fold the CASE into a literal integer, 
because we then couldn't distinguish between ordinal "1" from the original 
query versus constant value "1" obtained after rewriting.

It seems like we need some additional information associated with the ordering 
and grouping expressions to distinguish between those two cases. If we did 
that, then we could safely rewrite the expression to a constant and avoid this 
other issue.

> Impala query failed with error AnalysisException from 2.9 onwards
> -----------------------------------------------------------------
>
>                 Key: IMPALA-7083
>                 URL: https://issues.apache.org/jira/browse/IMPALA-7083
>             Project: IMPALA
>          Issue Type: Bug
>          Components: Frontend
>    Affects Versions: Impala 2.9.0
>            Reporter: Eric Lin
>            Priority: Critical
>              Labels: regression
>
> To reproduce, please run below impala query:
> {code}
> DROP TABLE IF EXISTS test;
> CREATE TABLE test (a int);
> SELECT   ( 
>     CASE 
>        WHEN (1 =1) 
>        THEN 1
>        ELSE a
>     end) AS b
> FROM  test 
> GROUP BY 1 
> ORDER BY ( 
>     CASE 
>        WHEN (1 =1) 
>        THEN 1
>        ELSE a
>     end);
> {code}
> It will fail with below error:
> {code}
> ERROR: AnalysisException: ORDER BY expression not produced by aggregation 
> output (missing from GROUP BY clause?): (CASE WHEN TRUE THEN 1 ELSE a END)
> {code}
> However, if I replace column name "a" as a constant value, it works:
> {code}
> SELECT   ( 
>     CASE 
>        WHEN (1 =1) 
>        THEN 1
>        ELSE 2
>     end) AS b
> FROM  test 
> GROUP BY 1 
> ORDER BY ( 
>     CASE 
>        WHEN (1 =1) 
>        THEN 1
>        ELSE 2
>     end);
> {code}
> This issue is identified in CDH5.12.x (Impala 2.9), and no issues in 5.11.x 
> (Impala 2.8).
> We know that it can be worked around by re-write as below:
> {code}
> SELECT   ( 
>     CASE 
>        WHEN (1 =1) 
>        THEN 1
>        ELSE a
>     end) AS b
> FROM  test 
> GROUP BY 1 
> ORDER BY 1;
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to