Search in sources :

Example 1 with ColumnMetadata

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

the class AtopMetadata method getTableMetadata.

@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle tableHandle) {
    AtopTableHandle atopTableHandle = (AtopTableHandle) tableHandle;
    ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
    for (AtopColumn column : atopTableHandle.getTable().getColumns()) {
        columns.add(new ColumnMetadata(column.getName(), typeManager.getType(column.getType())));
    }
    SchemaTableName schemaTableName = new SchemaTableName(atopTableHandle.getSchema(), atopTableHandle.getTable().getName());
    return new ConnectorTableMetadata(schemaTableName, columns.build());
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) AtopColumn(com.facebook.presto.atop.AtopTable.AtopColumn) ImmutableList(com.google.common.collect.ImmutableList) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Example 2 with ColumnMetadata

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

the class AtopMetadata method getColumnMetadata.

@Override
public ColumnMetadata getColumnMetadata(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle columnHandle) {
    String columnName = ((AtopColumnHandle) columnHandle).getName();
    for (ColumnMetadata column : getTableMetadata(session, tableHandle).getColumns()) {
        if (column.getName().equals(columnName)) {
            return column;
        }
    }
    AtopTableHandle atopTableHandle = (AtopTableHandle) tableHandle;
    SchemaTableName tableName = new SchemaTableName(atopTableHandle.getSchema(), atopTableHandle.getTable().getName());
    throw new ColumnNotFoundException(tableName, columnName);
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ColumnNotFoundException(com.facebook.presto.spi.ColumnNotFoundException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 3 with ColumnMetadata

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

the class AccumuloClient method autoGenerateMapping.

/**
     * Auto-generates the mapping of Presto column name to Accumulo family/qualifier, respecting the locality groups (if any).
     *
     * @param columns Presto columns for the table
     * @param groups Mapping of locality groups to a set of Presto columns, or null if none
     * @return Column mappings
     */
private static Map<String, Pair<String, String>> autoGenerateMapping(List<ColumnMetadata> columns, Optional<Map<String, Set<String>>> groups) {
    Map<String, Pair<String, String>> mapping = new HashMap<>();
    for (ColumnMetadata column : columns) {
        Optional<String> family = getColumnLocalityGroup(column.getName(), groups);
        mapping.put(column.getName(), Pair.of(family.orElse(column.getName()), column.getName()));
    }
    return mapping;
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) HashMap(java.util.HashMap) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with ColumnMetadata

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

the class AccumuloClient method getColumnHandles.

private static List<AccumuloColumnHandle> getColumnHandles(ConnectorTableMetadata meta, String rowIdColumn) {
    // Get the column mappings from the table property or auto-generate columns if not defined
    Map<String, Pair<String, String>> mapping = AccumuloTableProperties.getColumnMapping(meta.getProperties()).orElse(autoGenerateMapping(meta.getColumns(), AccumuloTableProperties.getLocalityGroups(meta.getProperties())));
    // The list of indexed columns
    Optional<List<String>> indexedColumns = AccumuloTableProperties.getIndexColumns(meta.getProperties());
    // And now we parse the configured columns and create handles for the metadata manager
    ImmutableList.Builder<AccumuloColumnHandle> cBuilder = ImmutableList.builder();
    for (int ordinal = 0; ordinal < meta.getColumns().size(); ++ordinal) {
        ColumnMetadata cm = meta.getColumns().get(ordinal);
        // Special case if this column is the row ID
        if (cm.getName().equalsIgnoreCase(rowIdColumn)) {
            cBuilder.add(new AccumuloColumnHandle(rowIdColumn, Optional.empty(), Optional.empty(), cm.getType(), ordinal, "Accumulo row ID", false));
        } else {
            if (!mapping.containsKey(cm.getName())) {
                throw new InvalidParameterException(format("Misconfigured mapping for presto column %s", cm.getName()));
            }
            // Get the mapping for this column
            Pair<String, String> famqual = mapping.get(cm.getName());
            boolean indexed = indexedColumns.isPresent() && indexedColumns.get().contains(cm.getName().toLowerCase(Locale.ENGLISH));
            String comment = format("Accumulo column %s:%s. Indexed: %b", famqual.getLeft(), famqual.getRight(), indexed);
            // Create a new AccumuloColumnHandle object
            cBuilder.add(new AccumuloColumnHandle(cm.getName(), Optional.of(famqual.getLeft()), Optional.of(famqual.getRight()), cm.getType(), ordinal, comment, indexed));
        }
    }
    return cBuilder.build();
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) AccumuloColumnConstraint(com.facebook.presto.accumulo.model.AccumuloColumnConstraint) InvalidParameterException(java.security.InvalidParameterException) AccumuloColumnHandle(com.facebook.presto.accumulo.model.AccumuloColumnHandle) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Pair(org.apache.commons.lang3.tuple.Pair)

Example 5 with ColumnMetadata

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

the class AccumuloClient method validateColumns.

private static void validateColumns(ConnectorTableMetadata meta) {
    // Check all the column types, and throw an exception if the types of a map are complex
    // While it is a rare case, this is not supported by the Accumulo connector
    ImmutableSet.Builder<String> columnNameBuilder = ImmutableSet.builder();
    for (ColumnMetadata column : meta.getColumns()) {
        if (Types.isMapType(column.getType())) {
            if (Types.isMapType(Types.getKeyType(column.getType())) || Types.isMapType(Types.getValueType(column.getType())) || Types.isArrayType(Types.getKeyType(column.getType())) || Types.isArrayType(Types.getValueType(column.getType()))) {
                throw new PrestoException(INVALID_TABLE_PROPERTY, "Key/value types of a MAP column must be plain types");
            }
        }
        columnNameBuilder.add(column.getName().toLowerCase(Locale.ENGLISH));
    }
    // Validate the columns are distinct
    if (columnNameBuilder.build().size() != meta.getColumns().size()) {
        throw new PrestoException(INVALID_TABLE_PROPERTY, "Duplicate column names are not supported");
    }
    Optional<Map<String, Pair<String, String>>> columnMapping = AccumuloTableProperties.getColumnMapping(meta.getProperties());
    if (columnMapping.isPresent()) {
        // Validate there are no duplicates in the column mapping
        long distinctMappings = columnMapping.get().values().stream().distinct().count();
        if (distinctMappings != columnMapping.get().size()) {
            throw new PrestoException(INVALID_TABLE_PROPERTY, "Duplicate column family/qualifier pair detected in column mapping, check the value of " + AccumuloTableProperties.COLUMN_MAPPING);
        }
        // Validate no column is mapped to the reserved entry
        String reservedRowIdColumn = AccumuloPageSink.ROW_ID_COLUMN.toString();
        if (columnMapping.get().values().stream().filter(pair -> pair.getKey().equals(reservedRowIdColumn) && pair.getValue().equals(reservedRowIdColumn)).count() > 0) {
            throw new PrestoException(INVALID_TABLE_PROPERTY, format("Column familiy/qualifier mapping of %s:%s is reserved", reservedRowIdColumn, reservedRowIdColumn));
        }
    } else if (AccumuloTableProperties.isExternal(meta.getProperties())) {
        // But column generation is for internal tables only
        throw new PrestoException(INVALID_TABLE_PROPERTY, "Column generation for external tables is not supported, must specify " + AccumuloTableProperties.COLUMN_MAPPING);
    }
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) PrestoException(com.facebook.presto.spi.PrestoException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

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