Search in sources :

Example 21 with ColumnMetadata

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

the class InformationSchemaMetadata method getColumnMetadata.

@Override
public ColumnMetadata getColumnMetadata(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle columnHandle) {
    InformationSchemaTableHandle informationSchemaTableHandle = checkTableHandle(tableHandle);
    ConnectorTableMetadata tableMetadata = TABLES.get(informationSchemaTableHandle.getSchemaTableName());
    String columnName = ((InformationSchemaColumnHandle) columnHandle).getColumnName();
    ColumnMetadata columnMetadata = findColumnMetadata(tableMetadata, columnName);
    checkArgument(columnMetadata != null, "Column %s on table %s does not exist", columnName, tableMetadata.getTable());
    return columnMetadata;
}
Also used : MetadataUtil.findColumnMetadata(com.facebook.presto.metadata.MetadataUtil.findColumnMetadata) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Example 22 with ColumnMetadata

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

the class InformationSchemaPageSourceProvider method buildColumns.

private InternalTable buildColumns(Session session, String catalogName, Map<String, NullableValue> filters) {
    InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns(TABLE_COLUMNS));
    QualifiedTablePrefix prefix = extractQualifiedTablePrefix(catalogName, filters);
    for (Entry<SchemaTableName, List<ColumnMetadata>> entry : listTableColumns(session, metadata, accessControl, prefix).entrySet()) {
        SchemaTableName tableName = entry.getKey();
        int ordinalPosition = 1;
        for (ColumnMetadata column : entry.getValue()) {
            if (column.isHidden()) {
                continue;
            }
            table.add(catalogName, tableName.getSchemaName(), tableName.getTableName(), column.getName(), ordinalPosition, null, "YES", column.getType().getDisplayName(), column.getComment(), column.getExtraInfo());
            ordinalPosition++;
        }
    }
    return table.build();
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) QualifiedTablePrefix(com.facebook.presto.metadata.QualifiedTablePrefix) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) InternalTable(com.facebook.presto.metadata.InternalTable) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Constraint(com.facebook.presto.spi.Constraint)

Example 23 with ColumnMetadata

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

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 = symbolAllocator.newSymbol(column.getName(), column.getType());
        int index = insert.getColumns().indexOf(columns.get(column.getName()));
        if (index < 0) {
            assignments.put(output, new NullLiteral());
        } else {
            Symbol input = plan.getSymbol(index);
            Type tableType = column.getType();
            Type queryType = symbolAllocator.getTypes().get(input);
            if (queryType.equals(tableType) || metadata.getTypeManager().isTypeOnlyCoercion(queryType, tableType)) {
                assignments.put(output, input.toSymbolReference());
            } else {
                Expression cast = new Cast(input.toSymbolReference(), tableType.getTypeSignature().toString());
                assignments.put(output, 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(new RelationType(fields)).build();
    plan = new RelationPlan(projectNode, scope, projectNode.getOutputSymbols());
    Optional<NewTableLayout> newTableLayout = metadata.getInsertLayout(session, insert.getTarget());
    return createTableWriterPlan(analysis, plan, new InsertReference(insert.getTarget()), visibleTableColumnNames, newTableLayout);
}
Also used : QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) TableMetadata(com.facebook.presto.metadata.TableMetadata) ExplainAnalyzeNode(com.facebook.presto.sql.planner.plan.ExplainAnalyzeNode) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) Field(com.facebook.presto.sql.analyzer.Field) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) Delete(com.facebook.presto.sql.tree.Delete) Map(java.util.Map) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Assignments(com.facebook.presto.sql.planner.plan.Assignments) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) ImmutableSet(com.google.common.collect.ImmutableSet) Query(com.facebook.presto.sql.tree.Query) Explain(com.facebook.presto.sql.tree.Explain) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) ConnectorId(com.facebook.presto.connector.ConnectorId) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) NewTableLayout(com.facebook.presto.metadata.NewTableLayout) WriterTarget(com.facebook.presto.sql.planner.plan.TableWriterNode.WriterTarget) PrestoException(com.facebook.presto.spi.PrestoException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Type(com.facebook.presto.spi.type.Type) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Objects.requireNonNull(java.util.Objects.requireNonNull) Cast(com.facebook.presto.sql.tree.Cast) VARBINARY(com.facebook.presto.spi.type.VarbinaryType.VARBINARY) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) PlanSanityChecker(com.facebook.presto.sql.planner.sanity.PlanSanityChecker) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Session(com.facebook.presto.Session) InsertReference(com.facebook.presto.sql.planner.plan.TableWriterNode.InsertReference) RelationType(com.facebook.presto.sql.analyzer.RelationType) DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) Insert(com.facebook.presto.sql.tree.Insert) Scope(com.facebook.presto.sql.analyzer.Scope) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) Expression(com.facebook.presto.sql.tree.Expression) ColumnHandle(com.facebook.presto.spi.ColumnHandle) CreateName(com.facebook.presto.sql.planner.plan.TableWriterNode.CreateName) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) Metadata(com.facebook.presto.metadata.Metadata) Statement(com.facebook.presto.sql.tree.Statement) Cast(com.facebook.presto.sql.tree.Cast) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NewTableLayout(com.facebook.presto.metadata.NewTableLayout) Assignments(com.facebook.presto.sql.planner.plan.Assignments) InsertReference(com.facebook.presto.sql.planner.plan.TableWriterNode.InsertReference) Field(com.facebook.presto.sql.analyzer.Field) RelationType(com.facebook.presto.sql.analyzer.RelationType) TableMetadata(com.facebook.presto.metadata.TableMetadata) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Type(com.facebook.presto.spi.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) Scope(com.facebook.presto.sql.analyzer.Scope) Expression(com.facebook.presto.sql.tree.Expression) Analysis(com.facebook.presto.sql.analyzer.Analysis) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) NullLiteral(com.facebook.presto.sql.tree.NullLiteral)

