Search in sources :

Example 6 with RexVisitorImpl

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;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) HiveSqlFunction(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSqlFunction) SqlOperator(org.apache.calcite.sql.SqlOperator) RexUtil(org.apache.calcite.rex.RexUtil) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Util(org.apache.calcite.util.Util) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl)

Example 7 with RexVisitorImpl

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();
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptUtil(org.apache.calcite.plan.RelOptUtil) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) Util(org.apache.calcite.util.Util) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with RexVisitorImpl

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);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RexVisitor(org.apache.calcite.rex.RexVisitor) RexInputRef(org.apache.calcite.rex.RexInputRef) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl) RexNode(org.apache.calcite.rex.RexNode) HashSet(java.util.HashSet)

Example 9 with RexVisitorImpl

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;
        }
    };
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl) RexSubQuery(org.apache.calcite.rex.RexSubQuery) RexNode(org.apache.calcite.rex.RexNode) Pair(org.apache.calcite.util.Pair)

Example 10 with RexVisitorImpl

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();
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) FieldsReWriterUtil(org.apache.drill.exec.planner.logical.FieldsReWriterUtil) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Util(org.apache.calcite.util.Util) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexVisitorImpl (org.apache.calcite.rex.RexVisitorImpl)11 RexNode (org.apache.calcite.rex.RexNode)9 RexCall (org.apache.calcite.rex.RexCall)8 RelOptUtil (org.apache.calcite.plan.RelOptUtil)7 RexInputRef (org.apache.calcite.rex.RexInputRef)7 Util (org.apache.calcite.util.Util)7 SqlValidatorUtil (org.apache.calcite.sql.validate.SqlValidatorUtil)5 RexUtil (org.apache.calcite.rex.RexUtil)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 RelNode (org.apache.calcite.rel.RelNode)2 RelMdUtil (org.apache.calcite.rel.metadata.RelMdUtil)2 RexSubQuery (org.apache.calcite.rex.RexSubQuery)2 Pair (org.apache.calcite.util.Pair)2 DrillRelOptUtil (org.apache.drill.exec.planner.common.DrillRelOptUtil)2 FieldsReWriterUtil (org.apache.drill.exec.planner.logical.FieldsReWriterUtil)2 PrelUtil (org.apache.drill.exec.planner.physical.PrelUtil)2 SlidingWindow (com.hazelcast.jet.sql.impl.opt.SlidingWindow)1 EnumSet (java.util.EnumSet)1 Set (java.util.Set)1