Search in sources :

Example 16 with RelRoot

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

the class RelOptRulesTest method transitiveInference.

private void transitiveInference(RelOptRule... extraRules) throws Exception {
    final DiffRepository diffRepos = getDiffRepos();
    final String sql = diffRepos.expand(null, "${sql}");
    final HepProgram program = new HepProgramBuilder().addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).build();
    final HepPlanner planner = new HepPlanner(program);
    final RelRoot root = tester.convertSqlToRel(sql);
    final RelNode relInitial = root.rel;
    assertTrue(relInitial != null);
    List<RelMetadataProvider> list = Lists.newArrayList();
    list.add(DefaultRelMetadataProvider.INSTANCE);
    planner.registerMetadataProviders(list);
    RelMetadataProvider plannerChain = ChainedRelMetadataProvider.of(list);
    relInitial.getCluster().setMetadataProvider(new CachingRelMetadataProvider(plannerChain, planner));
    planner.setRoot(relInitial);
    RelNode relBefore = planner.findBestExp();
    String planBefore = NL + RelOptUtil.toString(relBefore);
    diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
    HepProgram program2 = new HepProgramBuilder().addMatchOrder(HepMatchOrder.BOTTOM_UP).addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).addRuleInstance(JoinPushTransitivePredicatesRule.INSTANCE).addRuleCollection(Arrays.asList(extraRules)).build();
    final HepPlanner planner2 = new HepPlanner(program2);
    planner.registerMetadataProviders(list);
    planner2.setRoot(relBefore);
    RelNode relAfter = planner2.findBestExp();
    String planAfter = NL + RelOptUtil.toString(relAfter);
    diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) RelNode(org.apache.calcite.rel.RelNode) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) RelRoot(org.apache.calcite.rel.RelRoot) HepPlanner(org.apache.calcite.plan.hep.HepPlanner)

Example 17 with RelRoot

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

the class RelMetadataTest method convertSql.

// ~ Methods ----------------------------------------------------------------
// ----------------------------------------------------------------------
// Tests for getPercentageOriginalRows
// ----------------------------------------------------------------------
private RelNode convertSql(String sql) {
    final RelRoot root = tester.convertSqlToRel(sql);
    root.rel.getCluster().setMetadataProvider(DefaultRelMetadataProvider.INSTANCE);
    return root.rel;
}
Also used : RelRoot(org.apache.calcite.rel.RelRoot)

Example 18 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project coral by linkedin.

the class HiveViewTable method toRel.

@Override
public RelNode toRel(RelOptTable.ToRelContext relContext, RelOptTable relOptTable) {
    try {
        RelRoot root = relContext.expandView(relOptTable.getRowType(), hiveTable.getViewExpandedText(), schemaPath, ImmutableList.of(hiveTable.getTableName()));
        root = root.withRel(createCastRel(root.rel, relOptTable.getRowType(), RelFactories.DEFAULT_PROJECT_FACTORY));
        // root = root.withRel(RelOptUtil.createCastRel(root.rel, relOptTable.getRowType()));
        return root.rel;
    } catch (Exception e) {
        Throwables.propagateIfInstanceOf(e, RuntimeException.class);
        throw new RuntimeException("Error while parsing view definition", e);
    }
}
Also used : RelRoot(org.apache.calcite.rel.RelRoot)

Example 19 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project ignite-3 by apache.

the class PlannerTest method testJoinPushExpressionRule.

