Search in sources :

Example 61 with RexBuilder

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

the class RexProgramTest method setUp.

@Before
public void setUp() {
    typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    rexBuilder = new RexBuilder(typeFactory);
    RexExecutor executor = new RexExecutorImpl(new DummyTestDataContext());
    simplify = new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, false, executor);
    trueLiteral = rexBuilder.makeLiteral(true);
    falseLiteral = rexBuilder.makeLiteral(false);
    final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
    nullLiteral = rexBuilder.makeNullLiteral(intType);
    unknownLiteral = rexBuilder.makeNullLiteral(trueLiteral.getType());
}
Also used : RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RexSimplify(org.apache.calcite.rex.RexSimplify) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RexExecutor(org.apache.calcite.rex.RexExecutor) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) Before(org.junit.Before)

Example 62 with RexBuilder

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

the class FrameworksTest method testOptimize.

@Test
public void testOptimize() {
    RelNode x = Frameworks.withPlanner(new Frameworks.PlannerAction<RelNode>() {

        public RelNode apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
            final Table table = new AbstractTable() {

                public RelDataType getRowType(RelDataTypeFactory typeFactory) {
                    final RelDataType stringType = typeFactory.createJavaType(String.class);
                    final RelDataType integerType = typeFactory.createJavaType(Integer.class);
                    return typeFactory.builder().add("s", stringType).add("i", integerType).build();
                }
            };
            // "SELECT * FROM myTable"
            final RelOptAbstractTable relOptTable = new RelOptAbstractTable(relOptSchema, "myTable", table.getRowType(typeFactory)) {
            };
            final EnumerableTableScan tableRel = EnumerableTableScan.create(cluster, relOptTable);
            // "WHERE i > 1"
            final RexBuilder rexBuilder = cluster.getRexBuilder();
            final RexNode condition = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(tableRel), "i", true), rexBuilder.makeExactLiteral(BigDecimal.ONE));
            final LogicalFilter filter = LogicalFilter.create(tableRel, condition);
            // Specify that the result should be in Enumerable convention.
            final RelNode rootRel = filter;
            final RelOptPlanner planner = cluster.getPlanner();
            RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
            final RelNode rootRel2 = planner.changeTraits(rootRel, desiredTraits);
            planner.setRoot(rootRel2);
            // Now, plan.
            return planner.findBestExp();
        }
    });
    String s = RelOptUtil.dumpPlan("", x, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES);
    assertThat(Util.toLinux(s), equalTo("EnumerableFilter(condition=[>($1, 1)])\n" + "  EnumerableTableScan(table=[[myTable]])\n"));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelOptSchema(org.apache.calcite.plan.RelOptSchema) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) Table(org.apache.calcite.schema.Table) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) RelNode(org.apache.calcite.rel.RelNode) EnumerableTableScan(org.apache.calcite.adapter.enumerable.EnumerableTableScan) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 63 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder 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 64 with RexBuilder

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

the class SemiJoinRule method perform.

protected void perform(RelOptRuleCall call, Project project, Join join, RelNode left, Aggregate aggregate) {
    final RelOptCluster cluster = join.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    if (project != null) {
        final ImmutableBitSet bits = RelOptUtil.InputFinder.bits(project.getProjects(), null);
        final ImmutableBitSet rightBits = ImmutableBitSet.range(left.getRowType().getFieldCount(), join.getRowType().getFieldCount());
        if (bits.intersects(rightBits)) {
            return;
        }
    }
    final JoinInfo joinInfo = join.analyzeCondition();
    if (!joinInfo.rightSet().equals(ImmutableBitSet.range(aggregate.getGroupCount()))) {
        // By the way, neither a super-set nor a sub-set would work.
        return;
    }
    if (!joinInfo.isEqui()) {
        return;
    }
    final RelBuilder relBuilder = call.builder();
    relBuilder.push(left);
    switch(join.getJoinType()) {
        case INNER:
            final List<Integer> newRightKeyBuilder = Lists.newArrayList();
            final List<Integer> aggregateKeys = aggregate.getGroupSet().asList();
            for (int key : joinInfo.rightKeys) {
                newRightKeyBuilder.add(aggregateKeys.get(key));
            }
            final ImmutableIntList newRightKeys = ImmutableIntList.copyOf(newRightKeyBuilder);
            relBuilder.push(aggregate.getInput());
            final RexNode newCondition = RelOptUtil.createEquiJoinCondition(relBuilder.peek(2, 0), joinInfo.leftKeys, relBuilder.peek(2, 1), newRightKeys, rexBuilder);
            relBuilder.semiJoin(newCondition);
            break;
        case LEFT:
            // we can eliminate the semi-join.
            break;
        default:
            throw new AssertionError(join.getJoinType());
    }
    if (project != null) {
        relBuilder.project(project.getProjects(), project.getRowType().getFieldNames());
    }
    call.transformTo(relBuilder.build());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) JoinInfo(org.apache.calcite.rel.core.JoinInfo) RelBuilder(org.apache.calcite.tools.RelBuilder) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RexBuilder(org.apache.calcite.rex.RexBuilder) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) RexNode(org.apache.calcite.rex.RexNode)

