Search in sources :

Example 1 with IgniteCorrelatedNestedLoopJoin

use of org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin in project ignite by apache.

the class CorrelateToNestedLoopRule method convert.

/**
 * {@inheritDoc}
 */
@Override
protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalCorrelate rel) {
    final RelOptCluster cluster = rel.getCluster();
    final Set<CorrelationId> correlationIds = Collections.singleton(rel.getCorrelationId());
    CorrelationTrait corrTrait = CorrelationTrait.correlations(correlationIds);
    RelTraitSet outTraits = cluster.traitSetOf(IgniteConvention.INSTANCE);
    RelTraitSet leftInTraits = cluster.traitSetOf(IgniteConvention.INSTANCE);
    RelTraitSet rightInTraits = cluster.traitSetOf(IgniteConvention.INSTANCE).replace(RewindabilityTrait.REWINDABLE).replace(corrTrait);
    RelNode left = convert(rel.getLeft(), leftInTraits);
    RelNode right = convert(rel.getRight(), rightInTraits);
    return new IgniteCorrelatedNestedLoopJoin(cluster, outTraits, left, right, cluster.getRexBuilder().makeLiteral(true), correlationIds, rel.getJoinType());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) CorrelationTrait(org.apache.ignite.internal.processors.query.calcite.trait.CorrelationTrait) RelNode(org.apache.calcite.rel.RelNode) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RelTraitSet(org.apache.calcite.plan.RelTraitSet) IgniteCorrelatedNestedLoopJoin(org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin)

Example 2 with IgniteCorrelatedNestedLoopJoin

use of org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin in project ignite by apache.

the class CorrelatedSubqueryPlannerTest method test.

/**
 * Test verifies the row type is consistent for correlation variable and
 * node that actually puts this variable into a context.
 *
 * In this particular test the row type of the left input of CNLJ node should
 * match the row type correlated variable in the filter was created with.
 *
 * @throws Exception In case of any unexpected error.
 */
@Test
public void test() throws Exception {
    IgniteSchema schema = createSchema(createTable("T1", IgniteDistributions.single(), "A", Integer.class, "B", Integer.class, "C", Integer.class, "D", Integer.class, "E", Integer.class));
    String sql = "" + "SELECT (SELECT count(*) FROM t1 AS x WHERE x.b<t1.b)\n" + "  FROM t1\n" + " WHERE (a>e-2 AND a<e+2)\n" + "    OR c>d\n" + " ORDER BY 1;";
    IgniteRel rel = physicalPlan(sql, schema, "FilterTableScanMergeRule");
    IgniteFilter filter = findFirstNode(rel, byClass(IgniteFilter.class).and(f -> RexUtils.hasCorrelation(((Filter) f).getCondition())));
    RexFieldAccess fieldAccess = findFirst(filter.getCondition(), RexFieldAccess.class);
    assertNotNull(fieldAccess);
    assertEquals(RexCorrelVariable.class, fieldAccess.getReferenceExpr().getClass());
    IgniteCorrelatedNestedLoopJoin join = findFirstNode(rel, byClass(IgniteCorrelatedNestedLoopJoin.class));
    assertEquals(fieldAccess.getReferenceExpr().getType(), join.getLeft().getRowType());
}
Also used : Project(org.apache.calcite.rel.core.Project) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) IgnitePlanner(org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) PlannerPhase(org.apache.ignite.internal.processors.query.calcite.prepare.PlannerPhase) PlanningContext(org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext) Test(org.junit.Test) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) IgniteSchema(org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema) IgniteCorrelatedNestedLoopJoin(org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin) IgniteFilter(org.apache.ignite.internal.processors.query.calcite.rel.IgniteFilter) List(java.util.List) SqlNode(org.apache.calcite.sql.SqlNode) IgniteRel(org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel) RexNode(org.apache.calcite.rex.RexNode) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) Collections(java.util.Collections) RexUtils(org.apache.ignite.internal.processors.query.calcite.util.RexUtils) IgniteDistributions(org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions) RexCall(org.apache.calcite.rex.RexCall) IgniteFilter(org.apache.ignite.internal.processors.query.calcite.rel.IgniteFilter) IgniteRel(org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel) IgniteSchema(org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema) IgniteCorrelatedNestedLoopJoin(org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) Test(org.junit.Test)

