Search in sources :

Example 11 with Function

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));
}
Also used : AggregationContext(io.crate.execution.engine.aggregation.AggregationContext) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Aggregation(io.crate.expression.symbol.Aggregation) Function(io.crate.expression.symbol.Function) Input(io.crate.data.Input) RowN(io.crate.data.RowN) InputColumn(io.crate.expression.symbol.InputColumn) Row(io.crate.data.Row) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 12 with Function

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);
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) AggregationContext(io.crate.execution.engine.aggregation.AggregationContext) Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) CollectExpression(io.crate.execution.engine.collect.CollectExpression) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 13 with Function

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)));
}
Also used : Function(io.crate.expression.symbol.Function) Input(io.crate.data.Input) FunctionImplementation(io.crate.metadata.FunctionImplementation) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 14 with Function

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();
}
Also used : InputFactory(io.crate.expression.InputFactory) Function(io.crate.expression.symbol.Function) InputColumn(io.crate.expression.symbol.InputColumn) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Before(org.junit.Before)

Example 15 with Function

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()));
}
Also used : SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) Function(io.crate.expression.symbol.Function) DocTableInfo(io.crate.metadata.doc.DocTableInfo) SQLExecutor(io.crate.testing.SQLExecutor) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableStats(io.crate.statistics.TableStats) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

Function (io.crate.expression.symbol.Function)80 Test (org.junit.Test)56 Symbol (io.crate.expression.symbol.Symbol)43 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)43 SymbolMatchers.isFunction (io.crate.testing.SymbolMatchers.isFunction)35 SubscriptFunction (io.crate.expression.scalar.SubscriptFunction)25 TryCastFunction (io.crate.expression.scalar.cast.TryCastFunction)24 ExplicitCastFunction (io.crate.expression.scalar.cast.ExplicitCastFunction)23 DistanceFunction (io.crate.expression.scalar.geo.DistanceFunction)23 AliasSymbol (io.crate.expression.symbol.AliasSymbol)13 ParameterSymbol (io.crate.expression.symbol.ParameterSymbol)13 SelectSymbol (io.crate.expression.symbol.SelectSymbol)13 InputColumn (io.crate.expression.symbol.InputColumn)10 ArrayList (java.util.ArrayList)10 Literal (io.crate.expression.symbol.Literal)9 ArraySliceFunction (io.crate.expression.scalar.ArraySliceFunction)8 FunctionImplementation (io.crate.metadata.FunctionImplementation)8 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)7 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)6 Reference (io.crate.metadata.Reference)6