Search in sources :

Example 1 with LogicalCalc

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCalc in project calcite by apache.

the class EnumerableCalcRule method convert.

public RelNode convert(RelNode rel) {
    final LogicalCalc calc = (LogicalCalc) rel;
    final RelNode input = calc.getInput();
    return EnumerableCalc.create(convert(input, input.getTraitSet().replace(EnumerableConvention.INSTANCE)), calc.getProgram());
}
Also used : RelNode(org.apache.calcite.rel.RelNode) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc)

Example 2 with LogicalCalc

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCalc in project calcite by apache.

the class FilterCalcMergeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final LogicalFilter filter = call.rel(0);
    final LogicalCalc calc = call.rel(1);
    // We'll have chance to merge later, when the over is expanded.
    if (calc.getProgram().containsAggs()) {
        return;
    }
    // Create a program containing the filter.
    final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
    final RexProgramBuilder progBuilder = new RexProgramBuilder(calc.getRowType(), rexBuilder);
    progBuilder.addIdentity();
    progBuilder.addCondition(filter.getCondition());
    RexProgram topProgram = progBuilder.getProgram();
    RexProgram bottomProgram = calc.getProgram();
    // Merge the programs together.
    RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder);
    final LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), mergedProgram);
    call.transformTo(newCalc);
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RexBuilder(org.apache.calcite.rex.RexBuilder) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder)

Example 3 with LogicalCalc

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCalc in project calcite by apache.

the class RelOptUtil method permute.

/**
 * Creates a relational expression which permutes the output fields of a
 * relational expression according to a permutation.
 *
 * <p>Optimizations:</p>
 *
 * <ul>
 * <li>If the relational expression is a
 * {@link org.apache.calcite.rel.logical.LogicalCalc} or
 * {@link org.apache.calcite.rel.logical.LogicalProject} that is already
 * acting as a permutation, combines the new permutation with the old;</li>
 *
 * <li>If the permutation is the identity, returns the original relational
 * expression.</li>
 * </ul>
 *
 * <p>If a permutation is combined with its inverse, these optimizations
 * would combine to remove them both.
 *
 * @param rel         Relational expression
 * @param permutation Permutation to apply to fields
 * @param fieldNames  Field names; if null, or if a particular entry is null,
 *                    the name of the permuted field is used
 * @return relational expression which permutes its input fields
 */
public static RelNode permute(RelNode rel, Permutation permutation, List<String> fieldNames) {
    if (permutation.isIdentity()) {
        return rel;
    }
    if (rel instanceof LogicalCalc) {
        LogicalCalc calc = (LogicalCalc) rel;
        Permutation permutation1 = calc.getProgram().getPermutation();
        if (permutation1 != null) {
            Permutation permutation2 = permutation.product(permutation1);
            return permute(rel, permutation2, null);
        }
    }
    if (rel instanceof LogicalProject) {
        Permutation permutation1 = ((LogicalProject) rel).getPermutation();
        if (permutation1 != null) {
            Permutation permutation2 = permutation.product(permutation1);
            return permute(rel, permutation2, null);
        }
    }
    final List<RelDataType> outputTypeList = new ArrayList<>();
    final List<String> outputNameList = new ArrayList<>();
    final List<RexNode> exprList = new ArrayList<>();
    final List<RexLocalRef> projectRefList = new ArrayList<>();
    final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
    final RelOptCluster cluster = rel.getCluster();
    for (int i = 0; i < permutation.getTargetCount(); i++) {
        int target = permutation.getTarget(i);
        final RelDataTypeField targetField = fields.get(target);
        outputTypeList.add(targetField.getType());
        outputNameList.add(((fieldNames == null) || (fieldNames.size() <= i) || (fieldNames.get(i) == null)) ? targetField.getName() : fieldNames.get(i));
        exprList.add(cluster.getRexBuilder().makeInputRef(fields.get(i).getType(), i));
        final int source = permutation.getSource(i);
        projectRefList.add(new RexLocalRef(source, fields.get(source).getType()));
    }
    final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
    final RexProgram program = new RexProgram(rel.getRowType(), exprList, projectRefList, null, typeFactory.createStructType(outputTypeList, outputNameList));
    return LogicalCalc.create(rel, program);
}
Also used : Permutation(org.apache.calcite.util.Permutation) RexProgram(org.apache.calcite.rex.RexProgram) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexLocalRef(org.apache.calcite.rex.RexLocalRef) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with LogicalCalc

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCalc in project calcite by apache.

the class ReduceDecimalsRule method onMatch.

// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    LogicalCalc calc = call.rel(0);
    // Expand decimals in every expression in this program. If no
    // expression changes, don't apply the rule.
    final RexProgram program = calc.getProgram();
    if (!RexUtil.requiresDecimalExpansion(program, true)) {
        return;
    }
    final RexBuilder rexBuilder = calc.getCluster().getRexBuilder();
    final RexShuttle shuttle = new DecimalShuttle(rexBuilder);
    RexProgramBuilder programBuilder = RexProgramBuilder.create(rexBuilder, calc.getInput().getRowType(), program.getExprList(), program.getProjectList(), program.getCondition(), program.getOutputRowType(), shuttle, true);
    final RexProgram newProgram = programBuilder.getProgram();
    LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), newProgram);
    call.transformTo(newCalc);
}
Also used : RexShuttle(org.apache.calcite.rex.RexShuttle) RexProgram(org.apache.calcite.rex.RexProgram) RexBuilder(org.apache.calcite.rex.RexBuilder) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder)

