Search in sources :

Example 11 with FunctionImplementation

use of io.crate.metadata.FunctionImplementation in project crate by crate.

the class StdDevAggregationTest method test_functions_return_type_is_always_double_for_any_argument_type.

@Test
public void test_functions_return_type_is_always_double_for_any_argument_type() {
    for (DataType<?> type : Stream.concat(DataTypes.NUMERIC_PRIMITIVE_TYPES.stream(), Stream.of(DataTypes.TIMESTAMPZ)).collect(Collectors.toList())) {
        FunctionImplementation stddev = nodeCtx.functions().get(null, StandardDeviationAggregation.NAME, List.of(Literal.of(type, null)), SearchPath.pathWithPGCatalogAndDoc());
        assertThat(stddev.boundSignature().getReturnType().createType(), is(DataTypes.DOUBLE));
    }
}
Also used : FunctionImplementation(io.crate.metadata.FunctionImplementation) Test(org.junit.Test)

Example 12 with FunctionImplementation

use of io.crate.metadata.FunctionImplementation in project crate by crate.

the class AbstractWindowFunctionTest method assertEvaluate.

@SuppressWarnings("unchecked")
protected <T> void assertEvaluate(String functionExpression, Matcher<T> expectedValue, List<ColumnIdent> rowsColumnDescription, Object[]... inputRows) throws Throwable {
    performInputSanityChecks(inputRows);
    Symbol normalizedFunctionSymbol = sqlExpressions.normalize(sqlExpressions.asSymbol(functionExpression));
    assertThat(normalizedFunctionSymbol, instanceOf(io.crate.expression.symbol.WindowFunction.class));
    var windowFunctionSymbol = (io.crate.expression.symbol.WindowFunction) normalizedFunctionSymbol;
    ReferenceResolver<InputCollectExpression> referenceResolver = r -> new InputCollectExpression(rowsColumnDescription.indexOf(r.column()));
    var sourceSymbols = Lists2.map(rowsColumnDescription, x -> sqlExpressions.normalize(sqlExpressions.asSymbol(x.sqlFqn())));
    ensureInputRowsHaveCorrectType(sourceSymbols, inputRows);
    var argsCtx = inputFactory.ctxForRefs(txnCtx, referenceResolver);
    argsCtx.add(windowFunctionSymbol.arguments());
    FunctionImplementation impl = sqlExpressions.nodeCtx.functions().getQualified(windowFunctionSymbol, txnCtx.sessionSettings().searchPath());
    assert impl instanceof WindowFunction || impl instanceof AggregationFunction : "Got " + impl + " but expected a window function";
    WindowFunction windowFunctionImpl;
    if (impl instanceof AggregationFunction) {
        windowFunctionImpl = new AggregateToWindowFunctionAdapter((AggregationFunction) impl, new ExpressionsInput<>(Literal.BOOLEAN_TRUE, List.of()), Version.CURRENT, RamAccounting.NO_ACCOUNTING, memoryManager, Version.CURRENT);
    } else {
        windowFunctionImpl = (WindowFunction) impl;
    }
    int numCellsInSourceRows = inputRows[0].length;
    var windowDef = windowFunctionSymbol.windowDefinition();
    var partitionOrderBy = windowDef.partitions().isEmpty() ? null : new OrderBy(windowDef.partitions());
    Comparator<Object[]> cmpOrderBy = createComparator(() -> inputFactory.ctxForRefs(txnCtx, referenceResolver), windowDef.orderBy());
    InputColumns.SourceSymbols inputColSources = new InputColumns.SourceSymbols(sourceSymbols);
    var mappedWindowDef = windowDef.map(s -> InputColumns.create(s, inputColSources));
    BatchIterator<Row> iterator = WindowFunctionBatchIterator.of(InMemoryBatchIterator.of(Arrays.stream(inputRows).map(RowN::new).collect(Collectors.toList()), SENTINEL, true), new IgnoreRowAccounting(), WindowProjector.createComputeStartFrameBoundary(numCellsInSourceRows, txnCtx, sqlExpressions.nodeCtx, mappedWindowDef, cmpOrderBy), WindowProjector.createComputeEndFrameBoundary(numCellsInSourceRows, txnCtx, sqlExpressions.nodeCtx, mappedWindowDef, cmpOrderBy), createComparator(() -> inputFactory.ctxForRefs(txnCtx, referenceResolver), partitionOrderBy), cmpOrderBy, numCellsInSourceRows, () -> 1, Runnable::run, List.of(windowFunctionImpl), argsCtx.expressions(), new Boolean[] { windowFunctionSymbol.ignoreNulls() }, argsCtx.topLevelInputs().toArray(new Input[0]));
    List<Object> actualResult;
    try {
        actualResult = BatchIterators.collect(iterator, Collectors.mapping(row -> row.get(numCellsInSourceRows), Collectors.toList())).get(5, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        throw e.getCause();
    }
    assertThat((T) actualResult, expectedValue);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) SqlExpressions(io.crate.testing.SqlExpressions) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) RelationName(io.crate.metadata.RelationName) SENTINEL(io.crate.data.SentinelRow.SENTINEL) BatchIterator(io.crate.data.BatchIterator) ReferenceResolver(io.crate.expression.reference.ReferenceResolver) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RowN(io.crate.data.RowN) Map(java.util.Map) SQLExecutor(io.crate.testing.SQLExecutor) Before(org.junit.Before) DocTableInfo(io.crate.metadata.doc.DocTableInfo) User(io.crate.user.User) AbstractModule(org.elasticsearch.common.inject.AbstractModule) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) ColumnIdent(io.crate.metadata.ColumnIdent) Comparators.createComparator(io.crate.execution.engine.sort.Comparators.createComparator) RamAccounting(io.crate.breaker.RamAccounting) Collectors(java.util.stream.Collectors) Lists2(io.crate.common.collections.Lists2) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ExpressionsInput(io.crate.expression.ExpressionsInput) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) BatchIterators(io.crate.data.BatchIterators) Version(org.elasticsearch.Version) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) Matcher(org.hamcrest.Matcher) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) OnHeapMemoryManager(io.crate.memory.OnHeapMemoryManager) InputFactory(io.crate.expression.InputFactory) Comparator(java.util.Comparator) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Symbol(io.crate.expression.symbol.Symbol) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) Input(io.crate.data.Input) ExpressionsInput(io.crate.expression.ExpressionsInput) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) FunctionImplementation(io.crate.metadata.FunctionImplementation) ExecutionException(java.util.concurrent.ExecutionException) OrderBy(io.crate.analyze.OrderBy) InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) Row(io.crate.data.Row) ExpressionsInput(io.crate.expression.ExpressionsInput)

