use of org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext in project ignite by apache.
the class PlannerTest method testSplitterNonColocated.
/**
* @throws Exception If failed.
*/
@Test
public void testSplitterNonColocated() 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, 2));
}
@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));
}
@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 p.id0, d.id " + "FROM PUBLIC.Developer d JOIN (" + "SELECT pp.id as id0, pp.ver as ver0 FROM PUBLIC.Project pp" + ") p " + "ON d.projectId = p.ver0 " + "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(ctx);
assertNotNull(phys);
MultiStepPlan plan = new MultiStepQueryPlan(new QueryTemplate(new Splitter().go(phys)), null);
assertNotNull(plan);
plan.init(this::intermediateMapping, Commons.mapContext(F.first(nodes), AffinityTopologyVersion.NONE));
assertNotNull(plan);
assertEquals(2, plan.fragments().size());
}
use of org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext in project ignite by apache.
the class PlannerTest method testPhysicalPlan.
/**
* @throws Exception If failed.
*/
@Test
public void testPhysicalPlan() throws Exception {
executors = new ArrayList<>();
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 <Row> Iterable<Row> scan(ExecutionContext<Row> execCtx, ColocationGroup group, Predicate<Row> filter, Function<Row, Row> transformer, ImmutableBitSet requiredColumns) {
return Arrays.asList(row(execCtx, requiredColumns, 0, "Igor", 0), row(execCtx, requiredColumns, 1, "Roman", 0));
}
@Override
public ColocationGroup colocationGroup(MappingQueryContext ctx) {
return ColocationGroup.forNodes(select(nodes, 1));
}
@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 <Row> Iterable<Row> scan(ExecutionContext<Row> execCtx, ColocationGroup group, Predicate<Row> filter, Function<Row, Row> transformer, ImmutableBitSet requiredColumns) {
return Arrays.asList(row(execCtx, requiredColumns, 0, "Calcite", 1), row(execCtx, requiredColumns, 1, "Ignite", 1));
}
@Override
public ColocationGroup colocationGroup(MappingQueryContext ctx) {
return ColocationGroup.forNodes(select(nodes, 1));
}
@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.name, d.projectId, p.name0, p.ver0 " + "FROM PUBLIC.Developer d JOIN (" + "SELECT pp.id as id0, pp.name as name0, pp.ver as ver0 FROM PUBLIC.Project pp" + ") p " + "ON d.projectId = p.id0 " + "WHERE (d.projectId + 1) > ?";
BaseQueryContext qctx = BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).build()).build();
PlanningContext ctx = PlanningContext.builder().parentContext(qctx).query(sql).parameters(-10).build();
IgniteRel phys = physicalPlan(ctx);
assertNotNull(phys);
MultiStepPlan plan = new MultiStepQueryPlan(new QueryTemplate(new Splitter().go(phys)), null);
assertNotNull(plan);
plan.init(this::intermediateMapping, Commons.mapContext(F.first(nodes), AffinityTopologyVersion.NONE));
List<Fragment> fragments = plan.fragments();
assertEquals(2, fragments.size());
UUID qryId = UUID.randomUUID();
TestIoManager mgr = new TestIoManager();
GridTestKernalContext kernal;
QueryTaskExecutorImpl taskExecutor;
MessageServiceImpl msgSvc;
MailboxRegistryImpl mailboxRegistry;
ExchangeServiceImpl exchangeSvc;
ExecutionContext<Object[]> ectx;
Node<Object[]> exec;
// // Local part
Fragment fragment = fragments.get(0);
assert fragment.rootFragment();
kernal = newContext();
taskExecutor = new QueryTaskExecutorImpl(kernal);
taskExecutor.stripedThreadPoolExecutor(new IgniteStripedThreadPoolExecutor(kernal.config().getQueryThreadPoolSize(), kernal.igniteInstanceName(), "calciteQry", (t, ex) -> {
log().error(ex.getMessage(), ex);
lastE = ex;
}, true, DFLT_THREAD_KEEP_ALIVE_TIME));
executors.add(taskExecutor);
msgSvc = new TestMessageServiceImpl(kernal, mgr);
msgSvc.localNodeId(nodes.get(0));
msgSvc.taskExecutor(taskExecutor);
mgr.register(msgSvc);
mailboxRegistry = new MailboxRegistryImpl(kernal);
exchangeSvc = new ExchangeServiceImpl(kernal);
exchangeSvc.taskExecutor(taskExecutor);
exchangeSvc.messageService(msgSvc);
exchangeSvc.mailboxRegistry(mailboxRegistry);
exchangeSvc.queryRegistry(new QueryRegistryImpl(kernal));
exchangeSvc.init();
ectx = new ExecutionContext<>(qctx, taskExecutor, qryId, F.first(nodes), F.first(nodes), AffinityTopologyVersion.NONE, new FragmentDescription(fragment.fragmentId(), fragment.mapping(), plan.target(fragment), plan.remotes(fragment)), ArrayRowHandler.INSTANCE, Commons.parametersMap(ctx.parameters()));
exec = new LogicalRelImplementor<>(ectx, c1 -> r1 -> 0, mailboxRegistry, exchangeSvc, new TestFailureProcessor(kernal)).go(fragment.root());
RootNode<Object[]> consumer = new RootNode<>(ectx, exec.rowType());
consumer.register(exec);
// // Remote part
fragment = fragments.get(1);
assert !fragment.rootFragment();
kernal = newContext();
taskExecutor = new QueryTaskExecutorImpl(kernal);
taskExecutor.stripedThreadPoolExecutor(new IgniteStripedThreadPoolExecutor(kernal.config().getQueryThreadPoolSize(), kernal.igniteInstanceName(), "calciteQry", (t, ex) -> {
log().error(ex.getMessage(), ex);
lastE = ex;
}, true, DFLT_THREAD_KEEP_ALIVE_TIME));
executors.add(taskExecutor);
msgSvc = new TestMessageServiceImpl(kernal, mgr);
msgSvc.localNodeId(nodes.get(1));
msgSvc.taskExecutor(taskExecutor);
mgr.register(msgSvc);
mailboxRegistry = new MailboxRegistryImpl(kernal);
exchangeSvc = new ExchangeServiceImpl(kernal);
exchangeSvc.taskExecutor(taskExecutor);
exchangeSvc.messageService(msgSvc);
exchangeSvc.mailboxRegistry(mailboxRegistry);
exchangeSvc.queryRegistry(new QueryRegistryImpl(kernal));
exchangeSvc.init();
ectx = new ExecutionContext<>(qctx, taskExecutor, qryId, nodes.get(1), F.first(nodes), AffinityTopologyVersion.NONE, new FragmentDescription(fragment.fragmentId(), fragment.mapping(), plan.target(fragment), plan.remotes(fragment)), ArrayRowHandler.INSTANCE, Commons.parametersMap(ctx.parameters()));
exec = new LogicalRelImplementor<>(ectx, c -> r -> 0, mailboxRegistry, exchangeSvc, new TestFailureProcessor(kernal)).go(fragment.root());
assert exec instanceof Outbox;
Outbox<Object[]> outbox = (Outbox<Object[]>) exec;
exec.context().execute(outbox::init, outbox::onError);
ArrayList<Object[]> res = new ArrayList<>();
while (consumer.hasNext()) res.add(consumer.next());
assertFalse(res.isEmpty());
Assert.assertArrayEquals(new Object[] { 0, "Igor", 0, "Calcite", 1 }, res.get(0));
Assert.assertArrayEquals(new Object[] { 1, "Roman", 0, "Calcite", 1 }, res.get(1));
}
use of org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext in project ignite by apache.
the class PlannerTimeoutTest method testLongPlanningTimeout.
/**
*/
@Test
public void testLongPlanningTimeout() throws Exception {
IgniteSchema schema = createSchema(createTestTable("T1", "A", Integer.class, "B", Integer.class), createTestTable("T2", "A", Integer.class, "B", Integer.class));
String sql = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
PlanningContext ctx = PlanningContext.builder().parentContext(baseQueryContext(Collections.singletonList(schema))).plannerTimeout(PLANNER_TIMEOUT).query(sql).build();
AtomicReference<IgniteRel> plan = new AtomicReference<>();
AtomicReference<RelOptPlanner.CannotPlanException> plannerError = new AtomicReference<>();
GridTestUtils.assertTimeout(3 * PLANNER_TIMEOUT, TimeUnit.MILLISECONDS, () -> {
try (IgnitePlanner planner = ctx.planner()) {
plan.set(physicalPlan(planner, ctx.query()));
VolcanoPlanner volcanoPlanner = (VolcanoPlanner) ctx.cluster().getPlanner();
assertNotNull(volcanoPlanner);
GridTestUtils.assertThrowsWithCause(volcanoPlanner::checkCancel, VolcanoTimeoutException.class);
} catch (RelOptPlanner.CannotPlanException e) {
plannerError.set(e);
} catch (Exception e) {
throw new RuntimeException("Planning failed", e);
}
});
assertTrue(plan.get() != null || plannerError.get() != null);
if (plan.get() != null) {
new RelVisitor() {
@Override
public void visit(RelNode node, int ordinal, RelNode parent) {
assertNotNull(node.getTraitSet().getTrait(IgniteConvention.INSTANCE.getTraitDef()));
super.visit(node, ordinal, parent);
}
}.go(plan.get());
}
}
use of org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext in project ignite by apache.
the class CorrelatedSubqueryPlannerTest method testCorrelatesCollisionsLeftHand.
/**
* Test verifies resolving of collisions in the left hand of correlates.
*/
@Test
public void testCorrelatesCollisionsLeftHand() throws Exception {
IgniteSchema schema = createSchema(createTable("T1", IgniteDistributions.single(), "A", Integer.class, "B", Integer.class, "C", Integer.class, "D", Integer.class));
String sql = "SELECT * FROM t1 as cor WHERE " + "EXISTS (SELECT 1 FROM t1 WHERE t1.b = cor.a) AND " + "EXISTS (SELECT 1 FROM t1 WHERE t1.c = cor.a) AND " + "EXISTS (SELECT 1 FROM t1 WHERE t1.d = cor.a)";
PlanningContext ctx = plannerCtx(sql, schema);
try (IgnitePlanner planner = ctx.planner()) {
RelNode rel = convertSubQueries(planner, ctx);
List<LogicalCorrelate> correlates = findNodes(rel, byClass(LogicalCorrelate.class));
assertEquals(3, correlates.size());
// There are collisions by correlation id.
assertEquals(correlates.get(0).getCorrelationId(), correlates.get(1).getCorrelationId());
assertEquals(correlates.get(0).getCorrelationId(), correlates.get(2).getCorrelationId());
rel = planner.replaceCorrelatesCollisions(rel);
correlates = findNodes(rel, byClass(LogicalCorrelate.class));
assertEquals(3, correlates.size());
// There are no collisions by correlation id.
assertFalse(correlates.get(0).getCorrelationId().equals(correlates.get(1).getCorrelationId()));
assertFalse(correlates.get(0).getCorrelationId().equals(correlates.get(2).getCorrelationId()));
assertFalse(correlates.get(1).getCorrelationId().equals(correlates.get(2).getCorrelationId()));
List<LogicalFilter> filters = findNodes(rel, byClass(LogicalFilter.class).and(f -> RexUtils.hasCorrelation(((Filter) f).getCondition())));
assertEquals(3, filters.size());
// Filters match correlates in reverse order (we find outer correlate first, but inner filter first).
assertEquals(Collections.singleton(correlates.get(0).getCorrelationId()), RexUtils.extractCorrelationIds(filters.get(2).getCondition()));
assertEquals(Collections.singleton(correlates.get(1).getCorrelationId()), RexUtils.extractCorrelationIds(filters.get(1).getCondition()));
assertEquals(Collections.singleton(correlates.get(2).getCorrelationId()), RexUtils.extractCorrelationIds(filters.get(0).getCondition()));
}
}
use of org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext in project ignite by apache.
the class AbstractPlannerTest method plannerCtx.
/**
*/
protected PlanningContext plannerCtx(String sql, Collection<IgniteSchema> schemas, String... disabledRules) {
PlanningContext ctx = PlanningContext.builder().parentContext(baseQueryContext(schemas)).query(sql).build();
IgnitePlanner planner = ctx.planner();
assertNotNull(planner);
planner.setDisabledRules(ImmutableSet.copyOf(disabledRules));
return ctx;
}
Aggregations