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