Example 13 with FunctionImplementation

use of io.crate.metadata.FunctionImplementation 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(i -> Literal.of("foo"));
    Input<?> input = ctx.add(function);
    FunctionExpression expression = (FunctionExpression) input;
    java.lang.reflect.Field f = FunctionExpression.class.getDeclaredField("functionImplementation");
    f.setAccessible(true);
    FunctionImplementation impl = (FunctionImplementation) f.get(expression);
    assertThat(impl.info(), is(function.info()));
    FunctionImplementation uncompiled = expressions.functions().get(function.info().ident());
    assertThat(uncompiled, not(sameInstance(impl)));
}
Also used : AddFunction(io.crate.operation.scalar.arithmetic.AddFunction) Input(io.crate.data.Input) FunctionExpression(io.crate.operation.aggregation.FunctionExpression) FunctionImplementation(io.crate.metadata.FunctionImplementation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 14 with FunctionImplementation

use of io.crate.metadata.FunctionImplementation in project crate by crate.

the class WindowAggProjectionSerialisationTest method testWindowAggProjectionSerialisation.

@Test
public void testWindowAggProjectionSerialisation() throws IOException {
    FunctionImplementation sumFunctionImpl = getSumFunction();
    WindowDefinition partitionByOneWindowDef = new WindowDefinition(singletonList(Literal.of(1L)), null, null);
    WindowDefinition partitionByTwoWindowDef = new WindowDefinition(singletonList(Literal.of(2L)), null, null);
    WindowFunction firstWindowFunction = new WindowFunction(sumFunctionImpl.signature(), singletonList(Literal.of(1L)), sumFunctionImpl.boundSignature().getReturnType().createType(), null, partitionByOneWindowDef, true);
    WindowFunction secondWindowFunction = new WindowFunction(sumFunctionImpl.signature(), singletonList(Literal.of(2L)), sumFunctionImpl.boundSignature().getReturnType().createType(), null, partitionByTwoWindowDef, null);
    Symbol standaloneInput = Literal.of(42L);
    var expectedWindowAggProjection = new WindowAggProjection(partitionByOneWindowDef, List.of(firstWindowFunction, secondWindowFunction), List.of(standaloneInput));
    var output = new BytesStreamOutput();
    expectedWindowAggProjection.writeTo(output);
    var in = output.bytes().streamInput();
    var actualWindowAggProjection = new WindowAggProjection(in);
    assertThat(actualWindowAggProjection.outputs(), contains(standaloneInput, firstWindowFunction, secondWindowFunction));
    assertThat(actualWindowAggProjection, is(expectedWindowAggProjection));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) WindowDefinition(io.crate.analyze.WindowDefinition) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 15 with FunctionImplementation

use of io.crate.metadata.FunctionImplementation in project crate by crate.

the class CollectSetAggregationTest method testReturnType.

@Test
public void testReturnType() throws Exception {
    FunctionImplementation collectSet = nodeCtx.functions().get(null, "collect_set", List.of(Literal.of(DataTypes.INTEGER, null)), SearchPath.pathWithPGCatalogAndDoc());
    assertEquals(new ArrayType<>(DataTypes.INTEGER), collectSet.info().returnType());
}
Also used : FunctionImplementation(io.crate.metadata.FunctionImplementation) Test(org.junit.Test)

Aggregations

FunctionImplementation (io.crate.metadata.FunctionImplementation)21 Symbol (io.crate.expression.symbol.Symbol)11 Test (org.junit.Test)11 Input (io.crate.data.Input)9 Function (io.crate.expression.symbol.Function)6 Literal (io.crate.expression.symbol.Literal)5 List (java.util.List)5 TransactionContext (io.crate.metadata.TransactionContext)4 Map (java.util.Map)4 Lists2 (io.crate.common.collections.Lists2)3 ArrayFunction (io.crate.expression.scalar.arithmetic.ArrayFunction)3 ArrayList (java.util.ArrayList)3 OrderBy (io.crate.analyze.OrderBy)2 ParamTypeHints (io.crate.analyze.ParamTypeHints)2 WindowDefinition (io.crate.analyze.WindowDefinition)2 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 Function (io.crate.analyze.symbol.Function)2 Literal (io.crate.analyze.symbol.Literal)2 RamAccounting (io.crate.breaker.RamAccounting)2