Search in sources :

Example 6 with Row

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);
}
Also used : ListMultimap(com.google.common.collect.ListMultimap) SubsetDefinition(io.trino.sql.tree.SubsetDefinition) CorrelatedJoinNode(io.trino.sql.planner.plan.CorrelatedJoinNode) PlanNode(io.trino.sql.planner.plan.PlanNode) SetOperation(io.trino.sql.tree.SetOperation) Values(io.trino.sql.tree.Values) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) AliasedRelation(io.trino.sql.tree.AliasedRelation) Node(io.trino.sql.tree.Node) ONE(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ONE) Map(java.util.Map) RowPattern(io.trino.sql.tree.RowPattern) Union(io.trino.sql.tree.Union) SemanticExceptions.semanticException(io.trino.sql.analyzer.SemanticExceptions.semanticException) ENGLISH(java.util.Locale.ENGLISH) TableScanNode(io.trino.sql.planner.plan.TableScanNode) Identifier(io.trino.sql.tree.Identifier) Lateral(io.trino.sql.tree.Lateral) SampledRelation(io.trino.sql.tree.SampledRelation) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Assignments(io.trino.sql.planner.plan.Assignments) Join(io.trino.sql.tree.Join) Set(java.util.Set) PAST_LAST(io.trino.sql.tree.SkipTo.Position.PAST_LAST) SortItem(io.trino.sql.tree.SortItem) NodeUtils.getSortItemsFromOrderBy(io.trino.sql.NodeUtils.getSortItemsFromOrderBy) RelationType(io.trino.sql.analyzer.RelationType) IntersectNode(io.trino.sql.planner.plan.IntersectNode) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) QueryPlanner.pruneInvisibleFields(io.trino.sql.planner.QueryPlanner.pruneInvisibleFields) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern) ValuesNode(io.trino.sql.planner.plan.ValuesNode) TRUE(java.lang.Boolean.TRUE) Session(io.trino.Session) SkipTo(io.trino.sql.tree.SkipTo) TypeCoercion(io.trino.type.TypeCoercion) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) NodeRef(io.trino.sql.tree.NodeRef) INNER(io.trino.sql.tree.Join.Type.INNER) ColumnHandle(io.trino.spi.connector.ColumnHandle) AggregationNode(io.trino.sql.planner.plan.AggregationNode) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Query(io.trino.sql.tree.Query) ExpressionUtils(io.trino.sql.ExpressionUtils) Relation(io.trino.sql.tree.Relation) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) UnnestNode(io.trino.sql.planner.plan.UnnestNode) AggregationNode.singleGroupingSet(io.trino.sql.planner.plan.AggregationNode.singleGroupingSet) TableHandle(io.trino.metadata.TableHandle) ExceptNode(io.trino.sql.planner.plan.ExceptNode) RowPatternToIrRewriter(io.trino.sql.planner.rowpattern.RowPatternToIrRewriter) Table(io.trino.sql.tree.Table) SampleNode(io.trino.sql.planner.plan.SampleNode) MeasureDefinition(io.trino.sql.tree.MeasureDefinition) Intersect(io.trino.sql.tree.Intersect) Scope(io.trino.sql.analyzer.Scope) FilterNode(io.trino.sql.planner.plan.FilterNode) LambdaArgumentDeclaration(io.trino.sql.tree.LambdaArgumentDeclaration) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) IMPLICIT(io.trino.sql.tree.Join.Type.IMPLICIT) UnnestAnalysis(io.trino.sql.analyzer.Analysis.UnnestAnalysis) QueryPlanner.coerce(io.trino.sql.planner.QueryPlanner.coerce) TableSubquery(io.trino.sql.tree.TableSubquery) JoinNode(io.trino.sql.planner.plan.JoinNode) QueryPlanner.planWindowSpecification(io.trino.sql.planner.QueryPlanner.planWindowSpecification) QuerySpecification(io.trino.sql.tree.QuerySpecification) RowType(io.trino.spi.type.RowType) ImmutableSet(com.google.common.collect.ImmutableSet) QueryPlanner.coerceIfNecessary(io.trino.sql.planner.QueryPlanner.coerceIfNecessary) ImmutableMap(com.google.common.collect.ImmutableMap) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) VariableDefinition(io.trino.sql.tree.VariableDefinition) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CROSS(io.trino.sql.tree.Join.Type.CROSS) INITIAL(io.trino.sql.tree.PatternSearchMode.Mode.INITIAL) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) List(java.util.List) PatternSearchMode(io.trino.sql.tree.PatternSearchMode) NaturalJoin(io.trino.sql.tree.NaturalJoin) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) WindowNode(io.trino.sql.planner.plan.WindowNode) PlannerContext(io.trino.sql.PlannerContext) Analysis(io.trino.sql.analyzer.Analysis) UnionNode(io.trino.sql.planner.plan.UnionNode) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) Type(io.trino.spi.type.Type) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) HashMap(java.util.HashMap) Unnest(io.trino.sql.tree.Unnest) Function(java.util.function.Function) Cast(io.trino.sql.tree.Cast) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Field(io.trino.sql.analyzer.Field) ProjectNode(io.trino.sql.planner.plan.ProjectNode) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) AstVisitor(io.trino.sql.tree.AstVisitor) JoinUsing(io.trino.sql.tree.JoinUsing) Except(io.trino.sql.tree.Except) LogicalIndexExtractor(io.trino.sql.planner.rowpattern.LogicalIndexExtractor) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) QualifiedName(io.trino.sql.tree.QualifiedName) QueryPlanner.extractPatternRecognitionExpressions(io.trino.sql.planner.QueryPlanner.extractPatternRecognitionExpressions) Row(io.trino.sql.tree.Row) JoinCriteria(io.trino.sql.tree.JoinCriteria) ValuesNode(io.trino.sql.planner.plan.ValuesNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowType(io.trino.spi.type.RowType) Field(io.trino.sql.analyzer.Field) Scope(io.trino.sql.analyzer.Scope) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) Row(io.trino.sql.tree.Row)

