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));
}
}
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);
}
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)));
}
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));
}
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());
}
Aggregations