use of io.crate.expression.symbol.Function in project crate by crate.
the class InputFactoryTest method testProcessGroupByProjectionSymbolsAggregation.
@Test
public void testProcessGroupByProjectionSymbolsAggregation() throws Exception {
// select count(x), x, y * 2 ... group by x, y * 2
// keys: [ in(0), in(1) + 10 ]
List<Symbol> keys = Arrays.asList(new InputColumn(0, DataTypes.LONG), add);
Function countX = (Function) expressions.asSymbol("count(x)");
// values: [ count(in(0)) ]
List<Aggregation> values = List.of(new Aggregation(countX.signature(), countX.valueType(), List.of(new InputColumn(0))));
InputFactory.Context<CollectExpression<Row, ?>> ctx = factory.ctxForAggregations(txnCtx);
ctx.add(keys);
// inputs: [ x, add ]
List<Input<?>> keyInputs = ctx.topLevelInputs();
ctx.add(values);
List<AggregationContext> aggregations = ctx.aggregations();
assertThat(aggregations.size(), is(1));
// collectExpressions: [ in0, in1 ]
List<CollectExpression<Row, ?>> expressions = new ArrayList<>(ctx.expressions());
assertThat(expressions.size(), is(2));
List<Input<?>> allInputs = ctx.topLevelInputs();
// only 2 because count is no input
assertThat(allInputs.size(), is(2));
RowN row = new RowN(1L, 2L);
for (CollectExpression<Row, ?> expression : expressions) {
expression.setNextRow(row);
}
assertThat(expressions.get(0).value(), is(1L));
// raw input value
assertThat(expressions.get(1).value(), is(2L));
assertThat(keyInputs.size(), is(2));
assertThat(keyInputs.get(0).value(), is(1L));
// 2 + 10
assertThat(keyInputs.get(1).value(), is(12));
}
use of io.crate.expression.symbol.Function 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.Function in project crate by crate.
the class InputFactoryTest method testCompiled.
@Test
public void testCompiled() throws Exception {
Function function = (Function) expressions.normalize(expressions.asSymbol("a like 'f%'"));
InputFactory.Context<Input<?>> ctx = factory.ctxForRefs(txnCtx, i -> Literal.of("foo"));
Input<?> input = ctx.add(function);
FunctionExpression expression = (FunctionExpression) input;
java.lang.reflect.Field f = FunctionExpression.class.getDeclaredField("scalar");
f.setAccessible(true);
FunctionImplementation impl = (FunctionImplementation) f.get(expression);
assertThat(impl.signature(), is(function.signature()));
FunctionImplementation uncompiled = expressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
assertThat(uncompiled, not(sameInstance(impl)));
}
use of io.crate.expression.symbol.Function 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.Function in project crate by crate.
the class FetchRewriteTest method test_fetch_rewrite_on_eval_removes_eval_and_extends_replaced_outputs.
@Test
public void test_fetch_rewrite_on_eval_removes_eval_and_extends_replaced_outputs() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
DocTableInfo tableInfo = e.resolveTableInfo("tbl");
var x = e.asSymbol("x");
var relation = new DocTableRelation(tableInfo);
var collect = new Collect(relation, List.of(x), WhereClause.MATCH_ALL, 1L, DataTypes.INTEGER.fixedSize());
var eval = new Eval(collect, List.of(new Function(Signature.scalar("add", DataTypes.INTEGER.getTypeSignature(), DataTypes.INTEGER.getTypeSignature(), DataTypes.INTEGER.getTypeSignature()), List.of(x, x), DataTypes.INTEGER)));
FetchRewrite fetchRewrite = eval.rewriteToFetch(new TableStats(), List.of());
assertThat(fetchRewrite, Matchers.notNullValue());
assertThat(fetchRewrite.newPlan(), isPlan("Collect[doc.tbl | [_fetchid] | true]"));
assertThat(fetchRewrite.replacedOutputs(), Matchers.hasEntry(isFunction("add", isReference("x"), isReference("x")), isFunction("add", isFetchStub("_doc['x']"), isFetchStub("_doc['x']"))));
assertThat(List.copyOf(fetchRewrite.replacedOutputs().keySet()), is(eval.outputs()));
}
Aggregations