Search in sources :

Example 1 with Relation

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());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) AliasedRelation(io.trino.sql.tree.AliasedRelation) SampledRelation(io.trino.sql.tree.SampledRelation) Relation(io.trino.sql.tree.Relation) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) RelationType(io.trino.sql.analyzer.RelationType) RowType(io.trino.spi.type.RowType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Type(io.trino.spi.type.Type) PlanNode(io.trino.sql.planner.plan.PlanNode) RelationType(io.trino.sql.analyzer.RelationType) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 2 with Relation

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);
}
Also used : AliasedRelation(io.trino.sql.tree.AliasedRelation) SampledRelation(io.trino.sql.tree.SampledRelation) Relation(io.trino.sql.tree.Relation) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) Identifier(io.trino.sql.tree.Identifier) AliasedRelation(io.trino.sql.tree.AliasedRelation)

Example 3 with Relation

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);
}
Also used : OrderBy(io.trino.sql.tree.OrderBy) VariableDefinition(io.trino.sql.tree.VariableDefinition) MeasureDefinition(io.trino.sql.tree.MeasureDefinition) SkipTo(io.trino.sql.tree.SkipTo) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) AliasedRelation(io.trino.sql.tree.AliasedRelation) SampledRelation(io.trino.sql.tree.SampledRelation) Relation(io.trino.sql.tree.Relation) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) SortItem(io.trino.sql.tree.SortItem) SubsetDefinition(io.trino.sql.tree.SubsetDefinition) Identifier(io.trino.sql.tree.Identifier) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TryExpression(io.trino.sql.tree.TryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) PatternSearchMode(io.trino.sql.tree.PatternSearchMode) AliasedRelation(io.trino.sql.tree.AliasedRelation)

Example 4 with Relation

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));
}
Also used : AliasedRelation(io.trino.sql.tree.AliasedRelation) SampledRelation(io.trino.sql.tree.SampledRelation) Relation(io.trino.sql.tree.Relation) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) Identifier(io.trino.sql.tree.Identifier) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TryExpression(io.trino.sql.tree.TryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) JoinCriteria(io.trino.sql.tree.JoinCriteria) NaturalJoin(io.trino.sql.tree.NaturalJoin) Join(io.trino.sql.tree.Join) JoinUsing(io.trino.sql.tree.JoinUsing) NaturalJoin(io.trino.sql.tree.NaturalJoin) JoinOn(io.trino.sql.tree.JoinOn)

Example 5 with Relation

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());
}
Also used : QuerySpecification(io.trino.sql.tree.QuerySpecification) AliasedRelation(io.trino.sql.tree.AliasedRelation) SampledRelation(io.trino.sql.tree.SampledRelation) Relation(io.trino.sql.tree.Relation) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) GroupBy(io.trino.sql.tree.GroupBy) SimpleGroupBy(io.trino.sql.tree.SimpleGroupBy) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TryExpression(io.trino.sql.tree.TryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) SelectItem(io.trino.sql.tree.SelectItem) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) Select(io.trino.sql.tree.Select) NaturalJoin(io.trino.sql.tree.NaturalJoin) Join(io.trino.sql.tree.Join) WindowDefinition(io.trino.sql.tree.WindowDefinition)

Aggregations

Relation (io.trino.sql.tree.Relation)6 AliasedRelation (io.trino.sql.tree.AliasedRelation)5 PatternRecognitionRelation (io.trino.sql.tree.PatternRecognitionRelation)5 SampledRelation (io.trino.sql.tree.SampledRelation)5 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)4 Expression (io.trino.sql.tree.Expression)4 IfExpression (io.trino.sql.tree.IfExpression)4 LambdaExpression (io.trino.sql.tree.LambdaExpression)4 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)3 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)3 BindExpression (io.trino.sql.tree.BindExpression)3 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)3 DereferenceExpression (io.trino.sql.tree.DereferenceExpression)3 Identifier (io.trino.sql.tree.Identifier)3 InListExpression (io.trino.sql.tree.InListExpression)3 LogicalExpression (io.trino.sql.tree.LogicalExpression)3 NotExpression (io.trino.sql.tree.NotExpression)3 NullIfExpression (io.trino.sql.tree.NullIfExpression)3 QuantifiedComparisonExpression (io.trino.sql.tree.QuantifiedComparisonExpression)3 SearchedCaseExpression (io.trino.sql.tree.SearchedCaseExpression)3