use of io.trino.sql.tree.Relation in project trino by trinodb.
the class RelationPlanner method process.
private SetOperationPlan process(SetOperation node) {
RelationType outputFields = analysis.getOutputDescriptor(node);
List<Symbol> outputs = outputFields.getAllFields().stream().map(symbolAllocator::newSymbol).collect(toImmutableList());
ImmutableListMultimap.Builder<Symbol, Symbol> symbolMapping = ImmutableListMultimap.builder();
ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();
for (Relation child : node.getRelations()) {
RelationPlan plan = process(child, null);
NodeAndMappings planAndMappings;
List<Type> types = analysis.getRelationCoercion(child);
if (types == null) {
// no coercion required, only prune invisible fields from child outputs
planAndMappings = pruneInvisibleFields(plan, idAllocator);
} else {
// apply required coercion and prune invisible fields from child outputs
planAndMappings = coerce(plan, types, symbolAllocator, idAllocator);
}
for (int i = 0; i < outputFields.getAllFields().size(); i++) {
symbolMapping.put(outputs.get(i), planAndMappings.getFields().get(i));
}
sources.add(planAndMappings.getNode());
}
return new SetOperationPlan(sources.build(), symbolMapping.build());
}
use of io.trino.sql.tree.Relation in project trino by trinodb.
the class AstBuilder method visitAliasedRelation.
@Override
public Node visitAliasedRelation(SqlBaseParser.AliasedRelationContext context) {
Relation child = (Relation) visit(context.relationPrimary());
if (context.identifier() == null) {
return child;
}
List<Identifier> aliases = null;
if (context.columnAliases() != null) {
aliases = visit(context.columnAliases().identifier(), Identifier.class);
}
return new AliasedRelation(getLocation(context), child, (Identifier) visit(context.identifier()), aliases);
}
use of io.trino.sql.tree.Relation in project trino by trinodb.
the class AstBuilder method visitPatternRecognition.
@Override
public Node visitPatternRecognition(SqlBaseParser.PatternRecognitionContext context) {
Relation child = (Relation) visit(context.aliasedRelation());
if (context.MATCH_RECOGNIZE() == null) {
return child;
}
Optional<OrderBy> orderBy = Optional.empty();
if (context.ORDER() != null) {
orderBy = Optional.of(new OrderBy(getLocation(context.ORDER()), visit(context.sortItem(), SortItem.class)));
}
Optional<PatternSearchMode> searchMode = Optional.empty();
if (context.INITIAL() != null) {
searchMode = Optional.of(new PatternSearchMode(getLocation(context.INITIAL()), INITIAL));
} else if (context.SEEK() != null) {
searchMode = Optional.of(new PatternSearchMode(getLocation(context.SEEK()), SEEK));
}
PatternRecognitionRelation relation = new PatternRecognitionRelation(getLocation(context), child, visit(context.partition, Expression.class), orderBy, visit(context.measureDefinition(), MeasureDefinition.class), getRowsPerMatch(context.rowsPerMatch()), visitIfPresent(context.skipTo(), SkipTo.class), searchMode, (RowPattern) visit(context.rowPattern()), visit(context.subsetDefinition(), SubsetDefinition.class), visit(context.variableDefinition(), VariableDefinition.class));
if (context.identifier() == null) {
return relation;
}
List<Identifier> aliases = null;
if (context.columnAliases() != null) {
aliases = visit(context.columnAliases().identifier(), Identifier.class);
}
return new AliasedRelation(getLocation(context), relation, (Identifier) visit(context.identifier()), aliases);
}
use of io.trino.sql.tree.Relation in project trino by trinodb.
the class AstBuilder method visitJoinRelation.
// *************** from clause *****************
@Override
public Node visitJoinRelation(SqlBaseParser.JoinRelationContext context) {
Relation left = (Relation) visit(context.left);
Relation right;
if (context.CROSS() != null) {
right = (Relation) visit(context.right);
return new Join(getLocation(context), Join.Type.CROSS, left, right, Optional.empty());
}
JoinCriteria criteria;
if (context.NATURAL() != null) {
right = (Relation) visit(context.right);
criteria = new NaturalJoin();
} else {
right = (Relation) visit(context.rightRelation);
if (context.joinCriteria().ON() != null) {
criteria = new JoinOn((Expression) visit(context.joinCriteria().booleanExpression()));
} else if (context.joinCriteria().USING() != null) {
criteria = new JoinUsing(visit(context.joinCriteria().identifier(), Identifier.class));
} else {
throw new IllegalArgumentException("Unsupported join criteria");
}
}
Join.Type joinType;
if (context.joinType().LEFT() != null) {
joinType = Join.Type.LEFT;
} else if (context.joinType().RIGHT() != null) {
joinType = Join.Type.RIGHT;
} else if (context.joinType().FULL() != null) {
joinType = Join.Type.FULL;
} else {
joinType = Join.Type.INNER;
}
return new Join(getLocation(context), joinType, left, right, Optional.of(criteria));
}
use of io.trino.sql.tree.Relation in project trino by trinodb.
the class AstBuilder method visitQuerySpecification.
@Override
public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) {
Optional<Relation> from = Optional.empty();
List<SelectItem> selectItems = visit(context.selectItem(), SelectItem.class);
List<Relation> relations = visit(context.relation(), Relation.class);
if (!relations.isEmpty()) {
// synthesize implicit join nodes
Iterator<Relation> iterator = relations.iterator();
Relation relation = iterator.next();
while (iterator.hasNext()) {
relation = new Join(getLocation(context), Join.Type.IMPLICIT, relation, iterator.next(), Optional.empty());
}
from = Optional.of(relation);
}
return new QuerySpecification(getLocation(context), new Select(getLocation(context.SELECT()), isDistinct(context.setQuantifier()), selectItems), from, visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), visit(context.windowDefinition(), WindowDefinition.class), Optional.empty(), Optional.empty(), Optional.empty());
}
Aggregations