Search in sources :

Example 6 with Relation

use of com.facebook.presto.sql.tree.Relation in project presto by prestodb.

the class PredicateStitcher method visitTable.

@Override
protected Node visitTable(Table table, PredicateStitcherContext context) {
    SchemaTableName schemaTableName = toSchemaTableName(createQualifiedObjectName(session, table, table.getName()));
    if (!predicates.containsKey(schemaTableName)) {
        return table;
    }
    QuerySpecification queryWithPredicateStitching = new QuerySpecification(selectList(new AllColumns()), Optional.of(table), Optional.of(predicates.get(schemaTableName)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    Relation subquery = subquery(new Query(Optional.empty(), queryWithPredicateStitching, Optional.empty(), Optional.empty(), Optional.empty()));
    if (context.isCreateAlias()) {
        return new AliasedRelation(subquery, identifier(schemaTableName.getTableName()), null);
    }
    return subquery;
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) Relation(com.facebook.presto.sql.tree.Relation) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) WithQuery(com.facebook.presto.sql.tree.WithQuery) Query(com.facebook.presto.sql.tree.Query) AllColumns(com.facebook.presto.sql.tree.AllColumns) SchemaTableName(com.facebook.presto.spi.SchemaTableName) MetadataUtil.toSchemaTableName(com.facebook.presto.metadata.MetadataUtil.toSchemaTableName) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation)

Example 7 with Relation

use of com.facebook.presto.sql.tree.Relation in project presto by prestodb.

the class PredicateStitcher method visitExcept.

@Override
protected Node visitExcept(Except node, PredicateStitcherContext context) {
    Relation rewrittenLeft = (Relation) process(node.getLeft(), context);
    Relation rewrittenRight = (Relation) process(node.getRight(), context);
    return new Except(rewrittenLeft, rewrittenRight, node.isDistinct());
}
Also used : AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) Relation(com.facebook.presto.sql.tree.Relation) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) Except(com.facebook.presto.sql.tree.Except)

Example 8 with Relation

use of com.facebook.presto.sql.tree.Relation in project presto by prestodb.

the class RelationPlanner method process.

