Java Code Examples for io.prestosql.spi.connector.ColumnMetadata#getType()

The following examples show how to use io.prestosql.spi.connector.ColumnMetadata#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShowStatsRewrite.java    From presto with Apache License 2.0 6 votes vote down vote up
private List<Expression> buildStatisticsRows(TableMetadata tableMetadata, Map<String, ColumnHandle> columnHandles, Set<String> resultColumns, TableStatistics tableStatistics)
{
    ImmutableList.Builder<Expression> rowsBuilder = ImmutableList.builder();
    for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
        if (columnMetadata.isHidden()) {
            continue;
        }
        String columnName = columnMetadata.getName();
        Type columnType = columnMetadata.getType();
        if (!resultColumns.contains(columnName)) {
            continue;
        }
        ColumnHandle columnHandle = columnHandles.get(columnName);
        ColumnStatistics columnStatistics = tableStatistics.getColumnStatistics().get(columnHandle);
        if (columnStatistics != null) {
            rowsBuilder.add(createColumnStatsRow(columnName, columnType, columnStatistics));
        }
        else {
            rowsBuilder.add(createEmptyColumnStatsRow(columnName));
        }
    }
    // Stats for whole table
    rowsBuilder.add(createTableStatsRow(tableStatistics));
    return rowsBuilder.build();
}
 
Example 2
Source File: TestCassandraConnector.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void assertReadFields(RecordCursor cursor, List<ColumnMetadata> schema)
{
    for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
        ColumnMetadata column = schema.get(columnIndex);
        if (!cursor.isNull(columnIndex)) {
            Type type = column.getType();
            if (BOOLEAN.equals(type)) {
                cursor.getBoolean(columnIndex);
            }
            else if (INTEGER.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (BIGINT.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (TIMESTAMP.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (DOUBLE.equals(type)) {
                cursor.getDouble(columnIndex);
            }
            else if (REAL.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (isVarcharType(type) || VARBINARY.equals(type)) {
                try {
                    cursor.getSlice(columnIndex);
                }
                catch (RuntimeException e) {
                    throw new RuntimeException("column " + column, e);
                }
            }
            else {
                fail("Unknown primitive type " + columnIndex);
            }
        }
    }
}
 
Example 3
Source File: KuduClientSession.java    From presto with Apache License 2.0 5 votes vote down vote up
private void setTypeAttributes(ColumnMetadata columnMetadata, ColumnSchema.ColumnSchemaBuilder builder)
{
    if (columnMetadata.getType() instanceof DecimalType) {
        DecimalType type = (DecimalType) columnMetadata.getType();
        ColumnTypeAttributes attributes = new ColumnTypeAttributes.ColumnTypeAttributesBuilder()
                .precision(type.getPrecision())
                .scale(type.getScale()).build();
        builder.typeAttributes(attributes);
    }
}
 
Example 4
Source File: BlackHoleColumnHandle.java    From presto with Apache License 2.0 4 votes vote down vote up
public BlackHoleColumnHandle(ColumnMetadata columnMetadata)
{
    this(columnMetadata.getName(), columnMetadata.getType());
}
 
Example 5
Source File: AccumuloClient.java    From presto with Apache License 2.0 4 votes vote down vote up
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");
            }
        }

        if (column.getType() instanceof TimestampType && ((TimestampType) column.getType()).getPrecision() != 3) {
            throw new PrestoException(NOT_SUPPORTED, format("%s type not supported", column.getType()));
        }

        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())) {
        // Column mapping is not defined (i.e. use column generation) and table is external
        // 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);
    }
}
 
Example 6
Source File: ThriftColumnHandle.java    From presto with Apache License 2.0 4 votes vote down vote up
public ThriftColumnHandle(ColumnMetadata columnMetadata)
{
    this(columnMetadata.getName(), columnMetadata.getType(), columnMetadata.getComment(), columnMetadata.isHidden());
}