Search in sources :

Example 1 with FunctionImplementation

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

the class AnyLikeOperatorTest method testNegateLike.

@Test
public void testNegateLike() throws Exception {
    Literal patternLiteral = Literal.of("A");
    Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), new Object[] { new BytesRef("A"), new BytesRef("B") });
    FunctionImplementation impl = new AnyLikeOperator.AnyLikeResolver().getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));
    Function anyLikeFunction = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    Input<Boolean> normalized = (Input<Boolean>) impl.normalizeSymbol(anyLikeFunction, new TransactionContext(SessionContext.SYSTEM_SESSION));
    assertThat(normalized.value(), is(true));
    assertThat(new NotPredicate().evaluate(normalized), is(false));
}
Also used : ArrayType(io.crate.types.ArrayType) Function(io.crate.analyze.symbol.Function) Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) Literal(io.crate.analyze.symbol.Literal) FunctionImplementation(io.crate.metadata.FunctionImplementation) NotPredicate(io.crate.operation.predicate.NotPredicate) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with FunctionImplementation

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

the class AnyNotLikeOperatorTest method testNegateNotLike.

@Test
public void testNegateNotLike() throws Exception {
    Literal patternLiteral = Literal.of("A");
    Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), new Object[] { new BytesRef("A"), new BytesRef("B") });
    FunctionImplementation impl = new AnyNotLikeOperator.AnyNotLikeResolver().getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));
    Function anyNotLikeFunction = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    Input<Boolean> normalized = (Input<Boolean>) impl.normalizeSymbol(anyNotLikeFunction, new TransactionContext(SessionContext.SYSTEM_SESSION));
    assertThat(normalized.value(), is(true));
    assertThat(new NotPredicate().evaluate(normalized), is(false));
}
Also used : ArrayType(io.crate.types.ArrayType) Function(io.crate.analyze.symbol.Function) Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) Literal(io.crate.analyze.symbol.Literal) FunctionImplementation(io.crate.metadata.FunctionImplementation) NotPredicate(io.crate.operation.predicate.NotPredicate) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with FunctionImplementation

use of io.crate.metadata.FunctionImplementation 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 4 with FunctionImplementation

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

the class AbstractFunctionModule method register.

public void register(Signature signature, BiFunction<Signature, Signature, FunctionImplementation> factory) {
    List<FunctionProvider> functions = functionImplementations.computeIfAbsent(signature.getName(), k -> new ArrayList<>());
    var duplicate = functions.stream().filter(fr -> fr.getSignature().equals(signature)).findFirst();
    if (duplicate.isPresent()) {
        throw new IllegalStateException("A function already exists for signature = " + signature);
    }
    functions.add(new FunctionProvider(signature, factory));
}
Also used : List(java.util.List) FunctionImplementation(io.crate.metadata.FunctionImplementation) AbstractModule(org.elasticsearch.common.inject.AbstractModule) FunctionName(io.crate.metadata.FunctionName) Map(java.util.Map) BiFunction(java.util.function.BiFunction) FunctionProvider(io.crate.metadata.FunctionProvider) HashMap(java.util.HashMap) Signature(io.crate.metadata.functions.Signature) MapBinder(org.elasticsearch.common.inject.multibindings.MapBinder) TypeLiteral(org.elasticsearch.common.inject.TypeLiteral) ArrayList(java.util.ArrayList) FunctionProvider(io.crate.metadata.FunctionProvider)

Example 5 with FunctionImplementation

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

the class WindowProjector method fromProjection.

