Search in sources :

Example 6 with PlanningContext

use of org.apache.ignite.internal.sql.engine.prepare.PlanningContext in project ignite-3 by apache.

the class JoinCommutePlannerTest method testInnerCommute.

@Test
public void testInnerCommute() throws Exception {
    String sql = "SELECT COUNT(*) FROM SMALL s JOIN HUGE h on h.id = s.id";
    IgniteRel phys = physicalPlan(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin");
    assertNotNull(phys);
    IgniteNestedLoopJoin join = findFirstNode(phys, byClass(IgniteNestedLoopJoin.class));
    assertNotNull(join);
    IgniteProject proj = findFirstNode(phys, byClass(IgniteProject.class));
    assertNotNull(proj);
    IgniteTableScan rightScan = findFirstNode(join.getRight(), byClass(IgniteTableScan.class));
    assertNotNull(rightScan);
    IgniteTableScan leftScan = findFirstNode(join.getLeft(), byClass(IgniteTableScan.class));
    assertNotNull(leftScan);
    List<String> rightSchemaWithName = rightScan.getTable().getQualifiedName();
    assertEquals(2, rightSchemaWithName.size());
    assertEquals(rightSchemaWithName.get(1), "SMALL");
    List<String> leftSchemaWithName = leftScan.getTable().getQualifiedName();
    assertEquals(leftSchemaWithName.get(1), "HUGE");
    assertEquals(JoinRelType.INNER, join.getJoinType());
    PlanningContext ctx = plannerCtx(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin");
    RelOptPlanner pl = ctx.cluster().getPlanner();
    RelOptCost costWithCommute = pl.getCost(phys, phys.getCluster().getMetadataQuery());
    assertNotNull(costWithCommute);
    System.out.println("plan: " + RelOptUtil.toString(phys));
    assertNotNull(phys);
    phys = physicalPlan(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin", "JoinCommuteRule");
    join = findFirstNode(phys, byClass(IgniteNestedLoopJoin.class));
    proj = findFirstNode(phys, byClass(IgniteProject.class));
    rightScan = findFirstNode(join.getRight(), byClass(IgniteTableScan.class));
    leftScan = findFirstNode(join.getLeft(), byClass(IgniteTableScan.class));
    assertNotNull(join);
    assertNull(proj);
    assertNotNull(rightScan);
    assertNotNull(leftScan);
    rightSchemaWithName = rightScan.getTable().getQualifiedName();
    assertEquals(2, rightSchemaWithName.size());
    // no commute
    assertEquals(rightSchemaWithName.get(1), "HUGE");
    leftSchemaWithName = leftScan.getTable().getQualifiedName();
    assertEquals(leftSchemaWithName.get(1), "SMALL");
    // no commute
    assertEquals(JoinRelType.INNER, join.getJoinType());
    ctx = plannerCtx(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin", "JoinCommuteRule");
    pl = ctx.cluster().getPlanner();
    RelOptCost costWithoutCommute = pl.getCost(phys, phys.getCluster().getMetadataQuery());
    System.out.println("plan: " + RelOptUtil.toString(phys));
    assertTrue(costWithCommute.isLt(costWithoutCommute));
}
Also used : IgniteProject(org.apache.ignite.internal.sql.engine.rel.IgniteProject) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) RelOptCost(org.apache.calcite.plan.RelOptCost) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IgniteNestedLoopJoin(org.apache.ignite.internal.sql.engine.rel.IgniteNestedLoopJoin) IgniteTableScan(org.apache.ignite.internal.sql.engine.rel.IgniteTableScan) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Test(org.junit.jupiter.api.Test)

Example 7 with PlanningContext

use of org.apache.ignite.internal.sql.engine.prepare.PlanningContext in project ignite-3 by apache.

the class JoinCommutePlannerTest method testOuterCommute.

@Test
public void testOuterCommute() throws Exception {
    String sql = "SELECT COUNT(*) FROM SMALL s RIGHT JOIN HUGE h on h.id = s.id";
    IgniteRel phys = physicalPlan(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin");
    assertNotNull(phys);
    IgniteNestedLoopJoin join = findFirstNode(phys, byClass(IgniteNestedLoopJoin.class));
    IgniteProject proj = findFirstNode(phys, byClass(IgniteProject.class));
    assertNotNull(proj);
    assertEquals(JoinRelType.LEFT, join.getJoinType());
    PlanningContext ctx = plannerCtx(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin");
    RelOptPlanner pl = ctx.cluster().getPlanner();
    RelOptCost costWithCommute = pl.getCost(phys, phys.getCluster().getMetadataQuery());
    assertNotNull(costWithCommute);
    System.out.println("plan: " + RelOptUtil.toString(phys));
    assertNotNull(phys);
    phys = physicalPlan(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin", "JoinCommuteRule");
    join = findFirstNode(phys, byClass(IgniteNestedLoopJoin.class));
    proj = findFirstNode(phys, byClass(IgniteProject.class));
    assertNull(proj);
    // no commute
    assertEquals(JoinRelType.RIGHT, join.getJoinType());
    ctx = plannerCtx(sql, publicSchema, "MergeJoinConverter", "CorrelatedNestedLoopJoin", "JoinCommuteRule");
    pl = ctx.cluster().getPlanner();
    RelOptCost costWithoutCommute = pl.getCost(phys, phys.getCluster().getMetadataQuery());
    System.out.println("plan: " + RelOptUtil.toString(phys));
    assertTrue(costWithCommute.isLt(costWithoutCommute));
}
Also used : IgniteProject(org.apache.ignite.internal.sql.engine.rel.IgniteProject) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) RelOptCost(org.apache.calcite.plan.RelOptCost) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IgniteNestedLoopJoin(org.apache.ignite.internal.sql.engine.rel.IgniteNestedLoopJoin) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Test(org.junit.jupiter.api.Test)

Example 8 with PlanningContext

use of org.apache.ignite.internal.sql.engine.prepare.PlanningContext in project ignite-3 by apache.

the class AbstractPlannerTest method plannerCtx.

/**
 * Create planner context for specified query.
 */
protected PlanningContext plannerCtx(String sql, IgniteSchema publicSchema, String... disabledRules) {
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).build()).logger(log).build()).query(sql).build();
    IgnitePlanner planner = ctx.planner();
    assertNotNull(planner);
    planner.setDisabledRules(new HashSet<>(Arrays.asList(disabledRules)));
    return ctx;
}
Also used : PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) IgnitePlanner(org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 9 with PlanningContext

use of org.apache.ignite.internal.sql.engine.prepare.PlanningContext in project ignite-3 by apache.

the class PlannerTest method testSplitterColocatedReplicatedReplicated.

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forNodes(select(NODES, 0, 1, 2, 3));
        }

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forNodes(select(NODES, 0, 1, 2, 3));
        }

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("DEVELOPER", developer);
    publicSchema.addTable("PROJECT", project);
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    String sql = "SELECT d.id, (d.id + 1) as id2, d.name, d.projectId, p.id0, p.ver0 " + "FROM PUBLIC.Developer d JOIN (" + "SELECT pp.id as id0, pp.ver as ver0 FROM PUBLIC.Project pp" + ") p " + "ON d.id = p.id0 " + "WHERE (d.projectId + 1) > ?";
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).build()).build()).query(sql).parameters(2).build();
    IgniteRel phys = physicalPlan(sql, ctx);
    assertNotNull(phys);
    MultiStepPlan plan = new MultiStepQueryPlan(new QueryTemplate(new Splitter().go(phys)), null);
    assertNotNull(plan);
    plan.init(this::intermediateMapping, mapContext(CollectionUtils.first(NODES), 0L));
    assertNotNull(plan);
    assertEquals(1, plan.fragments().size());
}
Also used : QueryTemplate(org.apache.ignite.internal.sql.engine.prepare.QueryTemplate) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) Splitter(org.apache.ignite.internal.sql.engine.prepare.Splitter) MultiStepPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MultiStepQueryPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepQueryPlan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Example 10 with PlanningContext

