Search in sources :

Example 46 with ColumnMetadata

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

the class TestingMetadata method renameColumn.

@Override
public void renameColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle source, String target) {
    ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
    SchemaTableName tableName = getTableName(tableHandle);
    ColumnMetadata columnMetadata = getColumnMetadata(session, tableHandle, source);
    List<ColumnMetadata> columns = new ArrayList<>(tableMetadata.getColumns());
    columns.set(columns.indexOf(columnMetadata), new ColumnMetadata(target, columnMetadata.getType(), columnMetadata.getComment(), columnMetadata.isHidden()));
    tables.put(tableName, new ConnectorTableMetadata(tableName, ImmutableList.copyOf(columns), tableMetadata.getProperties()));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ArrayList(java.util.ArrayList) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Example 47 with ColumnMetadata

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

the class TestingMetadata method getColumnHandles.

@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle) {
    ImmutableMap.Builder<String, ColumnHandle> builder = ImmutableMap.builder();
    int index = 0;
    for (ColumnMetadata columnMetadata : getTableMetadata(session, tableHandle).getColumns()) {
        builder.put(columnMetadata.getName(), new InMemoryColumnHandle(columnMetadata.getName(), index, columnMetadata.getType()));
        index++;
    }
    return builder.build();
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) Constraint(com.facebook.presto.spi.Constraint)

Example 48 with ColumnMetadata

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

the class TestAnalyzer method setup.

@BeforeMethod(alwaysRun = true)
public void setup() throws Exception {
    TypeManager typeManager = new TypeRegistry();
    CatalogManager catalogManager = new CatalogManager();
    transactionManager = createTestTransactionManager(catalogManager);
    accessControl = new AccessControlManager(transactionManager);
    metadata = new MetadataManager(new FeaturesConfig(), typeManager, new BlockEncodingManager(typeManager), new SessionPropertyManager(), new SchemaPropertyManager(), new TablePropertyManager(), transactionManager);
    metadata.getFunctionRegistry().addFunctions(ImmutableList.of(APPLY_FUNCTION));
    catalogManager.registerCatalog(createTestingCatalog(TPCH_CATALOG, TPCH_CONNECTOR_ID));
    catalogManager.registerCatalog(createTestingCatalog(SECOND_CATALOG, SECOND_CONNECTOR_ID));
    catalogManager.registerCatalog(createTestingCatalog(THIRD_CATALOG, THIRD_CONNECTOR_ID));
    SchemaTableName table1 = new SchemaTableName("s1", "t1");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table1, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT), new ColumnMetadata("c", BIGINT), new ColumnMetadata("d", BIGINT)))));
    SchemaTableName table2 = new SchemaTableName("s1", "t2");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table2, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT)))));
    SchemaTableName table3 = new SchemaTableName("s1", "t3");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table3, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT), new ColumnMetadata("x", BIGINT, null, true)))));
    // table in different catalog
    SchemaTableName table4 = new SchemaTableName("s2", "t4");
    inSetupTransaction(session -> metadata.createTable(session, SECOND_CATALOG, new ConnectorTableMetadata(table4, ImmutableList.of(new ColumnMetadata("a", BIGINT)))));
    // table with a hidden column
    SchemaTableName table5 = new SchemaTableName("s1", "t5");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table5, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT, null, true)))));
    // table with a varchar column
    SchemaTableName table6 = new SchemaTableName("s1", "t6");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table6, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", VARCHAR), new ColumnMetadata("c", BIGINT), new ColumnMetadata("d", BIGINT)))));
    // table with bigint, double, array of bigints and array of doubles column
    SchemaTableName table7 = new SchemaTableName("s1", "t7");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table7, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", DOUBLE), new ColumnMetadata("c", new ArrayType(BIGINT)), new ColumnMetadata("d", new ArrayType(DOUBLE))))));
    // valid view referencing table in same schema
    String viewData1 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select a from t1", Optional.of(TPCH_CATALOG), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(TPCH_CATALOG, "s1", "v1"), viewData1, false));
    // stale view (different column type)
    String viewData2 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select a from t1", Optional.of(TPCH_CATALOG), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", VARCHAR)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(TPCH_CATALOG, "s1", "v2"), viewData2, false));
    // view referencing table in different schema from itself and session
    String viewData3 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select a from t4", Optional.of(SECOND_CATALOG), Optional.of("s2"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("owner")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(THIRD_CATALOG, "s3", "v3"), viewData3, false));
    // valid view with uppercase column name
    String viewData4 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select A from t1", Optional.of("tpch"), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName("tpch", "s1", "v4"), viewData4, false));
    // recursive view referencing to itself
    String viewData5 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select * from v5", Optional.of(TPCH_CATALOG), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(TPCH_CATALOG, "s1", "v5"), viewData5, false));
    this.metadata = metadata;
}
Also used : AccessControlManager(com.facebook.presto.security.AccessControlManager) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) TypeRegistry(com.facebook.presto.type.TypeRegistry) SchemaTableName(com.facebook.presto.spi.SchemaTableName) CatalogManager(com.facebook.presto.metadata.CatalogManager) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) ArrayType(com.facebook.presto.type.ArrayType) MetadataManager(com.facebook.presto.metadata.MetadataManager) BlockEncodingManager(com.facebook.presto.block.BlockEncodingManager) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) TypeManager(com.facebook.presto.spi.type.TypeManager) TablePropertyManager(com.facebook.presto.metadata.TablePropertyManager) SchemaPropertyManager(com.facebook.presto.metadata.SchemaPropertyManager) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 49 with ColumnMetadata

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

