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);
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations