use of io.crate.expression.symbol.InputColumn in project crate by crate.
the class InputFactoryTest method testAggregationSymbolsInputReuse.
@Test
public void testAggregationSymbolsInputReuse() throws Exception {
Function countX = (Function) expressions.asSymbol("count(x)");
Function avgX = (Function) expressions.asSymbol("avg(x)");
List<Symbol> aggregations = Arrays.asList(new Aggregation(countX.signature(), countX.signature().getReturnType().createType(), List.of(new InputColumn(0))), new Aggregation(avgX.signature(), avgX.signature().getReturnType().createType(), List.of(new InputColumn(0))));
InputFactory.Context<CollectExpression<Row, ?>> ctx = factory.ctxForAggregations(txnCtx);
ctx.add(aggregations);
List<AggregationContext> aggregationContexts = ctx.aggregations();
Input<?> inputCount = aggregationContexts.get(0).inputs()[0];
Input<?> inputAverage = aggregationContexts.get(1).inputs()[0];
assertSame(inputCount, inputAverage);
}
use of io.crate.expression.symbol.InputColumn in project crate by crate.
the class MapRowUsingInputsTest method createInputs.
@Before
public void createInputs() throws Exception {
InputFactory inputFactory = new InputFactory(createNodeContext());
InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(txnCtx);
var addFunction = new Function(Signature.scalar(ArithmeticFunctions.Names.ADD, DataTypes.LONG.getTypeSignature(), DataTypes.LONG.getTypeSignature(), DataTypes.LONG.getTypeSignature()).withFeatures(Scalar.DETERMINISTIC_AND_COMPARISON_REPLACEMENT), List.of(new InputColumn(0, DataTypes.LONG), Literal.of(2L)), DataTypes.LONG);
inputs = Collections.singletonList(ctx.add(addFunction));
expressions = ctx.expressions();
}
use of io.crate.expression.symbol.InputColumn in project crate by crate.
the class GroupByPlannerTest method testGroupByWithHavingAndNoSelectListReordering.
@Test
public void testGroupByWithHavingAndNoSelectListReordering() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
Merge planNode = e.plan("select name, count(*) from users group by name having count(*) > 1");
Merge reducerMerge = (Merge) planNode.subPlan();
MergePhase reduceMergePhase = reducerMerge.mergePhase();
// group projection
// outputs: name, count(*)
// filter projection
// outputs: name, count(*)
assertThat(reduceMergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(FilterProjection.class)));
Projection projection = reduceMergePhase.projections().get(1);
assertThat(projection, instanceOf(FilterProjection.class));
FilterProjection filterProjection = (FilterProjection) projection;
Symbol countArgument = ((Function) filterProjection.query()).arguments().get(0);
assertThat(countArgument, instanceOf(InputColumn.class));
// pointing to second output from group projection
assertThat(((InputColumn) countArgument).index(), is(1));
// outputs: name, count(*)
assertThat(((InputColumn) filterProjection.outputs().get(0)).index(), is(0));
assertThat(((InputColumn) filterProjection.outputs().get(1)).index(), is(1));
MergePhase localMerge = planNode.mergePhase();
assertThat(localMerge.projections(), empty());
}
use of io.crate.expression.symbol.InputColumn in project crate by crate.
the class GroupByPlannerTest method testGroupByWithAggregationPlan.
@Test
public void testGroupByWithAggregationPlan() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
Merge distributedGroupByMerge = e.plan("select count(*), name from users group by name");
Merge reducerMerge = (Merge) distributedGroupByMerge.subPlan();
// distributed collect
RoutedCollectPhase collectPhase = ((RoutedCollectPhase) ((Collect) reducerMerge.subPlan()).collectPhase());
assertThat(collectPhase.maxRowGranularity(), is(RowGranularity.DOC));
assertThat(collectPhase.nodeIds().size(), is(2));
assertThat(collectPhase.toCollect().size(), is(1));
assertThat(collectPhase.projections().size(), is(1));
assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
assertThat(collectPhase.outputTypes().size(), is(2));
assertEquals(DataTypes.STRING, collectPhase.outputTypes().get(0));
assertEquals(CountAggregation.LongStateType.INSTANCE, collectPhase.outputTypes().get(1));
MergePhase mergePhase = reducerMerge.mergePhase();
assertThat(mergePhase.numUpstreams(), is(2));
assertThat(mergePhase.nodeIds().size(), is(2));
assertEquals(mergePhase.inputTypes(), collectPhase.outputTypes());
// for function evaluation and column-reordering there is always a EvalProjection
assertThat(mergePhase.projections().size(), is(2));
assertThat(mergePhase.projections().get(1), instanceOf(EvalProjection.class));
assertThat(mergePhase.projections().get(0), instanceOf(GroupProjection.class));
GroupProjection groupProjection = (GroupProjection) mergePhase.projections().get(0);
InputColumn inputColumn = (InputColumn) groupProjection.values().get(0).inputs().get(0);
assertThat(inputColumn.index(), is(1));
assertThat(mergePhase.outputTypes().size(), is(2));
assertEquals(DataTypes.LONG, mergePhase.outputTypes().get(0));
assertEquals(DataTypes.STRING, mergePhase.outputTypes().get(1));
MergePhase localMerge = distributedGroupByMerge.mergePhase();
assertThat(localMerge.numUpstreams(), is(2));
assertThat(localMerge.nodeIds().size(), is(1));
assertThat(localMerge.nodeIds().iterator().next(), is(NODE_ID));
assertEquals(mergePhase.outputTypes(), localMerge.inputTypes());
assertThat(localMerge.projections(), empty());
}
use of io.crate.expression.symbol.InputColumn in project crate by crate.
the class GroupByPlannerTest method testGroupByHavingAndNoSelectListReOrderingWithLimit.
@Test
public void testGroupByHavingAndNoSelectListReOrderingWithLimit() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
Merge planNode = e.plan("select name, count(*) from users group by name having count(*) > 1 limit 100");
Merge reducerMerge = (Merge) planNode.subPlan();
MergePhase reducePhase = reducerMerge.mergePhase();
// group projection
// outputs: name, count(*)
// filter projection
// outputs: name, count(*)
// topN projection
// outputs: name, count(*)
Projection projection = reducePhase.projections().get(1);
assertThat(projection, instanceOf(FilterProjection.class));
FilterProjection filterProjection = (FilterProjection) projection;
Symbol countArgument = ((Function) filterProjection.query()).arguments().get(0);
assertThat(countArgument, instanceOf(InputColumn.class));
// pointing to second output from group projection
assertThat(((InputColumn) countArgument).index(), is(1));
// outputs: name, count(*)
assertThat(((InputColumn) filterProjection.outputs().get(0)).index(), is(0));
assertThat(((InputColumn) filterProjection.outputs().get(1)).index(), is(1));
// outputs: name, count(*)
TopNProjection topN = (TopNProjection) reducePhase.projections().get(2);
assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
MergePhase localMerge = planNode.mergePhase();
// topN projection
// outputs: name, count(*)
topN = (TopNProjection) localMerge.projections().get(0);
assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
}
Aggregations