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);
}
Aggregations