Search in sources :

Example 76 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class InputColumns method visitFunction.

@Override
public Symbol visitFunction(Function symbol, final SourceSymbols sourceSymbols) {
    Symbol replacement = getFunctionReplacementOrNull(symbol, sourceSymbols);
    if (replacement != null) {
        return replacement;
    }
    ArrayList<Symbol> replacedFunctionArgs = getProcessedArgs(symbol.arguments(), sourceSymbols);
    return new Function(symbol.signature(), replacedFunctionArgs, symbol.valueType());
}
Also used : SubscriptObjectFunction(io.crate.expression.scalar.SubscriptObjectFunction) Function(io.crate.expression.symbol.Function) WindowFunction(io.crate.expression.symbol.WindowFunction) AliasSymbol(io.crate.expression.symbol.AliasSymbol) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol)

Example 77 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class ProjectionBuilder method getAggregations.

private ArrayList<Aggregation> getAggregations(Collection<Function> functions, AggregateMode mode, InputColumns.SourceSymbols sourceSymbols, SearchPath searchPath, java.util.function.Function<Symbol, Symbol> subQueryAndParamBinder) {
    ArrayList<Aggregation> aggregations = new ArrayList<>(functions.size());
    for (Function function : functions) {
        assert function.type() == FunctionType.AGGREGATE : "function type must be " + FunctionType.AGGREGATE;
        List<Symbol> aggregationInputs;
        Symbol filterInput;
        switch(mode) {
            case ITER_FINAL:
            case ITER_PARTIAL:
                // ITER means that there is no aggregation part upfront, therefore the input
                // symbols need to be in arguments
                aggregationInputs = InputColumns.create(function.arguments(), sourceSymbols);
                Symbol filter = function.filter();
                if (filter != null) {
                    filterInput = InputColumns.create(filter, sourceSymbols);
                } else {
                    filterInput = Literal.BOOLEAN_TRUE;
                }
                break;
            case PARTIAL_FINAL:
                aggregationInputs = List.of(sourceSymbols.getICForSource(function));
                filterInput = Literal.BOOLEAN_TRUE;
                break;
            default:
                throw new AssertionError("Invalid mode: " + mode.name());
        }
        AggregationFunction<?, ?> aggregationFunction = (AggregationFunction<?, ?>) nodeCtx.functions().getQualified(function, searchPath);
        assert aggregationFunction != null : "Aggregation function implementation not found using full qualified lookup: " + function;
        var valueType = mode.returnType(aggregationFunction);
        var functionInfo = FunctionInfo.of(aggregationFunction.signature(), aggregationFunction.boundSignature().getArgumentDataTypes(), valueType);
        Aggregation aggregation = new Aggregation(aggregationFunction.signature(), functionInfo, aggregationFunction.boundSignature().getReturnType().createType(), valueType, Lists2.map(aggregationInputs, subQueryAndParamBinder), subQueryAndParamBinder.apply(filterInput));
        aggregations.add(aggregation);
    }
    return aggregations;
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList)

Example 78 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class FilterProjectionTest method testStreaming.

@Test
public void testStreaming() throws Exception {
    var eqFunction = new Function(EqOperator.SIGNATURE, List.of(new InputColumn(0, DataTypes.INTEGER), new InputColumn(1, DataTypes.INTEGER)), EqOperator.RETURN_TYPE);
    FilterProjection p = new FilterProjection(eqFunction, Collections.singletonList(new InputColumn(0)));
    p.requiredGranularity(RowGranularity.SHARD);
    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(p, out);
    StreamInput in = out.bytes().streamInput();
    FilterProjection p2 = (FilterProjection) Projection.fromStream(in);
    assertEquals(p, p2);
}
Also used : Function(io.crate.expression.symbol.Function) InputColumn(io.crate.expression.symbol.InputColumn) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 79 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class ScalarTestCase method assertNormalize.

public void assertNormalize(String functionExpression, Matcher<? super Symbol> expectedSymbol, boolean evaluate) {
    // Explicit normalization happens further below
    sqlExpressions.context().allowEagerNormalize(false);
    Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
    if (functionSymbol instanceof Literal) {
        assertThat(functionSymbol, expectedSymbol);
        return;
    }
    Function function = (Function) functionSymbol;
    FunctionImplementation impl = sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assertThat("Function implementation not found using full qualified lookup", impl, Matchers.notNullValue());
    Symbol normalized = sqlExpressions.normalize(function);
    assertThat(String.format(Locale.ENGLISH, "expected <%s> to normalize to %s", functionExpression, expectedSymbol), normalized, expectedSymbol);
    if (evaluate && normalized instanceof Input && allArgsAreInputs(function.arguments())) {
        Input[] inputs = new Input[function.arguments().size()];
        for (int i = 0; i < inputs.length; i++) {
            inputs[i] = ((Input) function.arguments().get(i));
        }
        Object expectedValue = ((Input) normalized).value();
        assertThat(((Scalar) impl).evaluate(txnCtx, null, inputs), is(expectedValue));
        assertThat(((Scalar) impl).compile(function.arguments()).evaluate(txnCtx, sqlExpressions.nodeCtx, inputs), is(expectedValue));
    }
}
Also used : Function(io.crate.expression.symbol.Function) Input(io.crate.data.Input) Symbol(io.crate.expression.symbol.Symbol) Literal(io.crate.expression.symbol.Literal) FunctionImplementation(io.crate.metadata.FunctionImplementation) Scalar(io.crate.metadata.Scalar)

Example 80 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class ScalarTestCase method assertCompile.

public void assertCompile(String functionExpression, java.util.function.Function<Scalar, Matcher<Scalar>> matcher) {
    Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
    functionSymbol = sqlExpressions.normalize(functionSymbol);
    assertThat("function expression was normalized, compile would not be hit", functionSymbol, not(instanceOf(Literal.class)));
    Function function = (Function) functionSymbol;
    Scalar scalar = (Scalar) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assertThat("Function implementation not found using full qualified lookup", scalar, Matchers.notNullValue());
    Scalar compiled = scalar.compile(function.arguments());
    assertThat(compiled, matcher.apply(scalar));
}
Also used : Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) Scalar(io.crate.metadata.Scalar)

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