use of io.crate.execution.dsl.phases.TableFunctionCollectPhase in project crate by crate.
the class TableFunction method build.
@Override
public ExecutionPlan build(PlannerContext plannerContext, Set<PlanHint> planHints, ProjectionBuilder projectionBuilder, int limit, int offset, @Nullable OrderBy order, @Nullable Integer pageSizeHint, Row params, SubQueryResults subQueryResults) {
List<Symbol> args = relation.function().arguments();
ArrayList<Literal<?>> functionArguments = new ArrayList<>(args.size());
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(plannerContext.nodeContext(), RowGranularity.CLUSTER, null, relation);
var binder = new SubQueryAndParamBinder(params, subQueryResults).andThen(x -> normalizer.normalize(x, plannerContext.transactionContext()));
for (Symbol arg : args) {
// It's not possible to use columns as argument to a table function, so it's safe to evaluate at this point.
functionArguments.add(Literal.ofUnchecked(arg.valueType(), SymbolEvaluator.evaluate(plannerContext.transactionContext(), plannerContext.nodeContext(), arg, params, subQueryResults)));
}
TableFunctionCollectPhase collectPhase = new TableFunctionCollectPhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), plannerContext.handlerNode(), relation.functionImplementation(), functionArguments, Lists2.map(toCollect, binder), binder.apply(where.queryOrFallback()));
return new Collect(collectPhase, TopN.NO_LIMIT, 0, toCollect.size(), TopN.NO_LIMIT, null);
}
use of io.crate.execution.dsl.phases.TableFunctionCollectPhase in project crate by crate.
the class TableFunctionCollectSource method getIterator.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<BatchIterator<Row>> getIterator(TransactionContext txnCtx, CollectPhase collectPhase, CollectTask collectTask, boolean supportMoveToStart) {
TableFunctionCollectPhase phase = (TableFunctionCollectPhase) collectPhase;
TableFunctionImplementation<?> functionImplementation = phase.functionImplementation();
RowType rowType = functionImplementation.returnType();
// noinspection unchecked Only literals can be passed to table functions. Anything else is invalid SQL
List<Input<?>> inputs = (List<Input<?>>) (List<?>) phase.functionArguments();
List<Input<?>> topLevelInputs = new ArrayList<>(phase.toCollect().size());
List<String> columns = rowType.fieldNames();
InputFactory.Context<InputCollectExpression> ctx = inputFactory.ctxForRefs(txnCtx, ref -> {
for (int i = 0; i < columns.size(); i++) {
String column = columns.get(i);
if (ref.column().isTopLevel() && ref.column().name().equals(column)) {
return new InputCollectExpression(i);
}
}
throw new IllegalStateException("Column `" + ref + "` not found in " + functionImplementation.signature().getName().displayName());
});
for (Symbol symbol : phase.toCollect()) {
topLevelInputs.add(ctx.add(symbol));
}
var inputRow = new ValueAndInputRow<>(topLevelInputs, ctx.expressions());
Input<Boolean> condition = (Input<Boolean>) ctx.add(phase.where());
Iterable<Row> rows = () -> StreamSupport.stream(functionImplementation.evaluate(txnCtx, nodeCtx, inputs.toArray(new Input[0])).spliterator(), false).map(inputRow).filter(InputCondition.asPredicate(condition)).iterator();
return CompletableFuture.completedFuture(InMemoryBatchIterator.of(rows, SentinelRow.SENTINEL, functionImplementation.hasLazyResultSet()));
}
Aggregations