Search in sources :

Example 76 with IgniteTypeFactory

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

the class ProjectScanMergeRule method onMatch.

/**
 * {@inheritDoc}
 */
@Override
public void onMatch(RelOptRuleCall call) {
    LogicalProject relProject = call.rel(0);
    T scan = call.rel(1);
    RelOptCluster cluster = scan.getCluster();
    List<RexNode> projects = relProject.getProjects();
    RexNode cond = scan.condition();
    ImmutableBitSet requiredColumns = scan.requiredColumns();
    List<RexNode> scanProjects = scan.projects();
    // Set default traits, real traits will be calculated for physical node.
    RelTraitSet traits = cluster.traitSet();
    IgniteTable tbl = scan.getTable().unwrap(IgniteTable.class);
    IgniteTypeFactory typeFactory = Commons.typeFactory(cluster);
    if (requiredColumns == null) {
        assert scanProjects == null;
        ImmutableBitSet.Builder builder = ImmutableBitSet.builder();
        new RexShuttle() {

            @Override
            public RexNode visitInputRef(RexInputRef ref) {
                builder.set(ref.getIndex());
                return ref;
            }
        }.apply(projects);
        new RexShuttle() {

            @Override
            public RexNode visitLocalRef(RexLocalRef inputRef) {
                builder.set(inputRef.getIndex());
                return inputRef;
            }
        }.apply(cond);
        requiredColumns = builder.build();
        Mappings.TargetMapping targetMapping = Commons.mapping(requiredColumns, tbl.getRowType(typeFactory).getFieldCount());
        projects = new RexShuttle() {

            @Override
            public RexNode visitInputRef(RexInputRef ref) {
                return new RexLocalRef(targetMapping.getTarget(ref.getIndex()), ref.getType());
            }
        }.apply(projects);
        cond = new RexShuttle() {

            @Override
            public RexNode visitLocalRef(RexLocalRef ref) {
                return new RexLocalRef(targetMapping.getTarget(ref.getIndex()), ref.getType());
            }
        }.apply(cond);
    } else {
        projects = RexUtils.replaceInputRefs(projects);
    }
    if (scanProjects != null) {
        // Merge projects.
        projects = new RexShuttle() {

            @Override
            public RexNode visitLocalRef(RexLocalRef ref) {
                return scanProjects.get(ref.getIndex());
            }
        }.apply(projects);
    }
    if (RexUtils.isIdentity(projects, tbl.getRowType(typeFactory, requiredColumns), true)) {
        projects = null;
    }
    call.transformTo(createNode(cluster, scan, traits, projects, cond, requiredColumns));
    if (!RexUtils.hasCorrelation(relProject.getProjects())) {
        cluster.getPlanner().prune(relProject);
    }
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RexShuttle(org.apache.calcite.rex.RexShuttle) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelTraitSet(org.apache.calcite.plan.RelTraitSet) Mappings(org.apache.calcite.util.mapping.Mappings) RexInputRef(org.apache.calcite.rex.RexInputRef) RexLocalRef(org.apache.calcite.rex.RexLocalRef) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexNode(org.apache.calcite.rex.RexNode)

Example 77 with IgniteTypeFactory

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

the class ExpressionFactoryImplTest method prepare.

@BeforeEach
public void prepare() {
    RelDataTypeSystem typeSys = CALCITE_CONNECTION_CONFIG.typeSystem(RelDataTypeSystem.class, IgniteTypeSystem.INSTANCE);
    typeFactory = new IgniteTypeFactory(typeSys);
    expFactory = new ExpressionFactoryImpl<>(null, typeFactory, SqlConformanceEnum.DEFAULT);
}
Also used : RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 78 with IgniteTypeFactory

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

the class AbstractSetOpExecutionTest method testSingleWithEmptySet.

@Test
public void testSingleWithEmptySet() {
    ExecutionContext<Object[]> ctx = executionContext();
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, String.class, int.class);
    List<Object[]> data = Arrays.asList(row("Igor", 1), row("Roman", 1));
    // For single distribution set operations, node should not request rows from the next source if result after
    // the previous source is already empty.
    ScanNode<Object[]> scan1 = new ScanNode<>(ctx, rowType, data);
    ScanNode<Object[]> scan2 = new ScanNode<>(ctx, rowType, data);
    ScanNode<Object[]> scan3 = new ScanNode<>(ctx, rowType, Collections.emptyList());
    Node<Object[]> node4 = new AbstractNode<>(ctx, rowType) {

        @Override
        protected void rewindInternal() {
        // No-op.
        }

        @Override
        protected Downstream<Object[]> requestDownstream(int idx) {
            return null;
        }

        @Override
        public void request(int rowsCnt) {
            fail("Node should not be requested");
        }
    };
    List<Node<Object[]>> inputs = Arrays.asList(scan1, scan2, scan3, node4);
    AbstractSetOpNode<Object[]> setOpNode = setOpNodeFactory(ctx, rowType, SINGLE, false, inputs.size());
    setOpNode.register(inputs);
    RootNode<Object[]> root = new RootNode<>(ctx, rowType);
    root.register(setOpNode);
    assertFalse(root.hasNext());
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) Test(org.junit.jupiter.api.Test)

