Search in sources :

Example 61 with IgniteSchema

use of org.apache.ignite.internal.sql.engine.schema.IgniteSchema in project ignite-3 by apache.

the class AggregatePlannerTest method expandDistinctAggregates.

/**
 * ExpandDistinctAggregates.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 *
 * @throws Exception If failed.
 */
@ParameterizedTest
@EnumSource
public void expandDistinctAggregates(AggregateAlgorithm algo) throws Exception {
    TestTable tbl = createAffinityTable().addIndex(RelCollations.of(ImmutableIntList.of(3, 1, 0)), "idx_val0").addIndex(RelCollations.of(ImmutableIntList.of(3, 2, 0)), "idx_val1");
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("TEST", tbl);
    String sql = "SELECT " + "/*+ EXPAND_DISTINCT_AGG */ " + "SUM(DISTINCT val0), AVG(DISTINCT val1) FROM test GROUP BY grp0";
    IgniteRel phys = physicalPlan(sql, publicSchema, algo.rulesToDisable);
    // Plan must not contain distinct accumulators.
    assertFalse(findNodes(phys, byClass(IgniteAggregate.class)).stream().anyMatch(n -> ((Aggregate) n).getAggCallList().stream().anyMatch(AggregateCall::isDistinct)), "Invalid plan\n" + RelOptUtil.toString(phys, SqlExplainLevel.ALL_ATTRIBUTES));
    assertNotNull(findFirstNode(phys, byClass(Join.class)), "Invalid plan\n" + RelOptUtil.toString(phys, SqlExplainLevel.ALL_ATTRIBUTES));
    // Check the first aggrgation step is SELECT DISTINCT (doesn't contains any accumulators)
    assertTrue(findNodes(phys, byClass(algo.reduce)).stream().allMatch(n -> ((IgniteReduceAggregateBase) n).getAggregateCalls().isEmpty()), "Invalid plan\n" + RelOptUtil.toString(phys, SqlExplainLevel.ALL_ATTRIBUTES));
    assertTrue(findNodes(phys, byClass(algo.map)).stream().allMatch(n -> ((Aggregate) n).getAggCallList().isEmpty()), "Invalid plan\n" + RelOptUtil.toString(phys, SqlExplainLevel.ALL_ATTRIBUTES));
    // Check the second aggrgation step contains accumulators.
    assertTrue(findNodes(phys, byClass(algo.single)).stream().noneMatch(n -> ((Aggregate) n).getAggCallList().isEmpty()), "Invalid plan\n" + RelOptUtil.toString(phys, SqlExplainLevel.ALL_ATTRIBUTES));
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) IgniteSort(org.apache.ignite.internal.sql.engine.rel.IgniteSort) EnumSource(org.junit.jupiter.params.provider.EnumSource) SqlAvgAggFunction(org.apache.calcite.sql.fun.SqlAvgAggFunction) IgniteSingleAggregateBase(org.apache.ignite.internal.sql.engine.rel.agg.IgniteSingleAggregateBase) IgniteSingleSortAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteSingleSortAggregate) IgniteAggregate(org.apache.ignite.internal.sql.engine.rel.IgniteAggregate) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Join(org.apache.calcite.rel.core.Join) IgniteIndexScan(org.apache.ignite.internal.sql.engine.rel.IgniteIndexScan) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) IgniteMapAggregateBase(org.apache.ignite.internal.sql.engine.rel.agg.IgniteMapAggregateBase) IgniteReduceSortAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteReduceSortAggregate) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArrayUtils.concat(org.apache.ignite.internal.util.ArrayUtils.concat) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IsInstanceOf(org.hamcrest.core.IsInstanceOf) RelCollations(org.apache.calcite.rel.RelCollations) IgniteDistributions(org.apache.ignite.internal.sql.engine.trait.IgniteDistributions) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) SqlExplainLevel(org.apache.calcite.sql.SqlExplainLevel) Aggregate(org.apache.calcite.rel.core.Aggregate) IgniteMapSortAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteMapSortAggregate) IgniteReduceHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteReduceHashAggregate) IgniteReduceAggregateBase(org.apache.ignite.internal.sql.engine.rel.agg.IgniteReduceAggregateBase) TraitUtils(org.apache.ignite.internal.sql.engine.trait.TraitUtils) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AggregateCall(org.apache.calcite.rel.core.AggregateCall) IgniteMapHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteMapHashAggregate) CollectionUtils.first(org.apache.ignite.internal.util.CollectionUtils.first) IgniteSingleHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteSingleHashAggregate) AggregateCall(org.apache.calcite.rel.core.AggregateCall) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) IgniteSingleSortAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteSingleSortAggregate) IgniteAggregate(org.apache.ignite.internal.sql.engine.rel.IgniteAggregate) IgniteReduceSortAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteReduceSortAggregate) Aggregate(org.apache.calcite.rel.core.Aggregate) IgniteMapSortAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteMapSortAggregate) IgniteReduceHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteReduceHashAggregate) IgniteMapHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteMapHashAggregate) IgniteSingleHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteSingleHashAggregate) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 62 with IgniteSchema

use of org.apache.ignite.internal.sql.engine.schema.IgniteSchema in project ignite-3 by apache.

the class AggregatePlannerTest method singleWithoutIndex.

/**
 * SingleWithoutIndex.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 *
 * @throws Exception If failed.
 */
