Search in sources :

Example 1 with IgniteFilter

use of org.apache.ignite.internal.sql.engine.rel.IgniteFilter in project ignite-3 by apache.

the class PlannerTest method testMergeFilters.

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

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.single();
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("TEST", testTbl);
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    String sql = "SELECT val from (\n" + "   SELECT * \n" + "   FROM TEST \n" + "   WHERE VAL = 10) \n" + "WHERE VAL = 10";
    RelNode phys = physicalPlan(sql, publicSchema);
    assertNotNull(phys);
    AtomicInteger filterCnt = new AtomicInteger();
    // Counts filters af the plan.
    phys.childrenAccept(new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof IgniteFilter) {
                filterCnt.incrementAndGet();
            }
            super.visit(node, ordinal, parent);
        }
    });
    // Checks that two filter merged into one filter.
    // Expected plan:
    // IgniteProject(VAL=[$1])
    // IgniteProject(ID=[$0], VAL=[$1])
    // IgniteFilter(condition=[=(CAST($1):INTEGER, 10)])
    // IgniteTableScan(table=[[PUBLIC, TEST]])
    assertEquals(0, filterCnt.get());
}
Also used : RelNode(org.apache.calcite.rel.RelNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFilter(org.apache.ignite.internal.sql.engine.rel.IgniteFilter) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) Frameworks.newConfigBuilder(org.apache.calcite.tools.Frameworks.newConfigBuilder) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelVisitor(org.apache.calcite.rel.RelVisitor) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Example 2 with IgniteFilter

use of org.apache.ignite.internal.sql.engine.rel.IgniteFilter in project ignite-3 by apache.

the class FilterConverterRule method convert.

/**
 * {@inheritDoc}
 */
@Override
protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalFilter rel) {
    RelOptCluster cluster = rel.getCluster();
    RelTraitSet traits = cluster.traitSetOf(IgniteConvention.INSTANCE).replace(IgniteDistributions.single());
    Set<CorrelationId> corrIds = RexUtils.extractCorrelationIds(rel.getCondition());
    if (!corrIds.isEmpty()) {
        traits = traits.replace(CorrelationTrait.correlations(corrIds)).replace(RewindabilityTrait.REWINDABLE);
    }
    return new IgniteFilter(cluster, traits, convert(rel.getInput(), traits.replace(CorrelationTrait.UNCORRELATED)), rel.getCondition());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) IgniteFilter(org.apache.ignite.internal.sql.engine.rel.IgniteFilter) RelTraitSet(org.apache.calcite.plan.RelTraitSet) CorrelationId(org.apache.calcite.rel.core.CorrelationId)

Example 3 with IgniteFilter

use of org.apache.ignite.internal.sql.engine.rel.IgniteFilter in project ignite-3 by apache.

the class FilterSpoolMergeToSortedIndexSpoolRule method onMatch.

/**
 * {@inheritDoc}
 */
@Override
public void onMatch(RelOptRuleCall call) {
    final IgniteFilter filter = call.rel(0);
    final IgniteTableSpool spool = call.rel(1);
    RelOptCluster cluster = spool.getCluster();
    RelTraitSet trait = spool.getTraitSet();
    CorrelationTrait filterCorr = TraitUtils.correlation(filter);
    if (filterCorr.correlated()) {
        trait = trait.replace(filterCorr);
    }
    RelNode input = spool.getInput();
    IndexConditions idxCond = RexUtils.buildSortedIndexConditions(cluster, TraitUtils.collation(input), filter.getCondition(), spool.getRowType(), null);
    if (nullOrEmpty(idxCond.lowerCondition()) && nullOrEmpty(idxCond.upperCondition())) {
        return;
    }
    RelCollation collation = TraitUtils.createCollation(idxCond.keys());
    RelNode res = new IgniteSortedIndexSpool(cluster, trait.replace(collation), convert(input, input.getTraitSet().replace(collation)), collation, filter.getCondition(), idxCond);
    call.transformTo(res);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) IgniteSortedIndexSpool(org.apache.ignite.internal.sql.engine.rel.IgniteSortedIndexSpool) CorrelationTrait(org.apache.ignite.internal.sql.engine.trait.CorrelationTrait) RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) IgniteFilter(org.apache.ignite.internal.sql.engine.rel.IgniteFilter) IgniteTableSpool(org.apache.ignite.internal.sql.engine.rel.IgniteTableSpool) IndexConditions(org.apache.ignite.internal.sql.engine.util.IndexConditions) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 4 with IgniteFilter

