Search in sources :

Example 66 with Function

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

the class SelectStatementAnalyzerTest method testAggregationSelect.

@Test
public void testAggregationSelect() throws Exception {
    var executor = SQLExecutor.builder(clusterService).build();
    QueriedSelectRelation relation = executor.analyze("select avg(load['5']) from sys.nodes");
    assertThat(relation.groupBy().isEmpty(), is(true));
    assertThat(relation.outputs().size(), is(1));
    Function col1 = (Function) relation.outputs().get(0);
    assertThat(col1.signature().getKind(), is(FunctionType.AGGREGATE));
    assertThat(col1.name(), is(AverageAggregation.NAME));
}
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) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 67 with Function

use of io.crate.expression.symbol.Function 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 68 with Function

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

the class SelectStatementAnalyzerTest method testRewriteRegexpNoMatch.

@Test
public void testRewriteRegexpNoMatch() throws Exception {
    var executor = SQLExecutor.builder(clusterService).build();
    String statement = "select * from sys.nodes where sys.nodes.name !~ '[sS]omething'";
    QueriedSelectRelation relation = executor.analyze(statement);
    Function notFunction = (Function) relation.where();
    assertThat(notFunction.name(), is(NotPredicate.NAME));
    assertThat(notFunction.arguments().size(), is(1));
    Function eqFunction = (Function) notFunction.arguments().get(0);
    assertThat(eqFunction.name(), is(RegexpMatchOperator.NAME));
    assertThat(eqFunction.arguments().size(), is(2));
    List<Symbol> eqArguments = eqFunction.arguments();
    assertThat(eqArguments.get(0), isReference("name"));
    assertThat(eqArguments.get(1), isLiteral("[sS]omething"));
}
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) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) AliasSymbol(io.crate.expression.symbol.AliasSymbol) BitString(io.crate.sql.tree.BitString) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 69 with Function

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

the class SelectStatementAnalyzerTest method testIsNullInWhereQuery.

@Test
public void testIsNullInWhereQuery() {
    var executor = SQLExecutor.builder(clusterService).build();
    QueriedSelectRelation relation = executor.analyze("select * from sys.nodes where name is null");
    Function isNullFunction = (Function) relation.where();
    assertThat(isNullFunction.name(), is(IsNullPredicate.NAME));
    assertThat(isNullFunction.arguments().size(), is(1));
    assertThat(isNullFunction.arguments().get(0), isReference("name"));
    assertNotNull(relation.where());
}
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) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 70 with Function

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

the class SelectStatementAnalyzerTest method testSelectWithParameters.

@Test
public void testSelectWithParameters() throws Exception {
    var executor = SQLExecutor.builder(clusterService).build();
    QueriedSelectRelation relation = executor.analyze("select load from sys.nodes " + "where load['1'] = ? or load['5'] <= ? or load['15'] >= ? or load['1'] = ? " + "or load['1'] = ? or name = ?");
    Function whereClause = (Function) relation.where();
    assertThat(whereClause.name(), is(OrOperator.NAME));
    assertThat(whereClause.signature().getKind() == FunctionType.AGGREGATE, is(false));
    Function function = (Function) whereClause.arguments().get(0);
    assertThat(function.name(), is(OrOperator.NAME));
    function = (Function) function.arguments().get(1);
    assertThat(function.name(), is(EqOperator.NAME));
    assertThat(function.arguments().get(1), IsInstanceOf.instanceOf(ParameterSymbol.class));
    assertThat(function.arguments().get(1).valueType(), is(DataTypes.DOUBLE));
    function = (Function) whereClause.arguments().get(1);
    assertThat(function.name(), is(EqOperator.NAME));
    assertThat(function.arguments().get(1), IsInstanceOf.instanceOf(ParameterSymbol.class));
    assertThat(function.arguments().get(1).valueType(), is(DataTypes.STRING));
}
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) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) 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