public static Projector fromProjection(WindowAggProjection projection, NodeContext nodeCtx, InputFactory inputFactory, TransactionContext txnCtx, RamAccounting ramAccounting, MemoryManager memoryManager, Version minNodeVersion, Version indexVersionCreated, IntSupplier numThreads, Executor executor) {
    var windowFunctionSymbols = projection.windowFunctions();
    var numWindowFunctions = windowFunctionSymbols.size();
    assert numWindowFunctions > 0 : "WindowAggProjection must have at least 1 window function.";
    ArrayList<WindowFunction> windowFunctions = new ArrayList<>(numWindowFunctions);
    ArrayList<CollectExpression<Row, ?>> windowFuncArgsExpressions = new ArrayList<>(numWindowFunctions);
    Input[][] windowFuncArgsInputs = new Input[numWindowFunctions][];
    Boolean[] ignoreNulls = new Boolean[numWindowFunctions];
    for (int idx = 0; idx < numWindowFunctions; idx++) {
        var windowFunctionSymbol = windowFunctionSymbols.get(idx);
        InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(txnCtx);
        ctx.add(windowFunctionSymbol.arguments());
        FunctionImplementation impl = nodeCtx.functions().getQualified(windowFunctionSymbol, txnCtx.sessionSettings().searchPath());
        assert impl != null : "Function implementation not found using full qualified lookup";
        if (impl instanceof AggregationFunction) {
            var filterInputFactoryCtx = inputFactory.ctxForInputColumns(txnCtx);
            var filterSymbol = windowFunctionSymbol.filter();
            // noinspection unchecked
            Input<Boolean> filterInput = filterSymbol == null ? Literal.BOOLEAN_TRUE : (Input<Boolean>) filterInputFactoryCtx.add(filterSymbol);
            ExpressionsInput<Row, Boolean> filter = new ExpressionsInput<>(filterInput, filterInputFactoryCtx.expressions());
            windowFunctions.add(new AggregateToWindowFunctionAdapter((AggregationFunction) impl, filter, indexVersionCreated, ramAccounting, memoryManager, minNodeVersion));
        } else if (impl instanceof WindowFunction) {
            windowFunctions.add((WindowFunction) impl);
        } else {
            throw new AssertionError("Function needs to be either a window or an aggregate function");
        }
        windowFuncArgsExpressions.addAll(ctx.expressions());
        windowFuncArgsInputs[idx] = ctx.topLevelInputs().toArray(new Input[0]);
        ignoreNulls[idx] = windowFunctionSymbol.ignoreNulls();
    }
    var windowDefinition = projection.windowDefinition();
    var partitions = windowDefinition.partitions();
    Supplier<InputFactory.Context<CollectExpression<Row, ?>>> createInputFactoryContext = () -> inputFactory.ctxForInputColumns(txnCtx);
    int arrayListElementOverHead = 32;
    RowAccountingWithEstimators accounting = new RowAccountingWithEstimators(Symbols.typeView(projection.standalone()), ramAccounting, arrayListElementOverHead);
    Comparator<Object[]> cmpPartitionBy = partitions.isEmpty() ? null : createComparator(createInputFactoryContext, new OrderBy(windowDefinition.partitions()));
    Comparator<Object[]> cmpOrderBy = createComparator(createInputFactoryContext, windowDefinition.orderBy());
    int numCellsInSourceRow = projection.standalone().size();
    ComputeFrameBoundary<Object[]> computeFrameStart = createComputeStartFrameBoundary(numCellsInSourceRow, txnCtx, nodeCtx, windowDefinition, cmpOrderBy);
    ComputeFrameBoundary<Object[]> computeFrameEnd = createComputeEndFrameBoundary(numCellsInSourceRow, txnCtx, nodeCtx, windowDefinition, cmpOrderBy);
    return sourceRows -> WindowFunctionBatchIterator.of(sourceRows, accounting, computeFrameStart, computeFrameEnd, cmpPartitionBy, cmpOrderBy, numCellsInSourceRow, numThreads, executor, windowFunctions, windowFuncArgsExpressions, ignoreNulls, windowFuncArgsInputs);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) InputColumn(io.crate.expression.symbol.InputColumn) WindowFrame(io.crate.sql.tree.WindowFrame) BiFunction(java.util.function.BiFunction) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Projector(io.crate.data.Projector) IntervalType(io.crate.types.IntervalType) Symbols(io.crate.expression.symbol.Symbols) Nullable(javax.annotation.Nullable) IntSupplier(java.util.function.IntSupplier) NodeContext(io.crate.metadata.NodeContext) Executor(java.util.concurrent.Executor) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) SymbolType(io.crate.expression.symbol.SymbolType) Comparators.createComparator(io.crate.execution.engine.sort.Comparators.createComparator) DataType(io.crate.types.DataType) SymbolEvaluator.evaluateWithoutParams(io.crate.analyze.SymbolEvaluator.evaluateWithoutParams) MemoryManager(io.crate.memory.MemoryManager) RamAccounting(io.crate.breaker.RamAccounting) ExpressionsInput(io.crate.expression.ExpressionsInput) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) WindowDefinition(io.crate.analyze.WindowDefinition) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) InputFactory(io.crate.expression.InputFactory) Comparator(java.util.Comparator) InputFactory(io.crate.expression.InputFactory) ArrayList(java.util.ArrayList) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) Input(io.crate.data.Input) ExpressionsInput(io.crate.expression.ExpressionsInput) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) FunctionImplementation(io.crate.metadata.FunctionImplementation) TransactionContext(io.crate.metadata.TransactionContext) NodeContext(io.crate.metadata.NodeContext) OrderBy(io.crate.analyze.OrderBy) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Row(io.crate.data.Row) ExpressionsInput(io.crate.expression.ExpressionsInput)

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