Search in sources :

Example 61 with ImmutableBitSet

use of org.apache.calcite.util.ImmutableBitSet in project ignite-3 by apache.

the class SortAggregateExecutionTest method createSingleAggregateNodesChain.

/**
 * {@inheritDoc}
 */
@Override
protected SingleNode<Object[]> createSingleAggregateNodesChain(ExecutionContext<Object[]> ctx, List<ImmutableBitSet> grpSets, AggregateCall call, RelDataType inRowType, RelDataType aggRowType, RowHandler.RowFactory<Object[]> rowFactory, ScanNode<Object[]> scan) {
    assert grpSets.size() == 1;
    ImmutableBitSet grpSet = first(grpSets);
    assert !grpSet.isEmpty() : "Not applicable for sort aggregate";
    RelCollation collation = RelCollations.of(ImmutableIntList.copyOf(grpSet.asList()));
    Comparator<Object[]> cmp = ctx.expressionFactory().comparator(collation);
    SortNode<Object[]> sort = new SortNode<>(ctx, inRowType, cmp);
    sort.register(scan);
    SortAggregateNode<Object[]> agg = new SortAggregateNode<>(ctx, aggRowType, SINGLE, grpSet, accFactory(ctx, call, SINGLE, inRowType), rowFactory, cmp);
    agg.register(sort);
    return agg;
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet)

Example 62 with ImmutableBitSet

use of org.apache.calcite.util.ImmutableBitSet in project ignite-3 by apache.

the class SortAggregateExecutionTest method createMapReduceAggregateNodesChain.

/**
 * {@inheritDoc}
 */
@Override
protected SingleNode<Object[]> createMapReduceAggregateNodesChain(ExecutionContext<Object[]> ctx, List<ImmutableBitSet> grpSets, AggregateCall call, RelDataType inRowType, RelDataType aggRowType, RowHandler.RowFactory<Object[]> rowFactory, ScanNode<Object[]> scan) {
    assert grpSets.size() == 1;
    ImmutableBitSet grpSet = first(grpSets);
    assert !grpSet.isEmpty() : "Not applicable for sort aggregate";
    RelCollation collation = RelCollations.of(ImmutableIntList.copyOf(grpSet.asList()));
    Comparator<Object[]> cmp = ctx.expressionFactory().comparator(collation);
    SortNode<Object[]> sort = new SortNode<>(ctx, inRowType, cmp);
    sort.register(scan);
    SortAggregateNode<Object[]> aggMap = new SortAggregateNode<>(ctx, aggRowType, MAP, grpSet, accFactory(ctx, call, MAP, inRowType), rowFactory, cmp);
    aggMap.register(sort);
    // The group's fields placed on the begin of the output row (planner
    // does this by Projection node for aggregate input).
    // Hash aggregate doesn't use groups set on reducer because send GroupKey as object.
    ImmutableIntList reduceGrpFields = ImmutableIntList.copyOf(IntStream.range(0, grpSet.cardinality()).boxed().collect(Collectors.toList()));
    RelCollation rdcCollation = RelCollations.of(reduceGrpFields);
    Comparator<Object[]> rdcCmp = ctx.expressionFactory().comparator(rdcCollation);
    SortAggregateNode<Object[]> aggRdc = new SortAggregateNode<>(ctx, aggRowType, REDUCE, ImmutableBitSet.of(reduceGrpFields), accFactory(ctx, call, REDUCE, aggRowType), rowFactory, rdcCmp);
    aggRdc.register(aggMap);
    return aggRdc;
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableIntList(org.apache.calcite.util.ImmutableIntList)

Example 63 with ImmutableBitSet

use of org.apache.calcite.util.ImmutableBitSet 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 64 with ImmutableBitSet

use of org.apache.calcite.util.ImmutableBitSet 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 65 with ImmutableBitSet

use of org.apache.calcite.util.ImmutableBitSet in project ignite-3 by apache.

the class HashAggregateExecutionTest method countOfEmptyWithRewind.

/**
 * Test verifies that after rewind all groups are properly initialized.
 */
@ParameterizedTest
@EnumSource
public void countOfEmptyWithRewind(TestAggregateType testAgg) {
    ExecutionContext<Object[]> ctx = executionContext();
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, int.class);
    ScanNode<Object[]> scan = new ScanNode<>(ctx, rowType, Collections.emptyList());
    AggregateCall call = AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, false, ImmutableIntList.of(), -1, null, RelCollations.EMPTY, tf.createJavaType(int.class), null);
    ImmutableList<ImmutableBitSet> grpSets = ImmutableList.of(ImmutableBitSet.of());
    RelDataType aggRowType = TypeUtils.createRowType(tf, int.class);
    SingleNode<Object[]> aggChain = createAggregateNodesChain(testAgg, ctx, grpSets, call, rowType, aggRowType, rowFactory(), scan);
    for (int i = 0; i < 2; i++) {
        RootNode<Object[]> root = new RootNode<>(ctx, aggRowType) {

            /**
             * {@inheritDoc}
             */
            @Override
            public void close() {
            // NO-OP
            }
        };
        root.register(aggChain);
        assertTrue(root.hasNext());
        assertArrayEquals(row(0), root.next());
        assertFalse(root.hasNext());
        aggChain.rewind();
    }
}
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)

Aggregations

ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)365 RexNode (org.apache.calcite.rex.RexNode)196 RelNode (org.apache.calcite.rel.RelNode)183 ArrayList (java.util.ArrayList)179 AggregateCall (org.apache.calcite.rel.core.AggregateCall)116 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)98 RexBuilder (org.apache.calcite.rex.RexBuilder)92 RelDataType (org.apache.calcite.rel.type.RelDataType)89 RexInputRef (org.apache.calcite.rex.RexInputRef)79 HashMap (java.util.HashMap)68 Aggregate (org.apache.calcite.rel.core.Aggregate)62 RelBuilder (org.apache.calcite.tools.RelBuilder)59 Pair (org.apache.calcite.util.Pair)57 List (java.util.List)49 ImmutableList (com.google.common.collect.ImmutableList)48 HashSet (java.util.HashSet)45 LinkedHashSet (java.util.LinkedHashSet)45 Join (org.apache.calcite.rel.core.Join)45 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)45 Nullable (org.checkerframework.checker.nullness.qual.Nullable)43