Search in sources :

Example 21 with TableHandle

use of com.facebook.presto.spi.TableHandle in project presto by prestodb.

the class TableWriteInfo method createDeleteScanInfo.

private static Optional<DeleteScanInfo> createDeleteScanInfo(StreamingSubPlan plan, Optional<ExecutionWriterTarget> writerTarget, Metadata metadata, Session session) {
    if (writerTarget.isPresent() && writerTarget.get() instanceof ExecutionWriterTarget.DeleteHandle) {
        TableHandle tableHandle = ((ExecutionWriterTarget.DeleteHandle) writerTarget.get()).getHandle();
        DeleteNode delete = getOnlyElement(findPlanNodes(plan, DeleteNode.class));
        TableScanNode tableScan = getDeleteTableScan(delete);
        TupleDomain<ColumnHandle> originalEnforcedConstraint = tableScan.getEnforcedConstraint();
        TableLayoutResult layoutResult = metadata.getLayout(session, tableHandle, new Constraint<>(originalEnforcedConstraint), Optional.of(ImmutableSet.copyOf(tableScan.getAssignments().values())));
        return Optional.of(new DeleteScanInfo(tableScan.getId(), layoutResult.getLayout().getNewTableHandle()));
    }
    return Optional.empty();
}
Also used : DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) TableHandle(com.facebook.presto.spi.TableHandle) AnalyzeTableHandle(com.facebook.presto.metadata.AnalyzeTableHandle) TableLayoutResult(com.facebook.presto.metadata.TableLayoutResult)

Example 22 with TableHandle

use of com.facebook.presto.spi.TableHandle in project presto by prestodb.

the class LogicalPlanner method createRefreshMaterializedViewPlan.

private RelationPlan createRefreshMaterializedViewPlan(Analysis analysis, RefreshMaterializedView refreshMaterializedViewStatement) {
    Analysis.RefreshMaterializedViewAnalysis viewAnalysis = analysis.getRefreshMaterializedViewAnalysis().get();
    TableHandle tableHandle = viewAnalysis.getTarget();
    List<ColumnHandle> columnHandles = viewAnalysis.getColumns();
    WriterTarget target = new RefreshMaterializedViewReference(tableHandle, metadata.getTableMetadata(session, tableHandle).getTable());
    return buildInternalInsertPlan(tableHandle, columnHandles, viewAnalysis.getQuery(), analysis, target);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) Analysis(com.facebook.presto.sql.analyzer.Analysis) TableHandle(com.facebook.presto.spi.TableHandle) WriterTarget(com.facebook.presto.sql.planner.plan.TableWriterNode.WriterTarget) RefreshMaterializedViewReference(com.facebook.presto.sql.planner.plan.TableWriterNode.RefreshMaterializedViewReference)

Example 23 with TableHandle

use of com.facebook.presto.spi.TableHandle in project presto by prestodb.

the class LogicalPlanner method createInsertPlan.

private RelationPlan createInsertPlan(Analysis analysis, Insert insertStatement) {
    Analysis.Insert insertAnalysis = analysis.getInsert().get();
    TableHandle tableHandle = insertAnalysis.getTarget();
    List<ColumnHandle> columnHandles = insertAnalysis.getColumns();
    WriterTarget target = new InsertReference(tableHandle, metadata.getTableMetadata(session, tableHandle).getTable());
    return buildInternalInsertPlan(tableHandle, columnHandles, insertStatement.getQuery(), analysis, target);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) Analysis(com.facebook.presto.sql.analyzer.Analysis) TableHandle(com.facebook.presto.spi.TableHandle) WriterTarget(com.facebook.presto.sql.planner.plan.TableWriterNode.WriterTarget) InsertReference(com.facebook.presto.sql.planner.plan.TableWriterNode.InsertReference)

Example 24 with TableHandle

use of com.facebook.presto.spi.TableHandle in project presto by prestodb.

the class LogicalPlanner method buildInternalInsertPlan.

