You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues-all@impala.apache.org by "Paul Rogers (JIRA)" <ji...@apache.org> on 2018/10/26 01:54:00 UTC

[jira] [Created] (IMPALA-7769) Constant folding sometimes introduces an unnecessary cast

Paul Rogers created IMPALA-7769:
-----------------------------------

             Summary: Constant folding sometimes introduces an unnecessary cast
                 Key: IMPALA-7769
                 URL: https://issues.apache.org/jira/browse/IMPALA-7769
             Project: IMPALA
          Issue Type: Improvement
          Components: Frontend
    Affects Versions: Impala 3.0
            Reporter: Paul Rogers
            Assignee: Paul Rogers


Consider the following query:

{code:sql}
SELECT IFNULL(NULL + 1, id) FROM alltypessmall;
{code}

Visualize the rewritten query after analysis (using the new {{FullRewriteTest}}.) We get:

{code:sql}
SELECT CASE WHEN NULL IS NULL THEN id ELSE NULL END FROM alltypessmall
{code}

Expected:

{code:sql}
SELECT id FROM alltypessmall
{code}

The reason is that there are multiple rewrites, one of which has a flaw.

# Rewrite IFNULL to CASE
# Rewrite NULL + 1 to CAST(NULL AS SMALLINT)
# Try to rewrite CAST(NULL AS SMALLINT) IS NULL, but fail because CAST is not a literal.

The code in question in {{FoldConstantsRule.apply()}} is:

{code:java}
    for (Expr child: expr.getChildren()) if (!child.isLiteral()) return expr;
{code}

In fact, this check is too restrictive. Need a new {{isLiteralLike()}} should work like {{IsNullLiteral()}}:

* True if the node is a literal.
* True if the node is a cast of a literal.

(Can't change {{isLiteral()}} since there are places that assume that this existing predicate indicates that the node is exactly a {{LiteralExpr}}.)

Impala already has a predicate that does what is needed: {{isConstant()}}. However, the code in {{FoldConstantsRule.apply()}} specifically excludes calling it:

{code:java}
    // Avoid calling Expr.isConstant() because that would lead to repeated traversals
    // of the Expr tree. Assumes the bottom-up application of this rule. Constant
    // children should have been folded at this point.
{code}

The new method solves the problem and the test query now simplifies to the expected result.



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

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-all-unsubscribe@impala.apache.org
For additional commands, e-mail: issues-all-help@impala.apache.org