Search in sources :

Example 1 with FunctionExpression

use of io.crate.operation.aggregation.FunctionExpression in project crate by crate.

the class BaseImplementationSymbolVisitor method visitFunction.

@Override
public Input<?> visitFunction(Function function, C context) {
    final FunctionImplementation functionImplementation = functions.get(function.info().ident());
    if (functionImplementation instanceof Scalar<?, ?>) {
        List<Symbol> arguments = function.arguments();
        Scalar<?, ?> scalarImpl = ((Scalar) functionImplementation).compile(arguments);
        Input[] argumentInputs = new Input[arguments.size()];
        int i = 0;
        for (Symbol argument : function.arguments()) {
            argumentInputs[i++] = process(argument, context);
        }
        return new FunctionExpression<>(scalarImpl, argumentInputs);
    } else {
        throw new IllegalArgumentException(SymbolFormatter.format("Cannot find implementation for function %s", function));
    }
}
Also used : Input(io.crate.data.Input) FunctionExpression(io.crate.operation.aggregation.FunctionExpression) FunctionImplementation(io.crate.metadata.FunctionImplementation) Scalar(io.crate.metadata.Scalar)

Example 2 with FunctionExpression

use of io.crate.operation.aggregation.FunctionExpression 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 3 with FunctionExpression

use of io.crate.operation.aggregation.FunctionExpression in project crate by crate.

the class SimpleTopNProjectorTest method testFunctionExpression.

@Test
public void testFunctionExpression() throws Throwable {
    Scalar floor = (Scalar) TestingHelpers.getFunctions().get(new FunctionIdent("floor", Collections.<DataType>singletonList(DataTypes.DOUBLE)));
    FunctionExpression<Number, ?> funcExpr = new FunctionExpression<>(floor, new Input[] { input });
    Projector projector = new SimpleTopNProjector(ImmutableList.<Input<?>>of(funcExpr), COLLECT_EXPRESSIONS, 10, TopN.NO_OFFSET);
    Iterable<Row> rows = RowGenerator.fromSingleColValues(() -> IntStream.range(0, 12).mapToDouble(i -> 42.3d).iterator());
    BatchIterator batchIterator = projector.apply(RowsBatchIterator.newInstance(rows, 1));
    consumer.accept(batchIterator, null);
    Bucket result = consumer.getBucket();
    assertThat(result.size(), is(10));
    assertThat(result.iterator().next(), isRow(42L));
}
Also used : FunctionIdent(io.crate.metadata.FunctionIdent) FunctionExpression(io.crate.operation.aggregation.FunctionExpression) TestingHelpers.isRow(io.crate.testing.TestingHelpers.isRow) Scalar(io.crate.metadata.Scalar) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

FunctionExpression (io.crate.operation.aggregation.FunctionExpression)3 Input (io.crate.data.Input)2 FunctionImplementation (io.crate.metadata.FunctionImplementation)2 Scalar (io.crate.metadata.Scalar)2 CrateUnitTest (io.crate.test.integration.CrateUnitTest)2 Test (org.junit.Test)2 FunctionIdent (io.crate.metadata.FunctionIdent)1 AddFunction (io.crate.operation.scalar.arithmetic.AddFunction)1 TestingHelpers.isRow (io.crate.testing.TestingHelpers.isRow)1