Search in sources :

Example 1 with RelationType

use of io.prestosql.sql.analyzer.RelationType in project hetu-core by openlookeng.

the class QueryPlanner method planUpdateRowAsInsert.

public UpdateDeleteRelationPlan planUpdateRowAsInsert(Update node) {
    Table table = node.getTable();
    RelationType descriptor = analysis.getOutputDescriptor(table);
    TableHandle handle = analysis.getTableHandle(table);
    ColumnHandle rowIdHandle = analysis.getRowIdHandle(table);
    ColumnMetadata rowIdColumnMetadata = metadata.getColumnMetadata(session, handle, rowIdHandle);
    // add table columns
    ImmutableList.Builder<Symbol> outputSymbols = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, ColumnHandle> columnsBuilder = ImmutableMap.builder();
    ImmutableList.Builder<Field> fields = ImmutableList.builder();
    for (Field field : descriptor.getAllFields()) {
        Symbol symbol = planSymbolAllocator.newSymbol(field);
        outputSymbols.add(symbol);
        columnsBuilder.put(symbol, analysis.getColumn(field));
        fields.add(field);
    }
    // create table scan
    ImmutableMap<Symbol, ColumnHandle> columns = columnsBuilder.build();
    PlanNode tableScan = TableScanNode.newInstance(idAllocator.getNextId(), handle, outputSymbols.build(), columns, ReuseExchangeOperator.STRATEGY.REUSE_STRATEGY_DEFAULT, new UUID(0, 0), 0, true);
    Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields.build())).build();
    RelationPlan relationPlan = new RelationPlan(tableScan, scope, outputSymbols.build());
    TranslationMap translations = new TranslationMap(relationPlan, analysis, lambdaDeclarationToSymbolMap);
    translations.setFieldMappings(relationPlan.getFieldMappings());
    PlanBuilder builder = new PlanBuilder(translations, relationPlan.getRoot());
    Optional<RowExpression> predicate = Optional.empty();
    if (node.getWhere().isPresent()) {
        builder = filter(builder, node.getWhere().get(), node);
        if (builder.getRoot() instanceof FilterNode) {
            predicate = Optional.of(((FilterNode) builder.getRoot()).getPredicate());
        }
    }
    List<AssignmentItem> assignmentItems = node.getAssignmentItems();
    Analysis.Update update = analysis.getUpdate().get();
    Assignments.Builder assignments = Assignments.builder();
    TableMetadata tableMetadata = metadata.getTableMetadata(session, update.getTarget());
    Symbol orderBySymbol = null;
    for (Map.Entry<Symbol, ColumnHandle> entry : columns.entrySet()) {
        ColumnMetadata column;
        ColumnHandle columnHandle = entry.getValue();
        Symbol input = entry.getKey();
        if (columnHandle.getColumnName().equals(rowIdHandle.getColumnName())) {
            column = rowIdColumnMetadata;
        } else {
            column = tableMetadata.getColumn(columnHandle.getColumnName());
        }
        if (column != rowIdColumnMetadata && column.isHidden()) {
            continue;
        }
        Symbol output = planSymbolAllocator.newSymbol(column.getName(), column.getType());
        Type tableType = column.getType();
        Type queryType = planSymbolAllocator.getTypes().get(input);
        List<AssignmentItem> assignment = assignmentItems.stream().filter(item -> item.getName().equals(QualifiedName.of(column.getName()))).collect(Collectors.toList());
        if (!assignment.isEmpty()) {
            Expression expression = assignment.get(0).getValue();
            Expression cast;
            if (expression instanceof Identifier) {
                // assigning by column reference
                Optional<Symbol> first = columns.entrySet().stream().filter(e -> e.getValue().getColumnName().equals(((Identifier) expression).getValue())).map(Entry::getKey).findFirst();
                Symbol source = (first.orElseThrow(() -> new IllegalArgumentException("Unable to find column " + ((Identifier) expression).getValue())));
                cast = new Cast(toSymbolReference(source), tableType.getTypeSignature().toString());
            } else {
                cast = new Cast(expression, tableType.getTypeSignature().toString());
            }
            assignments.put(output, castToRowExpression(cast));
        } else if (queryType.equals(tableType) || typeCoercion.isTypeOnlyCoercion(queryType, tableType)) {
            assignments.put(output, castToRowExpression(toSymbolReference(input)));
        } else {
            Expression cast = new Cast(toSymbolReference(input), tableType.getTypeSignature().toString());
            assignments.put(output, castToRowExpression(cast));
        }
        if (column == rowIdColumnMetadata) {
            orderBySymbol = output;
        }
    }
    ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), builder.getRoot(), assignments.build());
    PlanBuilder planBuilder = new PlanBuilder(translations, projectNode);
    SortOrder sortOrder = SortOrder.ASC_NULLS_LAST;
    Symbol sortSymbol = orderBySymbol;
    Map<Symbol, SortOrder> sortOrderMap = ImmutableMap.<Symbol, SortOrder>builder().put(sortSymbol, sortOrder).build();
    OrderingScheme orderingScheme = new OrderingScheme(ImmutableList.of(sortSymbol), sortOrderMap);
    builder = sort(planBuilder, Optional.of(orderingScheme));
    ImmutableList.Builder<Field> projectFields = ImmutableList.builder();
    projectFields.addAll(fields.build().stream().filter(x -> !x.isHidden()).collect(toImmutableList()));
    scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(projectFields.build())).build();
    RelationPlan plan = new RelationPlan(builder.getRoot(), scope, projectNode.getOutputSymbols());
    List<String> visibleTableColumnNames = tableMetadata.getColumns().stream().filter(c -> !c.isHidden()).map(ColumnMetadata::getName).collect(Collectors.toList());
    visibleTableColumnNames.add(rowIdColumnMetadata.getName());
    return new UpdateDeleteRelationPlan(plan, visibleTableColumnNames, columns, predicate);
}
Also used : Table(io.prestosql.sql.tree.Table) SortNode(io.prestosql.sql.planner.plan.SortNode) Property(io.prestosql.sql.tree.Property) SortOrder(io.prestosql.spi.block.SortOrder) AggregationNode(io.prestosql.spi.plan.AggregationNode) Cast(io.prestosql.sql.tree.Cast) HeuristicIndexUtils(io.prestosql.utils.HeuristicIndexUtils) Statement(io.prestosql.sql.tree.Statement) HetuConstant(io.prestosql.spi.HetuConstant) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) FetchFirst(io.prestosql.sql.tree.FetchFirst) Identifier(io.prestosql.sql.tree.Identifier) PlanNodeId(io.prestosql.spi.plan.PlanNodeId) CreateIndexNode(io.prestosql.sql.planner.plan.CreateIndexNode) AssignmentItem(io.prestosql.sql.tree.AssignmentItem) Delete(io.prestosql.sql.tree.Delete) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TableScanNode(io.prestosql.spi.plan.TableScanNode) Set(java.util.Set) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) NodeRef(io.prestosql.sql.tree.NodeRef) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) ReuseExchangeOperator(io.prestosql.spi.operator.ReuseExchangeOperator) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) SymbolReference(io.prestosql.sql.tree.SymbolReference) StringLiteral(io.prestosql.sql.tree.StringLiteral) LEVEL_PROP_KEY(io.prestosql.spi.connector.CreateIndexMetadata.LEVEL_PROP_KEY) SymbolUtils.from(io.prestosql.sql.planner.SymbolUtils.from) Field(io.prestosql.sql.analyzer.Field) OriginalExpressionUtils(io.prestosql.sql.relational.OriginalExpressionUtils) GroupIdNode(io.prestosql.spi.plan.GroupIdNode) Iterables(com.google.common.collect.Iterables) TableMetadata(io.prestosql.metadata.TableMetadata) NodeUtils.getSortItemsFromOrderBy(io.prestosql.sql.NodeUtils.getSortItemsFromOrderBy) Node(io.prestosql.sql.tree.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OrderingSchemeUtils.sortItemToSortOrder(io.prestosql.sql.planner.OrderingSchemeUtils.sortItemToSortOrder) MetadataUtil(io.prestosql.metadata.MetadataUtil) Session(io.prestosql.Session) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) FrameBoundType(io.prestosql.spi.sql.expression.Types.FrameBoundType) DeleteNode(io.prestosql.sql.planner.plan.DeleteNode) AssignmentUtils(io.prestosql.sql.planner.plan.AssignmentUtils) Properties(java.util.Properties) Query(io.prestosql.sql.tree.Query) Assignments(io.prestosql.spi.plan.Assignments) Streams.stream(com.google.common.collect.Streams.stream) DeleteTarget(io.prestosql.sql.planner.plan.TableWriterNode.DeleteTarget) VARBINARY(io.prestosql.spi.type.VarbinaryType.VARBINARY) ValuesNode(io.prestosql.spi.plan.ValuesNode) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) CreateIndexMetadata(io.prestosql.spi.connector.CreateIndexMetadata) WindowNode(io.prestosql.spi.plan.WindowNode) LimitNode(io.prestosql.spi.plan.LimitNode) Expression(io.prestosql.sql.tree.Expression) OffsetNode(io.prestosql.sql.planner.plan.OffsetNode) CURRENT_ROW(io.prestosql.spi.sql.expression.Types.FrameBoundType.CURRENT_ROW) UpdateIndexNode(io.prestosql.sql.planner.plan.UpdateIndexNode) UNBOUNDED_PRECEDING(io.prestosql.spi.sql.expression.Types.FrameBoundType.UNBOUNDED_PRECEDING) QualifiedName(io.prestosql.sql.tree.QualifiedName) FieldReference(io.prestosql.sql.tree.FieldReference) ExchangeNode(io.prestosql.sql.planner.plan.ExchangeNode) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FilterNode(io.prestosql.spi.plan.FilterNode) Expressions.call(io.prestosql.sql.relational.Expressions.call) WindowFrame(io.prestosql.sql.tree.WindowFrame) Locale(java.util.Locale) Window(io.prestosql.sql.tree.Window) Type(io.prestosql.spi.type.Type) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) TypeCoercion(io.prestosql.type.TypeCoercion) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) ImmutableSet(com.google.common.collect.ImmutableSet) WindowFrameType(io.prestosql.spi.sql.expression.Types.WindowFrameType) ImmutableMap(com.google.common.collect.ImmutableMap) GroupingOperation(io.prestosql.sql.tree.GroupingOperation) SystemSessionProperties.isSkipRedundantSort(io.prestosql.SystemSessionProperties.isSkipRedundantSort) RANGE(io.prestosql.spi.sql.expression.Types.WindowFrameType.RANGE) CreateIndex(io.prestosql.sql.tree.CreateIndex) PropertyService(io.prestosql.spi.service.PropertyService) UUID(java.util.UUID) RelationType(io.prestosql.sql.analyzer.RelationType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Pair(io.prestosql.spi.heuristicindex.Pair) String.format(java.lang.String.format) Scope(io.prestosql.sql.analyzer.Scope) List(java.util.List) AggregationNode.groupingSets(io.prestosql.spi.plan.AggregationNode.groupingSets) Entry(java.util.Map.Entry) Optional(java.util.Optional) Analysis(io.prestosql.sql.analyzer.Analysis) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) FieldId(io.prestosql.sql.analyzer.FieldId) IntStream(java.util.stream.IntStream) UpdateIndex(io.prestosql.sql.tree.UpdateIndex) UpdateIndexMetadata(io.prestosql.spi.connector.UpdateIndexMetadata) HashMap(java.util.HashMap) RelationId(io.prestosql.sql.analyzer.RelationId) TableHandle(io.prestosql.spi.metadata.TableHandle) DecimalLiteral(io.prestosql.sql.tree.DecimalLiteral) INDEX_SUPPORTED_TYPES(io.prestosql.spi.connector.CreateIndexMetadata.INDEX_SUPPORTED_TYPES) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) OrderingScheme(io.prestosql.spi.plan.OrderingScheme) Objects.requireNonNull(java.util.Objects.requireNonNull) SortItem(io.prestosql.sql.tree.SortItem) Symbol(io.prestosql.spi.plan.Symbol) TableWriterNode(io.prestosql.sql.planner.plan.TableWriterNode) Iterator(java.util.Iterator) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) OrderBy(io.prestosql.sql.tree.OrderBy) UpdateNode(io.prestosql.sql.planner.plan.UpdateNode) LambdaArgumentDeclaration(io.prestosql.sql.tree.LambdaArgumentDeclaration) Update(io.prestosql.sql.tree.Update) Offset(io.prestosql.sql.tree.Offset) AUTOLOAD_PROP_KEY(io.prestosql.spi.connector.CreateIndexMetadata.AUTOLOAD_PROP_KEY) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) RowExpression(io.prestosql.spi.relation.RowExpression) Cast(io.prestosql.sql.tree.Cast) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) FilterNode(io.prestosql.spi.plan.FilterNode) Assignments(io.prestosql.spi.plan.Assignments) AssignmentItem(io.prestosql.sql.tree.AssignmentItem) Field(io.prestosql.sql.analyzer.Field) PlanNode(io.prestosql.spi.plan.PlanNode) Identifier(io.prestosql.sql.tree.Identifier) RelationType(io.prestosql.sql.analyzer.RelationType) UUID(java.util.UUID) TableMetadata(io.prestosql.metadata.TableMetadata) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) OrderingScheme(io.prestosql.spi.plan.OrderingScheme) Table(io.prestosql.sql.tree.Table) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) SortOrder(io.prestosql.spi.block.SortOrder) OrderingSchemeUtils.sortItemToSortOrder(io.prestosql.sql.planner.OrderingSchemeUtils.sortItemToSortOrder) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) FrameBoundType(io.prestosql.spi.sql.expression.Types.FrameBoundType) Type(io.prestosql.spi.type.Type) WindowFrameType(io.prestosql.spi.sql.expression.Types.WindowFrameType) RelationType(io.prestosql.sql.analyzer.RelationType) Scope(io.prestosql.sql.analyzer.Scope) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) Expression(io.prestosql.sql.tree.Expression) RowExpression(io.prestosql.spi.relation.RowExpression) Analysis(io.prestosql.sql.analyzer.Analysis) TableHandle(io.prestosql.spi.metadata.TableHandle) ProjectNode(io.prestosql.spi.plan.ProjectNode) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 2 with RelationType