Example 7 with Row

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));
}
Also used : Cast(io.trino.sql.tree.Cast) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) Field(io.trino.sql.analyzer.Field) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) RelationType(io.trino.sql.analyzer.RelationType) Type(io.trino.spi.type.Type) PlanNode(io.trino.sql.planner.plan.PlanNode) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) NotExpression(io.trino.sql.tree.NotExpression) EnforceSingleRowNode(io.trino.sql.planner.plan.EnforceSingleRowNode) RelationType(io.trino.sql.analyzer.RelationType) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Row(io.trino.sql.tree.Row)

Example 8 with Row

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);
}
Also used : TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) RelationType(io.trino.sql.analyzer.RelationType) Type(io.trino.spi.type.Type) Assignments(io.trino.sql.planner.plan.Assignments) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Row(io.trino.sql.tree.Row)

Example 9 with Row

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);
}
Also used : Cast(io.trino.sql.tree.Cast) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) Field(io.trino.sql.analyzer.Field) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) RelationType(io.trino.sql.analyzer.RelationType) Type(io.trino.spi.type.Type) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) NotExpression(io.trino.sql.tree.NotExpression) RelationType(io.trino.sql.analyzer.RelationType) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Row(io.trino.sql.tree.Row)

Example 10 with Row

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())))));
}
Also used : TableExecuteNode(io.trino.sql.planner.plan.TableExecuteNode) ValuesNode(io.trino.sql.planner.plan.ValuesNode) PlanNode(io.trino.sql.planner.plan.PlanNode) Row(io.trino.sql.tree.Row) NullLiteral(io.trino.sql.tree.NullLiteral)

Aggregations

Row (io.trino.sql.tree.Row)26 ImmutableList (com.google.common.collect.ImmutableList)15 Cast (io.trino.sql.tree.Cast)15 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)14 ValuesNode (io.trino.sql.planner.plan.ValuesNode)13 Symbol (io.trino.sql.planner.Symbol)12 Expression (io.trino.sql.tree.Expression)11 FunctionCall (io.trino.sql.tree.FunctionCall)11 LongLiteral (io.trino.sql.tree.LongLiteral)11 Assignments (io.trino.sql.planner.plan.Assignments)10 NullLiteral (io.trino.sql.tree.NullLiteral)10 QualifiedName (io.trino.sql.tree.QualifiedName)10 StringLiteral (io.trino.sql.tree.StringLiteral)10 Test (org.testng.annotations.Test)10 RowType (io.trino.spi.type.RowType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 BIGINT (io.trino.spi.type.BigintType.BIGINT)8 VARCHAR (io.trino.spi.type.VarcharType.VARCHAR)8 Type (io.trino.spi.type.Type)7 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)7