Search in sources :

Example 31 with Function

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

the class QueriedDocTableFetchPushDownTest method testPushDownWithNestedOrderInOutput.

@Test
public void testPushDownWithNestedOrderInOutput() 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(funcOfI), 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']), FETCH(INPUT(0), s.t._doc['i']), INPUT(1) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, abs(s.t.i) ORDER BY abs(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)

Example 32 with Function

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

the class PlannerTest method testAllocateRouting.

@Test
public void testAllocateRouting() throws Exception {
    TableIdent custom = new TableIdent("custom", "t1");
    TableInfo tableInfo1 = TestingTableInfo.builder(custom, shardRouting("t1")).add("id", DataTypes.INTEGER, null).build();
    TableInfo tableInfo2 = TestingTableInfo.builder(custom, shardRoutingForReplicas("t1")).add("id", DataTypes.INTEGER, null).build();
    Planner.Context plannerContext = new Planner.Context(e.planner, clusterService, UUID.randomUUID(), null, normalizer, new TransactionContext(SessionContext.SYSTEM_SESSION), 0, 0);
    WhereClause whereClause = new WhereClause(new Function(new FunctionInfo(new FunctionIdent(EqOperator.NAME, Arrays.<DataType>asList(DataTypes.INTEGER, DataTypes.INTEGER)), DataTypes.BOOLEAN), Arrays.asList(tableInfo1.getReference(new ColumnIdent("id")), Literal.of(2))));
    plannerContext.allocateRouting(tableInfo1, WhereClause.MATCH_ALL, null);
    plannerContext.allocateRouting(tableInfo2, whereClause, null);
    // 2 routing allocations with different where clause must result in 2 allocated routings
    java.lang.reflect.Field tableRoutings = Planner.Context.class.getDeclaredField("tableRoutings");
    tableRoutings.setAccessible(true);
    Multimap<TableIdent, Planner.TableRouting> routing = (Multimap<TableIdent, Planner.TableRouting>) tableRoutings.get(plannerContext);
    assertThat(routing.size(), is(2));
    // The routings must be the same after merging the locations
    Iterator<Planner.TableRouting> iterator = routing.values().iterator();
    Routing routing1 = iterator.next().routing;
    Routing routing2 = iterator.next().routing;
    assertThat(routing1, is(routing2));
}
Also used : SessionContext(io.crate.action.sql.SessionContext) WhereClause(io.crate.analyze.WhereClause) TableDefinitions.shardRouting(io.crate.analyze.TableDefinitions.shardRouting) Function(io.crate.analyze.symbol.Function) Multimap(com.google.common.collect.Multimap) DataType(io.crate.types.DataType) TableInfo(io.crate.metadata.table.TableInfo) TestingTableInfo(io.crate.metadata.table.TestingTableInfo) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 33 with Function

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

the class RelationAnalyzer method visitTableFunction.

@Override
public AnalyzedRelation visitTableFunction(TableFunction node, StatementAnalysisContext statementContext) {
    RelationAnalysisContext context = statementContext.currentRelationContext();
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, statementContext.sessionContext(), statementContext.convertParamFunction(), new FieldProvider() {

        @Override
        public Symbol resolveField(QualifiedName qualifiedName, Operation operation) {
            throw new UnsupportedOperationException("Can only resolve literals");
        }

        @Override
        public Symbol resolveField(QualifiedName qualifiedName, @Nullable List path, Operation operation) {
            throw new UnsupportedOperationException("Can only resolve literals");
        }
    }, null);
    Function function = (Function) expressionAnalyzer.convert(node.functionCall(), context.expressionAnalysisContext());
    FunctionImplementation functionImplementation = functions.getSafe(function.info().ident());
    if (functionImplementation.info().type() != FunctionInfo.Type.TABLE) {
        throw new UnsupportedFeatureException("Non table function " + function.info().ident().name() + " is not supported in from clause");
    }
    TableFunctionImplementation tableFunction = (TableFunctionImplementation) functionImplementation;
    TableInfo tableInfo = tableFunction.createTableInfo(clusterService);
    Operation.blockedRaiseException(tableInfo, statementContext.currentOperation());
    TableRelation tableRelation = new TableFunctionRelation(tableInfo, tableFunction, function);
    context.addSourceRelation(node.name(), tableRelation);
    return tableRelation;
}
Also used : TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) Operation(io.crate.metadata.table.Operation) Function(io.crate.analyze.symbol.Function) ImmutableList(com.google.common.collect.ImmutableList) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) SysClusterTableInfo(io.crate.metadata.sys.SysClusterTableInfo)

Example 34 with Function

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

the class InsertFromSubQueryAnalyzerTest method testImplicitTypeCasting.

@Test
public void testImplicitTypeCasting() throws Exception {
    InsertFromSubQueryAnalyzedStatement statement = e.analyze("insert into users (id, name, shape) (" + "  select id, other_id, name from users " + "  where name = 'Trillian'" + ")");
    List<Symbol> outputSymbols = statement.subQueryRelation().querySpec().outputs();
    assertThat(statement.columns().size(), is(outputSymbols.size()));
    assertThat(outputSymbols.get(1), instanceOf(Function.class));
    Function castFunction = (Function) outputSymbols.get(1);
    assertThat(castFunction, isFunction(CastFunctionResolver.FunctionNames.TO_STRING));
    Function geoCastFunction = (Function) outputSymbols.get(2);
    assertThat(geoCastFunction, isFunction(CastFunctionResolver.FunctionNames.TO_GEO_SHAPE));
}
Also used : Function(io.crate.analyze.symbol.Function) SubstrFunction(io.crate.operation.scalar.SubstrFunction) Symbol(io.crate.analyze.symbol.Symbol) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 35 with Function

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

the class DeleteAnalyzerTest method testDeleteWhere.

@Test
public void testDeleteWhere() throws Exception {
    DeleteAnalyzedStatement statement = e.analyze("delete from users where name='Trillian'");
    DocTableRelation tableRelation = statement.analyzedRelation;
    TableInfo tableInfo = tableRelation.tableInfo();
    assertThat(USER_TABLE_IDENT, equalTo(tableInfo.ident()));
    assertThat(tableInfo.rowGranularity(), is(RowGranularity.DOC));
    Function whereClause = (Function) statement.whereClauses.get(0).query();
    assertEquals(EqOperator.NAME, whereClause.info().ident().name());
    assertFalse(whereClause.info().type() == FunctionInfo.Type.AGGREGATE);
    assertThat(whereClause.arguments().get(0), isReference("name"));
    assertThat(whereClause.arguments().get(1), isLiteral("Trillian"));
}
Also used : Function(io.crate.analyze.symbol.Function) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableInfo(io.crate.metadata.table.TableInfo) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

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