use of io.trino.sql.tree.Row in project trino by trinodb.
the class RelationPlanner method visitValues.
@Override
protected RelationPlan visitValues(Values node, Void context) {
Scope scope = analysis.getScope(node);
ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder();
for (Field field : scope.getRelationType().getVisibleFields()) {
Symbol symbol = symbolAllocator.newSymbol(field);
outputSymbolsBuilder.add(symbol);
}
List<Symbol> outputSymbols = outputSymbolsBuilder.build();
TranslationMap translationMap = new TranslationMap(outerContext, analysis.getScope(node), analysis, lambdaDeclarationToSymbolMap, outputSymbols);
ImmutableList.Builder<Expression> rows = ImmutableList.builder();
for (Expression row : node.getRows()) {
if (row instanceof Row) {
rows.add(new Row(((Row) row).getItems().stream().map(item -> coerceIfNecessary(analysis, item, translationMap.rewrite(item))).collect(toImmutableList())));
} else if (analysis.getType(row) instanceof RowType) {
rows.add(coerceIfNecessary(analysis, row, translationMap.rewrite(row)));
} else {
rows.add(new Row(ImmutableList.of(coerceIfNecessary(analysis, row, translationMap.rewrite(row)))));
}
}
ValuesNode valuesNode = new ValuesNode(idAllocator.getNextId(), outputSymbols, rows.build());
return new RelationPlan(valuesNode, scope, outputSymbols, outerContext);
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class SubqueryPlanner method planScalarSubquery.
private PlanBuilder planScalarSubquery(PlanBuilder subPlan, Cluster<SubqueryExpression> cluster) {
// Plan one of the predicates from the cluster
SubqueryExpression scalarSubquery = cluster.getRepresentative();
RelationPlan relationPlan = planSubquery(scalarSubquery, subPlan.getTranslations());
PlanBuilder subqueryPlan = newPlanBuilder(relationPlan, analysis, lambdaDeclarationToSymbolMap);
PlanNode root = new EnforceSingleRowNode(idAllocator.getNextId(), subqueryPlan.getRoot());
Type type = analysis.getType(scalarSubquery);
RelationType descriptor = relationPlan.getDescriptor();
List<Symbol> fieldMappings = relationPlan.getFieldMappings();
Symbol column;
if (descriptor.getVisibleFieldCount() > 1) {
column = symbolAllocator.newSymbol("row", type);
ImmutableList.Builder<Expression> fields = ImmutableList.builder();
for (int i = 0; i < descriptor.getAllFieldCount(); i++) {
Field field = descriptor.getFieldByIndex(i);
if (!field.isHidden()) {
fields.add(fieldMappings.get(i).toSymbolReference());
}
}
Expression expression = new Cast(new Row(fields.build()), TypeSignatureTranslator.toSqlType(type));
root = new ProjectNode(idAllocator.getNextId(), root, Assignments.of(column, expression));
} else {
column = getOnlyElement(fieldMappings);
}
return appendCorrelatedJoin(subPlan, root, scalarSubquery.getQuery(), CorrelatedJoinNode.Type.INNER, TRUE_LITERAL, mapAll(cluster, subPlan.getScope(), column));
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class SubqueryPlanner method planValue.
private PlanAndMappings planValue(PlanBuilder subPlan, Expression value, Type actualType, Optional<Type> coercion) {
subPlan = subPlan.appendProjections(ImmutableList.of(value), symbolAllocator, idAllocator);
// Adapt implicit row type (in the SQL spec, <row value special case>) by wrapping it with a row constructor
Symbol column = subPlan.translate(value);
Type declaredType = analysis.getType(value);
if (!actualType.equals(declaredType)) {
Symbol wrapped = symbolAllocator.newSymbol("row", actualType);
Assignments assignments = Assignments.builder().putIdentities(subPlan.getRoot().getOutputSymbols()).put(wrapped, new Row(ImmutableList.of(column.toSymbolReference()))).build();
subPlan = subPlan.withNewRoot(new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), assignments));
column = wrapped;
}
return coerceIfNecessary(subPlan, column, value, actualType, coercion);
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class SubqueryPlanner method planSubquery.
private PlanAndMappings planSubquery(Expression subquery, Optional<Type> coercion, TranslationMap outerContext) {
Type type = analysis.getType(subquery);
Symbol column = symbolAllocator.newSymbol("row", type);
RelationPlan relationPlan = planSubquery(subquery, outerContext);
PlanBuilder subqueryPlan = newPlanBuilder(relationPlan, analysis, lambdaDeclarationToSymbolMap, ImmutableMap.of(scopeAwareKey(subquery, analysis, relationPlan.getScope()), column));
RelationType descriptor = relationPlan.getDescriptor();
ImmutableList.Builder<Expression> fields = ImmutableList.builder();
for (int i = 0; i < descriptor.getAllFieldCount(); i++) {
Field field = descriptor.getFieldByIndex(i);
if (!field.isHidden()) {
fields.add(relationPlan.getFieldMappings().get(i).toSymbolReference());
}
}
subqueryPlan = subqueryPlan.withNewRoot(new ProjectNode(idAllocator.getNextId(), relationPlan.getRoot(), Assignments.of(column, new Cast(new Row(fields.build()), toSqlType(type)))));
return coerceIfNecessary(subqueryPlan, column, subquery, analysis.getType(subquery), coercion);
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class RemoveEmptyTableExecute method apply.
@Override
public Result apply(TableFinishNode finishNode, Captures captures, Context context) {
Optional<PlanNode> finishSource = getSingleSourceSkipExchange(finishNode, context.getLookup());
if (finishSource.isEmpty() || !(finishSource.get() instanceof TableExecuteNode)) {
return Result.empty();
}
Optional<PlanNode> tableExecuteSource = getSingleSourceSkipExchange(finishSource.get(), context.getLookup());
if (tableExecuteSource.isEmpty() || !(tableExecuteSource.get() instanceof ValuesNode)) {
return Result.empty();
}
ValuesNode valuesNode = (ValuesNode) tableExecuteSource.get();
verify(valuesNode.getRowCount() == 0, "Unexpected non-empty Values as source of TableExecuteNode");
return Result.ofPlanNode(new ValuesNode(finishNode.getId(), finishNode.getOutputSymbols(), ImmutableList.of(new Row(ImmutableList.of(new NullLiteral())))));
}
Aggregations