use of org.apache.ignite.internal.sql.engine.prepare.PlanningContext in project ignite-3 by apache.

the class PlannerTest method testSplitterPartiallyColocatedReplicatedAndPartitioned.

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forNodes(select(NODES, 0));
        }

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forAssignments(Arrays.asList(select(NODES, 1, 2), select(NODES, 2, 3), select(NODES, 3, 0), select(NODES, 0, 1)));
        }

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.affinity(0, "Project", "hash");
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("DEVELOPER", developer);
    publicSchema.addTable("PROJECT", project);
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    String sql = "SELECT d.id, d.name, d.projectId, p.id0, p.ver0 " + "FROM PUBLIC.Developer d JOIN (" + "SELECT pp.id as id0, pp.ver as ver0 FROM PUBLIC.Project pp" + ") p " + "ON d.id = p.id0 " + "WHERE (d.projectId + 1) > ?";
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).build()).build()).query(sql).parameters(2).build();
    IgniteRel phys = physicalPlan(sql, ctx);
    assertNotNull(phys);
    MultiStepPlan plan = new MultiStepQueryPlan(new QueryTemplate(new Splitter().go(phys)), null);
    assertNotNull(plan);
    plan.init(this::intermediateMapping, mapContext(CollectionUtils.first(NODES), 0L));
    assertEquals(3, plan.fragments().size());
}
Also used : QueryTemplate(org.apache.ignite.internal.sql.engine.prepare.QueryTemplate) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) Splitter(org.apache.ignite.internal.sql.engine.prepare.Splitter) MultiStepPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MultiStepQueryPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepQueryPlan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Aggregations

PlanningContext (org.apache.ignite.internal.sql.engine.prepare.PlanningContext)10 IgniteRel (org.apache.ignite.internal.sql.engine.rel.IgniteRel)9 Test (org.junit.jupiter.api.Test)9 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)7 IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)7 IgniteTypeFactory (org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory)7 MappingQueryContext (org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext)6 MultiStepPlan (org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan)6 MultiStepQueryPlan (org.apache.ignite.internal.sql.engine.prepare.MultiStepQueryPlan)6 QueryTemplate (org.apache.ignite.internal.sql.engine.prepare.QueryTemplate)6 Splitter (org.apache.ignite.internal.sql.engine.prepare.Splitter)6 RelOptCost (org.apache.calcite.plan.RelOptCost)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)2 IgnitePlanner (org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner)2 IgniteNestedLoopJoin (org.apache.ignite.internal.sql.engine.rel.IgniteNestedLoopJoin)2 IgniteProject (org.apache.ignite.internal.sql.engine.rel.IgniteProject)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 RelNode (org.apache.calcite.rel.RelNode)1