use of io.prestosql.sql.analyzer.RelationType in project hetu-core by openlookeng.

the class LogicalPlanner method createInsertPlan.

private RelationPlan createInsertPlan(Analysis analysis, Insert insertStatement) {
    Analysis.Insert insert = analysis.getInsert().get();
    TableMetadata tableMetadata = metadata.getTableMetadata(session, insert.getTarget());
    List<ColumnMetadata> visibleTableColumns = tableMetadata.getColumns().stream().filter(column -> !column.isHidden()).collect(toImmutableList());
    List<String> visibleTableColumnNames = visibleTableColumns.stream().map(ColumnMetadata::getName).collect(toImmutableList());
    RelationPlan plan = createRelationPlan(analysis, insertStatement.getQuery());
    Map<String, ColumnHandle> columns = metadata.getColumnHandles(session, insert.getTarget());
    Assignments.Builder assignments = Assignments.builder();
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        if (column.isHidden()) {
            continue;
        }
        Symbol output = planSymbolAllocator.newSymbol(column.getName(), column.getType());
        int index = insert.getColumns().indexOf(columns.get(column.getName()));
        if (index < 0) {
            Expression cast = new Cast(new NullLiteral(), column.getType().getTypeSignature().toString());
            assignments.put(output, castToRowExpression(cast));
        } else {
            Symbol input = plan.getSymbol(index);
            Type tableType = column.getType();
            Type queryType = planSymbolAllocator.getTypes().get(input);
            if (queryType.equals(tableType) || typeCoercion.isTypeOnlyCoercion(queryType, tableType)) {
                assignments.put(output, castToRowExpression(toSymbolReference(input)));
            } else {
                Expression cast = noTruncationCast(toSymbolReference(input), queryType, tableType);
                assignments.put(output, castToRowExpression(cast));
            }
        }
    }
    ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build());
    List<Field> fields = visibleTableColumns.stream().map(column -> Field.newUnqualified(column.getName(), column.getType())).collect(toImmutableList());
    Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields)).build();
    plan = new RelationPlan(projectNode, scope, projectNode.getOutputSymbols());
    Optional<NewTableLayout> newTableLayout = metadata.getInsertLayout(session, insert.getTarget());
    String catalogName = insert.getTarget().getCatalogName().getCatalogName();
    TableStatisticsMetadata statisticsMetadata = metadata.getStatisticsCollectionMetadataForWrite(session, catalogName, tableMetadata.getMetadata());
    return createTableWriterPlan(analysis, plan, new InsertReference(insert.getTarget(), analysis.isInsertOverwrite()), visibleTableColumnNames, newTableLayout, statisticsMetadata);
}
Also used : GREATER_THAN_OR_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) LongSupplier(java.util.function.LongSupplier) CostCalculator(io.prestosql.cost.CostCalculator) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CreateReference(io.prestosql.sql.planner.plan.TableWriterNode.CreateReference) AggregationNode(io.prestosql.spi.plan.AggregationNode) Cast(io.prestosql.sql.tree.Cast) Statement(io.prestosql.sql.tree.Statement) WarningCollector(io.prestosql.execution.warnings.WarningCollector) ExpressionFormatter(io.prestosql.sql.ExpressionFormatter) SystemSessionProperties.isSkipAttachingStatsWithPlan(io.prestosql.SystemSessionProperties.isSkipAttachingStatsWithPlan) PlanSanityChecker(io.prestosql.sql.planner.sanity.PlanSanityChecker) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OutputNode(io.prestosql.sql.planner.plan.OutputNode) CubeFilter(io.hetu.core.spi.cube.CubeFilter) Identifier(io.prestosql.sql.tree.Identifier) CostProvider(io.prestosql.cost.CostProvider) Delete(io.prestosql.sql.tree.Delete) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TableScanNode(io.prestosql.spi.plan.TableScanNode) TableStatisticsMetadata(io.prestosql.spi.statistics.TableStatisticsMetadata) Set(java.util.Set) NullLiteral(io.prestosql.sql.tree.NullLiteral) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) Insert(io.prestosql.sql.tree.Insert) NodeRef(io.prestosql.sql.tree.NodeRef) CachingCostProvider(io.prestosql.cost.CachingCostProvider) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) ReuseExchangeOperator(io.prestosql.spi.operator.ReuseExchangeOperator) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ExpressionUtils(io.prestosql.sql.ExpressionUtils) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) VacuumTargetReference(io.prestosql.sql.planner.plan.TableWriterNode.VacuumTargetReference) StringLiteral(io.prestosql.sql.tree.StringLiteral) StatisticAggregations(io.prestosql.sql.planner.plan.StatisticAggregations) StatisticsWriterNode(io.prestosql.sql.planner.plan.StatisticsWriterNode) VacuumTableNode(io.prestosql.sql.planner.plan.VacuumTableNode) WriterTarget(io.prestosql.sql.planner.plan.TableWriterNode.WriterTarget) Field(io.prestosql.sql.analyzer.Field) OriginalExpressionUtils(io.prestosql.sql.relational.OriginalExpressionUtils) Analyze(io.prestosql.sql.tree.Analyze) FIXED_HASH_DISTRIBUTION(io.prestosql.sql.planner.SystemPartitioningHandle.FIXED_HASH_DISTRIBUTION) TableMetadata(io.prestosql.metadata.TableMetadata) VacuumTable(io.prestosql.sql.tree.VacuumTable) CharType(io.prestosql.spi.type.CharType) Node(io.prestosql.sql.tree.Node) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) Session(io.prestosql.Session) ParsingUtil.createParsingOptions(io.prestosql.sql.ParsingUtil.createParsingOptions) Signature(io.prestosql.spi.function.Signature) DeleteNode(io.prestosql.sql.planner.plan.DeleteNode) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) StatsProvider(io.prestosql.cost.StatsProvider) Query(io.prestosql.sql.tree.Query) Assignments(io.prestosql.spi.plan.Assignments) QUERY_REJECTED(io.prestosql.spi.StandardErrorCode.QUERY_REJECTED) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) Explain(io.prestosql.sql.tree.Explain) MetadataUtil.toSchemaTableName(io.prestosql.metadata.MetadataUtil.toSchemaTableName) VARBINARY(io.prestosql.spi.type.VarbinaryType.VARBINARY) ValuesNode(io.prestosql.spi.plan.ValuesNode) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) LimitNode(io.prestosql.spi.plan.LimitNode) CubeMetadata(io.hetu.core.spi.cube.CubeMetadata) PlanOptimizer(io.prestosql.sql.planner.optimizations.PlanOptimizer) VarcharType(io.prestosql.spi.type.VarcharType) Expression(io.prestosql.sql.tree.Expression) StatisticAggregationsDescriptor(io.prestosql.sql.planner.plan.StatisticAggregationsDescriptor) QualifiedName(io.prestosql.sql.tree.QualifiedName) APPLY_ALL_RULES(io.prestosql.spi.plan.PlanNode.SkipOptRuleLevel.APPLY_ALL_RULES) SqlParser(io.prestosql.sql.parser.SqlParser) CachingStatsProvider(io.prestosql.cost.CachingStatsProvider) TableFinishNode(io.prestosql.sql.planner.plan.TableFinishNode) Type(io.prestosql.spi.type.Type) TypeCoercion(io.prestosql.type.TypeCoercion) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) StatsCalculator(io.prestosql.cost.StatsCalculator) NewTableLayout(io.prestosql.metadata.NewTableLayout) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CatalogName(io.prestosql.spi.connector.CatalogName) ROW_COUNT(io.prestosql.spi.statistics.TableStatisticType.ROW_COUNT) UUID(java.util.UUID) RelationType(io.prestosql.sql.analyzer.RelationType) CubeFinishNode(io.prestosql.sql.planner.plan.CubeFinishNode) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) OptimizerUtils(io.prestosql.utils.OptimizerUtils) Scope(io.prestosql.sql.analyzer.Scope) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) Analysis(io.prestosql.sql.analyzer.Analysis) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) IfExpression(io.prestosql.sql.tree.IfExpression) StatsAndCosts(io.prestosql.cost.StatsAndCosts) HashMap(java.util.HashMap) RelationId(io.prestosql.sql.analyzer.RelationId) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) NOT_FOUND(io.prestosql.spi.StandardErrorCode.NOT_FOUND) TableHandle(io.prestosql.spi.metadata.TableHandle) Function(java.util.function.Function) HashSet(java.util.HashSet) CubeStatus(io.hetu.core.spi.cube.CubeStatus) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) Verify.verify(com.google.common.base.Verify.verify) InsertReference(io.prestosql.sql.planner.plan.TableWriterNode.InsertReference) Objects.requireNonNull(java.util.Objects.requireNonNull) TableStatisticAggregation(io.prestosql.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation) Symbol(io.prestosql.spi.plan.Symbol) TableWriterNode(io.prestosql.sql.planner.plan.TableWriterNode) CubeUpdateMetadata(io.prestosql.spi.cube.CubeUpdateMetadata) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) UpdateNode(io.prestosql.sql.planner.plan.UpdateNode) LambdaArgumentDeclaration(io.prestosql.sql.tree.LambdaArgumentDeclaration) Update(io.prestosql.sql.tree.Update) InsertCube(io.prestosql.sql.tree.InsertCube) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) DISTRIBUTED_PLAN_SANITY_CHECKER(io.prestosql.sql.planner.sanity.PlanSanityChecker.DISTRIBUTED_PLAN_SANITY_CHECKER) ExplainAnalyzeNode(io.prestosql.sql.planner.plan.ExplainAnalyzeNode) Streams.zip(com.google.common.collect.Streams.zip) Cast(io.prestosql.sql.tree.Cast) TableStatisticsMetadata(io.prestosql.spi.statistics.TableStatisticsMetadata) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) Symbol(io.prestosql.spi.plan.Symbol) NewTableLayout(io.prestosql.metadata.NewTableLayout) Assignments(io.prestosql.spi.plan.Assignments) InsertReference(io.prestosql.sql.planner.plan.TableWriterNode.InsertReference) Field(io.prestosql.sql.analyzer.Field) RelationType(io.prestosql.sql.analyzer.RelationType) TableMetadata(io.prestosql.metadata.TableMetadata) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) Type(io.prestosql.spi.type.Type) RelationType(io.prestosql.sql.analyzer.RelationType) Scope(io.prestosql.sql.analyzer.Scope) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) IfExpression(io.prestosql.sql.tree.IfExpression) Analysis(io.prestosql.sql.analyzer.Analysis) ProjectNode(io.prestosql.spi.plan.ProjectNode) NullLiteral(io.prestosql.sql.tree.NullLiteral)