@Test
public void testJoinPushExpressionRule() throws Exception {
    IgniteTypeFactory f = new IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
    TestTable emp = new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("NAME", f.createJavaType(String.class)).add("DEPTNO", f.createJavaType(Integer.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    };
    TestTable dept = new TestTable(new RelDataTypeFactory.Builder(f).add("DEPTNO", f.createJavaType(Integer.class)).add("NAME", f.createJavaType(String.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("EMP", emp);
    publicSchema.addTable("DEPT", dept);
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    String sql = "select d.deptno, e.deptno " + "from dept d, emp e " + "where d.deptno + e.deptno = 2";
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).costFactory(new IgniteCostFactory(1, 100, 1, 1)).build()).build()).query(sql).build();
    RelRoot relRoot;
    try (IgnitePlanner planner = ctx.planner()) {
        assertNotNull(planner);
        String qry = ctx.query();
        assertNotNull(qry);
        // Parse
        SqlNode sqlNode = planner.parse(qry);
        // Validate
        sqlNode = planner.validate(sqlNode);
        // Convert to Relational operators graph
        relRoot = planner.rel(sqlNode);
        RelNode rel = relRoot.rel;
        assertNotNull(rel);
        assertEquals("LogicalProject(DEPTNO=[$0], DEPTNO0=[$4])\n" + "  LogicalFilter(condition=[=(CAST(+($0, $4)):INTEGER, 2)])\n" + "    LogicalJoin(condition=[true], joinType=[inner])\n" + "      IgniteLogicalTableScan(table=[[PUBLIC, DEPT]])\n" + "      IgniteLogicalTableScan(table=[[PUBLIC, EMP]])\n", RelOptUtil.toString(rel));
        // Transformation chain
        RelTraitSet desired = rel.getCluster().traitSet().replace(IgniteConvention.INSTANCE).replace(IgniteDistributions.single()).replace(CorrelationTrait.UNCORRELATED).simplify();
        IgniteRel phys = planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
        assertNotNull(phys);
        assertEquals("IgniteProject(DEPTNO=[$3], DEPTNO0=[$2])\n" + "  IgniteCorrelatedNestedLoopJoin(condition=[=(CAST(+($3, $2)):INTEGER, 2)], joinType=[inner], " + "correlationVariables=[[$cor2]])\n" + "    IgniteTableScan(table=[[PUBLIC, EMP]])\n" + "    IgniteTableScan(table=[[PUBLIC, DEPT]], filters=[=(CAST(+($t0, $cor2.DEPTNO)):INTEGER, 2)])\n", RelOptUtil.toString(phys), "Invalid plan:\n" + RelOptUtil.toString(phys));
        checkSplitAndSerialization(phys, publicSchema);
    }
}
Also used : IgniteCostFactory(org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) Frameworks.newConfigBuilder(org.apache.calcite.tools.Frameworks.newConfigBuilder) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelRoot(org.apache.calcite.rel.RelRoot) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePlanner(org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner) RelNode(org.apache.calcite.rel.RelNode) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.jupiter.api.Test)

Example 20 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project ignite-3 by apache.

the class PlannerHelper method optimize.

/**
 * Optimize.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 *
 * @param sqlNode Sql node.
 * @param planner Planner.
 */
public static IgniteRel optimize(SqlNode sqlNode, IgnitePlanner planner) {
    try {
        // Convert to Relational operators graph
        RelRoot root = planner.rel(sqlNode);
        RelNode rel = root.rel;
        if (HintUtils.containsDisabledRules(root.hints)) {
            planner.setDisabledRules(HintUtils.disabledRules(root.hints));
        }
        // Transformation chain
        rel = planner.transform(PlannerPhase.HEP_DECORRELATE, rel.getTraitSet(), rel);
        rel = planner.trimUnusedFields(root.withRel(rel)).rel;
        rel = planner.transform(PlannerPhase.HEP_FILTER_PUSH_DOWN, rel.getTraitSet(), rel);
        rel = planner.transform(PlannerPhase.HEP_PROJECT_PUSH_DOWN, rel.getTraitSet(), rel);
        RelTraitSet desired = rel.getCluster().traitSet().replace(IgniteConvention.INSTANCE).replace(IgniteDistributions.single()).replace(root.collation == null ? RelCollations.EMPTY : root.collation).simplify();
        IgniteRel igniteRel = planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
        if (!root.isRefTrivial()) {
            final List<RexNode> projects = new ArrayList<>();
            final RexBuilder rexBuilder = igniteRel.getCluster().getRexBuilder();
            for (int field : Pair.left(root.fields)) {
                projects.add(rexBuilder.makeInputRef(igniteRel, field));
            }
            igniteRel = new IgniteProject(igniteRel.getCluster(), desired, igniteRel, projects, root.validatedRowType);
        }
        if (sqlNode.isA(Set.of(SqlKind.INSERT, SqlKind.UPDATE, SqlKind.MERGE))) {
            igniteRel = new FixDependentModifyNodeShuttle().visit(igniteRel);
        }
        return igniteRel;
    } catch (Throwable ex) {
        LOG.error("Unexpected error at query optimizer.", ex);
        if (LOG.isDebugEnabled()) {
            LOG.error(planner.dump());
        }
        throw ex;
    }
}
Also used : IgniteProject(org.apache.ignite.internal.sql.engine.rel.IgniteProject) RelNode(org.apache.calcite.rel.RelNode) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelRoot(org.apache.calcite.rel.RelRoot) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelRoot (org.apache.calcite.rel.RelRoot)77 SqlNode (org.apache.calcite.sql.SqlNode)30 RelNode (org.apache.calcite.rel.RelNode)22 Test (org.junit.jupiter.api.Test)18 NlsString (org.apache.calcite.util.NlsString)17 DingoTableScan (io.dingodb.calcite.rel.DingoTableScan)13 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)11 SqlToRelConverter (org.apache.calcite.sql2rel.SqlToRelConverter)11 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)9 SamzaSqlDslConverterFactory (org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory)8 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)7 RelDataType (org.apache.calcite.rel.type.RelDataType)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)7 DslConverter (org.apache.samza.sql.interfaces.DslConverter)7 Test (org.junit.Test)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 SqlParser (org.apache.calcite.sql.parser.SqlParser)6 Planner (org.apache.calcite.tools.Planner)6 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)5