-
Notifications
You must be signed in to change notification settings - Fork 4.8k
[WIP] HIVE-28931: Convert IN to SEARCH in RexNodeConverter #6495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,10 @@ | |
| import org.apache.hadoop.hive.common.type.HiveVarchar; | ||
| import org.apache.hadoop.hive.common.type.Timestamp; | ||
| import org.apache.hadoop.hive.common.type.TimestampTZ; | ||
| import org.apache.hadoop.hive.conf.HiveConf; | ||
| import org.apache.hadoop.hive.ql.exec.FunctionRegistry; | ||
| import org.apache.hadoop.hive.ql.metadata.Hive; | ||
| import org.apache.hadoop.hive.ql.metadata.HiveException; | ||
| import org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException; | ||
| import org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException.UnsupportedFeature; | ||
| import org.apache.hadoop.hive.ql.optimizer.calcite.HiveCalciteUtil; | ||
|
|
@@ -117,6 +120,16 @@ public class RexNodeConverter { | |
| private final RexBuilder rexBuilder; | ||
| private final RelDataTypeFactory typeFactory; | ||
|
|
||
| private static final int MAX_NODES_FOR_IN_TO_OR_TRANSFORMATION; | ||
|
|
||
| static { | ||
| try { | ||
| MAX_NODES_FOR_IN_TO_OR_TRANSFORMATION = HiveConf.getIntVar( | ||
| Hive.get().getConf(), HiveConf.ConfVars.HIVEOPT_TRANSFORM_IN_MAXNODES); | ||
| } catch (HiveException e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Constructor used by HiveRexExecutorImpl. | ||
|
|
@@ -259,19 +272,12 @@ private RexNode convert(ExprNodeGenericFuncDesc func) throws SemanticException { | |
| // If it is a floor <date> operator, we need to rewrite it | ||
| childRexNodeLst = rewriteFloorDateChildren(calciteOp, childRexNodeLst, rexBuilder); | ||
| } else if (HiveIn.INSTANCE.equals(calciteOp) && isAllPrimitive) { | ||
| if (childRexNodeLst.size() == 2) { | ||
| // if it is a single item in an IN clause, transform A IN (B) to A = B | ||
| // from IN [A,B] => EQUALS [A,B] | ||
| // except complex types | ||
| calciteOp = SqlStdOperatorTable.EQUALS; | ||
| } else if (RexUtil.isReferenceOrAccess(childRexNodeLst.get(0), true)){ | ||
| // if it is more than an single item in an IN clause, | ||
| // transform from IN [A,B,C] => OR [EQUALS [A,B], EQUALS [A,C]] | ||
| // except complex types | ||
| // Rewrite to OR is done only if number of operands are less than | ||
| // the threshold configured | ||
| childRexNodeLst = rewriteInClauseChildren(calciteOp, childRexNodeLst, rexBuilder); | ||
| calciteOp = SqlStdOperatorTable.OR; | ||
| if (childRexNodeLst.size() == 2 || RexUtil.isReferenceOrAccess(childRexNodeLst.get(0), true)) { | ||
| RexNode rewritten = rewriteInClause(childRexNodeLst, rexBuilder); | ||
| assert rewritten instanceof RexCall; | ||
| RexCall call = (RexCall) rewritten; | ||
| calciteOp = call.op; | ||
| childRexNodeLst = call.operands; | ||
| } | ||
| } else if (calciteOp.getKind() == SqlKind.COALESCE && | ||
| childRexNodeLst.size() > 1) { | ||
|
|
@@ -577,17 +583,40 @@ public static List<RexNode> transformInToOrOperands(List<RexNode> operands, RexB | |
| return disjuncts; | ||
| } | ||
|
|
||
| public static List<RexNode> rewriteInClauseChildren(SqlOperator op, List<RexNode> childRexNodeLst, | ||
| RexBuilder rexBuilder) throws SemanticException { | ||
| assert op == HiveIn.INSTANCE; | ||
| RexNode firstPred = childRexNodeLst.get(0); | ||
| List<RexNode> newChildRexNodeLst = new ArrayList<RexNode>(); | ||
| for (int i = 1; i < childRexNodeLst.size(); i++) { | ||
| newChildRexNodeLst.add( | ||
| rexBuilder.makeCall( | ||
| SqlStdOperatorTable.EQUALS, firstPred, childRexNodeLst.get(i))); | ||
| /** | ||
| * 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); | ||
| } | ||
|
Comment on lines
+598
to
+600
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this become part of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know... Calcite's code just creates an IN with one element in this case, which is not incorrect (and I guess it's the expected behavior). I've just kept this special case on Hive's side for the moment to minimize side effects by this refactoring. |
||
|
|
||
| RexNode arg = childRexNodeLst.get(0); | ||
| List<RexNode> ranges = childRexNodeLst.subList(1, childRexNodeLst.size()); | ||
| // Avoid SEARCH on rows for the moment (it can lead to issues in Calcite), and check all types are SEARCH-compatible | ||
| if (!arg.getType().isStruct() | ||
| && ranges.stream().allMatch(range -> SqlTypeUtil.inSameFamily(arg.getType(), range.getType()))) { | ||
| RexNode search = rexBuilder.makeIn(arg, ranges); | ||
| assert search.getKind() == SqlKind.SEARCH; | ||
| return search; | ||
| } | ||
| return newChildRexNodeLst; | ||
|
|
||
| // Calcite SEARCH conversion was not possible: generate our own OR expression | ||
| if (MAX_NODES_FOR_IN_TO_OR_TRANSFORMATION == 0 || childRexNodeLst.size() <= MAX_NODES_FOR_IN_TO_OR_TRANSFORMATION) { | ||
| List<RexNode> newInputs = RexNodeConverter.transformInToOrOperands(childRexNodeLst, rexBuilder); | ||
| if (newInputs != null) { | ||
| return newInputs.size() == 1 ? newInputs.get(0) : rexBuilder.makeCall(SqlStdOperatorTable.OR, newInputs); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public static List<RexNode> rewriteCoalesceChildren( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we limit the transformation? What are the pros/cons of doing an unconditional rewrite?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just keeping the original logic to minimize the side effects of the change. I'll investigate why this condition was added in the first place and whether it can be removed...