Search in sources :

Example 1 with TableFunctionImplementation

use of io.crate.metadata.tablefunctions.TableFunctionImplementation in project crate by crate.

the class UnnestFunctionTest method execute.

private Bucket execute(String expr) {
    Symbol functionSymbol = sqlExpressions.asSymbol(expr);
    functionSymbol = sqlExpressions.normalize(functionSymbol);
    Function function = (Function) functionSymbol;
    TableFunctionImplementation tableFunction = (TableFunctionImplementation) functions.getSafe(function.info().ident());
    return tableFunction.execute(function.arguments().stream().map(a -> (Input) a).collect(Collectors.toList()));
}
Also used : Function(io.crate.analyze.symbol.Function) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) Symbol(io.crate.analyze.symbol.Symbol)

Example 2 with TableFunctionImplementation

use of io.crate.metadata.tablefunctions.TableFunctionImplementation 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)

Example 3 with TableFunctionImplementation

use of io.crate.metadata.tablefunctions.TableFunctionImplementation 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)

Aggregations

Symbol (io.crate.analyze.symbol.Symbol)3 TableFunctionImplementation (io.crate.metadata.tablefunctions.TableFunctionImplementation)3 Function (io.crate.analyze.symbol.Function)2 TableInfo (io.crate.metadata.table.TableInfo)2 ImmutableList (com.google.common.collect.ImmutableList)1 OrderBy (io.crate.analyze.OrderBy)1 WhereClause (io.crate.analyze.WhereClause)1 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)1 Input (io.crate.data.Input)1 Row (io.crate.data.Row)1 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 Reference (io.crate.metadata.Reference)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 SysClusterTableInfo (io.crate.metadata.sys.SysClusterTableInfo)1 Operation (io.crate.metadata.table.Operation)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