Search in sources :

Example 16 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project hive by apache.

the class FieldTrimmerBench method initTrial.

@Setup(Level.Trial)
public void initTrial() {
    // Init cluster and builder
    final RelOptPlanner planner = CalcitePlanner.createPlanner(new HiveConf());
    final RexBuilder rexBuilder = new RexBuilder(new JavaTypeFactoryImpl(new HiveTypeSystemImpl()));
    relOptCluster = RelOptCluster.create(planner, rexBuilder);
    relBuilder = HiveRelFactories.HIVE_BUILDER.create(relOptCluster, null);
    // Create operator tree
    DummyNode0 d0 = new DummyNode0(relOptCluster, relOptCluster.traitSet());
    DummyNode1 d1 = new DummyNode1(relOptCluster, relOptCluster.traitSet());
    DummyNode2 d2 = new DummyNode2(relOptCluster, relOptCluster.traitSet());
    DummyNode3 d3 = new DummyNode3(relOptCluster, relOptCluster.traitSet());
    DummyNode4 d4 = new DummyNode4(relOptCluster, relOptCluster.traitSet(), d0);
    DummyNode5 d5 = new DummyNode5(relOptCluster, relOptCluster.traitSet(), d1);
    DummyNode6 d6 = new DummyNode6(relOptCluster, relOptCluster.traitSet(), d2);
    DummyNode7 d7 = new DummyNode7(relOptCluster, relOptCluster.traitSet(), d3);
    DummyNode8 d8 = new DummyNode8(relOptCluster, relOptCluster.traitSet(), d4, d5);
    DummyNode9 d9 = new DummyNode9(relOptCluster, relOptCluster.traitSet(), d6, d7);
    root = new DummyNode9(relOptCluster, relOptCluster.traitSet(), d8, d9);
}
Also used : RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RexBuilder(org.apache.calcite.rex.RexBuilder) HiveConf(org.apache.hadoop.hive.conf.HiveConf) HiveTypeSystemImpl(org.apache.hadoop.hive.ql.optimizer.calcite.HiveTypeSystemImpl) Setup(org.openjdk.jmh.annotations.Setup)

Example 17 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project beam by apache.

the class BeamAggregationRel method beamComputeSelfCost.

@Override
public BeamCostModel beamComputeSelfCost(RelOptPlanner planner, BeamRelMetadataQuery mq) {
    NodeStats inputStat = BeamSqlRelUtils.getNodeStats(this.input, mq);
    inputStat = computeWindowingCostEffect(inputStat);
    // Aggregates with more aggregate functions cost a bit more
    float multiplier = 1f + (float) aggCalls.size() * 0.125f;
    for (AggregateCall aggCall : aggCalls) {
        if (aggCall.getAggregation().getName().equals("SUM")) {
            // Pretend that SUM costs a little bit more than $SUM0,
            // to make things deterministic.
            multiplier += 0.0125f;
        }
    }
    return BeamCostModel.FACTORY.makeCost(inputStat.getRowCount() * multiplier, inputStat.getRate() * multiplier);
}
Also used : AggregateCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall) NodeStats(org.apache.beam.sdk.extensions.sql.impl.planner.NodeStats)

Example 18 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project calcite by apache.

the class Prepare method optimize.

/**
 * Optimizes a query plan.
 *
 * @param root Root of relational expression tree
 * @param materializations Tables known to be populated with a given query
 * @param lattices Lattices
 * @return an equivalent optimized relational expression
 */
protected RelRoot optimize(RelRoot root, final List<Materialization> materializations, final List<CalciteSchema.LatticeEntry> lattices) {
    final RelOptPlanner planner = root.rel.getCluster().getPlanner();
    final DataContext dataContext = context.getDataContext();
    planner.setExecutor(new RexExecutorImpl(dataContext));
    final List<RelOptMaterialization> materializationList = new ArrayList<>();
    for (Materialization materialization : materializations) {
        List<String> qualifiedTableName = materialization.materializedTable.path();
        materializationList.add(new RelOptMaterialization(materialization.tableRel, materialization.queryRel, materialization.starRelOptTable, qualifiedTableName));
    }
    final List<RelOptLattice> latticeList = new ArrayList<>();
    for (CalciteSchema.LatticeEntry lattice : lattices) {
        final CalciteSchema.TableEntry starTable = lattice.getStarTable();
        final JavaTypeFactory typeFactory = context.getTypeFactory();
        final RelOptTableImpl starRelOptTable = RelOptTableImpl.create(catalogReader, starTable.getTable().getRowType(typeFactory), starTable, null);
        latticeList.add(new RelOptLattice(lattice.getLattice(), starRelOptTable));
    }
    final RelTraitSet desiredTraits = getDesiredRootTraitSet(root);
    // Work around
    // [CALCITE-1774] Allow rules to be registered during planning process
    // by briefly creating each kind of physical table to let it register its
    // rules. The problem occurs when plans are created via RelBuilder, not
    // the usual process (SQL and SqlToRelConverter.Config.isConvertTableAccess
    // = true).
    final RelVisitor visitor = new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof TableScan) {
                final RelOptCluster cluster = node.getCluster();
                final RelOptTable.ToRelContext context = RelOptUtil.getContext(cluster);
                final RelNode r = node.getTable().toRel(context);
                planner.registerClass(r);
            }
            super.visit(node, ordinal, parent);
        }
    };
    visitor.go(root.rel);
    final Program program = getProgram();
    final RelNode rootRel4 = program.run(planner, root.rel, desiredTraits, materializationList, latticeList);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Plan after physical tweaks: {}", RelOptUtil.toString(rootRel4, SqlExplainLevel.ALL_ATTRIBUTES));
    }
    return root.withRel(rootRel4);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) TableScan(org.apache.calcite.rel.core.TableScan) Program(org.apache.calcite.tools.Program) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) LatticeEntry(org.apache.calcite.jdbc.CalciteSchema.LatticeEntry) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) DataContext(org.apache.calcite.DataContext) RelNode(org.apache.calcite.rel.RelNode) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelOptLattice(org.apache.calcite.plan.RelOptLattice) RelVisitor(org.apache.calcite.rel.RelVisitor) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 19 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project calcite by apache.

