Search in sources :

Example 26 with Function

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

the class ExpressionAnalyzerTest method testAnalyzeSubscriptFunctionCall.

@Test
public void testAnalyzeSubscriptFunctionCall() throws Exception {
    // Test when use subscript function is used explicitly then it's handled (and validated)
    // the same way it's handled when the subscript operator `[]` is used
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, new SessionContext(0, EnumSet.of(Option.ALLOW_QUOTED_SUBSCRIPT), null), paramTypeHints, new FullQualifedNameFieldProvider(dummySources), null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    FunctionCall subscriptFunctionCall = new FunctionCall(new QualifiedName("subscript"), ImmutableList.of(new ArrayLiteral(ImmutableList.of(new StringLiteral("obj"))), new LongLiteral("1")));
    Function function = (Function) expressionAnalyzer.convert(subscriptFunctionCall, expressionAnalysisContext);
    assertEquals("subscript(_array(Literal{obj, type=string}),Literal{1, type=integer})", function.toString());
}
Also used : Function(io.crate.analyze.symbol.Function) SessionContext(io.crate.action.sql.SessionContext) FullQualifedNameFieldProvider(io.crate.analyze.relations.FullQualifedNameFieldProvider) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 27 with Function

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

the class ExpressionAnalyzerTest method testNonDeterministicFunctionsAlwaysNew.

@Test
public void testNonDeterministicFunctionsAlwaysNew() throws Exception {
    ExpressionAnalysisContext localContext = new ExpressionAnalysisContext();
    FunctionInfo info1 = new FunctionInfo(new FunctionIdent("inc", Collections.singletonList(DataTypes.BOOLEAN)), DataTypes.INTEGER, FunctionInfo.Type.SCALAR, FunctionInfo.NO_FEATURES);
    Function fn1 = localContext.allocateFunction(info1, Collections.singletonList(Literal.BOOLEAN_FALSE));
    Function fn2 = localContext.allocateFunction(info1, Collections.singletonList(Literal.BOOLEAN_FALSE));
    Function fn3 = localContext.allocateFunction(info1, Collections.singletonList(Literal.BOOLEAN_TRUE));
    // different instances
    assertThat(fn1, allOf(not(sameInstance(fn2)), not(sameInstance(fn3))));
    // but equal
    assertThat(fn1, is(equalTo(fn2)));
    assertThat(fn1, is(not(equalTo(fn3))));
}
Also used : Function(io.crate.analyze.symbol.Function) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 28 with Function

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

the class UpdateAnalyzerTest method testUsingFQColumnNameShouldBePossibleInWhereClause.

@Test
public void testUsingFQColumnNameShouldBePossibleInWhereClause() throws Exception {
    UpdateAnalyzedStatement statement = analyze("update users set name = 'foo' where users.name != 'foo'");
    Function eqFunction = (Function) ((Function) statement.nestedStatements().get(0).whereClause().query()).arguments().get(0);
    assertThat(eqFunction.arguments().get(0), isReference("name"));
}
Also used : Function(io.crate.analyze.symbol.Function) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 29 with Function

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

the class InputCreatingVisitorTest method testNonDeterministicFunctionsReplacement.

@Test
public void testNonDeterministicFunctionsReplacement() throws Exception {
    SqlExpressions sqlExpressions = new SqlExpressions(T3.SOURCES, T3.TR_1);
    Function fn1 = (Function) sqlExpressions.asSymbol("random()");
    Function fn2 = (Function) sqlExpressions.asSymbol("random()");
    List<Symbol> inputSymbols = Arrays.<Symbol>asList(Literal.BOOLEAN_FALSE, sqlExpressions.asSymbol("upper(a)"), fn1, fn2);
    Function newSameFn = (Function) sqlExpressions.asSymbol("random()");
    Function newDifferentFn = (Function) sqlExpressions.asSymbol("random()");
    InputCreatingVisitor.Context context = new InputCreatingVisitor.Context(inputSymbols);
    Symbol replaced1 = InputCreatingVisitor.INSTANCE.process(fn1, context);
    assertThat(replaced1, is(instanceOf(InputColumn.class)));
    assertThat(((InputColumn) replaced1).index(), is(2));
    Symbol replaced2 = InputCreatingVisitor.INSTANCE.process(fn2, context);
    assertThat(replaced2, is(instanceOf(InputColumn.class)));
    assertThat(((InputColumn) replaced2).index(), is(3));
    Symbol replaced3 = InputCreatingVisitor.INSTANCE.process(newSameFn, context);
    // not replaced
    assertThat(replaced3, is(equalTo((Symbol) newSameFn)));
    Symbol replaced4 = InputCreatingVisitor.INSTANCE.process(newDifferentFn, context);
    // not replaced
    assertThat(replaced4, is(equalTo((Symbol) newDifferentFn)));
}
Also used : Function(io.crate.analyze.symbol.Function) Symbol(io.crate.analyze.symbol.Symbol) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 30 with Function

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

the class QueriedDocTableFetchPushDownTest method testPushDownOrderRefUsedInFunction.

@Test
public void testPushDownOrderRefUsedInFunction() throws Exception {
    QuerySpec qs = new QuerySpec();
    Function funcOfI = abs(REF_I);
    qs.outputs(Lists.newArrayList(REF_A, REF_I, funcOfI));
    qs.orderBy(new OrderBy(Lists.newArrayList(REF_I), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), INPUT(1), abs(INPUT(1)) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, s.t.i ORDER BY s.t.i DESC NULLS LAST"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) Function(io.crate.analyze.symbol.Function) AbsFunction(io.crate.operation.scalar.arithmetic.AbsFunction) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Aggregations

Function (io.crate.analyze.symbol.Function)40 Test (org.junit.Test)25 Symbol (io.crate.analyze.symbol.Symbol)19 CrateUnitTest (io.crate.test.integration.CrateUnitTest)18 TransactionContext (io.crate.metadata.TransactionContext)8 Input (io.crate.data.Input)5 TableInfo (io.crate.metadata.table.TableInfo)5 Literal (io.crate.analyze.symbol.Literal)4 ArrayType (io.crate.types.ArrayType)4 QuerySpec (io.crate.analyze.QuerySpec)3 WhereClause (io.crate.analyze.WhereClause)3 Reference (io.crate.metadata.Reference)3 AbstractScalarFunctionsTest (io.crate.operation.scalar.AbstractScalarFunctionsTest)3 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)3 BytesRef (org.apache.lucene.util.BytesRef)3 SessionContext (io.crate.action.sql.SessionContext)2 OrderBy (io.crate.analyze.OrderBy)2 FullQualifedNameFieldProvider (io.crate.analyze.relations.FullQualifedNameFieldProvider)2 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)2 Field (io.crate.analyze.symbol.Field)2