Search in sources :

Example 16 with Function

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

the class HandlerSideLevelCollectTest method testInformationSchemaTables.

@Test
public void testInformationSchemaTables() throws Exception {
    InformationSchemaInfo schemaInfo = internalCluster().getInstance(InformationSchemaInfo.class);
    TableInfo tablesTableInfo = schemaInfo.getTableInfo("tables");
    Routing routing = tablesTableInfo.getRouting(clusterService().state(), routingProvider, WhereClause.MATCH_ALL, RoutingProvider.ShardSelection.ANY, SessionContext.systemSessionContext());
    List<Symbol> toCollect = new ArrayList<>();
    for (Reference reference : tablesTableInfo.columns()) {
        toCollect.add(reference);
    }
    Symbol tableNameRef = toCollect.get(12);
    List<Symbol> arguments = Arrays.asList(tableNameRef, Literal.of("shards"));
    FunctionImplementation eqImpl = functions.get(null, EqOperator.NAME, arguments, SearchPath.pathWithPGCatalogAndDoc());
    Function whereClause = new Function(eqImpl.signature(), arguments, EqOperator.RETURN_TYPE);
    RoutedCollectPhase collectNode = collectNode(routing, toCollect, RowGranularity.DOC, new WhereClause(whereClause));
    Bucket result = collect(collectNode);
    assertThat(TestingHelpers.printedTable(result), is("NULL| NULL| NULL| strict| NULL| NULL| NULL| SYSTEM GENERATED| NULL| NULL| NULL| sys| shards| sys| BASE TABLE| NULL\n"));
}
Also used : Function(io.crate.expression.symbol.Function) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo) Bucket(io.crate.data.Bucket) CollectionBucket(io.crate.data.CollectionBucket) Symbol(io.crate.expression.symbol.Symbol) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) WhereClause(io.crate.analyze.WhereClause) Routing(io.crate.metadata.Routing) TableInfo(io.crate.metadata.table.TableInfo) SysClusterTableInfo(io.crate.metadata.sys.SysClusterTableInfo) FunctionImplementation(io.crate.metadata.FunctionImplementation) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test)

Example 17 with Function

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

the class SimplifyEqualsOperationOnIdenticalReferencesTest method testSimplifyRefEqRefWhenRefIsNotNullable.

@Test
public void testSimplifyRefEqRefWhenRefIsNotNullable() {
    Function functionToOptimize = new Function(EqOperator.SIGNATURE, List.of(NOT_NULLABLE_REF, NOT_NULLABLE_REF), DataTypes.BOOLEAN);
    Match<Function> match = RULE.pattern().accept(functionToOptimize, Captures.empty());
    assertTrue(match.isPresent());
    // function to optimize has no parent
    Symbol optimizedFunction = RULE.apply(match.value(), match.captures(), NODE_CONTEXT, null);
    assertThat(optimizedFunction, is(Literal.BOOLEAN_TRUE));
    // function to optimize has a parent
    Function dummyParent = new Function(NotPredicate.SIGNATURE, List.of(functionToOptimize), DataTypes.BOOLEAN);
    optimizedFunction = RULE.apply(match.value(), match.captures(), NODE_CONTEXT, dummyParent);
    assertThat(optimizedFunction, is(Literal.BOOLEAN_TRUE));
}
Also used : Function(io.crate.expression.symbol.Function) Ignore3vlFunction(io.crate.expression.scalar.Ignore3vlFunction) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test)

Example 18 with Function

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

the class SimplifyEqualsOperationOnIdenticalReferencesTest method testSimplifyRefEqRefWhenParentIsIgnore3vlFunction.

@Test
public void testSimplifyRefEqRefWhenParentIsIgnore3vlFunction() {
    Function func = new Function(Ignore3vlFunction.SIGNATURE, List.of(new Function(EqOperator.SIGNATURE, List.of(NULLABLE_REF, NULLABLE_REF), DataTypes.BOOLEAN)), DataTypes.BOOLEAN);
    Match<Function> match = RULE.pattern().accept(func.arguments().get(0), Captures.empty());
    assertTrue(match.isPresent());
    Symbol optimizedFunction = RULE.apply(match.value(), match.captures(), NODE_CONTEXT, func);
    assertThat(optimizedFunction, is(Literal.BOOLEAN_TRUE));
}
Also used : Function(io.crate.expression.symbol.Function) Ignore3vlFunction(io.crate.expression.scalar.Ignore3vlFunction) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test)

Example 19 with Function

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

the class ScalarTestCase method assertEvaluate.

/**
 * asserts that the given functionExpression matches the given matcher.
 * If the functionExpression contains references the inputs will be used in the order the references appear.
 * <p>
 * E.g.
 * <code>
 * assertEvaluate("foo(name, age)", anyOf("expectedValue1", "expectedValue2"), inputForName, inputForAge)
 * </code>
 */
@SuppressWarnings("unchecked")
public <T> void assertEvaluate(String functionExpression, Matcher<T> expectedValue, Literal<?>... literals) {
    if (expectedValue == null) {
        expectedValue = (Matcher<T>) nullValue();
    }
    sqlExpressions.context().allowEagerNormalize(true);
    Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
    functionSymbol = sqlExpressions.normalize(functionSymbol);
    if (functionSymbol instanceof Literal) {
        Object value = ((Literal) functionSymbol).value();
        assertThat((T) value, expectedValue);
        return;
    }
    LinkedList<Literal<?>> unusedLiterals = new LinkedList<>(Arrays.asList(literals));
    Function function = (Function) RefReplacer.replaceRefs(functionSymbol, r -> {
        if (unusedLiterals.isEmpty()) {
            throw new IllegalArgumentException("No value literal for reference=" + r + ", please add more literals");
        }
        // Can be null.
        Literal<?> literal = unusedLiterals.pollFirst();
        return literal;
    });
    if (unusedLiterals.size() == literals.length) {
        // Currently it's supposed that literals will be either references or parameters.
        // One of replaceRefs and bindParameters does nothing and doesn't consume unusedLiterals.
        function = (Function) ParameterBinder.bindParameters(function, p -> {
            if (unusedLiterals.isEmpty()) {
                throw new IllegalArgumentException("No value literal for parameter=" + p + ", please add more literals");
            }
            // Can be null.
            Literal<?> literal = unusedLiterals.pollFirst();
            return literal;
        });
    }
    Scalar scalar = (Scalar) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assertThat("Function implementation not found using full qualified lookup", scalar, Matchers.notNullValue());
    AssertMax1ValueCallInput[] arguments = new AssertMax1ValueCallInput[function.arguments().size()];
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(txnCtx);
    for (int i = 0; i < function.arguments().size(); i++) {
        Symbol arg = function.arguments().get(i);
        Input<?> input = ctx.add(arg);
        arguments[i] = new AssertMax1ValueCallInput(input);
    }
    Object actualValue = scalar.compile(function.arguments()).evaluate(txnCtx, sqlExpressions.nodeCtx, (Input[]) arguments);
    assertThat((T) actualValue, expectedValue);
    // Reset calls
    for (AssertMax1ValueCallInput argument : arguments) {
        argument.calls = 0;
    }
    actualValue = scalar.evaluate(txnCtx, sqlExpressions.nodeCtx, arguments);
    assertThat((T) actualValue, expectedValue);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) SqlExpressions(io.crate.testing.SqlExpressions) InputColumn(io.crate.expression.symbol.InputColumn) Arrays(java.util.Arrays) ParameterBinder(io.crate.expression.symbol.ParameterBinder) RelationName(io.crate.metadata.RelationName) SessionSettings(io.crate.metadata.settings.SessionSettings) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Matchers.not(org.hamcrest.Matchers.not) SearchPath(io.crate.metadata.SearchPath) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RefReplacer(io.crate.expression.symbol.RefReplacer) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Is.is(org.hamcrest.core.Is.is) SQLExecutor(io.crate.testing.SQLExecutor) LinkedList(java.util.LinkedList) Before(org.junit.Before) DocTableInfo(io.crate.metadata.doc.DocTableInfo) DataType(io.crate.types.DataType) Matchers(org.hamcrest.Matchers) Function(io.crate.expression.symbol.Function) Lists2(io.crate.common.collections.Lists2) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) 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) DocSchemaInfo(io.crate.metadata.doc.DocSchemaInfo) Matcher(org.hamcrest.Matcher) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) InputFactory(io.crate.expression.InputFactory) Scalar(io.crate.metadata.Scalar) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) CollectExpression(io.crate.execution.engine.collect.CollectExpression) LinkedList(java.util.LinkedList) Scalar(io.crate.metadata.Scalar) Function(io.crate.expression.symbol.Function) Input(io.crate.data.Input) Literal(io.crate.expression.symbol.Literal)

Example 20 with Function

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

the class ValuesFunctionTest method test_function_return_type_of_the_next_nested_item.

@Test
public void test_function_return_type_of_the_next_nested_item() {
    Function function = (Function) sqlExpressions.asSymbol("_values([['a', 'b']])");
    var funcImplementation = (TableFunctionImplementation<?>) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assertThat(funcImplementation.returnType(), instanceOf(RowType.class));
    assertThat(funcImplementation.returnType().fieldTypes(), contains(DataTypes.STRING_ARRAY));
}
Also used : Function(io.crate.expression.symbol.Function) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) RowType(io.crate.types.RowType) Test(org.junit.Test)

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