Example 3 with RelationType

use of io.prestosql.sql.analyzer.RelationType in project hetu-core by openlookeng.

the class LogicalPlanner method createInsertCubePlan.

private RelationPlan createInsertCubePlan(Analysis analysis, InsertCube insertCubeStatement) {
    Analysis.CubeInsert insert = analysis.getCubeInsert().get();
    TableMetadata tableMetadata = metadata.getTableMetadata(session, insert.getTarget());
    List<ColumnMetadata> visibleTableColumns = tableMetadata.getColumns().stream().filter(column -> !column.isHidden()).collect(toImmutableList());
    List<String> visibleTableColumnNames = visibleTableColumns.stream().map(ColumnMetadata::getName).collect(toImmutableList());
    RelationPlan plan = createRelationPlan(analysis, insertCubeStatement.getQuery());
    Map<String, ColumnHandle> columns = metadata.getColumnHandles(session, insert.getTarget());
    Assignments.Builder assignments = Assignments.builder();
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        if (column.isHidden()) {
            continue;
        }
        Symbol output = planSymbolAllocator.newSymbol(column.getName(), column.getType());
        int index = insert.getColumns().indexOf(columns.get(column.getName()));
        if (index < 0) {
            Expression cast = new Cast(new NullLiteral(), column.getType().getTypeSignature().toString());
            assignments.put(output, castToRowExpression(cast));
        } else {
            Symbol input = plan.getSymbol(index);
            Type tableType = column.getType();
            Type queryType = planSymbolAllocator.getTypes().get(input);
            if (queryType.equals(tableType) || typeCoercion.isTypeOnlyCoercion(queryType, tableType)) {
                assignments.put(output, castToRowExpression(toSymbolReference(input)));
            } else {
                Expression cast = noTruncationCast(toSymbolReference(input), queryType, tableType);
                assignments.put(output, castToRowExpression(cast));
            }
        }
    }
    ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build());
    List<Field> fields = visibleTableColumns.stream().map(column -> Field.newUnqualified(column.getName(), column.getType())).collect(toImmutableList());
    Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields)).build();
    plan = new RelationPlan(projectNode, scope, projectNode.getOutputSymbols());
    Optional<NewTableLayout> newTableLayout = metadata.getInsertLayout(session, insert.getTarget());
    String catalogName = insert.getTarget().getCatalogName().getCatalogName();
    TableStatisticsMetadata statisticsMetadata = metadata.getStatisticsCollectionMetadataForWrite(session, catalogName, tableMetadata.getMetadata());
    RelationPlan tableWriterPlan = createTableWriterPlan(analysis, plan, new InsertReference(insert.getTarget(), analysis.isCubeOverwrite()), visibleTableColumnNames, newTableLayout, statisticsMetadata);
    Expression rewritten = null;
    Set<Identifier> predicateColumns = new HashSet<>();
    if (insertCubeStatement.getWhere().isPresent()) {
        rewritten = new QueryPlanner(analysis, planSymbolAllocator, idAllocator, buildLambdaDeclarationToSymbolMap(analysis, planSymbolAllocator), metadata, session, namedSubPlan, uniqueIdAllocator).rewriteExpression(tableWriterPlan, insertCubeStatement.getWhere().get(), analysis, buildLambdaDeclarationToSymbolMap(analysis, planSymbolAllocator));
        predicateColumns.addAll(ExpressionUtils.getIdentifiers(rewritten));
    }
    CubeMetadata cubeMetadata = insert.getMetadata();
    if (!insertCubeStatement.isOverwrite() && !insertCubeStatement.getWhere().isPresent() && cubeMetadata.getCubeStatus() != CubeStatus.INACTIVE) {
        // Means data some data was inserted before, but trying to insert entire dataset
        throw new PrestoException(QUERY_REJECTED, "Cannot allow insert. Inserting entire dataset but cube already has partial data");
    } else if (insertCubeStatement.getWhere().isPresent()) {
        if (!canSupportPredicate(rewritten)) {
            throw new PrestoException(QUERY_REJECTED, String.format("Cannot support predicate '%s'", ExpressionFormatter.formatExpression(rewritten, Optional.empty())));
        }
        if (!insertCubeStatement.isOverwrite() && arePredicatesOverlapping(rewritten, cubeMetadata)) {
            throw new PrestoException(QUERY_REJECTED, String.format("Cannot allow insert. Cube already contains data for the given predicate '%s'", ExpressionFormatter.formatExpression(insertCubeStatement.getWhere().get(), Optional.empty())));
        }
    }
    TableHandle sourceTableHandle = insert.getSourceTable();
    // At this point it has been verified that source table has not been updated
    // so insert into cube should be allowed
    LongSupplier tableLastModifiedTimeSupplier = metadata.getTableLastModifiedTimeSupplier(session, sourceTableHandle);
    checkState(tableLastModifiedTimeSupplier != null, "Table last modified time is null");
    Map<Symbol, Type> predicateColumnsType = predicateColumns.stream().map(identifier -> new Symbol(identifier.getValue())).collect(Collectors.toMap(Function.identity(), symbol -> planSymbolAllocator.getTypes().get(symbol), (key1, ignored) -> key1));
    CubeFinishNode cubeFinishNode = new CubeFinishNode(idAllocator.getNextId(), tableWriterPlan.getRoot(), planSymbolAllocator.newSymbol("rows", BIGINT), new CubeUpdateMetadata(tableMetadata.getQualifiedName().toString(), tableLastModifiedTimeSupplier.getAsLong(), rewritten != null ? ExpressionFormatter.formatExpression(rewritten, Optional.empty()) : null, insertCubeStatement.isOverwrite()), predicateColumnsType);
    return new RelationPlan(cubeFinishNode, analysis.getScope(insertCubeStatement), cubeFinishNode.getOutputSymbols());
}
Also used : GREATER_THAN_OR_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) LongSupplier(java.util.function.LongSupplier) CostCalculator(io.prestosql.cost.CostCalculator) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CreateReference(io.prestosql.sql.planner.plan.TableWriterNode.CreateReference) AggregationNode(io.prestosql.spi.plan.AggregationNode) Cast(io.prestosql.sql.tree.Cast) Statement(io.prestosql.sql.tree.Statement) WarningCollector(io.prestosql.execution.warnings.WarningCollector) ExpressionFormatter(io.prestosql.sql.ExpressionFormatter) SystemSessionProperties.isSkipAttachingStatsWithPlan(io.prestosql.SystemSessionProperties.isSkipAttachingStatsWithPlan) PlanSanityChecker(io.prestosql.sql.planner.sanity.PlanSanityChecker) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OutputNode(io.prestosql.sql.planner.plan.OutputNode) CubeFilter(io.hetu.core.spi.cube.CubeFilter) Identifier(io.prestosql.sql.tree.Identifier) CostProvider(io.prestosql.cost.CostProvider) Delete(io.prestosql.sql.tree.Delete) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TableScanNode(io.prestosql.spi.plan.TableScanNode) TableStatisticsMetadata(io.prestosql.spi.statistics.TableStatisticsMetadata) Set(java.util.Set) NullLiteral(io.prestosql.sql.tree.NullLiteral) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) Insert(io.prestosql.sql.tree.Insert) NodeRef(io.prestosql.sql.tree.NodeRef) CachingCostProvider(io.prestosql.cost.CachingCostProvider) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) ReuseExchangeOperator(io.prestosql.spi.operator.ReuseExchangeOperator) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ExpressionUtils(io.prestosql.sql.ExpressionUtils) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) VacuumTargetReference(io.prestosql.sql.planner.plan.TableWriterNode.VacuumTargetReference) StringLiteral(io.prestosql.sql.tree.StringLiteral) StatisticAggregations(io.prestosql.sql.planner.plan.StatisticAggregations) StatisticsWriterNode(io.prestosql.sql.planner.plan.StatisticsWriterNode) VacuumTableNode(io.prestosql.sql.planner.plan.VacuumTableNode) WriterTarget(io.prestosql.sql.planner.plan.TableWriterNode.WriterTarget) Field(io.prestosql.sql.analyzer.Field) OriginalExpressionUtils(io.prestosql.sql.relational.OriginalExpressionUtils) Analyze(io.prestosql.sql.tree.Analyze) FIXED_HASH_DISTRIBUTION(io.prestosql.sql.planner.SystemPartitioningHandle.FIXED_HASH_DISTRIBUTION) TableMetadata(io.prestosql.metadata.TableMetadata) VacuumTable(io.prestosql.sql.tree.VacuumTable) CharType(io.prestosql.spi.type.CharType) Node(io.prestosql.sql.tree.Node) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) Session(io.prestosql.Session) ParsingUtil.createParsingOptions(io.prestosql.sql.ParsingUtil.createParsingOptions) Signature(io.prestosql.spi.function.Signature) DeleteNode(io.prestosql.sql.planner.plan.DeleteNode) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) StatsProvider(io.prestosql.cost.StatsProvider) Query(io.prestosql.sql.tree.Query) Assignments(io.prestosql.spi.plan.Assignments) QUERY_REJECTED(io.prestosql.spi.StandardErrorCode.QUERY_REJECTED) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) Explain(io.prestosql.sql.tree.Explain) MetadataUtil.toSchemaTableName(io.prestosql.metadata.MetadataUtil.toSchemaTableName) VARBINARY(io.prestosql.spi.type.VarbinaryType.VARBINARY) ValuesNode(io.prestosql.spi.plan.ValuesNode) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) LimitNode(io.prestosql.spi.plan.LimitNode) CubeMetadata(io.hetu.core.spi.cube.CubeMetadata) PlanOptimizer(io.prestosql.sql.planner.optimizations.PlanOptimizer) VarcharType(io.prestosql.spi.type.VarcharType) Expression(io.prestosql.sql.tree.Expression) StatisticAggregationsDescriptor(io.prestosql.sql.planner.plan.StatisticAggregationsDescriptor) QualifiedName(io.prestosql.sql.tree.QualifiedName) APPLY_ALL_RULES(io.prestosql.spi.plan.PlanNode.SkipOptRuleLevel.APPLY_ALL_RULES) SqlParser(io.prestosql.sql.parser.SqlParser) CachingStatsProvider(io.prestosql.cost.CachingStatsProvider) TableFinishNode(io.prestosql.sql.planner.plan.TableFinishNode) Type(io.prestosql.spi.type.Type) TypeCoercion(io.prestosql.type.TypeCoercion) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) StatsCalculator(io.prestosql.cost.StatsCalculator) NewTableLayout(io.prestosql.metadata.NewTableLayout) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CatalogName(io.prestosql.spi.connector.CatalogName) ROW_COUNT(io.prestosql.spi.statistics.TableStatisticType.ROW_COUNT) UUID(java.util.UUID) RelationType(io.prestosql.sql.analyzer.RelationType) CubeFinishNode(io.prestosql.sql.planner.plan.CubeFinishNode) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) OptimizerUtils(io.prestosql.utils.OptimizerUtils) Scope(io.prestosql.sql.analyzer.Scope) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) Analysis(io.prestosql.sql.analyzer.Analysis) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) IfExpression(io.prestosql.sql.tree.IfExpression) StatsAndCosts(io.prestosql.cost.StatsAndCosts) HashMap(java.util.HashMap) RelationId(io.prestosql.sql.analyzer.RelationId) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) NOT_FOUND(io.prestosql.spi.StandardErrorCode.NOT_FOUND) TableHandle(io.prestosql.spi.metadata.TableHandle) Function(java.util.function.Function) HashSet(java.util.HashSet) CubeStatus(io.hetu.core.spi.cube.CubeStatus) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) Verify.verify(com.google.common.base.Verify.verify) InsertReference(io.prestosql.sql.planner.plan.TableWriterNode.InsertReference) Objects.requireNonNull(java.util.Objects.requireNonNull) TableStatisticAggregation(io.prestosql.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation) Symbol(io.prestosql.spi.plan.Symbol) TableWriterNode(io.prestosql.sql.planner.plan.TableWriterNode) CubeUpdateMetadata(io.prestosql.spi.cube.CubeUpdateMetadata) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) UpdateNode(io.prestosql.sql.planner.plan.UpdateNode) LambdaArgumentDeclaration(io.prestosql.sql.tree.LambdaArgumentDeclaration) Update(io.prestosql.sql.tree.Update) InsertCube(io.prestosql.sql.tree.InsertCube) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) DISTRIBUTED_PLAN_SANITY_CHECKER(io.prestosql.sql.planner.sanity.PlanSanityChecker.DISTRIBUTED_PLAN_SANITY_CHECKER) ExplainAnalyzeNode(io.prestosql.sql.planner.plan.ExplainAnalyzeNode) Streams.zip(com.google.common.collect.Streams.zip) Cast(io.prestosql.sql.tree.Cast) TableStatisticsMetadata(io.prestosql.spi.statistics.TableStatisticsMetadata) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) Symbol(io.prestosql.spi.plan.Symbol) NewTableLayout(io.prestosql.metadata.NewTableLayout) CubeUpdateMetadata(io.prestosql.spi.cube.CubeUpdateMetadata) Assignments(io.prestosql.spi.plan.Assignments) PrestoException(io.prestosql.spi.PrestoException) CubeMetadata(io.hetu.core.spi.cube.CubeMetadata) InsertReference(io.prestosql.sql.planner.plan.TableWriterNode.InsertReference) Field(io.prestosql.sql.analyzer.Field) Identifier(io.prestosql.sql.tree.Identifier) RelationType(io.prestosql.sql.analyzer.RelationType) HashSet(java.util.HashSet) TableMetadata(io.prestosql.metadata.TableMetadata) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) CubeFinishNode(io.prestosql.sql.planner.plan.CubeFinishNode) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) Type(io.prestosql.spi.type.Type) RelationType(io.prestosql.sql.analyzer.RelationType) Scope(io.prestosql.sql.analyzer.Scope) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) IfExpression(io.prestosql.sql.tree.IfExpression) Analysis(io.prestosql.sql.analyzer.Analysis) ProjectNode(io.prestosql.spi.plan.ProjectNode) TableHandle(io.prestosql.spi.metadata.TableHandle) LongSupplier(java.util.function.LongSupplier) NullLiteral(io.prestosql.sql.tree.NullLiteral)

