Search in sources :

Example 1 with TableFunctionCollectPhase

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);
}
Also used : EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Collect(io.crate.planner.node.dql.Collect) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) Literal(io.crate.expression.symbol.Literal) ArrayList(java.util.ArrayList) TableFunctionCollectPhase(io.crate.execution.dsl.phases.TableFunctionCollectPhase)

Example 2 with TableFunctionCollectPhase

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()));
}
Also used : InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) ValueAndInputRow(io.crate.execution.engine.collect.ValueAndInputRow) ArrayList(java.util.ArrayList) RowType(io.crate.types.RowType) Input(io.crate.data.Input) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.crate.data.Row) ValueAndInputRow(io.crate.execution.engine.collect.ValueAndInputRow) SentinelRow(io.crate.data.SentinelRow) TableFunctionCollectPhase(io.crate.execution.dsl.phases.TableFunctionCollectPhase)

Aggregations

TableFunctionCollectPhase (io.crate.execution.dsl.phases.TableFunctionCollectPhase)2 Symbol (io.crate.expression.symbol.Symbol)2 ArrayList (java.util.ArrayList)2 Input (io.crate.data.Input)1 Row (io.crate.data.Row)1 SentinelRow (io.crate.data.SentinelRow)1 InputCollectExpression (io.crate.execution.engine.collect.InputCollectExpression)1 ValueAndInputRow (io.crate.execution.engine.collect.ValueAndInputRow)1 InputFactory (io.crate.expression.InputFactory)1 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)1 Literal (io.crate.expression.symbol.Literal)1 SelectSymbol (io.crate.expression.symbol.SelectSymbol)1 Collect (io.crate.planner.node.dql.Collect)1 RowType (io.crate.types.RowType)1 List (java.util.List)1