private SetOperationPlan process(SetOperation node) {
    List<VariableReferenceExpression> outputs = null;
    ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();
    ImmutableListMultimap.Builder<VariableReferenceExpression, VariableReferenceExpression> variableMapping = ImmutableListMultimap.builder();
    List<RelationPlan> subPlans = node.getRelations().stream().map(relation -> processAndCoerceIfNecessary(relation, null)).collect(toImmutableList());
    for (RelationPlan relationPlan : subPlans) {
        List<VariableReferenceExpression> childOutputVariables = relationPlan.getFieldMappings();
        if (outputs == null) {
            // Use the first Relation to derive output variable names
            RelationType descriptor = relationPlan.getDescriptor();
            ImmutableList.Builder<VariableReferenceExpression> outputVariableBuilder = ImmutableList.builder();
            for (Field field : descriptor.getVisibleFields()) {
                int fieldIndex = descriptor.indexOf(field);
                VariableReferenceExpression variable = childOutputVariables.get(fieldIndex);
                outputVariableBuilder.add(variableAllocator.newVariable(variable));
            }
            outputs = outputVariableBuilder.build();
        }
        RelationType descriptor = relationPlan.getDescriptor();
        checkArgument(descriptor.getVisibleFieldCount() == outputs.size(), "Expected relation to have %s variables but has %s variables", descriptor.getVisibleFieldCount(), outputs.size());
        int fieldId = 0;
        for (Field field : descriptor.getVisibleFields()) {
            int fieldIndex = descriptor.indexOf(field);
            variableMapping.put(outputs.get(fieldId), childOutputVariables.get(fieldIndex));
            fieldId++;
        }
        sources.add(relationPlan.getRoot());
    }
    return new SetOperationPlan(sources.build(), variableMapping.build());
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) SampleNodeUtil(com.facebook.presto.sql.planner.optimizations.SampleNodeUtil) ListMultimap(com.google.common.collect.ListMultimap) Field(com.facebook.presto.sql.analyzer.Field) Values(com.facebook.presto.sql.tree.Values) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) JoinUsing(com.facebook.presto.sql.tree.JoinUsing) ExpressionTreeUtils.resolveEnumLiteral(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.resolveEnumLiteral) SetOperation(com.facebook.presto.sql.tree.SetOperation) Map(java.util.Map) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) Query(com.facebook.presto.sql.tree.Query) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) EnumLiteral(com.facebook.presto.sql.tree.EnumLiteral) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) DefaultTraversalVisitor(com.facebook.presto.sql.tree.DefaultTraversalVisitor) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ExpressionTreeUtils.getSourceLocation(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.getSourceLocation) SemanticExceptions.notSupportedException(com.facebook.presto.sql.analyzer.SemanticExceptions.notSupportedException) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) Iterables(com.google.common.collect.Iterables) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) Table(com.facebook.presto.sql.tree.Table) Assignments(com.facebook.presto.spi.plan.Assignments) ArrayList(java.util.ArrayList) Identifier(com.facebook.presto.sql.tree.Identifier) LinkedHashMap(java.util.LinkedHashMap) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) ArrayType(com.facebook.presto.common.type.ArrayType) TableHandle(com.facebook.presto.spi.TableHandle) Cast(com.facebook.presto.sql.tree.Cast) Union(com.facebook.presto.sql.tree.Union) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) Session(com.facebook.presto.Session) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) NodeRef(com.facebook.presto.sql.tree.NodeRef) Scope(com.facebook.presto.sql.analyzer.Scope) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) INNER(com.facebook.presto.sql.tree.Join.Type.INNER) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) Row(com.facebook.presto.sql.tree.Row) OriginalExpressionUtils.asSymbolReference(com.facebook.presto.sql.relational.OriginalExpressionUtils.asSymbolReference) RowType(com.facebook.presto.common.type.RowType) Metadata(com.facebook.presto.metadata.Metadata) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) IntersectNode(com.facebook.presto.spi.plan.IntersectNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Join(com.facebook.presto.sql.tree.Join) LOCAL(com.facebook.presto.spi.plan.ProjectNode.Locality.LOCAL) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) AssignmentUtils.identitiesAsSymbolReferences(com.facebook.presto.sql.planner.plan.AssignmentUtils.identitiesAsSymbolReferences) RelationId(com.facebook.presto.sql.analyzer.RelationId) Lateral(com.facebook.presto.sql.tree.Lateral) ImmutableMap(com.google.common.collect.ImmutableMap) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) InPredicate(com.facebook.presto.sql.tree.InPredicate) SampleNode(com.facebook.presto.sql.planner.plan.SampleNode) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) JoinNodeUtils(com.facebook.presto.sql.planner.optimizations.JoinNodeUtils) Except(com.facebook.presto.sql.tree.Except) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) MapType(com.facebook.presto.common.type.MapType) UnionNode(com.facebook.presto.spi.plan.UnionNode) Intersect(com.facebook.presto.sql.tree.Intersect) HashMap(java.util.HashMap) ExceptNode(com.facebook.presto.spi.plan.ExceptNode) ExpressionUtils(com.facebook.presto.sql.ExpressionUtils) Unnest(com.facebook.presto.sql.tree.Unnest) FilterNode(com.facebook.presto.spi.plan.FilterNode) ImmutableList(com.google.common.collect.ImmutableList) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) Type(com.facebook.presto.common.type.Type) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Iterator(java.util.Iterator) Relation(com.facebook.presto.sql.tree.Relation) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) RelationType(com.facebook.presto.sql.analyzer.RelationType) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) ExpressionTreeUtils.isEqualComparisonExpression(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.isEqualComparisonExpression) PlanNode(com.facebook.presto.spi.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Field(com.facebook.presto.sql.analyzer.Field) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RelationType(com.facebook.presto.sql.analyzer.RelationType) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Aggregations

AliasedRelation (com.facebook.presto.sql.tree.AliasedRelation)8 Relation (com.facebook.presto.sql.tree.Relation)8 SampledRelation (com.facebook.presto.sql.tree.SampledRelation)7 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)4 Expression (com.facebook.presto.sql.tree.Expression)4 Identifier (com.facebook.presto.sql.tree.Identifier)4 Join (com.facebook.presto.sql.tree.Join)4 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)4 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)3 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)3 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)3 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)3 Query (com.facebook.presto.sql.tree.Query)3 Session (com.facebook.presto.Session)2 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)2 Metadata (com.facebook.presto.metadata.Metadata)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 TableHandle (com.facebook.presto.spi.TableHandle)2 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2