Example 79 with IgniteTypeFactory

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

the class AbstractSetOpExecutionTest method checkSetOp.

/**
 * CheckSetOp.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 *
 * @param single Single.
 * @param all    All.
 */
protected void checkSetOp(boolean single, boolean all, List<List<Object[]>> dataSets, List<Object[]> expectedResult) {
    ExecutionContext<Object[]> ctx = executionContext();
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, String.class, int.class);
    List<Node<Object[]>> inputs = dataSets.stream().map(ds -> new ScanNode<>(ctx, rowType, ds)).collect(Collectors.toList());
    AbstractSetOpNode<Object[]> setOpNode;
    if (single) {
        setOpNode = setOpNodeFactory(ctx, rowType, SINGLE, all, inputs.size());
    } else {
        setOpNode = setOpNodeFactory(ctx, rowType, MAP, all, inputs.size());
    }
    setOpNode.register(inputs);
    if (!single) {
        AbstractSetOpNode<Object[]> reduceNode = setOpNodeFactory(ctx, rowType, REDUCE, all, 1);
        reduceNode.register(Collections.singletonList(setOpNode));
        setOpNode = reduceNode;
    }
    Comparator<Object[]> cmp = ctx.expressionFactory().comparator(RelCollations.of(ImmutableIntList.of(0, 1)));
    // Create sort node on the top to check sorted results.
    SortNode<Object[]> sortNode = new SortNode<>(ctx, rowType, cmp);
    sortNode.register(setOpNode);
    RootNode<Object[]> root = new RootNode<>(ctx, rowType);
    root.register(sortNode);
    assertTrue(nullOrEmpty(expectedResult) || root.hasNext());
    for (Object[] row : expectedResult) {
        assertArrayEquals(row, root.next());
    }
    assertFalse(root.hasNext());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) RelCollations(org.apache.calcite.rel.RelCollations) RelDataType(org.apache.calcite.rel.type.RelDataType) Arrays(java.util.Arrays) CollectionUtils.nullOrEmpty(org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) AggregateType(org.apache.ignite.internal.sql.engine.exec.exp.agg.AggregateType) TypeUtils(org.apache.ignite.internal.sql.engine.util.TypeUtils) SINGLE(org.apache.ignite.internal.sql.engine.exec.exp.agg.AggregateType.SINGLE) Collectors(java.util.stream.Collectors) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ExecutionContext(org.apache.ignite.internal.sql.engine.exec.ExecutionContext) MAP(org.apache.ignite.internal.sql.engine.exec.exp.agg.AggregateType.MAP) Comparator(java.util.Comparator) Collections(java.util.Collections) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) REDUCE(org.apache.ignite.internal.sql.engine.exec.exp.agg.AggregateType.REDUCE) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 80 with IgniteTypeFactory

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

the class TableDescriptorImpl method newColumnDefaultValue.

/**
 * {@inheritDoc}
 */
@Override
public RexNode newColumnDefaultValue(RelOptTable tbl, int colIdx, InitializerContext ctx) {
    final ColumnDescriptor desc = descriptors[colIdx];
    if (!desc.hasDefaultValue()) {
        return super.newColumnDefaultValue(tbl, colIdx, ctx);
    }
    final RexBuilder rexBuilder = ctx.getRexBuilder();
    final IgniteTypeFactory typeFactory = (IgniteTypeFactory) rexBuilder.getTypeFactory();
    return rexBuilder.makeLiteral(desc.defaultValue(), desc.logicalType(typeFactory), false);
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder)

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