@ParameterizedTest
@EnumSource
public void singleWithoutIndex(AggregateAlgorithm algo) throws Exception {
    TestTable tbl = createBroadcastTable().addIndex(RelCollations.of(ImmutableIntList.of(1, 2)), "val0_val1");
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("TEST", tbl);
    String sql = "SELECT AVG(val0) FROM test GROUP BY grp0";
    IgniteRel phys = physicalPlan(sql, publicSchema, algo.rulesToDisable);
    IgniteSingleAggregateBase agg = findFirstNode(phys, byClass(algo.single));
    assertNotNull(agg, "Invalid plan\n" + RelOptUtil.toString(phys));
    assertThat("Invalid plan\n" + RelOptUtil.toString(phys), first(agg.getAggCallList()).getAggregation(), IsInstanceOf.instanceOf(SqlAvgAggFunction.class));
    if (algo == AggregateAlgorithm.SORT) {
        assertNotNull(findFirstNode(phys, byClass(IgniteSort.class)));
    }
}
Also used : SqlAvgAggFunction(org.apache.calcite.sql.fun.SqlAvgAggFunction) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IgniteSingleAggregateBase(org.apache.ignite.internal.sql.engine.rel.agg.IgniteSingleAggregateBase) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 63 with IgniteSchema

use of org.apache.ignite.internal.sql.engine.schema.IgniteSchema in project ignite-3 by apache.

the class CorrelatedNestedLoopJoinPlannerTest method testInvalidIndexExpressions.

/**
 * Check join with not equi condition. Current implementation of the CorrelatedNestedLoopJoinTest is not applicable
 * for such case.
 */
@Test
public void testInvalidIndexExpressions() throws Exception {
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    IgniteTypeFactory f = new IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
    publicSchema.addTable("T0", new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("JID", f.createJavaType(Integer.class)).add("VAL", f.createJavaType(String.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    }.addIndex(RelCollations.of(ImmutableIntList.of(1, 0)), "t0_jid_idx"));
    publicSchema.addTable("T1", new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("JID", f.createJavaType(Integer.class)).add("VAL", f.createJavaType(String.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    }.addIndex(RelCollations.of(ImmutableIntList.of(1, 0)), "t1_jid_idx"));
    String sql = "select * " + "from t0 " + "join t1 on t0.jid + 2 > t1.jid * 2";
    IgniteRel phys = physicalPlan(sql, publicSchema, "MergeJoinConverter", "NestedLoopJoinConverter", "FilterSpoolMergeRule");
    assertNotNull(phys);
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Example 64 with IgniteSchema

use of org.apache.ignite.internal.sql.engine.schema.IgniteSchema 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)

Example 65 with IgniteSchema

use of org.apache.ignite.internal.sql.engine.schema.IgniteSchema in project ignite-3 by apache.

the class HashAggregatePlannerTest method noGroupByAggregate.

/**
 * NoGroupByAggregate.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 */
@Test
public void noGroupByAggregate() throws Exception {
    TestTable tbl = createAffinityTable().addIndex(RelCollations.of(ImmutableIntList.of(1, 2)), "val0_val1");
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("TEST", tbl);
    String sqlCount = "SELECT COUNT(*) FROM test";
    IgniteRel phys = physicalPlan(sqlCount, publicSchema);
    IgniteMapHashAggregate mapAgg = findFirstNode(phys, byClass(IgniteMapHashAggregate.class));
    IgniteReduceHashAggregate rdcAgg = findFirstNode(phys, byClass(IgniteReduceHashAggregate.class));
    assertNotNull(rdcAgg, "Invalid plan\n" + RelOptUtil.toString(phys));
    assertNotNull(mapAgg, "Invalid plan\n" + RelOptUtil.toString(phys));
    assertThat("Invalid plan\n" + RelOptUtil.toString(phys), first(rdcAgg.getAggregateCalls()).getAggregation(), IsInstanceOf.instanceOf(SqlCountAggFunction.class));
    assertThat("Invalid plan\n" + RelOptUtil.toString(phys), first(mapAgg.getAggCallList()).getAggregation(), IsInstanceOf.instanceOf(SqlCountAggFunction.class));
}
Also used : IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) IgniteReduceHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteReduceHashAggregate) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) IgniteMapHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteMapHashAggregate) SqlCountAggFunction(org.apache.calcite.sql.fun.SqlCountAggFunction) Test(org.junit.jupiter.api.Test)

Aggregations

IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)115 Test (org.junit.jupiter.api.Test)105 IgniteRel (org.apache.ignite.internal.sql.engine.rel.IgniteRel)93 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)66 IgniteSort (org.apache.ignite.internal.sql.engine.rel.IgniteSort)42 RelCollation (org.apache.calcite.rel.RelCollation)33 IgniteTypeFactory (org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory)28 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)20 RelNode (org.apache.calcite.rel.RelNode)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 IgniteIndex (org.apache.ignite.internal.sql.engine.schema.IgniteIndex)9 IgniteDistribution (org.apache.ignite.internal.sql.engine.trait.IgniteDistribution)9 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 RexFieldAccess (org.apache.calcite.rex.RexFieldAccess)7 PlanningContext (org.apache.ignite.internal.sql.engine.prepare.PlanningContext)7 IgniteIndexScan (org.apache.ignite.internal.sql.engine.rel.IgniteIndexScan)7 RexNode (org.apache.calcite.rex.RexNode)6 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