zabetak commented on code in PR #5681:
URL: https://github.com/apache/hive/pull/5681#discussion_r1984798935


##########
parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g:
##########
@@ -341,24 +341,47 @@ castExpression
     -> ^(TOK_FUNCTION {adaptor.create(Identifier, "cast_format")} 
NumberLiteral[Integer.toString(((CommonTree)toType.getTree()).token.getType())] 
expression StringLiteral 
NumberLiteral[((CommonTree)toType.getTree()).getChild(0).getText()])
     ;
 
-caseExpression
+whenExpression
 @init { gParent.pushMsg("case expression", state); }
 @after { gParent.popMsg(state); }
     :
-    KW_CASE expression
-    (KW_WHEN expression KW_THEN expression)+
+    KW_CASE
+     ( KW_WHEN expression KW_THEN expression)+
     (KW_ELSE expression)?
-    KW_END -> ^(TOK_FUNCTION KW_CASE expression*)
+    KW_END -> ^(TOK_FUNCTION KW_WHEN expression*)
     ;
 
-whenExpression
+// Make caseExpression to build a whenExpression tree
+// Rewrite
+// CASE a
+//   WHEN b THEN c
+//   [WHEN d THEN e]* [ELSE f]
+// END
+// to
+// CASE
+//   WHEN a=b THEN c
+//   [WHEN a=d THEN e]* [ELSE f]
+// END
+caseExpression
 @init { gParent.pushMsg("case expression", state); }
 @after { gParent.popMsg(state); }
     :
-    KW_CASE
-     ( KW_WHEN expression KW_THEN expression)+
-    (KW_ELSE expression)?
-    KW_END -> ^(TOK_FUNCTION KW_WHEN expression*)
+    KW_CASE caseOperand=expression
+    // Pass the case operand to the rule parses the when branches
+    whenBranches[$caseOperand.tree]
+    (KW_ELSE elseResult=expression)?
+    KW_END -> ^(TOK_FUNCTION Identifier["when"] whenBranches $elseResult?)

Review Comment:
   Is there any reason why we use `Identifier["when"]` instead of `KW_WHEN` in 
the rewrite rule?



##########
ql/src/test/queries/clientpositive/cbo_case_when_type_conversion.q:
##########
@@ -0,0 +1,17 @@
+CREATE TABLE t1 (col1 char(3));
+
+INSERT INTO t1 VALUES ('A'),('b'),('c'),(null);
+
+explain cbo
+select col1, case upper(col1) when 'A' then 'OK' else 'N/A' end as col2 from 
t1;
+explain
+select col1, case upper(col1) when 'A' then 'OK' else 'N/A' end as col2 from 
t1;
+
+select col1, case upper(col1) when 'A' then 'OK' else 'N/A' end as col2 from 
t1;
+
+select col1,

Review Comment:
   What's the test goal of this query?



##########
ql/src/test/results/clientpositive/llap/order_by_expr_2.q.out:
##########
@@ -139,7 +139,7 @@ POSTHOOK: Input: default@store
 CBO PLAN:
 HiveProject(c0=[$0], c1=[$1], c2=[$2])
   HiveSortLimit(sort0=[$3], dir0=[ASC])
-    HiveProject(c0=[$0], c1=[$1], c2=[$2], (tok_function case 
(tok_table_or_col store_name) 'HQ' tok_null (tok_table_or_col store_name))=[$1])
+    HiveProject(c0=[$0], c1=[$1], c2=[$2], (tok_function when (= 
(tok_table_or_col store_name) 'HQ') tok_null (tok_table_or_col 
store_name))=[$1])

Review Comment:
   It took me a few minutes to understand that this is an alias that is 
identical to AST :)



##########
ql/src/test/queries/clientpositive/cbo_case_when_type_conversion.q:
##########
@@ -0,0 +1,17 @@
+CREATE TABLE t1 (col1 char(3));
+
+INSERT INTO t1 VALUES ('A'),('b'),('c'),(null);
+
+explain cbo
+select col1, case upper(col1) when 'A' then 'OK' else 'N/A' end as col2 from 
t1;

Review Comment:
   Since we added a rewrite rule for `CASE x WHEN y` to `CASE WHEN x=y` I would 
expect to add an identical query using the other syntax and show that plans and 
results are the same.



##########
ql/src/test/queries/clientpositive/cbo_case_when_type_conversion.q:
##########
@@ -0,0 +1,17 @@
+CREATE TABLE t1 (col1 char(3));
+
+INSERT INTO t1 VALUES ('A'),('b'),('c'),(null);

Review Comment:
   Since we use check upper with 'A' in the queries below it would make more 
sense to have 'a' in the data otherwise the `upper` call is more or less a 
noop. Respectively for `lower` and comparison with `b`.
   
   In fact to simplify a bit the test we could just use upper or lower in both 
queries.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to