Search in sources :

Example 1 with AbstractTableMetadata

use of com.datastax.driver.core.AbstractTableMetadata in project calcite by apache.

the class CassandraSchema method getClusteringOrder.

/**
 * Get the collation of all clustering key columns.
 *
 * @return A RelCollations representing the collation of all clustering keys
 */
public List<RelFieldCollation> getClusteringOrder(String columnFamily, boolean view) {
    AbstractTableMetadata table;
    if (view) {
        table = getKeyspace().getMaterializedView(columnFamily);
    } else {
        table = getKeyspace().getTable(columnFamily);
    }
    List<ClusteringOrder> clusteringOrder = table.getClusteringOrder();
    List<RelFieldCollation> keyCollations = new ArrayList<RelFieldCollation>();
    int i = 0;
    for (ClusteringOrder order : clusteringOrder) {
        RelFieldCollation.Direction direction;
        switch(order) {
            case DESC:
                direction = RelFieldCollation.Direction.DESCENDING;
                break;
            case ASC:
            default:
                direction = RelFieldCollation.Direction.ASCENDING;
                break;
        }
        keyCollations.add(new RelFieldCollation(i, direction));
        i++;
    }
    return keyCollations;
}
Also used : AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList) ClusteringOrder(com.datastax.driver.core.ClusteringOrder)

Example 2 with AbstractTableMetadata

use of com.datastax.driver.core.AbstractTableMetadata in project presto by prestodb.

the class NativeCassandraSession method getTable.

@Override
public CassandraTable getTable(SchemaTableName schemaTableName) throws TableNotFoundException {
    KeyspaceMetadata keyspace = getKeyspaceByCaseInsensitiveName(schemaTableName.getSchemaName());
    AbstractTableMetadata tableMeta = getTableMetadata(keyspace, schemaTableName.getTableName());
    List<String> columnNames = new ArrayList<>();
    List<ColumnMetadata> columns = tableMeta.getColumns();
    checkColumnNames(columns);
    for (ColumnMetadata columnMetadata : columns) {
        columnNames.add(columnMetadata.getName());
    }
    // check if there is a comment to establish column ordering
    String comment = tableMeta.getOptions().getComment();
    Set<String> hiddenColumns = ImmutableSet.of();
    if (comment != null && comment.startsWith(PRESTO_COMMENT_METADATA)) {
        String columnOrderingString = comment.substring(PRESTO_COMMENT_METADATA.length());
        // column ordering
        List<ExtraColumnMetadata> extras = extraColumnMetadataCodec.fromJson(columnOrderingString);
        List<String> explicitColumnOrder = new ArrayList<>(ImmutableList.copyOf(transform(extras, ExtraColumnMetadata::getName)));
        hiddenColumns = ImmutableSet.copyOf(transform(filter(extras, ExtraColumnMetadata::isHidden), ExtraColumnMetadata::getName));
        // add columns not in the comment to the ordering
        Iterables.addAll(explicitColumnOrder, filter(columnNames, not(in(explicitColumnOrder))));
        // sort the actual columns names using the explicit column order (this allows for missing columns)
        columnNames = Ordering.explicit(explicitColumnOrder).sortedCopy(columnNames);
    }
    ImmutableList.Builder<CassandraColumnHandle> columnHandles = ImmutableList.builder();
    // add primary keys first
    Set<String> primaryKeySet = new HashSet<>();
    for (ColumnMetadata columnMeta : tableMeta.getPartitionKey()) {
        primaryKeySet.add(columnMeta.getName());
        boolean hidden = hiddenColumns.contains(columnMeta.getName());
        CassandraColumnHandle columnHandle = buildColumnHandle(tableMeta, columnMeta, true, false, columnNames.indexOf(columnMeta.getName()), hidden);
        columnHandles.add(columnHandle);
    }
    // add clustering columns
    for (ColumnMetadata columnMeta : tableMeta.getClusteringColumns()) {
        primaryKeySet.add(columnMeta.getName());
        boolean hidden = hiddenColumns.contains(columnMeta.getName());
        CassandraColumnHandle columnHandle = buildColumnHandle(tableMeta, columnMeta, false, true, columnNames.indexOf(columnMeta.getName()), hidden);
        columnHandles.add(columnHandle);
    }
    // add other columns
    for (ColumnMetadata columnMeta : columns) {
        if (!primaryKeySet.contains(columnMeta.getName())) {
            boolean hidden = hiddenColumns.contains(columnMeta.getName());
            CassandraColumnHandle columnHandle = buildColumnHandle(tableMeta, columnMeta, false, false, columnNames.indexOf(columnMeta.getName()), hidden);
            columnHandles.add(columnHandle);
        }
    }
    List<CassandraColumnHandle> sortedColumnHandles = columnHandles.build().stream().sorted(comparing(CassandraColumnHandle::getOrdinalPosition)).collect(toList());
    CassandraTableHandle tableHandle = new CassandraTableHandle(connectorId, tableMeta.getKeyspace().getName(), tableMeta.getName());
    return new CassandraTable(tableHandle, sortedColumnHandles);
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) HashSet(java.util.HashSet)