Example 5 with LogicalCalc

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCalc in project calcite by apache.

the class CalcRelSplitter method execute.

// ~ Methods ----------------------------------------------------------------
RelNode execute() {
    // expressions to the left.
    assert program.isValid(Litmus.THROW, null);
    final List<RexNode> exprList = program.getExprList();
    final RexNode[] exprs = exprList.toArray(new RexNode[exprList.size()]);
    assert !RexUtil.containComplexExprs(exprList);
    // Figure out what level each expression belongs to.
    int[] exprLevels = new int[exprs.length];
    // The type of a level is given by
    // relTypes[levelTypeOrdinals[level]].
    int[] levelTypeOrdinals = new int[exprs.length];
    int levelCount = chooseLevels(exprs, -1, exprLevels, levelTypeOrdinals);
    // For each expression, figure out which is the highest level where it
    // is used.
    int[] exprMaxUsingLevelOrdinals = new HighestUsageFinder(exprs, exprLevels).getMaxUsingLevelOrdinals();
    // If expressions are used as outputs, mark them as higher than that.
    final List<RexLocalRef> projectRefList = program.getProjectList();
    final RexLocalRef conditionRef = program.getCondition();
    for (RexLocalRef projectRef : projectRefList) {
        exprMaxUsingLevelOrdinals[projectRef.getIndex()] = levelCount;
    }
    if (conditionRef != null) {
        exprMaxUsingLevelOrdinals[conditionRef.getIndex()] = levelCount;
    }
    // Print out what we've got.
    if (RULE_LOGGER.isTraceEnabled()) {
        traceLevelExpressions(exprs, exprLevels, levelTypeOrdinals, levelCount);
    }
    // Now build the calcs.
    RelNode rel = child;
    final int inputFieldCount = program.getInputRowType().getFieldCount();
    int[] inputExprOrdinals = identityArray(inputFieldCount);
    boolean doneCondition = false;
    for (int level = 0; level < levelCount; level++) {
        final int[] projectExprOrdinals;
        final RelDataType outputRowType;
        if (level == (levelCount - 1)) {
            outputRowType = program.getOutputRowType();
            projectExprOrdinals = new int[projectRefList.size()];
            for (int i = 0; i < projectExprOrdinals.length; i++) {
                projectExprOrdinals[i] = projectRefList.get(i).getIndex();
            }
        } else {
            outputRowType = null;
            // Project the expressions which are computed at this level or
            // before, and will be used at later levels.
            List<Integer> projectExprOrdinalList = new ArrayList<>();
            for (int i = 0; i < exprs.length; i++) {
                RexNode expr = exprs[i];
                if (expr instanceof RexLiteral) {
                    // Don't project literals. They are always created in
                    // the level where they are used.
                    exprLevels[i] = -1;
                    continue;
                }
                if ((exprLevels[i] <= level) && (exprMaxUsingLevelOrdinals[i] > level)) {
                    projectExprOrdinalList.add(i);
                }
            }
            projectExprOrdinals = Ints.toArray(projectExprOrdinalList);
        }
        final RelType relType = relTypes[levelTypeOrdinals[level]];
        // Can we do the condition this level?
        int conditionExprOrdinal = -1;
        if ((conditionRef != null) && !doneCondition) {
            conditionExprOrdinal = conditionRef.getIndex();
            if ((exprLevels[conditionExprOrdinal] > level) || !relType.supportsCondition()) {
                // stand down -- we're not ready to do the condition yet
                conditionExprOrdinal = -1;
            } else {
                doneCondition = true;
            }
        }
        RexProgram program1 = createProgramForLevel(level, levelCount, rel.getRowType(), exprs, exprLevels, inputExprOrdinals, projectExprOrdinals, conditionExprOrdinal, outputRowType);
        rel = relType.makeRel(cluster, traits, relBuilder, rel, program1);
        // want these. They cause an explosion in the search space.
        if (rel instanceof LogicalCalc && ((LogicalCalc) rel).getProgram().isTrivial()) {
            rel = rel.getInput(0);
        }
        rel = handle(rel);
        // The outputs of this level will be the inputs to the next level.
        inputExprOrdinals = projectExprOrdinals;
    }
    Preconditions.checkArgument(doneCondition || (conditionRef == null), "unhandled condition");
    return rel;
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RexProgram(org.apache.calcite.rex.RexProgram) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelNode(org.apache.calcite.rel.RelNode) RexLocalRef(org.apache.calcite.rex.RexLocalRef) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

LogicalCalc (org.apache.calcite.rel.logical.LogicalCalc)10 RexProgram (org.apache.calcite.rex.RexProgram)9 RelNode (org.apache.calcite.rel.RelNode)6 RexProgramBuilder (org.apache.calcite.rex.RexProgramBuilder)5 RexBuilder (org.apache.calcite.rex.RexBuilder)4 RexNode (org.apache.calcite.rex.RexNode)4 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)3 RelDataType (org.apache.calcite.rel.type.RelDataType)3 RexLocalRef (org.apache.calcite.rex.RexLocalRef)3 ArrayList (java.util.ArrayList)2 RelOptCluster (org.apache.calcite.plan.RelOptCluster)2 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)2 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)1 LogicalCalc (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCalc)1 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)1 RexOver (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexOver)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 RexShuttle (org.apache.calcite.rex.RexShuttle)1