Search in sources :

Example 6 with ColumnMetadata

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

the class AccumuloClient method validateLocalityGroups.

private static void validateLocalityGroups(ConnectorTableMetadata meta) {
    // Validate any configured locality groups
    Optional<Map<String, Set<String>>> groups = AccumuloTableProperties.getLocalityGroups(meta.getProperties());
    if (!groups.isPresent()) {
        return;
    }
    String rowIdColumn = getRowIdColumn(meta);
    // For each locality group
    for (Map.Entry<String, Set<String>> g : groups.get().entrySet()) {
        if (g.getValue().contains(rowIdColumn)) {
            throw new PrestoException(INVALID_TABLE_PROPERTY, "Row ID column cannot be in a locality group");
        }
        // Validate the specified column names exist in the table definition,
        // incrementing a counter for each matching column
        int matchingColumns = 0;
        for (ColumnMetadata column : meta.getColumns()) {
            if (g.getValue().contains(column.getName().toLowerCase(Locale.ENGLISH))) {
                ++matchingColumns;
                // Break out early if all columns are found
                if (matchingColumns == g.getValue().size()) {
                    break;
                }
            }
        }
        // (or there is a duplicate column in the table DDL, which is also an issue but has been checked before in validateColumns).
        if (matchingColumns != g.getValue().size()) {
            throw new PrestoException(INVALID_TABLE_PROPERTY, "Unknown Presto column defined for locality group " + g.getKey());
        }
    }
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PrestoException(com.facebook.presto.spi.PrestoException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) AccumuloColumnConstraint(com.facebook.presto.accumulo.model.AccumuloColumnConstraint)

Example 7 with ColumnMetadata

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

the class HiveMetadata method getColumnHandles.

private static List<HiveColumnHandle> getColumnHandles(String connectorId, ConnectorTableMetadata tableMetadata, Set<String> partitionColumnNames, TypeTranslator typeTranslator) {
    validatePartitionColumns(tableMetadata);
    validateBucketColumns(tableMetadata);
    ImmutableList.Builder<HiveColumnHandle> columnHandles = ImmutableList.builder();
    int ordinal = 0;
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        HiveColumnHandle.ColumnType columnType;
        if (partitionColumnNames.contains(column.getName())) {
            columnType = PARTITION_KEY;
        } else if (column.isHidden()) {
            columnType = HIDDEN;
        } else {
            columnType = REGULAR;
        }
        columnHandles.add(new HiveColumnHandle(connectorId, column.getName(), toHiveType(typeTranslator, column.getType()), column.getType().getTypeSignature(), ordinal, columnType, Optional.ofNullable(column.getComment())));
        ordinal++;
    }
    return columnHandles.build();
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) Constraint(com.facebook.presto.spi.Constraint)

Example 8 with ColumnMetadata

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

the class HiveMetadata method getTableMetadata.

private ConnectorTableMetadata getTableMetadata(SchemaTableName tableName) {
    Optional<Table> table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName());
    if (!table.isPresent() || table.get().getTableType().equals(TableType.VIRTUAL_VIEW.name())) {
        throw new TableNotFoundException(tableName);
    }
    Function<HiveColumnHandle, ColumnMetadata> metadataGetter = columnMetadataGetter(table.get(), typeManager);
    ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
    for (HiveColumnHandle columnHandle : hiveColumnHandles(connectorId, table.get())) {
        columns.add(metadataGetter.apply(columnHandle));
    }
    ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
    if (table.get().getTableType().equals(EXTERNAL_TABLE.name())) {
        properties.put(EXTERNAL_LOCATION_PROPERTY, table.get().getStorage().getLocation());
    }
    try {
        HiveStorageFormat format = extractHiveStorageFormat(table.get());
        properties.put(STORAGE_FORMAT_PROPERTY, format);
    } catch (PrestoException ignored) {
    // todo fail if format is not known
    }
    List<String> partitionedBy = table.get().getPartitionColumns().stream().map(Column::getName).collect(toList());
    if (!partitionedBy.isEmpty()) {
        properties.put(PARTITIONED_BY_PROPERTY, partitionedBy);
    }
    Optional<HiveBucketProperty> bucketProperty = table.get().getStorage().getBucketProperty();
    if (bucketProperty.isPresent()) {
        properties.put(BUCKET_COUNT_PROPERTY, bucketProperty.get().getBucketCount());
        properties.put(BUCKETED_BY_PROPERTY, bucketProperty.get().getBucketedBy());
    }
    properties.putAll(tableParameterCodec.decode(table.get().getParameters()));
    return new ConnectorTableMetadata(tableName, columns.build(), properties.build());
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Table(com.facebook.presto.hive.metastore.Table) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) HiveTableProperties.getHiveStorageFormat(com.facebook.presto.hive.HiveTableProperties.getHiveStorageFormat) StorageFormat.fromHiveStorageFormat(com.facebook.presto.hive.metastore.StorageFormat.fromHiveStorageFormat) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Example 9 with ColumnMetadata

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

the class ExampleMetadata method getColumnHandles.

@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle) {
    ExampleTableHandle exampleTableHandle = (ExampleTableHandle) tableHandle;
    checkArgument(exampleTableHandle.getConnectorId().equals(connectorId), "tableHandle is not for this connector");
    ExampleTable table = exampleClient.getTable(exampleTableHandle.getSchemaName(), exampleTableHandle.getTableName());
    if (table == null) {
        throw new TableNotFoundException(exampleTableHandle.toSchemaTableName());
    }
    ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
    int index = 0;
    for (ColumnMetadata column : table.getColumnsMetadata()) {
        columnHandles.put(column.getName(), new ExampleColumnHandle(connectorId, column.getName(), column.getType(), index));
        index++;
    }
    return columnHandles.build();
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) 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 10 with ColumnMetadata

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

the class BlackHoleMetadata method getNewTableLayout.

@Override
public Optional<ConnectorNewTableLayout> getNewTableLayout(ConnectorSession connectorSession, ConnectorTableMetadata tableMetadata) {
    List<String> distributeColumns = (List<String>) tableMetadata.getProperties().get(DISTRIBUTED_ON);
    if (distributeColumns.isEmpty()) {
        return Optional.empty();
    }
    Set<String> undefinedColumns = Sets.difference(ImmutableSet.copyOf(distributeColumns), tableMetadata.getColumns().stream().map(ColumnMetadata::getName).collect(toSet()));
    if (!undefinedColumns.isEmpty()) {
        throw new PrestoException(INVALID_TABLE_PROPERTY, "Distribute columns not defined on table: " + undefinedColumns);
    }
    return Optional.of(new ConnectorNewTableLayout(BlackHolePartitioningHandle.INSTANCE, distributeColumns));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) PrestoException(com.facebook.presto.spi.PrestoException) ConnectorNewTableLayout(com.facebook.presto.spi.ConnectorNewTableLayout)

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