Search in sources :

Example 26 with IgniteTypeFactory

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

the class BaseAggregateTest method count.

/**
 * Count.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 */
@ParameterizedTest
@EnumSource
public void count(TestAggregateType testAgg) {
    ExecutionContext<Object[]> ctx = executionContext(true);
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, int.class);
    ScanNode<Object[]> scan = new ScanNode<>(ctx, rowType, Arrays.asList(row(0, 200), row(1, 300), row(1, 1400), row(0, 1000)));
    AggregateCall call = AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, false, ImmutableIntList.of(), -1, null, RelCollations.EMPTY, tf.createJavaType(int.class), null);
    List<ImmutableBitSet> grpSets = List.of(ImmutableBitSet.of(0));
    RelDataType aggRowType = TypeUtils.createRowType(tf, int.class);
    SingleNode<Object[]> aggChain = createAggregateNodesChain(testAgg, ctx, grpSets, call, rowType, aggRowType, rowFactory(), scan);
    RootNode<Object[]> root = new RootNode<>(ctx, aggRowType);
    root.register(aggChain);
    assertTrue(root.hasNext());
    assertArrayEquals(row(0, 2), root.next());
    assertArrayEquals(row(1, 2), root.next());
    assertFalse(root.hasNext());
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with IgniteTypeFactory

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

the class BaseAggregateTest method singleAggr.

/**
 * Checks single aggregate and appropriate {@link Accumulators.SingleVal} implementation.
 *
 * @param scanInput Input data.
 * @param output    Expectation result.
 * @param mustFail  {@code true} If expression must throw exception.
 */
@SuppressWarnings("ThrowableNotThrown")
public void singleAggr(TestAggregateType testAgg, List<Object[]> scanInput, Object[] output, boolean mustFail) {
    ExecutionContext<Object[]> ctx = executionContext();
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, int.class);
    ScanNode<Object[]> scan = new ScanNode<>(ctx, rowType, scanInput);
    AggregateCall call = AggregateCall.create(SqlStdOperatorTable.SINGLE_VALUE, false, false, false, ImmutableIntList.of(1), -1, RelCollations.EMPTY, tf.createJavaType(Integer.class), null);
    List<ImmutableBitSet> grpSets = List.of(ImmutableBitSet.of(0));
    RelDataType aggRowType = TypeUtils.createRowType(tf, int.class);
    SingleNode<Object[]> aggChain = createAggregateNodesChain(testAgg, ctx, grpSets, call, rowType, aggRowType, rowFactory(), scan);
    RootNode<Object[]> root = new RootNode<>(ctx, aggRowType);
    root.register(aggChain);
    Runnable r = () -> {
        assertTrue(root.hasNext());
        assertArrayEquals(row(0, output[0]), root.next());
        assertArrayEquals(row(1, output[1]), root.next());
        assertFalse(root.hasNext());
    };
    if (mustFail) {
        assertThrowsWithCause(r::run, IllegalArgumentException.class);
    } else {
        r.run();
    }
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) AggregateCall(org.apache.calcite.rel.core.AggregateCall) Supplier(java.util.function.Supplier) RowHandler(org.apache.ignite.internal.sql.engine.exec.RowHandler) AccumulatorWrapper(org.apache.ignite.internal.sql.engine.exec.exp.agg.AccumulatorWrapper)

Example 28 with IgniteTypeFactory

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

the class ExecutionTest method testFullOuterJoin.

@Test
public void testFullOuterJoin() {
    // select e.id, e.name, d.name as dep_name
    // from emp e
    // full outer join dep d
    // on e.depno = d.depno
    ExecutionContext<Object[]> ctx = executionContext(true);
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, String.class, Integer.class);
    ScanNode<Object[]> persons = new ScanNode<>(ctx, rowType, Arrays.asList(new Object[] { 0, "Igor", 1 }, new Object[] { 1, "Roman", 2 }, new Object[] { 2, "Ivan", null }, new Object[] { 3, "Alexey", 1 }));
    rowType = TypeUtils.createRowType(tf, int.class, String.class);
    ScanNode<Object[]> deps = new ScanNode<>(ctx, rowType, Arrays.asList(new Object[] { 1, "Core" }, new Object[] { 2, "SQL" }, new Object[] { 3, "QA" }));
    RelDataType outType = TypeUtils.createRowType(ctx.getTypeFactory(), int.class, String.class, Integer.class, int.class, String.class);
    RelDataType leftType = TypeUtils.createRowType(ctx.getTypeFactory(), int.class, String.class, Integer.class);
    RelDataType rightType = TypeUtils.createRowType(ctx.getTypeFactory(), int.class, String.class);
    RowHandler<Object[]> hnd = ctx.rowHandler();
    NestedLoopJoinNode<Object[]> join = NestedLoopJoinNode.create(ctx, outType, leftType, rightType, FULL, (r1, r2) -> getFieldFromBiRows(hnd, 2, r1, r2) == getFieldFromBiRows(hnd, 3, r1, r2));
    join.register(asList(persons, deps));
    rowType = TypeUtils.createRowType(tf, Integer.class, String.class, String.class);
    ProjectNode<Object[]> project = new ProjectNode<>(ctx, rowType, r -> new Object[] { r[0], r[1], r[4] });
    project.register(join);
    RootNode<Object[]> node = new RootNode<>(ctx, rowType);
    node.register(project);
    assert node.hasNext();
    ArrayList<Object[]> rows = new ArrayList<>();
    while (node.hasNext()) {
        rows.add(node.next());
    }
    assertEquals(5, rows.size());
    assertArrayEquals(new Object[] { 0, "Igor", "Core" }, rows.get(0));
    assertArrayEquals(new Object[] { 1, "Roman", "SQL" }, rows.get(1));
    assertArrayEquals(new Object[] { 2, "Ivan", null }, rows.get(2));
    assertArrayEquals(new Object[] { 3, "Alexey", "Core" }, rows.get(3));
    assertArrayEquals(new Object[] { null, null, "QA" }, rows.get(4));
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with IgniteTypeFactory

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

