Search in sources :

Example 1 with IgnitePlanner

use of org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner in project ignite-3 by apache.

the class PlannerTest method testJoinPushExpressionRule.

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

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    };
    TestTable dept = new TestTable(new RelDataTypeFactory.Builder(f).add("DEPTNO", f.createJavaType(Integer.class)).add("NAME", f.createJavaType(String.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("EMP", emp);
    publicSchema.addTable("DEPT", dept);
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    String sql = "select d.deptno, e.deptno " + "from dept d, emp e " + "where d.deptno + e.deptno = 2";
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).costFactory(new IgniteCostFactory(1, 100, 1, 1)).build()).build()).query(sql).build();
    RelRoot relRoot;
    try (IgnitePlanner planner = ctx.planner()) {
        assertNotNull(planner);
        String qry = ctx.query();
        assertNotNull(qry);
        // Parse
        SqlNode sqlNode = planner.parse(qry);
        // Validate
        sqlNode = planner.validate(sqlNode);
        // Convert to Relational operators graph
        relRoot = planner.rel(sqlNode);
        RelNode rel = relRoot.rel;
        assertNotNull(rel);
        assertEquals("LogicalProject(DEPTNO=[$0], DEPTNO0=[$4])\n" + "  LogicalFilter(condition=[=(CAST(+($0, $4)):INTEGER, 2)])\n" + "    LogicalJoin(condition=[true], joinType=[inner])\n" + "      IgniteLogicalTableScan(table=[[PUBLIC, DEPT]])\n" + "      IgniteLogicalTableScan(table=[[PUBLIC, EMP]])\n", RelOptUtil.toString(rel));
        // Transformation chain
        RelTraitSet desired = rel.getCluster().traitSet().replace(IgniteConvention.INSTANCE).replace(IgniteDistributions.single()).replace(CorrelationTrait.UNCORRELATED).simplify();
        IgniteRel phys = planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
        assertNotNull(phys);
        assertEquals("IgniteProject(DEPTNO=[$3], DEPTNO0=[$2])\n" + "  IgniteCorrelatedNestedLoopJoin(condition=[=(CAST(+($3, $2)):INTEGER, 2)], joinType=[inner], " + "correlationVariables=[[$cor2]])\n" + "    IgniteTableScan(table=[[PUBLIC, EMP]])\n" + "    IgniteTableScan(table=[[PUBLIC, DEPT]], filters=[=(CAST(+($t0, $cor2.DEPTNO)):INTEGER, 2)])\n", RelOptUtil.toString(phys), "Invalid plan:\n" + RelOptUtil.toString(phys));
        checkSplitAndSerialization(phys, publicSchema);
    }
}
Also used : IgniteCostFactory(org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) Frameworks.newConfigBuilder(org.apache.calcite.tools.Frameworks.newConfigBuilder) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelRoot(org.apache.calcite.rel.RelRoot) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePlanner(org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner) RelNode(org.apache.calcite.rel.RelNode) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.jupiter.api.Test)

Example 2 with IgnitePlanner

use of org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner in project ignite-3 by apache.

the class AbstractPlannerTest method plannerCtx.

/**
 * Create planner context for specified query.
 */
