Search in sources :

Example 71 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class PlannerTest method testSplitterColocatedReplicatedReplicated.

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forNodes(select(NODES, 0, 1, 2, 3));
        }

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forNodes(select(NODES, 0, 1, 2, 3));
        }

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("DEVELOPER", developer);
    publicSchema.addTable("PROJECT", project);
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    String sql = "SELECT d.id, (d.id + 1) as id2, d.name, d.projectId, p.id0, p.ver0 " + "FROM PUBLIC.Developer d JOIN (" + "SELECT pp.id as id0, pp.ver as ver0 FROM PUBLIC.Project pp" + ") p " + "ON d.id = p.id0 " + "WHERE (d.projectId + 1) > ?";
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).build()).build()).query(sql).parameters(2).build();
    IgniteRel phys = physicalPlan(sql, ctx);
    assertNotNull(phys);
    MultiStepPlan plan = new MultiStepQueryPlan(new QueryTemplate(new Splitter().go(phys)), null);
    assertNotNull(plan);
    plan.init(this::intermediateMapping, mapContext(CollectionUtils.first(NODES), 0L));
    assertNotNull(plan);
    assertEquals(1, plan.fragments().size());
}
Also used : QueryTemplate(org.apache.ignite.internal.sql.engine.prepare.QueryTemplate) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) Splitter(org.apache.ignite.internal.sql.engine.prepare.Splitter) MultiStepPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MultiStepQueryPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepQueryPlan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Example 72 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class PlannerTest method testSplitterPartiallyColocatedReplicatedAndPartitioned.

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forNodes(select(NODES, 0));
        }

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

        @Override
        public ColocationGroup colocationGroup(MappingQueryContext ctx) {
            return ColocationGroup.forAssignments(Arrays.asList(select(NODES, 1, 2), select(NODES, 2, 3), select(NODES, 3, 0), select(NODES, 0, 1)));
        }

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.affinity(0, "Project", "hash");
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("DEVELOPER", developer);
    publicSchema.addTable("PROJECT", project);
    SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
    String sql = "SELECT d.id, d.name, d.projectId, p.id0, p.ver0 " + "FROM PUBLIC.Developer d JOIN (" + "SELECT pp.id as id0, pp.ver as ver0 FROM PUBLIC.Project pp" + ") p " + "ON d.id = p.id0 " + "WHERE (d.projectId + 1) > ?";
    PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).build()).build()).query(sql).parameters(2).build();
    IgniteRel phys = physicalPlan(sql, ctx);
    assertNotNull(phys);
    MultiStepPlan plan = new MultiStepQueryPlan(new QueryTemplate(new Splitter().go(phys)), null);
    assertNotNull(plan);
    plan.init(this::intermediateMapping, mapContext(CollectionUtils.first(NODES), 0L));
    assertEquals(3, plan.fragments().size());
}
Also used : QueryTemplate(org.apache.ignite.internal.sql.engine.prepare.QueryTemplate) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) Splitter(org.apache.ignite.internal.sql.engine.prepare.Splitter) MultiStepPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MultiStepQueryPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepQueryPlan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Example 73 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class PrepareServiceImpl method explainFieldsMetadata.

private ResultSetMetadata explainFieldsMetadata(PlanningContext ctx) {
    IgniteTypeFactory factory = ctx.typeFactory();
    RelDataType planStrDataType = factory.createSqlType(SqlTypeName.VARCHAR, PRECISION_NOT_SPECIFIED);
    Map.Entry<String, RelDataType> planField = new IgniteBiTuple<>(ExplainPlan.PLAN_COL_NAME, planStrDataType);
    RelDataType planDataType = factory.createStructType(singletonList(planField));
    return resultSetMetadata(ctx, planDataType, null);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) Map(java.util.Map)

Example 74 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class AccumulatorsFactory method cast0.