private RelationPlan buildInternalInsertPlan(TableHandle tableHandle, List<ColumnHandle> columnHandles, Query query, Analysis analysis, WriterTarget target) {
    TableMetadata tableMetadata = metadata.getTableMetadata(session, tableHandle);
    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, query);
    Map<String, ColumnHandle> columns = metadata.getColumnHandles(session, tableHandle);
    Assignments.Builder assignments = Assignments.builder();
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        if (column.isHidden()) {
            continue;
        }
        VariableReferenceExpression output = variableAllocator.newVariable(getSourceLocation(query), column.getName(), column.getType());
        int index = columnHandles.indexOf(columns.get(column.getName()));
        if (index < 0) {
            Expression cast = new Cast(new NullLiteral(), column.getType().getTypeSignature().toString());
            assignments.put(output, castToRowExpression(cast));
        } else {
            VariableReferenceExpression input = plan.getVariable(index);
            Type tableType = column.getType();
            Type queryType = input.getType();
            if (queryType.equals(tableType) || metadata.getFunctionAndTypeManager().isTypeOnlyCoercion(queryType, tableType)) {
                assignments.put(output, castToRowExpression(createSymbolReference(input)));
            } else {
                Expression cast = new Cast(createSymbolReference(input), tableType.getTypeSignature().toString());
                assignments.put(output, castToRowExpression(cast));
            }
        }
    }
    ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build());
    List<Field> fields = visibleTableColumns.stream().map(column -> Field.newUnqualified(query.getLocation(), column.getName(), column.getType())).collect(toImmutableList());
    Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields)).build();
    plan = new RelationPlan(projectNode, scope, projectNode.getOutputVariables());
    Optional<NewTableLayout> newTableLayout = metadata.getInsertLayout(session, tableHandle);
    Optional<NewTableLayout> preferredShuffleLayout = metadata.getPreferredShuffleLayoutForInsert(session, tableHandle);
    String catalogName = tableHandle.getConnectorId().getCatalogName();
    TableStatisticsMetadata statisticsMetadata = metadata.getStatisticsCollectionMetadataForWrite(session, catalogName, tableMetadata.getMetadata());
    return createTableWriterPlan(analysis, plan, target, visibleTableColumnNames, visibleTableColumns, newTableLayout, preferredShuffleLayout, statisticsMetadata);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) FINAL(com.facebook.presto.spi.plan.LimitNode.Step.FINAL) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) Analyze(com.facebook.presto.sql.tree.Analyze) Field(com.facebook.presto.sql.analyzer.Field) CachingStatsProvider(com.facebook.presto.cost.CachingStatsProvider) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) TableStatisticAggregation(com.facebook.presto.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation) Delete(com.facebook.presto.sql.tree.Delete) Map(java.util.Map) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) Query(com.facebook.presto.sql.tree.Query) Explain(com.facebook.presto.sql.tree.Explain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) LimitNode(com.facebook.presto.spi.plan.LimitNode) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) DeleteHandle(com.facebook.presto.sql.planner.plan.TableWriterNode.DeleteHandle) ExpressionTreeUtils.getSourceLocation(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.getSourceLocation) TableStatisticsMetadata(com.facebook.presto.spi.statistics.TableStatisticsMetadata) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) SystemSessionProperties.isPrintStatsForNonJoinQuery(com.facebook.presto.SystemSessionProperties.isPrintStatsForNonJoinQuery) NewTableLayout(com.facebook.presto.metadata.NewTableLayout) Assignments(com.facebook.presto.spi.plan.Assignments) ArrayList(java.util.ArrayList) Identifier(com.facebook.presto.sql.tree.Identifier) LinkedHashMap(java.util.LinkedHashMap) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) TableHandle(com.facebook.presto.spi.TableHandle) Cast(com.facebook.presto.sql.tree.Cast) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) MetadataUtil.toSchemaTableName(com.facebook.presto.metadata.MetadataUtil.toSchemaTableName) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) Session(com.facebook.presto.Session) InsertReference(com.facebook.presto.sql.planner.plan.TableWriterNode.InsertReference) StatsProvider(com.facebook.presto.cost.StatsProvider) RefreshMaterializedViewReference(com.facebook.presto.sql.planner.plan.TableWriterNode.RefreshMaterializedViewReference) 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) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) Metadata(com.facebook.presto.metadata.Metadata) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) TableMetadata(com.facebook.presto.metadata.TableMetadata) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ExplainAnalyzeNode(com.facebook.presto.sql.planner.plan.ExplainAnalyzeNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) StatsCalculator(com.facebook.presto.cost.StatsCalculator) StatisticAggregations(com.facebook.presto.sql.planner.plan.StatisticAggregations) RefreshMaterializedView(com.facebook.presto.sql.tree.RefreshMaterializedView) CostProvider(com.facebook.presto.cost.CostProvider) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) RelationId(com.facebook.presto.sql.analyzer.RelationId) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CostCalculator(com.facebook.presto.cost.CostCalculator) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Entry(java.util.Map.Entry) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) ConnectorId(com.facebook.presto.spi.ConnectorId) CachingCostProvider(com.facebook.presto.cost.CachingCostProvider) WriterTarget(com.facebook.presto.sql.planner.plan.TableWriterNode.WriterTarget) PrestoException(com.facebook.presto.spi.PrestoException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) ImmutableList(com.google.common.collect.ImmutableList) PlanChecker(com.facebook.presto.sql.planner.sanity.PlanChecker) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) Type(com.facebook.presto.common.type.Type) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) ROW_COUNT(com.facebook.presto.spi.statistics.TableStatisticType.ROW_COUNT) PlanNodeSearcher(com.facebook.presto.sql.planner.optimizations.PlanNodeSearcher) RelationType(com.facebook.presto.sql.analyzer.RelationType) VARBINARY(com.facebook.presto.common.type.VarbinaryType.VARBINARY) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) Insert(com.facebook.presto.sql.tree.Insert) PlanNode(com.facebook.presto.spi.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) CreateName(com.facebook.presto.sql.planner.plan.TableWriterNode.CreateName) StatsAndCosts(com.facebook.presto.cost.StatsAndCosts) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) Streams.zip(com.google.common.collect.Streams.zip) StatisticsWriterNode(com.facebook.presto.sql.planner.plan.StatisticsWriterNode) Statement(com.facebook.presto.sql.tree.Statement) Cast(com.facebook.presto.sql.tree.Cast) TableStatisticsMetadata(com.facebook.presto.spi.statistics.TableStatisticsMetadata) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NewTableLayout(com.facebook.presto.metadata.NewTableLayout) Assignments(com.facebook.presto.spi.plan.Assignments) Field(com.facebook.presto.sql.analyzer.Field) RelationType(com.facebook.presto.sql.analyzer.RelationType) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) TableMetadata(com.facebook.presto.metadata.TableMetadata) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) Scope(com.facebook.presto.sql.analyzer.Scope) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) NullLiteral(com.facebook.presto.sql.tree.NullLiteral)