the class ExecutionTest method testSemiJoin.

@Test
public void testSemiJoin() {
    // select d.name as dep_name
    // from dep d
    // semi join emp e
    // on e.depno = d.depno
    ExecutionContext<Object[]> ctx = executionContext(true);
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, String.class, Integer.class);
    ScanNode<Object[]> persons = new ScanNode<>(ctx, rowType, Arrays.asList(new Object[] { 0, "Igor", 1 }, new Object[] { 1, "Roman", 2 }, new Object[] { 2, "Ivan", null }, new Object[] { 3, "Alexey", 1 }));
    rowType = TypeUtils.createRowType(tf, int.class, String.class);
    ScanNode<Object[]> deps = new ScanNode<>(ctx, rowType, Arrays.asList(new Object[] { 1, "Core" }, new Object[] { 2, "SQL" }, new Object[] { 3, "QA" }));
    RelDataType outType = TypeUtils.createRowType(ctx.getTypeFactory(), int.class, String.class, Integer.class);
    RelDataType leftType = TypeUtils.createRowType(ctx.getTypeFactory(), int.class, String.class, Integer.class);
    RelDataType rightType = TypeUtils.createRowType(ctx.getTypeFactory(), int.class, String.class);
    RowHandler<Object[]> hnd = ctx.rowHandler();
    NestedLoopJoinNode<Object[]> join = NestedLoopJoinNode.create(ctx, outType, leftType, rightType, SEMI, (r1, r2) -> getFieldFromBiRows(hnd, 0, r1, r2) == getFieldFromBiRows(hnd, 4, r1, r2));
    join.register(asList(deps, persons));
    rowType = TypeUtils.createRowType(tf, String.class);
    ProjectNode<Object[]> project = new ProjectNode<>(ctx, rowType, r -> new Object[] { r[1] });
    project.register(join);
    RootNode<Object[]> node = new RootNode<>(ctx, rowType);
    node.register(project);
    assert node.hasNext();
    ArrayList<Object[]> rows = new ArrayList<>();
    while (node.hasNext()) {
        rows.add(node.next());
    }
    assertEquals(2, rows.size());
    assertArrayEquals(new Object[] { "Core" }, rows.get(0));
    assertArrayEquals(new Object[] { "SQL" }, rows.get(1));
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with IgniteTypeFactory

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

the class ExecutionTest method testMergeJoin.

@Test
public void testMergeJoin() {
    ExecutionContext<Object[]> ctx = executionContext(true);
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, String.class, int.class);
    int inBufSize = Commons.IN_BUFFER_SIZE;
    int[] sizes = { 1, max(inBufSize / 3, 1), max(inBufSize / 2, 1), max(inBufSize - 1, 1), inBufSize, inBufSize + 1, 2 * inBufSize - 1, 2 * inBufSize, 2 * inBufSize + 1 };
    for (int leftSize : sizes) {
        for (int rightSize : sizes) {
            log.info("Check: leftSize=" + leftSize + ", rightSize=" + rightSize);
            ScanNode<Object[]> left = new ScanNode<>(ctx, rowType, new TestTable(leftSize, rowType));
            ScanNode<Object[]> right = new ScanNode<>(ctx, rowType, new TestTable(rightSize, rowType));
            RelDataType joinRowType = TypeUtils.createRowType(tf, int.class, String.class, int.class, int.class, String.class, int.class);
            MergeJoinNode<Object[]> join = MergeJoinNode.create(ctx, joinRowType, null, null, INNER, (r1, r2) -> {
                Object o1 = r1[0];
                Object o2 = r2[0];
                if (o1 == null || o2 == null) {
                    if (o1 != null) {
                        return 1;
                    } else if (o2 != null) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
                return Integer.compare((Integer) o1, (Integer) o2);
            });
            join.register(Arrays.asList(left, right));
            RootNode<Object[]> root = new RootNode<>(ctx, joinRowType);
            root.register(join);
            int cnt = 0;
            while (root.hasNext()) {
                root.next();
                cnt++;
            }
            assertEquals(min(leftSize, rightSize), cnt, "Invalid result size. [left=" + leftSize + ", right=" + rightSize + ", results=" + cnt);
        }
    }
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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