Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Author

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...

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) {
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this become part of RexBuilder#makeIn API if not already the case?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.util.Util;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.exec.FunctionInfo;
import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
import org.apache.hadoop.hive.ql.exec.HiveFunctionInfo;
import org.apache.hadoop.hive.ql.metadata.Hive;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRexExecutorImpl;
import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveExtractDate;
import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFloorDate;
Expand Down Expand Up @@ -102,16 +99,9 @@ public class HiveFunctionHelper implements FunctionHelper {
private static final Logger LOG = LoggerFactory.getLogger(HiveFunctionHelper.class);

private final RexBuilder rexBuilder;
private final int maxNodesForInToOrTransformation;

public HiveFunctionHelper(RexBuilder rexBuilder) {
this.rexBuilder = rexBuilder;
try {
this.maxNodesForInToOrTransformation = HiveConf.getIntVar(
Hive.get().getConf(), HiveConf.ConfVars.HIVEOPT_TRANSFORM_IN_MAXNODES);
} catch (HiveException e) {
throw new IllegalStateException(e);
}
}

/**
Expand Down Expand Up @@ -267,28 +257,12 @@ public RexNode getExpression(String functionText, FunctionInfo fi,
// If it is a floor <date> operator, we need to rewrite it
inputs = RexNodeConverter.rewriteFloorDateChildren(calciteOp, inputs, rexBuilder);
} else if (HiveIn.INSTANCE.equals(calciteOp)) {
// if it is a single item in an IN clause, transform A IN (B) to A = B
// from IN [A,B] => EQUALS [A,B]
// 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]]
// Rewrite to OR is done only if number of operands are less than
// the threshold configured
boolean rewriteToOr = true;
if(maxNodesForInToOrTransformation != 0) {
if(inputs.size() > maxNodesForInToOrTransformation) {
rewriteToOr = false;
}
}
if(rewriteToOr) {
// If there are non-deterministic functions, we cannot perform this rewriting
List<RexNode> newInputs = RexNodeConverter.transformInToOrOperands(inputs, rexBuilder);
if (newInputs != null) {
inputs = newInputs;
if (inputs.size() == 1) {
inputs.add(rexBuilder.makeLiteral(false));
}
calciteOp = SqlStdOperatorTable.OR;
}
RexNode rewritten = RexNodeConverter.rewriteInClause(inputs, rexBuilder);
if (rewritten != null) {
assert rewritten instanceof RexCall;
RexCall call = (RexCall) rewritten;
calciteOp = call.op;
inputs = call.operands;
}
} else if (calciteOp.getKind() == SqlKind.COALESCE &&
inputs.size() > 1) {
Expand Down
Loading