Example 3 with AbstractTableMetadata

use of com.datastax.driver.core.AbstractTableMetadata in project presto by prestodb.

the class NativeCassandraSession method getTableMetadata.

private static AbstractTableMetadata getTableMetadata(KeyspaceMetadata keyspace, String caseInsensitiveTableName) {
    List<AbstractTableMetadata> tables = Stream.concat(keyspace.getTables().stream(), keyspace.getMaterializedViews().stream()).filter(table -> table.getName().equalsIgnoreCase(caseInsensitiveTableName)).collect(toImmutableList());
    if (tables.size() == 0) {
        throw new TableNotFoundException(new SchemaTableName(keyspace.getName(), caseInsensitiveTableName));
    } else if (tables.size() == 1) {
        return tables.get(0);
    }
    String tableNames = tables.stream().map(AbstractTableMetadata::getName).sorted().collect(joining(", "));
    throw new PrestoException(NOT_SUPPORTED, format("More than one table has been found for the case insensitive table name: %s -> (%s)", caseInsensitiveTableName, tableNames));
}
Also used : QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) JsonCodec(com.facebook.airlift.json.JsonCodec) LoadingCache(com.google.common.cache.LoadingCache) Iterables.transform(com.google.common.collect.Iterables.transform) RegularStatement(com.datastax.driver.core.RegularStatement) Clause(com.datastax.driver.core.querybuilder.Clause) ByteBuffer(java.nio.ByteBuffer) Duration(io.airlift.units.Duration) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Predicates.in(com.google.common.base.Predicates.in) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Predicates.not(com.google.common.base.Predicates.not) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Session(com.datastax.driver.core.Session) Map(java.util.Map) VersionNumber(com.datastax.driver.core.VersionNumber) ENGLISH(java.util.Locale.ENGLISH) TableMetadata(com.datastax.driver.core.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) NullableValue(com.facebook.presto.common.predicate.NullableValue) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) Stream(java.util.stream.Stream) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) Select(com.datastax.driver.core.querybuilder.Select) CacheBuilder(com.google.common.cache.CacheBuilder) Iterables.filter(com.google.common.collect.Iterables.filter) Statement(com.datastax.driver.core.Statement) CASSANDRA_VERSION_ERROR(com.facebook.presto.cassandra.CassandraErrorCode.CASSANDRA_VERSION_ERROR) TokenRange(com.datastax.driver.core.TokenRange) Iterables(com.google.common.collect.Iterables) Logger(com.facebook.airlift.log.Logger) Row(com.datastax.driver.core.Row) Supplier(com.google.common.base.Supplier) HashMap(java.util.HashMap) MINUTES(java.util.concurrent.TimeUnit.MINUTES) PrestoException(com.facebook.presto.spi.PrestoException) ArrayList(java.util.ArrayList) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) PreparedStatement(com.datastax.driver.core.PreparedStatement) HashSet(java.util.HashSet) CassandraCqlUtils.validSchemaName(com.facebook.presto.cassandra.util.CassandraCqlUtils.validSchemaName) ResultSet(com.datastax.driver.core.ResultSet) ImmutableList(com.google.common.collect.ImmutableList) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Objects.requireNonNull(java.util.Objects.requireNonNull) Comparator.comparing(java.util.Comparator.comparing) Suppliers.memoize(com.google.common.base.Suppliers.memoize) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) IndexMetadata(com.datastax.driver.core.IndexMetadata) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) ExecutionException(java.util.concurrent.ExecutionException) Collectors.toList(java.util.stream.Collectors.toList) MaterializedViewMetadata(com.datastax.driver.core.MaterializedViewMetadata) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) Where(com.datastax.driver.core.querybuilder.Select.Where) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) Ordering(com.google.common.collect.Ordering) ColumnHandle(com.facebook.presto.spi.ColumnHandle) QueryBuilder.select(com.datastax.driver.core.querybuilder.QueryBuilder.select) DataType(com.datastax.driver.core.DataType) CassandraCqlUtils(com.facebook.presto.cassandra.util.CassandraCqlUtils) ReconnectionSchedule(com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 4 with AbstractTableMetadata

use of com.datastax.driver.core.AbstractTableMetadata in project presto by prestodb.

the class NativeCassandraSession method buildColumnHandle.

private CassandraColumnHandle buildColumnHandle(AbstractTableMetadata tableMetadata, ColumnMetadata columnMeta, boolean partitionKey, boolean clusteringKey, int ordinalPosition, boolean hidden) {
    CassandraType cassandraType = CassandraType.getCassandraType(columnMeta.getType().getName());
    List<CassandraType> typeArguments = null;
    if (cassandraType != null && cassandraType.getTypeArgumentSize() > 0) {
        List<DataType> typeArgs = columnMeta.getType().getTypeArguments();
        switch(cassandraType.getTypeArgumentSize()) {
            case 1:
                typeArguments = ImmutableList.of(CassandraType.getCassandraType(typeArgs.get(0).getName()));
                break;
            case 2:
                typeArguments = ImmutableList.of(CassandraType.getCassandraType(typeArgs.get(0).getName()), CassandraType.getCassandraType(typeArgs.get(1).getName()));
                break;
            default:
                throw new IllegalArgumentException("Invalid type arguments: " + typeArgs);
        }
    }
    boolean indexed = false;
    SchemaTableName schemaTableName = new SchemaTableName(tableMetadata.getKeyspace().getName(), tableMetadata.getName());
    if (!isMaterializedView(schemaTableName)) {
        TableMetadata table = (TableMetadata) tableMetadata;
        for (IndexMetadata idx : table.getIndexes()) {
            if (idx.getTarget().equals(columnMeta.getName())) {
                indexed = true;
                break;
            }
        }
    }
    return new CassandraColumnHandle(connectorId, columnMeta.getName(), ordinalPosition, cassandraType, typeArguments, partitionKey, clusteringKey, indexed, hidden);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) DataType(com.datastax.driver.core.DataType) IndexMetadata(com.datastax.driver.core.IndexMetadata) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 5 with AbstractTableMetadata

use of com.datastax.driver.core.AbstractTableMetadata in project calcite by apache.

the class CassandraSchema method getKeyFields.

/**
 * Get all primary key columns from the underlying CQL table
 *
 * @return A list of field names that are part of the partition and clustering keys
 */
Pair<List<String>, List<String>> getKeyFields(String columnFamily, boolean view) {
    AbstractTableMetadata table;
    if (view) {
        table = getKeyspace().getMaterializedView(columnFamily);
    } else {
        table = getKeyspace().getTable(columnFamily);
    }
    List<ColumnMetadata> partitionKey = table.getPartitionKey();
    List<String> pKeyFields = new ArrayList<String>();
    for (ColumnMetadata column : partitionKey) {
        pKeyFields.add(column.getName());
    }
    List<ColumnMetadata> clusteringKey = table.getClusteringColumns();
    List<String> cKeyFields = new ArrayList<String>();
    for (ColumnMetadata column : clusteringKey) {
        cKeyFields.add(column.getName());
    }
    return Pair.of((List<String>) ImmutableList.copyOf(pKeyFields), (List<String>) ImmutableList.copyOf(cKeyFields));
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) ArrayList(java.util.ArrayList)

Aggregations

AbstractTableMetadata (com.datastax.driver.core.AbstractTableMetadata)5 ArrayList (java.util.ArrayList)4 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)3 DataType (com.datastax.driver.core.DataType)2 IndexMetadata (com.datastax.driver.core.IndexMetadata)2 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)2 TableMetadata (com.datastax.driver.core.TableMetadata)2 SchemaTableName (com.facebook.presto.spi.SchemaTableName)2 Cluster (com.datastax.driver.core.Cluster)1 ClusteringOrder (com.datastax.driver.core.ClusteringOrder)1 Host (com.datastax.driver.core.Host)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 ResultSet (com.datastax.driver.core.ResultSet)1 Row (com.datastax.driver.core.Row)1 Session (com.datastax.driver.core.Session)1 Statement (com.datastax.driver.core.Statement)1 TokenRange (com.datastax.driver.core.TokenRange)1 VersionNumber (com.datastax.driver.core.VersionNumber)1