Example 24 with ColumnMetadata

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

the class LocalQueryRunner method createTableScanOperator.

public OperatorFactory createTableScanOperator(Session session, int operatorId, PlanNodeId planNodeId, String tableName, String... columnNames) {
    checkArgument(session.getCatalog().isPresent(), "catalog not set");
    checkArgument(session.getSchema().isPresent(), "schema not set");
    // look up the table
    QualifiedObjectName qualifiedTableName = new QualifiedObjectName(session.getCatalog().get(), session.getSchema().get(), tableName);
    TableHandle tableHandle = metadata.getTableHandle(session, qualifiedTableName).orElse(null);
    checkArgument(tableHandle != null, "Table %s does not exist", qualifiedTableName);
    // lookup the columns
    Map<String, ColumnHandle> allColumnHandles = metadata.getColumnHandles(session, tableHandle);
    ImmutableList.Builder<ColumnHandle> columnHandlesBuilder = ImmutableList.builder();
    ImmutableList.Builder<Type> columnTypesBuilder = ImmutableList.builder();
    for (String columnName : columnNames) {
        ColumnHandle columnHandle = allColumnHandles.get(columnName);
        checkArgument(columnHandle != null, "Table %s does not have a column %s", tableName, columnName);
        columnHandlesBuilder.add(columnHandle);
        ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle, columnHandle);
        columnTypesBuilder.add(columnMetadata.getType());
    }
    List<ColumnHandle> columnHandles = columnHandlesBuilder.build();
    List<Type> columnTypes = columnTypesBuilder.build();
    // get the split for this table
    List<TableLayoutResult> layouts = metadata.getLayouts(session, tableHandle, Constraint.alwaysTrue(), Optional.empty());
    Split split = getLocalQuerySplit(session, layouts.get(0).getLayout().getHandle());
    return new OperatorFactory() {

        @Override
        public List<Type> getTypes() {
            return columnTypes;
        }

        @Override
        public Operator createOperator(DriverContext driverContext) {
            OperatorContext operatorContext = driverContext.addOperatorContext(operatorId, planNodeId, "BenchmarkSource");
            ConnectorPageSource pageSource = pageSourceManager.createPageSource(session, split, columnHandles);
            return new PageSourceOperator(pageSource, columnTypes, operatorContext);
        }

        @Override
        public void close() {
        }

        @Override
        public OperatorFactory duplicate() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) DriverContext(com.facebook.presto.operator.DriverContext) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) TableLayoutResult(com.facebook.presto.metadata.TableLayoutResult) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) PageSourceOperator(com.facebook.presto.operator.PageSourceOperator) Type(com.facebook.presto.spi.type.Type) OperatorFactory(com.facebook.presto.operator.OperatorFactory) OperatorContext(com.facebook.presto.operator.OperatorContext) TableHandle(com.facebook.presto.metadata.TableHandle) ScheduledSplit(com.facebook.presto.ScheduledSplit) Split(com.facebook.presto.metadata.Split)

Example 25 with ColumnMetadata

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

the class TestingMetadata method addColumn.

@Override
public void addColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnMetadata column) {
    ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
    SchemaTableName tableName = getTableName(tableHandle);
    ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
    columns.addAll(tableMetadata.getColumns());
    columns.add(column);
    tables.put(tableName, new ConnectorTableMetadata(tableName, columns.build(), tableMetadata.getProperties()));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Aggregations

ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)63 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)29 SchemaTableName (com.facebook.presto.spi.SchemaTableName)24 ImmutableList (com.google.common.collect.ImmutableList)24 ImmutableMap (com.google.common.collect.ImmutableMap)18 Constraint (com.facebook.presto.spi.Constraint)16 PrestoException (com.facebook.presto.spi.PrestoException)16 ColumnHandle (com.facebook.presto.spi.ColumnHandle)15 Type (com.facebook.presto.spi.type.Type)13 List (java.util.List)13 Test (org.testng.annotations.Test)13 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)12 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 ConnectorSession (com.facebook.presto.spi.ConnectorSession)9 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)9 Optional (java.util.Optional)8 NullableValue (com.facebook.presto.spi.predicate.NullableValue)7 MaterializedResult (com.facebook.presto.testing.MaterializedResult)7