the class RaptorMetadata method getNewTableLayout.

@Override
public Optional<ConnectorNewTableLayout> getNewTableLayout(ConnectorSession session, ConnectorTableMetadata metadata) {
    ImmutableMap.Builder<String, RaptorColumnHandle> map = ImmutableMap.builder();
    long columnId = 1;
    for (ColumnMetadata column : metadata.getColumns()) {
        map.put(column.getName(), new RaptorColumnHandle(connectorId, column.getName(), columnId, column.getType()));
        columnId++;
    }
    Optional<DistributionInfo> distribution = getOrCreateDistribution(map.build(), metadata.getProperties());
    if (!distribution.isPresent()) {
        return Optional.empty();
    }
    List<String> partitionColumns = distribution.get().getBucketColumns().stream().map(RaptorColumnHandle::getColumnName).collect(toList());
    ConnectorPartitioningHandle partitioning = getPartitioningHandle(distribution.get().getDistributionId());
    return Optional.of(new ConnectorNewTableLayout(partitioning, partitionColumns));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ConnectorPartitioningHandle(com.facebook.presto.spi.connector.ConnectorPartitioningHandle) ConnectorNewTableLayout(com.facebook.presto.spi.ConnectorNewTableLayout) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 50 with ColumnMetadata

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

the class RaptorMetadata method beginCreateTable.

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout) {
    Optional<RaptorPartitioningHandle> partitioning = layout.map(ConnectorNewTableLayout::getPartitioning).map(RaptorPartitioningHandle.class::cast);
    ImmutableList.Builder<RaptorColumnHandle> columnHandles = ImmutableList.builder();
    ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();
    long columnId = 1;
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        columnHandles.add(new RaptorColumnHandle(connectorId, column.getName(), columnId, column.getType()));
        columnTypes.add(column.getType());
        columnId++;
    }
    Map<String, RaptorColumnHandle> columnHandleMap = Maps.uniqueIndex(columnHandles.build(), RaptorColumnHandle::getColumnName);
    List<RaptorColumnHandle> sortColumnHandles = getSortColumnHandles(getSortColumns(tableMetadata.getProperties()), columnHandleMap);
    Optional<RaptorColumnHandle> temporalColumnHandle = getTemporalColumnHandle(getTemporalColumn(tableMetadata.getProperties()), columnHandleMap);
    if (temporalColumnHandle.isPresent()) {
        RaptorColumnHandle column = temporalColumnHandle.get();
        if (!column.getColumnType().equals(TIMESTAMP) && !column.getColumnType().equals(DATE)) {
            throw new PrestoException(NOT_SUPPORTED, "Temporal column must be of type timestamp or date: " + column.getColumnName());
        }
    }
    boolean organized = isOrganized(tableMetadata.getProperties());
    if (organized) {
        if (temporalColumnHandle.isPresent()) {
            throw new PrestoException(NOT_SUPPORTED, "Table with temporal columns cannot be organized");
        }
        if (sortColumnHandles.isEmpty()) {
            throw new PrestoException(NOT_SUPPORTED, "Table organization requires an ordering");
        }
    }
    long transactionId = shardManager.beginTransaction();
    setTransactionId(transactionId);
    Optional<DistributionInfo> distribution = partitioning.map(handle -> getDistributionInfo(handle.getDistributionId(), columnHandleMap, tableMetadata.getProperties()));
    return new RaptorOutputTableHandle(connectorId, transactionId, tableMetadata.getTable().getSchemaName(), tableMetadata.getTable().getTableName(), columnHandles.build(), columnTypes.build(), sortColumnHandles, nCopies(sortColumnHandles.size(), ASC_NULLS_FIRST), temporalColumnHandle, distribution.map(info -> OptionalLong.of(info.getDistributionId())).orElse(OptionalLong.empty()), distribution.map(info -> OptionalInt.of(info.getBucketCount())).orElse(OptionalInt.empty()), organized, distribution.map(DistributionInfo::getBucketColumns).orElse(ImmutableList.of()));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) Type(com.facebook.presto.spi.type.Type) RaptorBucketFunction.validateBucketType(com.facebook.presto.raptor.RaptorBucketFunction.validateBucketType)

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