protected PlanningContext plannerCtx(String sql, IgniteSchema publicSchema, String... disabledRules) {
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).build()).logger(log).build()).query(sql).build();
    IgnitePlanner planner = ctx.planner();
    assertNotNull(planner);
    planner.setDisabledRules(new HashSet<>(Arrays.asList(disabledRules)));
    return ctx;
}
Also used : PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) IgnitePlanner(org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 3 with IgnitePlanner

use of org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner in project ignite-3 by apache.

the class DdlSqlToCommandConverter method convertCreateTable.

/**
 * Converts a given CreateTable AST to a CreateTable command.
 *
 * @param createTblNode Root node of the given AST.
 * @param ctx           Planning context.
 */
private CreateTableCommand convertCreateTable(IgniteSqlCreateTable createTblNode, PlanningContext ctx) {
    CreateTableCommand createTblCmd = new CreateTableCommand();
    createTblCmd.schemaName(deriveSchemaName(createTblNode.name(), ctx));
    createTblCmd.tableName(deriveObjectName(createTblNode.name(), ctx, "tableName"));
    createTblCmd.ifTableExists(createTblNode.ifNotExists());
    if (createTblNode.createOptionList() != null) {
        for (SqlNode optNode : createTblNode.createOptionList().getList()) {
            IgniteSqlCreateTableOption opt = (IgniteSqlCreateTableOption) optNode;
            tblOptionProcessors.getOrDefault(opt.key(), UNSUPPORTED_OPTION_PROCESSOR).process(opt, ctx, createTblCmd);
        }
    }
    List<SqlKeyConstraint> pkConstraints = createTblNode.columnList().getList().stream().filter(SqlKeyConstraint.class::isInstance).map(SqlKeyConstraint.class::cast).collect(Collectors.toList());
    if (nullOrEmpty(pkConstraints)) {
        throw new IgniteException("Table without PRIMARY KEY is not supported");
    } else if (pkConstraints.size() > 1) {
        throw new IgniteException("Unexpected amount of primary key constraints [" + "expected at most one, but was " + pkConstraints.size() + "; " + "querySql=\"" + ctx.query() + "\"]");
    }
    Set<String> dedupSetPk = new HashSet<>();
    List<String> pkCols = pkConstraints.stream().map(pk -> pk.getOperandList().get(1)).map(SqlNodeList.class::cast).flatMap(l -> l.getList().stream()).map(SqlIdentifier.class::cast).map(SqlIdentifier::getSimple).filter(dedupSetPk::add).collect(Collectors.toList());
    createTblCmd.primaryKeyColumns(pkCols);
    List<SqlColumnDeclaration> colDeclarations = createTblNode.columnList().getList().stream().filter(SqlColumnDeclaration.class::isInstance).map(SqlColumnDeclaration.class::cast).collect(Collectors.toList());
    IgnitePlanner planner = ctx.planner();
    List<ColumnDefinition> cols = new ArrayList<>(colDeclarations.size());
    for (SqlColumnDeclaration col : colDeclarations) {
        if (!col.name.isSimple()) {
            throw new IgniteException("Unexpected value of columnName [" + "expected a simple identifier, but was " + col.name + "; " + "querySql=\"" + ctx.query() + "\"]");
        }
        String name = col.name.getSimple();
        if (col.dataType.getNullable() != null && col.dataType.getNullable() && dedupSetPk.contains(name)) {
            throw new IgniteException("Primary key cannot contain nullable column [col=" + name + "]");
        }
        RelDataType relType = planner.convert(col.dataType, !dedupSetPk.contains(name));
        dedupSetPk.remove(name);
        Object dflt = null;
        if (col.expression != null) {
            dflt = ((SqlLiteral) col.expression).getValue();
        }
        cols.add(new ColumnDefinition(name, relType, dflt));
    }
    if (!dedupSetPk.isEmpty()) {
        throw new IgniteException("Primary key constrain contains undefined columns: [cols=" + dedupSetPk + "]");
    }
    createTblCmd.columns(cols);
    return createTblCmd;
}
Also used : IgniteSqlCreateTableOption(org.apache.ignite.internal.sql.engine.sql.IgniteSqlCreateTableOption) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlColumnDeclaration(org.apache.calcite.sql.ddl.SqlColumnDeclaration) SqlKeyConstraint(org.apache.calcite.sql.ddl.SqlKeyConstraint) IgnitePlanner(org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner) IgniteException(org.apache.ignite.lang.IgniteException) SqlNode(org.apache.calcite.sql.SqlNode) HashSet(java.util.HashSet)

Example 4 with IgnitePlanner

use of org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner 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

IgnitePlanner (org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner)4 SqlNode (org.apache.calcite.sql.SqlNode)3 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 PlanningContext (org.apache.ignite.internal.sql.engine.prepare.PlanningContext)2 IgniteRel (org.apache.ignite.internal.sql.engine.rel.IgniteRel)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 RelNode (org.apache.calcite.rel.RelNode)1 RelRoot (org.apache.calcite.rel.RelRoot)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlColumnDeclaration (org.apache.calcite.sql.ddl.SqlColumnDeclaration)1 SqlKeyConstraint (org.apache.calcite.sql.ddl.SqlKeyConstraint)1 Frameworks.newConfigBuilder (org.apache.calcite.tools.Frameworks.newConfigBuilder)1 IgniteCostFactory (org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory)1 IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)1 IgniteSqlCreateTableOption (org.apache.ignite.internal.sql.engine.sql.IgniteSqlCreateTableOption)1