the class RelMetadataTest method testMetadataHandlerCacheLimit.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1808">[CALCITE-1808]
 * JaninoRelMetadataProvider loading cache might cause
 * OutOfMemoryError</a>.
 */
@Test
public void testMetadataHandlerCacheLimit() {
    Assume.assumeTrue("If cache size is too large, this test may fail and the " + "test won't be to blame", SaffronProperties.INSTANCE.metadataHandlerCacheMaximumSize().get() < 10_000);
    final int iterationCount = 2_000;
    final RelNode rel = convertSql("select * from emp");
    final RelMetadataProvider metadataProvider = rel.getCluster().getMetadataProvider();
    final RelOptPlanner planner = rel.getCluster().getPlanner();
    for (int i = 0; i < iterationCount; i++) {
        RelMetadataQuery.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.of(new CachingRelMetadataProvider(metadataProvider, planner)));
        final RelMetadataQuery mq = RelMetadataQuery.instance();
        final Double result = mq.getRowCount(rel);
        assertThat(result, within(14d, 0.1d));
    }
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) ReflectiveRelMetadataProvider(org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Test(org.junit.Test)

Example 20 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project calcite by apache.

the class RelMetadataTest method testCustomProvider.

@Test
public void testCustomProvider() {
    final List<String> buf = Lists.newArrayList();
    ColTypeImpl.THREAD_LIST.set(buf);
    final String sql = "select deptno, count(*) from emp where deptno > 10 " + "group by deptno having count(*) = 0";
    final RelRoot root = tester.withClusterFactory(new Function<RelOptCluster, RelOptCluster>() {

        public RelOptCluster apply(RelOptCluster cluster) {
            // Create a custom provider that includes ColType.
            // Include the same provider twice just to be devious.
            final ImmutableList<RelMetadataProvider> list = ImmutableList.of(ColTypeImpl.SOURCE, ColTypeImpl.SOURCE, cluster.getMetadataProvider());
            cluster.setMetadataProvider(ChainedRelMetadataProvider.of(list));
            return cluster;
        }
    }).convertSqlToRel(sql);
    final RelNode rel = root.rel;
    // Top node is a filter. Its metadata uses getColType(RelNode, int).
    assertThat(rel, instanceOf(LogicalFilter.class));
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
    assertThat(colType(mq, rel, 1), equalTo("EXPR$1-rel"));
    // Next node is an aggregate. Its metadata uses
    // getColType(LogicalAggregate, int).
    final RelNode input = rel.getInput(0);
    assertThat(input, instanceOf(LogicalAggregate.class));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    // There is no caching. Another request causes another call to the provider.
    assertThat(buf.toString(), equalTo("[DEPTNO-rel, EXPR$1-rel, DEPTNO-agg]"));
    assertThat(buf.size(), equalTo(3));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(4));
    // Now add a cache. Only the first request for each piece of metadata
    // generates a new call to the provider.
    final RelOptPlanner planner = rel.getCluster().getPlanner();
    rel.getCluster().setMetadataProvider(new CachingRelMetadataProvider(rel.getCluster().getMetadataProvider(), planner));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(5));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(5));
    assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
    assertThat(buf.size(), equalTo(6));
    assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
    assertThat(buf.size(), equalTo(6));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(6));
    // With a different timestamp, a metadata item is re-computed on first call.
    long timestamp = planner.getRelMetadataTimestamp(rel);
    assertThat(timestamp, equalTo(0L));
    ((MockRelOptPlanner) planner).setRelMetadataTimestamp(timestamp + 1);
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(7));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(7));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelRoot(org.apache.calcite.rel.RelRoot) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Function(com.google.common.base.Function) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) RelNode(org.apache.calcite.rel.RelNode) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) ReflectiveRelMetadataProvider(org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) Test(org.junit.Test)

Aggregations

RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)26 RelNode (org.apache.calcite.rel.RelNode)16 RelOptCluster (org.apache.calcite.plan.RelOptCluster)9 RelTraitSet (org.apache.calcite.plan.RelTraitSet)9 RexBuilder (org.apache.calcite.rex.RexBuilder)7 RelOptMaterialization (org.apache.calcite.plan.RelOptMaterialization)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 RelOptRule (org.apache.calcite.plan.RelOptRule)4 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)4 VolcanoPlanner (org.apache.calcite.plan.volcano.VolcanoPlanner)4 JaninoRelMetadataProvider (org.apache.calcite.rel.metadata.JaninoRelMetadataProvider)4 Properties (java.util.Properties)3 DataContext (org.apache.calcite.DataContext)3 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 HepProgram (org.apache.calcite.plan.hep.HepProgram)3 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)3