use of org.apache.calcite.rex.RexVisitorImpl in project hive by apache.
the class HiveCalciteUtil method checkMaterializable.
/**
* Check if the expression is usable for query materialization, returning the first failing expression.
*/
public static RexCall checkMaterializable(RexNode expr) {
RexCall failingCall = null;
if (expr == null) {
return null;
}
RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
@Override
public Void visitCall(org.apache.calcite.rex.RexCall call) {
// non-deterministic functions as well as runtime constants are not materializable.
SqlOperator op = call.getOperator();
if (!op.isDeterministic() || op.isDynamicFunction() || (op instanceof HiveSqlFunction && ((HiveSqlFunction) op).isRuntimeConstant())) {
throw new Util.FoundOne(call);
}
return super.visitCall(call);
}
};
try {
expr.accept(visitor);
} catch (Util.FoundOne e) {
failingCall = (RexCall) e.getNode();
}
return failingCall;
}
use of org.apache.calcite.rex.RexVisitorImpl in project drill by apache.
the class DrillRelOptUtil method findItemOrFlatten.
/**
* Travesal RexNode to find the item/flattern operator. Continue search if RexNode has a
* RexInputRef which refers to a RexNode in project expressions.
*
* @param node : RexNode to search
* @param projExprs : the list of project expressions. Empty list means there is No project operator underneath.
* @return : Return null if there is NONE; return the first appearance of item/flatten RexCall.
*/
public static RexCall findItemOrFlatten(final RexNode node, final List<RexNode> projExprs) {
try {
RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
public Void visitCall(RexCall call) {
if ("item".equals(call.getOperator().getName().toLowerCase()) || "flatten".equals(call.getOperator().getName().toLowerCase())) {
throw new Util.FoundOne(call);
/* throw exception to interrupt tree walk (this is similar to
other utility methods in RexUtil.java */
}
return super.visitCall(call);
}
public Void visitInputRef(RexInputRef inputRef) {
if (projExprs.size() == 0) {
return super.visitInputRef(inputRef);
} else {
final int index = inputRef.getIndex();
RexNode n = projExprs.get(index);
if (n instanceof RexCall) {
RexCall r = (RexCall) n;
if ("item".equals(r.getOperator().getName().toLowerCase()) || "flatten".equals(r.getOperator().getName().toLowerCase())) {
throw new Util.FoundOne(r);
}
}
return super.visitInputRef(inputRef);
}
}
};
node.accept(visitor);
return null;
} catch (Util.FoundOne e) {
Util.swallow(e, null);
return (RexCall) e.getNode();
}
}
use of org.apache.calcite.rex.RexVisitorImpl in project calcite by apache.
the class RelMdColumnOrigins method getColumnOrigins.
public Set<RelColumnOrigin> getColumnOrigins(Project rel, final RelMetadataQuery mq, int iOutputColumn) {
final RelNode input = rel.getInput();
RexNode rexNode = rel.getProjects().get(iOutputColumn);
if (rexNode instanceof RexInputRef) {
// Direct reference: no derivation added.
RexInputRef inputRef = (RexInputRef) rexNode;
return mq.getColumnOrigins(input, inputRef.getIndex());
}
// Anything else is a derivation, possibly from multiple
// columns.
final Set<RelColumnOrigin> set = new HashSet<>();
RexVisitor visitor = new RexVisitorImpl<Void>(true) {
public Void visitInputRef(RexInputRef inputRef) {
Set<RelColumnOrigin> inputSet = mq.getColumnOrigins(input, inputRef.getIndex());
if (inputSet != null) {
set.addAll(inputSet);
}
return null;
}
};
rexNode.accept(visitor);
return createDerivedColumnOrigins(set);
}
use of org.apache.calcite.rex.RexVisitorImpl in project flink by apache.
the class SubQueryDecorrelator method handleSubQuery.
private RexVisitorImpl<Void> handleSubQuery(final RelNode rel) {
return new RexVisitorImpl<Void>(true) {
@Override
public Void visitSubQuery(RexSubQuery subQuery) {
RelNode newRel = subQuery.rel;
if (subQuery.getKind() == SqlKind.IN) {
newRel = addProjectionForIn(subQuery.rel);
}
final Frame frame = decorrelator.getInvoke(newRel);
if (frame != null && frame.c != null) {
Frame target = frame;
if (subQuery.getKind() == SqlKind.EXISTS) {
target = addProjectionForExists(frame);
}
final DecorrelateRexShuttle shuttle = new DecorrelateRexShuttle(rel.getRowType(), target.r.getRowType(), rel.getVariablesSet());
final RexNode newCondition = target.c.accept(shuttle);
Pair<RelNode, RexNode> newNodeAndCondition = new Pair<>(target.r, newCondition);
subQueryMap.put(subQuery, newNodeAndCondition);
}
return null;
}
};
}
use of org.apache.calcite.rex.RexVisitorImpl in project drill by apache.
the class DrillRelOptUtil method findOperators.
/**
* Travesal RexNode to find at least one operator in the given collection. Continue search if RexNode has a
* RexInputRef which refers to a RexNode in project expressions.
*
* @param node RexNode to search
* @param projExprs the list of project expressions. Empty list means there is No project operator underneath.
* @param operators collection of operators to find
* @return Return null if there is NONE; return the first appearance of item/flatten RexCall.
*/
public static RexCall findOperators(final RexNode node, final List<RexNode> projExprs, final Collection<String> operators) {
try {
RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
@Override
public Void visitCall(RexCall call) {
if (operators.contains(call.getOperator().getName().toLowerCase())) {
throw new Util.FoundOne(call);
/* throw exception to interrupt tree walk (this is similar to
other utility methods in RexUtil.java */
}
return super.visitCall(call);
}
@Override
public Void visitInputRef(RexInputRef inputRef) {
if (projExprs.size() == 0) {
return super.visitInputRef(inputRef);
} else {
final int index = inputRef.getIndex();
RexNode n = projExprs.get(index);
if (n instanceof RexCall) {
RexCall r = (RexCall) n;
if (operators.contains(r.getOperator().getName().toLowerCase())) {
throw new Util.FoundOne(r);
}
}
return super.visitInputRef(inputRef);
}
}
};
node.accept(visitor);
return null;
} catch (Util.FoundOne e) {
Util.swallow(e, null);
return (RexCall) e.getNode();
}
}
Aggregations