Repository: hive
Updated Branches:
  refs/heads/master 9ebf4023e -> bde285d33


HIVE-14619 : CASE folding can produce wrong expression (Jesus Camacho Rodriguez 
via Ashutosh Chauhan)

Signed-off-by: Ashutosh Chauhan <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/bde285d3
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/bde285d3
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/bde285d3

Branch: refs/heads/master
Commit: bde285d33d8733bd667bae4337052ad2524e0aac
Parents: 9ebf402
Author: Jesus Camacho Rodriguez <[email protected]>
Authored: Thu Aug 25 17:27:12 2016 -0700
Committer: Ashutosh Chauhan <[email protected]>
Committed: Thu Aug 25 17:28:39 2016 -0700

----------------------------------------------------------------------
 .../hive/ql/optimizer/calcite/HiveRexUtil.java  |  5 +++
 ql/src/test/queries/clientpositive/fold_case.q  |  3 ++
 .../test/results/clientpositive/fold_case.q.out | 37 ++++++++++++++++++++
 3 files changed, 45 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/bde285d3/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRexUtil.java
----------------------------------------------------------------------
diff --git 
a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRexUtil.java 
b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRexUtil.java
index 1b327fe..15707c1 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRexUtil.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRexUtil.java
@@ -299,6 +299,11 @@ public class HiveRexUtil {
         if (operand.isAlwaysTrue()) {
           // Predicate is always TRUE. Make value the ELSE and quit.
           newOperands.add(operands.get(i + 1));
+          if (unknownAsFalse && RexUtil.isNull(operands.get(i + 1))) {
+            values.add(rexBuilder.makeLiteral(false).toString());
+          } else {
+            values.add(operands.get(i + 1).toString());
+          }
           break;
         } else if (operand.isAlwaysFalse() || RexUtil.isNull(operand)) {
           // Predicate is always FALSE or NULL. Skip predicate and value.

http://git-wip-us.apache.org/repos/asf/hive/blob/bde285d3/ql/src/test/queries/clientpositive/fold_case.q
----------------------------------------------------------------------
diff --git a/ql/src/test/queries/clientpositive/fold_case.q 
b/ql/src/test/queries/clientpositive/fold_case.q
index 013a45f..e2b490e 100644
--- a/ql/src/test/queries/clientpositive/fold_case.q
+++ b/ql/src/test/queries/clientpositive/fold_case.q
@@ -21,3 +21,6 @@ select count(1) from src where (case key when '238' then null 
else 1=1 end);
 explain 
 select (CASE WHEN (-2) >= 0  THEN SUBSTRING(key, 1,CAST((-2) AS INT)) ELSE 
NULL END)
 from src;
+explain
+select (CASE WHEN key = value THEN '1' WHEN true THEN '0' ELSE NULL END)
+from src;

http://git-wip-us.apache.org/repos/asf/hive/blob/bde285d3/ql/src/test/results/clientpositive/fold_case.q.out
----------------------------------------------------------------------
diff --git a/ql/src/test/results/clientpositive/fold_case.q.out 
b/ql/src/test/results/clientpositive/fold_case.q.out
index 304d37a..b2f9807 100644
--- a/ql/src/test/results/clientpositive/fold_case.q.out
+++ b/ql/src/test/results/clientpositive/fold_case.q.out
@@ -474,3 +474,40 @@ STAGE PLANS:
             Statistics: Num rows: 500 Data size: 84 Basic stats: COMPLETE 
Column stats: COMPLETE
             ListSink
 
+PREHOOK: query: explain
+select (CASE WHEN key = value THEN '1' WHEN true THEN '0' ELSE NULL END)
+from src
+PREHOOK: type: QUERY
+POSTHOOK: query: explain
+select (CASE WHEN key = value THEN '1' WHEN true THEN '0' ELSE NULL END)
+from src
+POSTHOOK: type: QUERY
+STAGE DEPENDENCIES:
+  Stage-1 is a root stage
+  Stage-0 depends on stages: Stage-1
+
+STAGE PLANS:
+  Stage: Stage-1
+    Map Reduce
+      Map Operator Tree:
+          TableScan
+            alias: src
+            Statistics: Num rows: 500 Data size: 5312 Basic stats: COMPLETE 
Column stats: NONE
+            Select Operator
+              expressions: CASE WHEN ((key = value)) THEN ('1') ELSE ('0') END 
(type: string)
+              outputColumnNames: _col0
+              Statistics: Num rows: 500 Data size: 5312 Basic stats: COMPLETE 
Column stats: NONE
+              File Output Operator
+                compressed: false
+                Statistics: Num rows: 500 Data size: 5312 Basic stats: 
COMPLETE Column stats: NONE
+                table:
+                    input format: 
org.apache.hadoop.mapred.SequenceFileInputFormat
+                    output format: 
org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat
+                    serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
+
+  Stage: Stage-0
+    Fetch Operator
+      limit: -1
+      Processor Tree:
+        ListSink
+

Reply via email to