Search in sources :

Example 1 with TableFunctionCollectPhase

use of io.crate.planner.node.dql.TableFunctionCollectPhase in project crate by crate.

the class TableFunctionCollectSource method getCollector.

@Override
public CrateCollector getCollector(CollectPhase collectPhase, BatchConsumer consumer, JobCollectContext jobCollectContext) {
    TableFunctionCollectPhase phase = (TableFunctionCollectPhase) collectPhase;
    WhereClause whereClause = phase.whereClause();
    if (whereClause.noMatch()) {
        return RowsCollector.empty(consumer);
    }
    TableFunctionImplementation functionImplementation = phase.relation().functionImplementation();
    TableInfo tableInfo = functionImplementation.createTableInfo(clusterService);
    //noinspection unchecked  Only literals can be passed to table functions. Anything else is invalid SQL
    List<Input<?>> inputs = (List<Input<?>>) (List) phase.relation().function().arguments();
    List<Reference> columns = new ArrayList<>(tableInfo.columns());
    List<Input<?>> topLevelInputs = new ArrayList<>(phase.toCollect().size());
    InputFactory.Context<InputCollectExpression> ctx = inputFactory.ctxForRefs(i -> new InputCollectExpression(columns.indexOf(i)));
    for (Symbol symbol : phase.toCollect()) {
        topLevelInputs.add(ctx.add(symbol));
    }
    Iterable<Row> rows = Iterables.transform(functionImplementation.execute(inputs), new ValueAndInputRow<>(topLevelInputs, ctx.expressions()));
    if (whereClause.hasQuery()) {
        Input<Boolean> condition = (Input<Boolean>) ctx.add(whereClause.query());
        rows = Iterables.filter(rows, InputCondition.asPredicate(condition));
    }
    OrderBy orderBy = phase.orderBy();
    if (orderBy != null) {
        rows = RowsTransformer.sortRows(Iterables.transform(rows, Row::materialize), phase);
    }
    return RowsCollector.forRows(rows, phase.toCollect().size(), consumer);
}
Also used : OrderBy(io.crate.analyze.OrderBy) InputFactory(io.crate.operation.InputFactory) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) ArrayList(java.util.ArrayList) Input(io.crate.data.Input) TableInfo(io.crate.metadata.table.TableInfo) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.crate.data.Row) TableFunctionCollectPhase(io.crate.planner.node.dql.TableFunctionCollectPhase)

Aggregations

OrderBy (io.crate.analyze.OrderBy)1 WhereClause (io.crate.analyze.WhereClause)1 Symbol (io.crate.analyze.symbol.Symbol)1 Input (io.crate.data.Input)1 Row (io.crate.data.Row)1 Reference (io.crate.metadata.Reference)1 TableInfo (io.crate.metadata.table.TableInfo)1 TableFunctionImplementation (io.crate.metadata.tablefunctions.TableFunctionImplementation)1 InputFactory (io.crate.operation.InputFactory)1 TableFunctionCollectPhase (io.crate.planner.node.dql.TableFunctionCollectPhase)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1