Example 4 with RelationType

use of io.prestosql.sql.analyzer.RelationType in project hetu-core by openlookeng.

the class LogicalPlanner method createOutputPlan.

private PlanNode createOutputPlan(RelationPlan plan, Analysis analysis) {
    ImmutableList.Builder<Symbol> outputs = ImmutableList.builder();
    ImmutableList.Builder<String> names = ImmutableList.builder();
    int columnNumber = 0;
    RelationType outputDescriptor = analysis.getOutputDescriptor();
    for (Field field : outputDescriptor.getVisibleFields()) {
        String name = field.getName().orElse("_col" + columnNumber);
        names.add(name);
        int fieldIndex = outputDescriptor.indexOf(field);
        Symbol symbol = plan.getSymbol(fieldIndex);
        outputs.add(symbol);
        columnNumber++;
    }
    return new OutputNode(idAllocator.getNextId(), plan.getRoot(), names.build(), outputs.build());
}
Also used : Field(io.prestosql.sql.analyzer.Field) OutputNode(io.prestosql.sql.planner.plan.OutputNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) RelationType(io.prestosql.sql.analyzer.RelationType)

Example 5 with RelationType

use of io.prestosql.sql.analyzer.RelationType in project hetu-core by openlookeng.

the class RelationPlanner method planCrossJoinUnnest.

