Search in sources :

Example 56 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class LogicalCalc method collectVariablesUsed.

@Override
public void collectVariablesUsed(Set<CorrelationId> variableSet) {
    final RelOptUtil.VariableUsedVisitor vuv = new RelOptUtil.VariableUsedVisitor(null);
    for (RexNode expr : program.getExprList()) {
        expr.accept(vuv);
    }
    variableSet.addAll(vuv.variables);
}
Also used : RelOptUtil(org.apache.calcite.plan.RelOptUtil) RexNode(org.apache.calcite.rex.RexNode)

Example 57 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RelMdPredicates method getPredicates.

/**
 * Infers predicates for a Union.
 */
public RelOptPredicateList getPredicates(Union union, RelMetadataQuery mq) {
    RexBuilder rexBuilder = union.getCluster().getRexBuilder();
    Map<String, RexNode> finalPreds = new HashMap<>();
    List<RexNode> finalResidualPreds = new ArrayList<>();
    for (int i = 0; i < union.getInputs().size(); i++) {
        RelNode input = union.getInputs().get(i);
        RelOptPredicateList info = mq.getPulledUpPredicates(input);
        if (info.pulledUpPredicates.isEmpty()) {
            return RelOptPredicateList.EMPTY;
        }
        Map<String, RexNode> preds = new HashMap<>();
        List<RexNode> residualPreds = new ArrayList<>();
        for (RexNode pred : info.pulledUpPredicates) {
            final String predDigest = pred.toString();
            if (i == 0) {
                preds.put(predDigest, pred);
                continue;
            }
            if (finalPreds.containsKey(predDigest)) {
                preds.put(predDigest, pred);
            } else {
                residualPreds.add(pred);
            }
        }
        // Add new residual preds
        finalResidualPreds.add(RexUtil.composeConjunction(rexBuilder, residualPreds, false));
        // Add those that are not part of the final set to residual
        for (Entry<String, RexNode> e : finalPreds.entrySet()) {
            if (!preds.containsKey(e.getKey())) {
                // This node was in previous union inputs, but it is not in this one
                for (int j = 0; j < i; j++) {
                    finalResidualPreds.set(j, RexUtil.composeConjunction(rexBuilder, Lists.newArrayList(finalResidualPreds.get(j), e.getValue()), false));
                }
            }
        }
        // Final preds
        finalPreds = preds;
    }
    List<RexNode> preds = new ArrayList<>(finalPreds.values());
    final RelOptCluster cluster = union.getCluster();
    final RexExecutor executor = Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR);
    final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
    final RexSimplify simplify = new RexSimplify(rexBuilder, predicates, true, executor);
    RexNode disjPred = simplify.simplifyOrs(finalResidualPreds);
    if (!disjPred.isAlwaysTrue()) {
        preds.add(disjPred);
    }
    return RelOptPredicateList.of(rexBuilder, preds);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelNode(org.apache.calcite.rel.RelNode) RexSimplify(org.apache.calcite.rex.RexSimplify) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexExecutor(org.apache.calcite.rex.RexExecutor) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 58 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RelMdPredicates method getPredicates.

/**
 * Infers predicates for an Aggregate.
 *
 * <p>Pulls up predicates that only contains references to columns in the
 * GroupSet. For e.g.
 *
 * <blockquote><pre>
 * inputPullUpExprs : { a &gt; 7, b + c &lt; 10, a + e = 9}
 * groupSet         : { a, b}
 * pulledUpExprs    : { a &gt; 7}
 * </pre></blockquote>
 */
public RelOptPredicateList getPredicates(Aggregate agg, RelMetadataQuery mq) {
    final RelNode input = agg.getInput();
    final RexBuilder rexBuilder = agg.getCluster().getRexBuilder();
    final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input);
    final List<RexNode> aggPullUpPredicates = new ArrayList<>();
    ImmutableBitSet groupKeys = agg.getGroupSet();
    if (groupKeys.isEmpty()) {
        // no rows!) but not on the output (there is one row).
        return RelOptPredicateList.EMPTY;
    }
    Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION, input.getRowType().getFieldCount(), agg.getRowType().getFieldCount());
    int i = 0;
    for (int j : groupKeys) {
        m.set(j, i++);
    }
    for (RexNode r : inputInfo.pulledUpPredicates) {
        ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r);
        if (groupKeys.contains(rCols)) {
            r = r.accept(new RexPermuteInputsShuttle(m, input));
            aggPullUpPredicates.add(r);
        }
    }
    return RelOptPredicateList.of(rexBuilder, aggPullUpPredicates);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) Mapping(org.apache.calcite.util.mapping.Mapping) RexPermuteInputsShuttle(org.apache.calcite.rex.RexPermuteInputsShuttle) RexNode(org.apache.calcite.rex.RexNode)

Example 59 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RelMdSelectivity method getSelectivity.

public Double getSelectivity(SemiJoin rel, RelMetadataQuery mq, RexNode predicate) {
    // create a RexNode representing the selectivity of the
    // semijoin filter and pass it to getSelectivity
    RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    RexNode newPred = RelMdUtil.makeSemiJoinSelectivityRexNode(mq, rel);
    if (predicate != null) {
        newPred = rexBuilder.makeCall(SqlStdOperatorTable.AND, newPred, predicate);
    }
    return mq.getSelectivity(rel.getLeft(), newPred);
}
Also used : RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 60 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RelMdSelectivity method getSelectivity.

public Double getSelectivity(Project rel, RelMetadataQuery mq, RexNode predicate) {
    final List<RexNode> notPushable = new ArrayList<>();
    final List<RexNode> pushable = new ArrayList<>();
    RelOptUtil.splitFilters(ImmutableBitSet.range(rel.getRowType().getFieldCount()), predicate, pushable, notPushable);
    final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    RexNode childPred = RexUtil.composeConjunction(rexBuilder, pushable, true);
    RexNode modifiedPred;
    if (childPred == null) {
        modifiedPred = null;
    } else {
        modifiedPred = RelOptUtil.pushPastProject(childPred, rel);
    }
    Double selectivity = mq.getSelectivity(rel.getInput(), modifiedPred);
    if (selectivity == null) {
        return null;
    } else {
        RexNode pred = RexUtil.composeConjunction(rexBuilder, notPushable, true);
        return selectivity * RelMdUtil.guessSelectivity(pred);
    }
}
Also used : ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexNode (org.apache.calcite.rex.RexNode)1167 ArrayList (java.util.ArrayList)423 RelNode (org.apache.calcite.rel.RelNode)363 RelDataType (org.apache.calcite.rel.type.RelDataType)289 RexBuilder (org.apache.calcite.rex.RexBuilder)262 RexInputRef (org.apache.calcite.rex.RexInputRef)207 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)198 RexCall (org.apache.calcite.rex.RexCall)185 Test (org.junit.Test)138 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)136 RexLiteral (org.apache.calcite.rex.RexLiteral)104 HashMap (java.util.HashMap)102 List (java.util.List)97 AggregateCall (org.apache.calcite.rel.core.AggregateCall)83 Pair (org.apache.calcite.util.Pair)79 Project (org.apache.calcite.rel.core.Project)77 RelBuilder (org.apache.calcite.tools.RelBuilder)77 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)66 ImmutableList (com.google.common.collect.ImmutableList)64 Map (java.util.Map)63