You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by wz...@apache.org on 2022/02/16 17:18:33 UTC

[impala] 01/03: IMPALA-11049: Added expr analyzed check in 'SimplifyCastExprRule.java'

This is an automated email from the ASF dual-hosted git repository.

wzhou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git

commit 345ba685e74aa002fc5934b104183f141d9cb367
Author: skyyws <sk...@163.com>
AuthorDate: Mon Dec 13 20:36:09 2021 +0800

    IMPALA-11049: Added expr analyzed check in 'SimplifyCastExprRule.java'
    
    We added a new expr rewrite rule 'SimplifyCastExprRule.java' in
    IMPALA-10836. If expr is not analyzed, this rewrite rule would throw
    an 'AnalysisException', this is due to 'orderByElements_' not been
    analyzed. We try to substitute order by elements when creating
    'SortInfo', but caused some other problems. So we only add expr
    analyzed check in this rule to solve this problem. When adding other
    expr rewrite rules in the future, we should also add this check.
    
    Testing:
    - Added test cases in 'explain-level3.test'
    
    Change-Id: I2780e04a6d5a32e224cd0470cf6f166a832363ec
    Reviewed-on: http://gerrit.cloudera.org:8080/18099
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 .../impala/rewrite/SimplifyCastExprRule.java       |  7 +++
 .../queries/QueryTest/explain-level3.test          | 55 ++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/fe/src/main/java/org/apache/impala/rewrite/SimplifyCastExprRule.java b/fe/src/main/java/org/apache/impala/rewrite/SimplifyCastExprRule.java
index 4272e7b..ffb7fde 100644
--- a/fe/src/main/java/org/apache/impala/rewrite/SimplifyCastExprRule.java
+++ b/fe/src/main/java/org/apache/impala/rewrite/SimplifyCastExprRule.java
@@ -22,6 +22,7 @@ import org.apache.impala.analysis.Analyzer;
 import org.apache.impala.analysis.CastExpr;
 import org.apache.impala.analysis.Expr;
 import org.apache.impala.analysis.NullLiteral;
+import org.apache.impala.catalog.Type;
 
 /**
  * This rewrite rule simplifies cast expr when cast target data type is the same as inner
@@ -57,6 +58,12 @@ public class SimplifyCastExprRule implements ExprRewriteRule {
     //this situation, such as cast(cast(NULL as string) as timestamp FORMAT 'YYYY-MM-DD').
     //If we replace inner 'cast(NULL as string)' as 'NULL', query will throw exception.
     if (child instanceof NullLiteral) return castExpr;
+
+    //Expr must analyzed before rewrite. Without analyze, expr type is 'INVALID'
+    if (!castExpr.isAnalyzed()) castExpr.analyzeNoThrow(analyzer);
+    if (!child.isAnalyzed()) child.analyzeNoThrow(analyzer);
+    Preconditions.checkState(castExpr.getType() != Type.INVALID &&
+        child.getType() != Type.INVALID, "'castExpr' must be analyzed before rewrite");
     return castExpr.getType().matchesType(child.getType())? child : castExpr;
   }
 }
diff --git a/testdata/workloads/functional-query/queries/QueryTest/explain-level3.test b/testdata/workloads/functional-query/queries/QueryTest/explain-level3.test
index e278885..1865c9c 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/explain-level3.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/explain-level3.test
@@ -88,3 +88,58 @@ explain select * from functional_parquet.iceberg_partitioned;
 ---- RESULTS: VERIFY_IS_SUBSET
 '     file formats: [PARQUET]'
 ====
+---- QUERY
+# Tests 'SimplifyCastExprRule' rewrite when group by and order by clauses contain cast exprs
+# 'd_date' is STRING type, rewrite succeed
+explain
+select month(to_timestamp(cast(d_date AS string),'yyyy-MM-dd')) a
+from tpcds_parquet.date_dim
+group by month(to_timestamp(cast(d_date AS string),'yyyy-MM-dd'))
+order by month(to_timestamp(cast(d_date AS string),'yyyy-MM-dd'))
+---- RESULTS: VERIFY_IS_SUBSET
+'Analyzed query: SELECT month(to_timestamp(d_date, 'yyyy-MM-dd')) a FROM'
+'tpcds_parquet.date_dim GROUP BY month(to_timestamp(d_date, 'yyyy-MM-dd')) ORDER'
+'BY month(to_timestamp(d_date, 'yyyy-MM-dd')) ASC'
+====
+---- QUERY
+# Tests 'SimplifyCastExprRule' rewrite when group by and order by clauses contain aliases
+# 'd_date' is STRING type, rewrite succeed
+# Aliases in group by clauses will substituted by exprs
+explain
+select month(to_timestamp(cast(d_date AS string),'yyyy-MM-dd')) a
+from tpcds_parquet.date_dim
+group by a
+order by a
+---- RESULTS: VERIFY_IS_SUBSET
+'Analyzed query: SELECT month(to_timestamp(d_date, 'yyyy-MM-dd')) a FROM'
+'tpcds_parquet.date_dim GROUP BY month(to_timestamp(d_date, 'yyyy-MM-dd')) ORDER'
+'BY a ASC'
+====
+---- QUERY
+# Tests 'SimplifyCastExprRule' rewrite when group by and order by clauses contain exprs
+# 'd_date_sk' is INT type, not rewrite
+explain
+select month(to_timestamp(cast(d_date_sk AS string),'yyyy-MM-dd')) a
+from tpcds_parquet.date_dim
+group by month(to_timestamp(cast(d_date_sk AS string),'yyyy-MM-dd'))
+order by month(to_timestamp(cast(d_date_sk AS string),'yyyy-MM-dd'))
+---- RESULTS: VERIFY_IS_SUBSET
+'Analyzed query: SELECT month(to_timestamp(CAST(d_date_sk AS STRING),'
+''yyyy-MM-dd')) a FROM tpcds_parquet.date_dim GROUP BY'
+'month(to_timestamp(CAST(d_date_sk AS STRING), 'yyyy-MM-dd')) ORDER BY'
+'month(to_timestamp(CAST(d_date_sk AS STRING), 'yyyy-MM-dd')) ASC'
+====
+---- QUERY
+# Tests 'SimplifyCastExprRule' rewrite when group by and order by clauses contain aliases
+# 'd_date_sk' is INT type, not rewrite
+# Aliases in group by clauses will substituted by exprs
+explain
+select month(to_timestamp(cast(d_date_sk AS string),'yyyy-MM-dd')) a
+from tpcds_parquet.date_dim
+group by a
+order by a
+---- RESULTS: VERIFY_IS_SUBSET
+'Analyzed query: SELECT month(to_timestamp(CAST(d_date_sk AS STRING),'
+''yyyy-MM-dd')) a FROM tpcds_parquet.date_dim GROUP BY'
+'month(to_timestamp(CAST(d_date_sk AS STRING), 'yyyy-MM-dd')) ORDER BY a ASC'
+====