Example 65 with RexBuilder

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

the class ValuesReduceRule method apply.

// ~ Methods ----------------------------------------------------------------
/**
 * Does the work.
 *
 * @param call    Rule call
 * @param project Project, may be null
 * @param filter  Filter, may be null
 * @param values  Values rel to be reduced
 */
protected void apply(RelOptRuleCall call, LogicalProject project, LogicalFilter filter, LogicalValues values) {
    assert values != null;
    assert filter != null || project != null;
    final RexNode conditionExpr = (filter == null) ? null : filter.getCondition();
    final List<RexNode> projectExprs = (project == null) ? null : project.getProjects();
    RexBuilder rexBuilder = values.getCluster().getRexBuilder();
    // Find reducible expressions.
    final List<RexNode> reducibleExps = new ArrayList<>();
    final MyRexShuttle shuttle = new MyRexShuttle();
    for (final List<RexLiteral> literalList : values.getTuples()) {
        shuttle.literalList = literalList;
        if (conditionExpr != null) {
            RexNode c = conditionExpr.accept(shuttle);
            reducibleExps.add(c);
        }
        if (projectExprs != null) {
            int k = -1;
            for (RexNode projectExpr : projectExprs) {
                ++k;
                RexNode e = projectExpr.accept(shuttle);
                if (RexLiteral.isNullLiteral(e)) {
                    e = rexBuilder.makeAbstractCast(project.getRowType().getFieldList().get(k).getType(), e);
                }
                reducibleExps.add(e);
            }
        }
    }
    int fieldsPerRow = ((conditionExpr == null) ? 0 : 1) + ((projectExprs == null) ? 0 : projectExprs.size());
    assert fieldsPerRow > 0;
    assert reducibleExps.size() == (values.getTuples().size() * fieldsPerRow);
    // Compute the values they reduce to.
    final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
    ReduceExpressionsRule.reduceExpressions(values, reducibleExps, predicates, false, true);
    int changeCount = 0;
    final ImmutableList.Builder<ImmutableList<RexLiteral>> tuplesBuilder = ImmutableList.builder();
    for (int row = 0; row < values.getTuples().size(); ++row) {
        int i = 0;
        RexNode reducedValue;
        if (conditionExpr != null) {
            reducedValue = reducibleExps.get((row * fieldsPerRow) + i);
            ++i;
            if (!reducedValue.isAlwaysTrue()) {
                ++changeCount;
                continue;
            }
        }
        ImmutableList<RexLiteral> valuesList;
        if (projectExprs != null) {
            ++changeCount;
            final ImmutableList.Builder<RexLiteral> tupleBuilder = ImmutableList.builder();
            for (; i < fieldsPerRow; ++i) {
                reducedValue = reducibleExps.get((row * fieldsPerRow) + i);
                if (reducedValue instanceof RexLiteral) {
                    tupleBuilder.add((RexLiteral) reducedValue);
                } else if (RexUtil.isNullLiteral(reducedValue, true)) {
                    tupleBuilder.add(rexBuilder.constantNull());
                } else {
                    return;
                }
            }
            valuesList = tupleBuilder.build();
        } else {
            valuesList = values.getTuples().get(row);
        }
        tuplesBuilder.add(valuesList);
    }
    if (changeCount > 0) {
        final RelDataType rowType;
        if (projectExprs != null) {
            rowType = project.getRowType();
        } else {
            rowType = values.getRowType();
        }
        final RelNode newRel = LogicalValues.create(values.getCluster(), rowType, tuplesBuilder.build());
        call.transformTo(newRel);
    } else {
        // Filter had no effect, so we can say that Filter(Values) ==
        // Values.
        call.transformTo(values);
    }
    // can send the volcano planner into a loop; see dtbug 2070.)
    if (filter != null) {
        call.getPlanner().setImportance(filter, 0.0);
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexBuilder (org.apache.calcite.rex.RexBuilder)314 RexNode (org.apache.calcite.rex.RexNode)248 ArrayList (java.util.ArrayList)151 RelNode (org.apache.calcite.rel.RelNode)121 RelDataType (org.apache.calcite.rel.type.RelDataType)121 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)77 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)59 RexInputRef (org.apache.calcite.rex.RexInputRef)49 RelBuilder (org.apache.calcite.tools.RelBuilder)49 AggregateCall (org.apache.calcite.rel.core.AggregateCall)48 List (java.util.List)41 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)41 RelOptCluster (org.apache.calcite.plan.RelOptCluster)36 HashMap (java.util.HashMap)33 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)24 Project (org.apache.calcite.rel.core.Project)24 RexCall (org.apache.calcite.rex.RexCall)24 BigDecimal (java.math.BigDecimal)23 Collectors (java.util.stream.Collectors)23 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)22