rubenada commented on code in PR #6495:
URL: https://github.com/apache/hive/pull/6495#discussion_r3267157958
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/RexNodeConverter.java:
##########
@@ -577,6 +570,37 @@ public static List<RexNode>
transformInToOrOperands(List<RexNode> operands, RexB
return disjuncts;
}
+ /**
+ * This method tries to rewrite IN expression arguments into an equivalent
call.
+ * If there are only two elements, generates an EQUALS:
+ * IN [A,B] => EQUALS [A,B]
+ * Otherwise, tries to generate a SEARCH:
+ * IN [A,B,C] => SEARCH(A, SARG([B..B], [C..C]))
+ * If this is not possible (e.g., argument types not sufficiently compatible
to generate a Calcite SEARCH expression),
+ * tries to generate an OR expression:
+ * IN [A,B,C] => OR [EQUALS [A,B], EQUALS [A,C]]
+ * If this is not possible (e.g., non-deterministic calls are found in the
expressions), returns null
+ */
+ public static RexNode rewriteInClause(List<RexNode> childRexNodeLst,
RexBuilder rexBuilder) {
+ if (childRexNodeLst.size() == 2) {
+ return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, childRexNodeLst);
+ }
+
+ RexNode firstPred = childRexNodeLst.get(0);
+ List<RexNode> ranges = childRexNodeLst.subList(1, childRexNodeLst.size());
+ RexNode res = rexBuilder.makeIn(firstPred, ranges);
+ if (res.getKind() == SqlKind.SEARCH) {
+ return res;
+ }
+ // Calcite SEARCH conversion was not possible: generate our own OR
expression
+ List<RexNode> newInputs =
RexNodeConverter.transformInToOrOperands(childRexNodeLst, rexBuilder);
+ if (newInputs == null) {
+ return null;
+ }
+ return newInputs.size() == 1 ? newInputs.get(0) :
rexBuilder.makeCall(SqlStdOperatorTable.OR, newInputs);
Review Comment:
Hive's OR conversion is more thorough: it checks
`HiveCalciteUtil.isDeterministic` (and bails out if any expression is not), and
also includes a field by field comparison for ROW types. Calcite's code is
simply a "blind" OR of EQUALS
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]