Example 3 with IgniteCorrelatedNestedLoopJoin

use of org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin in project ignite by apache.

the class CorrelatedNestedLoopJoinRule method convert.

/**
 * {@inheritDoc}
 */
@Override
protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalJoin rel) {
    final int leftFieldCount = rel.getLeft().getRowType().getFieldCount();
    final RelOptCluster cluster = rel.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RelBuilder relBuilder = relBuilderFactory.create(rel.getCluster(), null);
    final Set<CorrelationId> correlationIds = new HashSet<>();
    final ArrayList<RexNode> corrVar = new ArrayList<>();
    if (corrVar.isEmpty()) {
        for (int i = 0; i < batchSize; i++) {
            CorrelationId correlationId = cluster.createCorrel();
            correlationIds.add(correlationId);
            corrVar.add(rexBuilder.makeCorrel(rel.getLeft().getRowType(), correlationId));
        }
    }
    // Generate first condition
    final RexNode condition = rel.getCondition().accept(new RexShuttle() {

        @Override
        public RexNode visitInputRef(RexInputRef input) {
            int field = input.getIndex();
            if (field >= leftFieldCount)
                return rexBuilder.makeInputRef(input.getType(), input.getIndex() - leftFieldCount);
            return rexBuilder.makeFieldAccess(corrVar.get(0), field);
        }
    });
    List<RexNode> conditionList = new ArrayList<>();
    conditionList.add(condition);
    // Add batchSize-1 other conditions
    for (int i = 1; i < batchSize; i++) {
        final int corrIndex = i;
        final RexNode condition2 = condition.accept(new RexShuttle() {

            @Override
            public RexNode visitCorrelVariable(RexCorrelVariable variable) {
                return corrVar.get(corrIndex);
            }
        });
        conditionList.add(condition2);
    }
    RelTraitSet filterInTraits = rel.getRight().getTraitSet();
    // Push a filter with batchSize disjunctions
    relBuilder.push(rel.getRight().copy(filterInTraits, rel.getRight().getInputs())).filter(relBuilder.or(conditionList));
    RelNode right = relBuilder.build();
    JoinRelType joinType = rel.getJoinType();
    RelTraitSet outTraits = cluster.traitSetOf(IgniteConvention.INSTANCE);
    RelTraitSet leftInTraits = cluster.traitSetOf(IgniteConvention.INSTANCE);
    CorrelationTrait corrTrait = CorrelationTrait.correlations(correlationIds);
    RelTraitSet rightInTraits = cluster.traitSetOf(IgniteConvention.INSTANCE).replace(RewindabilityTrait.REWINDABLE).replace(corrTrait);
    RelNode left = convert(rel.getLeft(), leftInTraits);
    right = convert(right, rightInTraits);
    return new IgniteCorrelatedNestedLoopJoin(cluster, outTraits, left, right, rel.getCondition(), correlationIds, joinType);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) CorrelationTrait(org.apache.ignite.internal.processors.query.calcite.trait.CorrelationTrait) RelBuilder(org.apache.calcite.tools.RelBuilder) RexShuttle(org.apache.calcite.rex.RexShuttle) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) IgniteCorrelatedNestedLoopJoin(org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin) JoinRelType(org.apache.calcite.rel.core.JoinRelType) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) CorrelationId(org.apache.calcite.rel.core.CorrelationId) HashSet(java.util.HashSet) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)3 IgniteCorrelatedNestedLoopJoin (org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 CorrelationId (org.apache.calcite.rel.core.CorrelationId)2 RexCorrelVariable (org.apache.calcite.rex.RexCorrelVariable)2 RexNode (org.apache.calcite.rex.RexNode)2 CorrelationTrait (org.apache.ignite.internal.processors.query.calcite.trait.CorrelationTrait)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Filter (org.apache.calcite.rel.core.Filter)1 JoinRelType (org.apache.calcite.rel.core.JoinRelType)1 Project (org.apache.calcite.rel.core.Project)1 LogicalCorrelate (org.apache.calcite.rel.logical.LogicalCorrelate)1 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)1 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexCall (org.apache.calcite.rex.RexCall)1