use of org.apache.ignite.internal.sql.engine.rel.IgniteFilter in project ignite-3 by apache.

the class FilterSpoolMergeToHashIndexSpoolRule method onMatch.

/**
 * {@inheritDoc}
 */
@Override
public void onMatch(RelOptRuleCall call) {
    final IgniteFilter filter = call.rel(0);
    final IgniteTableSpool spool = call.rel(1);
    RelOptCluster cluster = spool.getCluster();
    RelTraitSet trait = spool.getTraitSet();
    CorrelationTrait filterCorr = TraitUtils.correlation(filter);
    if (filterCorr.correlated()) {
        trait = trait.replace(filterCorr);
    }
    final RelNode input = spool.getInput();
    List<RexNode> searchRow = RexUtils.buildHashSearchRow(cluster, filter.getCondition(), spool.getRowType());
    if (nullOrEmpty(searchRow)) {
        return;
    }
    RelNode res = new IgniteHashIndexSpool(cluster, trait.replace(RelCollations.EMPTY), input, searchRow, filter.getCondition());
    call.transformTo(res);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) CorrelationTrait(org.apache.ignite.internal.sql.engine.trait.CorrelationTrait) RelNode(org.apache.calcite.rel.RelNode) IgniteFilter(org.apache.ignite.internal.sql.engine.rel.IgniteFilter) IgniteTableSpool(org.apache.ignite.internal.sql.engine.rel.IgniteTableSpool) IgniteHashIndexSpool(org.apache.ignite.internal.sql.engine.rel.IgniteHashIndexSpool) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RexNode(org.apache.calcite.rex.RexNode)

Example 5 with IgniteFilter

use of org.apache.ignite.internal.sql.engine.rel.IgniteFilter in project ignite-3 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.
 *
 * <p>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 : IgniteDistributions(org.apache.ignite.internal.sql.engine.trait.IgniteDistributions) RexUtils(org.apache.ignite.internal.sql.engine.util.RexUtils) Test(org.junit.jupiter.api.Test) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) IgniteCorrelatedNestedLoopJoin(org.apache.ignite.internal.sql.engine.rel.IgniteCorrelatedNestedLoopJoin) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) IgniteFilter(org.apache.ignite.internal.sql.engine.rel.IgniteFilter) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Filter(org.apache.calcite.rel.core.Filter) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IgniteFilter(org.apache.ignite.internal.sql.engine.rel.IgniteFilter) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) IgniteCorrelatedNestedLoopJoin(org.apache.ignite.internal.sql.engine.rel.IgniteCorrelatedNestedLoopJoin) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) Test(org.junit.jupiter.api.Test)

Aggregations

IgniteFilter (org.apache.ignite.internal.sql.engine.rel.IgniteFilter)5 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 RelTraitSet (org.apache.calcite.plan.RelTraitSet)3 RelNode (org.apache.calcite.rel.RelNode)3 IgniteTableSpool (org.apache.ignite.internal.sql.engine.rel.IgniteTableSpool)2 IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)2 CorrelationTrait (org.apache.ignite.internal.sql.engine.trait.CorrelationTrait)2 Test (org.junit.jupiter.api.Test)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RelCollation (org.apache.calcite.rel.RelCollation)1 RelVisitor (org.apache.calcite.rel.RelVisitor)1 CorrelationId (org.apache.calcite.rel.core.CorrelationId)1 Filter (org.apache.calcite.rel.core.Filter)1 RexCorrelVariable (org.apache.calcite.rex.RexCorrelVariable)1 RexFieldAccess (org.apache.calcite.rex.RexFieldAccess)1 RexNode (org.apache.calcite.rex.RexNode)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 Frameworks.newConfigBuilder (org.apache.calcite.tools.Frameworks.newConfigBuilder)1 IgniteCorrelatedNestedLoopJoin (org.apache.ignite.internal.sql.engine.rel.IgniteCorrelatedNestedLoopJoin)1 IgniteHashIndexSpool (org.apache.ignite.internal.sql.engine.rel.IgniteHashIndexSpool)1