Example 25 with TableHandle

use of com.facebook.presto.spi.TableHandle in project presto by prestodb.

the class TestDruidQueryBase method tableScan.

protected TableScanNode tableScan(PlanBuilder planBuilder, DruidTableHandle connectorTableHandle, DruidColumnHandle... columnHandles) {
    List<VariableReferenceExpression> variables = Arrays.stream(columnHandles).map(ch -> new VariableReferenceExpression(Optional.empty(), ch.getColumnName().toLowerCase(ENGLISH), ch.getColumnType())).collect(toImmutableList());
    ImmutableMap.Builder<VariableReferenceExpression, ColumnHandle> assignments = ImmutableMap.builder();
    for (int i = 0; i < variables.size(); ++i) {
        assignments.put(variables.get(i), columnHandles[i]);
    }
    TableHandle tableHandle = new TableHandle(druidConnectorId, connectorTableHandle, TestingTransactionHandle.create(), Optional.empty());
    return new TableScanNode(Optional.empty(), planBuilder.getIdAllocator().getNextId(), tableHandle, variables, assignments.build(), TupleDomain.all(), TupleDomain.all());
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) WarningCollector(com.facebook.presto.spi.WarningCollector) FINAL(com.facebook.presto.spi.plan.LimitNode.Step.FINAL) Arrays(java.util.Arrays) MetadataManager(com.facebook.presto.metadata.MetadataManager) SqlToRowExpressionTranslator(com.facebook.presto.sql.relational.SqlToRowExpressionTranslator) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) StandardFunctionResolution(com.facebook.presto.spi.function.StandardFunctionResolution) Collectors.toMap(java.util.stream.Collectors.toMap) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) SESSION(com.facebook.presto.testing.TestingConnectorSession.SESSION) Map(java.util.Map) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) ENGLISH(java.util.Locale.ENGLISH) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Ordering(com.facebook.presto.spi.plan.Ordering) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SqlParser(com.facebook.presto.sql.parser.SqlParser) ConnectorSession(com.facebook.presto.spi.ConnectorSession) LimitNode(com.facebook.presto.spi.plan.LimitNode) List(java.util.List) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) ExpressionAnalyzer.getExpressionTypes(com.facebook.presto.sql.analyzer.ExpressionAnalyzer.getExpressionTypes) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) ConnectorId(com.facebook.presto.spi.ConnectorId) ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) TABLE_COLUMN(com.facebook.presto.druid.DruidQueryGeneratorContext.Origin.TABLE_COLUMN) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) IntStream(java.util.stream.IntStream) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) Assignments(com.facebook.presto.spi.plan.Assignments) TIMESTAMP(com.facebook.presto.common.type.TimestampType.TIMESTAMP) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) ExpressionUtils(com.facebook.presto.sql.ExpressionUtils) TestingSession(com.facebook.presto.testing.TestingSession) LinkedHashMap(java.util.LinkedHashMap) FilterNode(com.facebook.presto.spi.plan.FilterNode) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) TableHandle(com.facebook.presto.spi.TableHandle) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) Type(com.facebook.presto.common.type.Type) RowExpression(com.facebook.presto.spi.relation.RowExpression) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) Session(com.facebook.presto.Session) TestingTransactionHandle(com.facebook.presto.testing.TestingTransactionHandle) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) DERIVED(com.facebook.presto.druid.DruidQueryGeneratorContext.Origin.DERIVED) NodeRef(com.facebook.presto.sql.tree.NodeRef) PlanNode(com.facebook.presto.spi.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) TopNNode(com.facebook.presto.spi.plan.TopNNode) REGULAR(com.facebook.presto.druid.DruidColumnHandle.DruidColumnType.REGULAR) Metadata(com.facebook.presto.metadata.Metadata) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableHandle(com.facebook.presto.spi.TableHandle) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

TableHandle (com.facebook.presto.spi.TableHandle)70 ConnectorId (com.facebook.presto.spi.ConnectorId)37 ColumnHandle (com.facebook.presto.spi.ColumnHandle)29 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)22 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)21 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)21 ImmutableList (com.google.common.collect.ImmutableList)20 ImmutableMap (com.google.common.collect.ImmutableMap)19 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)18 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)16 Session (com.facebook.presto.Session)15 Type (com.facebook.presto.common.type.Type)15 ConnectorOutputTableHandle (com.facebook.presto.spi.ConnectorOutputTableHandle)15 Optional (java.util.Optional)15 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)14 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)14 List (java.util.List)14 Metadata (com.facebook.presto.metadata.Metadata)13 ConnectorInsertTableHandle (com.facebook.presto.spi.ConnectorInsertTableHandle)13 ConnectorSession (com.facebook.presto.spi.ConnectorSession)13