Search in sources :

Example 51 with IgniteRel

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

the class MergeJoinPlannerTest method testRightPassThroughOrderByRight3.

/**
 * Test verifies the collation propagation from a parent node.
 *
 * <p>In case of right join, collation consisted of join keys only should be propagated as is.
 *
 * @throws Exception In case of any unexpected error.
 */
@Test
public void testRightPassThroughOrderByRight3() throws Exception {
    IgniteSchema schema = createSchema(createTable("LEFT_T", IgniteDistributions.single(), "C1", Integer.class, "C2", Integer.class, "C3", Integer.class), createTable("RIGHT_T", IgniteDistributions.single(), "C1", Integer.class, "C2", Integer.class, "C3", Integer.class));
    String sql = "" + "select * " + "  from LEFT_T " + " right join RIGHT_T " + "    on LEFT_T.c1 = RIGHT_T.c1 " + "   and LEFT_T.c2 = RIGHT_T.c2 " + " order by RIGHT_T.c1 DESC, RIGHT_T.c2 ASC";
    IgniteRel rel = physicalPlan(sql, schema, DISABLED_RULES);
    assertNull(sortOnTopOfJoin(rel));
    List<IgniteSort> sortNodes = sortOnTopOfScan(rel);
    RelCollation expected = RelCollations.of(new RelFieldCollation(0, DESCENDING, RelFieldCollation.NullDirection.LAST), new RelFieldCollation(1, ASCENDING, RelFieldCollation.NullDirection.FIRST));
    assertEquals(expected, sortNodes.get(0).collation());
    assertEquals(expected, sortNodes.get(1).collation());
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) IgniteSort(org.apache.ignite.internal.sql.engine.rel.IgniteSort) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Example 52 with IgniteRel

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

the class PlannerHelper method optimize.

/**
 * Optimize.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 *
 * @param sqlNode Sql node.
 * @param planner Planner.
 */
public static IgniteRel optimize(SqlNode sqlNode, IgnitePlanner planner) {
    try {
        // Convert to Relational operators graph
        RelRoot root = planner.rel(sqlNode);
        RelNode rel = root.rel;
        if (HintUtils.containsDisabledRules(root.hints)) {
            planner.setDisabledRules(HintUtils.disabledRules(root.hints));
        }
        // Transformation chain
        rel = planner.transform(PlannerPhase.HEP_DECORRELATE, rel.getTraitSet(), rel);
        rel = planner.trimUnusedFields(root.withRel(rel)).rel;
        rel = planner.transform(PlannerPhase.HEP_FILTER_PUSH_DOWN, rel.getTraitSet(), rel);
        rel = planner.transform(PlannerPhase.HEP_PROJECT_PUSH_DOWN, rel.getTraitSet(), rel);
        RelTraitSet desired = rel.getCluster().traitSet().replace(IgniteConvention.INSTANCE).replace(IgniteDistributions.single()).replace(root.collation == null ? RelCollations.EMPTY : root.collation).simplify();
        IgniteRel igniteRel = planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
        if (!root.isRefTrivial()) {
            final List<RexNode> projects = new ArrayList<>();
            final RexBuilder rexBuilder = igniteRel.getCluster().getRexBuilder();
            for (int field : Pair.left(root.fields)) {
                projects.add(rexBuilder.makeInputRef(igniteRel, field));
            }
            igniteRel = new IgniteProject(igniteRel.getCluster(), desired, igniteRel, projects, root.validatedRowType);
        }
        if (sqlNode.isA(Set.of(SqlKind.INSERT, SqlKind.UPDATE, SqlKind.MERGE))) {
            igniteRel = new FixDependentModifyNodeShuttle().visit(igniteRel);
        }
        return igniteRel;
    } catch (Throwable ex) {
        LOG.error("Unexpected error at query optimizer.", ex);
        if (LOG.isDebugEnabled()) {
            LOG.error(planner.dump());
        }
        throw ex;
    }
}
Also used : IgniteProject(org.apache.ignite.internal.sql.engine.rel.IgniteProject) RelNode(org.apache.calcite.rel.RelNode) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelRoot(org.apache.calcite.rel.RelRoot) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RexNode(org.apache.calcite.rex.RexNode)

Example 53 with IgniteRel

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

the class PrepareServiceImpl method prepareQuery.

private QueryPlan prepareQuery(SqlNode sqlNode, PlanningContext ctx) {
    IgnitePlanner planner = ctx.planner();
    // Validate
    ValidationResult validated = planner.validateAndGetTypeMetadata(sqlNode);
    sqlNode = validated.sqlNode();
    IgniteRel igniteRel = optimize(sqlNode, planner);
    // Split query plan to query fragments.
    List<Fragment> fragments = new Splitter().go(igniteRel);
    QueryTemplate template = new QueryTemplate(fragments);
    return new MultiStepQueryPlan(template, resultSetMetadata(ctx, validated.dataType(), validated.origins()));
}
Also used : IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel)

Example 54 with IgniteRel

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

the class Cloner method go.

/**
 * Clones and associates a plan with a new cluster.
 *
 * @param src Fragment to clone.
 * @return New plan.
 */
public Fragment go(Fragment src) {
    try {
        remotes = new ArrayList<>();
        IgniteRel newRoot = visit(src.root());
        return new Fragment(src.fragmentId(), newRoot, List.copyOf(remotes), src.serialized(), src.mapping());
    } finally {
        remotes = null;
    }
}
Also used : IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel)

Example 55 with IgniteRel

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

the class AbstractPlannerTest method physicalPlan.

protected IgniteRel physicalPlan(String sql, PlanningContext ctx) throws Exception {
    try (IgnitePlanner planner = ctx.planner()) {
        assertNotNull(planner);
        String qry = ctx.query();
        assertNotNull(qry);
        // Parse
        SqlNode sqlNode = planner.parse(qry);
        // Validate
        sqlNode = planner.validate(sqlNode);
        try {
            IgniteRel rel = PlannerHelper.optimize(sqlNode, planner);
            return rel;
        } catch (Throwable ex) {
            System.err.println(planner.dump());
            throw ex;
        }
    }
}
Also used : IgnitePlanner(org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

IgniteRel (org.apache.ignite.internal.sql.engine.rel.IgniteRel)102 IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)93 Test (org.junit.jupiter.api.Test)89 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)64 IgniteSort (org.apache.ignite.internal.sql.engine.rel.IgniteSort)39 RelCollation (org.apache.calcite.rel.RelCollation)33 IgniteTypeFactory (org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory)19 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)16 PlanningContext (org.apache.ignite.internal.sql.engine.prepare.PlanningContext)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 RexFieldAccess (org.apache.calcite.rex.RexFieldAccess)7 RexNode (org.apache.calcite.rex.RexNode)7 SchemaPlus (org.apache.calcite.schema.SchemaPlus)7 IgniteDistribution (org.apache.ignite.internal.sql.engine.trait.IgniteDistribution)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 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6