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