Search in sources :

Example 26 with Literal

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

the class SelectStatementAnalyzerTest method testLikeNoStringDataTypeInWhereQuery.

@Test
public void testLikeNoStringDataTypeInWhereQuery() {
    var executor = SQLExecutor.builder(clusterService).build();
    QueriedSelectRelation relation = executor.analyze("select * from sys.nodes where name like 1");
    // check if the implicit cast of the pattern worked
    List<DataType> argumentTypes = List.of(DataTypes.STRING, DataTypes.STRING);
    Function whereClause = (Function) relation.where();
    assertEquals(argumentTypes, Symbols.typeView(whereClause.arguments()));
    assertThat(whereClause.arguments().get(1), IsInstanceOf.instanceOf(Literal.class));
    Literal stringLiteral = (Literal) whereClause.arguments().get(1);
    assertThat(stringLiteral.value(), is("1"));
}
Also used : DistanceFunction(io.crate.expression.scalar.geo.DistanceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) TryCastFunction(io.crate.expression.scalar.cast.TryCastFunction) ExplicitCastFunction(io.crate.expression.scalar.cast.ExplicitCastFunction) Function(io.crate.expression.symbol.Function) SubscriptFunction(io.crate.expression.scalar.SubscriptFunction) Literal(io.crate.expression.symbol.Literal) SymbolMatchers.isLiteral(io.crate.testing.SymbolMatchers.isLiteral) DataType(io.crate.types.DataType) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 27 with Literal

use of io.crate.expression.symbol.Literal 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)

Aggregations

Literal (io.crate.expression.symbol.Literal)27 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)17 Symbol (io.crate.expression.symbol.Symbol)16 Test (org.junit.Test)16 SymbolMatchers.isLiteral (io.crate.testing.SymbolMatchers.isLiteral)13 HashMap (java.util.HashMap)12 ColumnIdent (io.crate.metadata.ColumnIdent)11 Reference (io.crate.metadata.Reference)11 Map (java.util.Map)11 Function (io.crate.expression.symbol.Function)5 DocTableInfo (io.crate.metadata.doc.DocTableInfo)5 List (java.util.List)5 Nullable (javax.annotation.Nullable)5 Lists2 (io.crate.common.collections.Lists2)4 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)4 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)4 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 Iterables (io.crate.common.collections.Iterables)3 Tuple (io.crate.common.collections.Tuple)3 Input (io.crate.data.Input)3