Search in sources :

Example 11 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata 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 12 with ColumnMetadata

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

the class NativeCassandraSession method checkColumnNames.

private static void checkColumnNames(List<ColumnMetadata> columns) {
    Map<String, ColumnMetadata> lowercaseNameToColumnMap = new HashMap<>();
    for (ColumnMetadata column : columns) {
        String lowercaseName = column.getName().toLowerCase(ENGLISH);
        if (lowercaseNameToColumnMap.containsKey(lowercaseName)) {
            throw new PrestoException(NOT_SUPPORTED, format("More than one column has been found for the case insensitive column name: %s -> (%s, %s)", lowercaseName, lowercaseNameToColumnMap.get(lowercaseName).getName(), column.getName()));
        }
        lowercaseNameToColumnMap.put(lowercaseName, column);
    }
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) HashMap(java.util.HashMap) PrestoException(com.facebook.presto.spi.PrestoException)

Example 13 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSyncTest method testDoNotDropColumn.

@Test
public void testDoNotDropColumn() {
    SyncOptions opt = SyncOptions.withOptions().add(SyncOptionTypes.DoNotDropColumns);
    SchemaSync.drop(keyspace, session, EntityWithIndexes.class);
    SchemaSync.sync(keyspace, session, EntityWithIndexes.class);
    SchemaSync.sync(keyspace, session, EntityWithIndexesV2.class, opt);
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
    TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
    // column counter2 should not be added
    ColumnMetadata columnMetadata = tableMetadata.getColumn("counter2");
    assertNull(columnMetadata);
    // column counter should have been dropped
    columnMetadata = tableMetadata.getColumn("counter");
    assertNotNull(columnMetadata);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Test(org.junit.Test)

Example 14 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSyncTest method testDoNotAddColumn.

@Test
public void testDoNotAddColumn() {
    SyncOptions opt = SyncOptions.withOptions().add(SyncOptionTypes.DoNotAddColumns);
    SchemaSync.drop(keyspace, session, EntityWithIndexes.class);
    SchemaSync.sync(keyspace, session, EntityWithIndexes.class);
    SchemaSync.sync(keyspace, session, EntityWithIndexesV2.class, opt);
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
    TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
    // column counter2 should not be added
    ColumnMetadata columnMetadata = tableMetadata.getColumn("counter2");
    assertNull(columnMetadata);
    // column counter should have been dropped
    columnMetadata = tableMetadata.getColumn("counter");
    assertNull(columnMetadata);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Test(org.junit.Test)

Example 15 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSyncTest method testCreateWithCompositeKey.

@Test
public void testCreateWithCompositeKey() {
    EntityTypeParser.getEntityMetadata(EntityWithCompositeKey.class).markUnSynced(keyspace);
    SchemaSync.sync(keyspace, session, EntityWithCompositeKey.class);
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithCompositeKey.class);
    TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
    assertNotNull(tableMetadata);
    assertEquals("test_entity_composites", tableMetadata.getName());
    assertEquals(8, tableMetadata.getColumns().size());
    ColumnMetadata columnMetadata = tableMetadata.getColumn("timestamp");
    assertNotNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("asof");
    assertNotNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("created");
    assertNotNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("email");
    assertNotNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("name");
    assertNotNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("rank");
    assertNotNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("key");
    assertNull(columnMetadata);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) EntityWithCompositeKey(com.datastax.driver.mapping.entity.EntityWithCompositeKey) Test(org.junit.Test)

Aggregations

ColumnMetadata (com.datastax.driver.core.ColumnMetadata)37 Test (org.testng.annotations.Test)11 TableMetadata (com.datastax.driver.core.TableMetadata)8 CassandraVersion (com.datastax.driver.core.utils.CassandraVersion)8 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 AbstractTableMetadata (com.datastax.driver.core.AbstractTableMetadata)2 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)2 ImmutableList (com.google.common.collect.ImmutableList)2 ByteBuffer (java.nio.ByteBuffer)2 BoundStatementMapper (org.simpleflatmapper.datastax.BoundStatementMapper)2 Cluster (com.datastax.driver.core.Cluster)1 DataType (com.datastax.driver.core.DataType)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 EntityWithCompositeKey (com.datastax.driver.mapping.entity.EntityWithCompositeKey)1 EntityWithIndexes (com.datastax.driver.mapping.entity.EntityWithIndexes)1 EntityWithTimeUUID (com.datastax.driver.mapping.entity.EntityWithTimeUUID)1 EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)1