private static Function<Object, Object> cast0(Pair<RelDataType, RelDataType> types) {
    IgniteTypeFactory typeFactory = Commons.typeFactory();
    RelDataType from = types.left;
    RelDataType to = types.right;
    Class<?> fromType = Primitives.wrap((Class<?>) typeFactory.getJavaClass(from));
    Class<?> toType = Primitives.wrap((Class<?>) typeFactory.getJavaClass(to));
    if (toType.isAssignableFrom(fromType)) {
        return Function.identity();
    }
    if (Void.class == toType) {
        return o -> null;
    }
    return compileCast(typeFactory, from, to);
}
Also used : RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) CollectionUtils.nullOrEmpty(org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty) RexProgram(org.apache.calcite.rex.RexProgram) JavaRowFormat(org.apache.calcite.adapter.enumerable.JavaRowFormat) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TypeUtils.createRowType(org.apache.ignite.internal.sql.engine.util.TypeUtils.createRowType) Expression(org.apache.calcite.linq4j.tree.Expression) PhysTypeImpl(org.apache.calcite.adapter.enumerable.PhysTypeImpl) RexNode(org.apache.calcite.rex.RexNode) Primitives(org.apache.ignite.internal.sql.engine.util.Primitives) Pair(org.apache.calcite.util.Pair) MethodDeclaration(org.apache.calcite.linq4j.tree.MethodDeclaration) ExecutionContext(org.apache.ignite.internal.sql.engine.exec.ExecutionContext) RelDataType(org.apache.calcite.rel.type.RelDataType) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) RowHandler(org.apache.ignite.internal.sql.engine.exec.RowHandler) RexBuilder(org.apache.calcite.rex.RexBuilder) DataContext(org.apache.calcite.DataContext) Expressions(org.apache.calcite.linq4j.tree.Expressions) SqlTypeUtil(org.apache.calcite.sql.type.SqlTypeUtil) Commons(org.apache.ignite.internal.sql.engine.util.Commons) EnumUtils(org.apache.calcite.adapter.enumerable.EnumUtils) List(java.util.List) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) Modifier(java.lang.reflect.Modifier) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) AggregateCall(org.apache.calcite.rel.core.AggregateCall) SqlConformanceEnum(org.apache.calcite.sql.validate.SqlConformanceEnum) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) NotNull(org.jetbrains.annotations.NotNull) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 75 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class LogicalOrToUnionRule method idxCollationCheck.

/**
 * Compares intersection (currently beginning position) of condition and index fields.
 * This rule need to be triggered only if appropriate indexes will be found otherwise it`s not applicable.
 *
 * @param call Set of appropriate RelNode.
 * @param operands Operands from OR expression.
 */
private boolean idxCollationCheck(RelOptRuleCall call, List<RexNode> operands) {
    final IgniteLogicalTableScan scan = call.rel(0);
    InternalIgniteTable tbl = scan.getTable().unwrap(InternalIgniteTable.class);
    IgniteTypeFactory typeFactory = Commons.typeFactory(scan.getCluster());
    int fieldCnt = tbl.getRowType(typeFactory).getFieldCount();
    BitSet idxsFirstFields = new BitSet(fieldCnt);
    for (IgniteIndex idx : tbl.indexes().values()) {
        List<RelFieldCollation> fieldCollations = idx.collation().getFieldCollations();
        if (!CollectionUtils.nullOrEmpty(fieldCollations)) {
            idxsFirstFields.set(fieldCollations.get(0).getFieldIndex());
        }
    }
    Mappings.TargetMapping mapping = scan.requiredColumns() == null ? null : Commons.inverseMapping(scan.requiredColumns(), fieldCnt);
    for (RexNode op : operands) {
        BitSet conditionFields = new BitSet(fieldCnt);
        new RexShuttle() {

            @Override
            public RexNode visitLocalRef(RexLocalRef inputRef) {
                conditionFields.set(mapping == null ? inputRef.getIndex() : mapping.getSourceOpt(inputRef.getIndex()));
                return inputRef;
            }
        }.apply(op);
        if (!conditionFields.intersects(idxsFirstFields)) {
            return false;
        }
    }
    return true;
}
Also used : IgniteIndex(org.apache.ignite.internal.sql.engine.schema.IgniteIndex) RexShuttle(org.apache.calcite.rex.RexShuttle) InternalIgniteTable(org.apache.ignite.internal.sql.engine.schema.InternalIgniteTable) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteLogicalTableScan(org.apache.ignite.internal.sql.engine.rel.logical.IgniteLogicalTableScan) BitSet(java.util.BitSet) Mappings(org.apache.calcite.util.mapping.Mappings) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexLocalRef(org.apache.calcite.rex.RexLocalRef) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

IgniteTypeFactory (org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory)80 Test (org.junit.jupiter.api.Test)48 RelDataType (org.apache.calcite.rel.type.RelDataType)47 IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)27 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 AggregateCall (org.apache.calcite.rel.core.AggregateCall)19 IgniteRel (org.apache.ignite.internal.sql.engine.rel.IgniteRel)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)17 RexNode (org.apache.calcite.rex.RexNode)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 ArrayList (java.util.ArrayList)10 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 IgniteDistribution (org.apache.ignite.internal.sql.engine.trait.IgniteDistribution)8 PlanningContext (org.apache.ignite.internal.sql.engine.prepare.PlanningContext)7 List (java.util.List)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 QueryTemplate (org.apache.ignite.internal.sql.engine.prepare.QueryTemplate)6