private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create symbols for the result of unnesting
    ImmutableList.Builder<Symbol> unnestedSymbolsBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
        Symbol symbol = planSymbolAllocator.newSymbol(field);
        unnestedSymbolsBuilder.add(symbol);
    }
    ImmutableList<Symbol> unnestedSymbols = unnestedSymbolsBuilder.build();
    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = planBuilder.appendProjections(node.getExpressions(), planSymbolAllocator, idAllocator);
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    UnmodifiableIterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Symbol inputSymbol = translations.get(expression);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (elementType instanceof RowType) {
                ImmutableList.Builder<Symbol> unnestSymbolBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestSymbolBuilder.add(unnestedSymbolsIterator.next());
                }
                unnestSymbols.put(inputSymbol, unnestSymbolBuilder.build());
            } else {
                unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputSymbols());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) RowType(io.prestosql.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(io.prestosql.spi.type.MapType) ArrayType(io.prestosql.spi.type.ArrayType) Field(io.prestosql.sql.analyzer.Field) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) UnnestNode(io.prestosql.sql.planner.plan.UnnestNode) RelationType(io.prestosql.sql.analyzer.RelationType) ProjectNode(io.prestosql.spi.plan.ProjectNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 Symbol (io.prestosql.spi.plan.Symbol)9 RelationType (io.prestosql.sql.analyzer.RelationType)9 Field (io.prestosql.sql.analyzer.Field)8 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)8 Expression (io.prestosql.sql.tree.Expression)8 ProjectNode (io.prestosql.spi.plan.ProjectNode)7 Type (io.prestosql.spi.type.Type)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 Assignments (io.prestosql.spi.plan.Assignments)6 PlanNode (io.prestosql.spi.plan.PlanNode)6 RowExpression (io.prestosql.spi.relation.RowExpression)6 Cast (io.prestosql.sql.tree.Cast)6 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)5 TableHandle (io.prestosql.spi.metadata.TableHandle)5 Scope (io.prestosql.sql.analyzer.Scope)5 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)5 ArrayList (java.util.ArrayList)5 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)4