use of com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata in project calcite by apache.
the class CassandraSchema method getTableMap.
@Override
protected Map<String, Table> getTableMap() {
final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
for (TableMetadata table : getKeyspace().getTables().values()) {
String tableName = table.getName().asInternal();
builder.put(tableName, new CassandraTable(this, tableName));
for (ViewMetadata view : getKeyspace().getViewsOnTable(table.getName()).values()) {
String viewName = view.getName().asInternal();
builder.put(viewName, new CassandraTable(this, viewName, true));
}
}
return builder.build();
}
use of com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata in project calcite by apache.
the class CassandraSchema method getRelDataType.
RelProtoDataType getRelDataType(String columnFamily, boolean view) {
Map<CqlIdentifier, ColumnMetadata> columns;
CqlIdentifier tableName = CqlIdentifier.fromInternal(columnFamily);
if (view) {
Optional<ViewMetadata> optionalViewMetadata = getKeyspace().getView(tableName);
if (optionalViewMetadata.isPresent()) {
columns = optionalViewMetadata.get().getColumns();
} else {
throw new IllegalStateException("Unknown view " + tableName + " in keyspace " + keyspace);
}
} else {
Optional<TableMetadata> optionalTableMetadata = getKeyspace().getTable(tableName);
if (optionalTableMetadata.isPresent()) {
columns = optionalTableMetadata.get().getColumns();
} else {
throw new IllegalStateException("Unknown table " + tableName + " in keyspace " + keyspace);
}
}
// Temporary type factory, just for the duration of this method. Allowable
// because we're creating a proto-type, not a type; before being used, the
// proto-type will be copied into a real type factory.
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
for (ColumnMetadata column : columns.values()) {
final DataType dataType = column.getType();
final String columnName = column.getName().asInternal();
if (dataType instanceof ListType) {
SqlTypeName arrayInnerType = CQL_TO_SQL_TYPE.lookup(((ListType) dataType).getElementType());
fieldInfo.add(columnName, typeFactory.createArrayType(typeFactory.createSqlType(arrayInnerType), -1)).nullable(true);
} else if (dataType instanceof SetType) {
SqlTypeName multiSetInnerType = CQL_TO_SQL_TYPE.lookup(((SetType) dataType).getElementType());
fieldInfo.add(columnName, typeFactory.createMultisetType(typeFactory.createSqlType(multiSetInnerType), -1)).nullable(true);
} else if (dataType instanceof MapType) {
MapType columnType = (MapType) dataType;
SqlTypeName keyType = CQL_TO_SQL_TYPE.lookup(columnType.getKeyType());
SqlTypeName valueType = CQL_TO_SQL_TYPE.lookup(columnType.getValueType());
fieldInfo.add(columnName, typeFactory.createMapType(typeFactory.createSqlType(keyType), typeFactory.createSqlType(valueType))).nullable(true);
} else if (dataType instanceof TupleType) {
List<DataType> typeArgs = ((TupleType) dataType).getComponentTypes();
List<Map.Entry<String, RelDataType>> typesList = IntStream.range(0, typeArgs.size()).mapToObj(i -> new Pair<>(// 1 indexed (as ARRAY)
Integer.toString(i + 1), typeFactory.createSqlType(CQL_TO_SQL_TYPE.lookup(typeArgs.get(i))))).collect(Collectors.toList());
fieldInfo.add(columnName, typeFactory.createStructType(typesList)).nullable(true);
} else {
SqlTypeName typeName = CQL_TO_SQL_TYPE.lookup(dataType);
fieldInfo.add(columnName, typeName).nullable(true);
}
}
return RelDataTypeImpl.proto(fieldInfo.build());
}
use of com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata in project java-driver by datastax.
the class ViewParserTest method should_parse_view.
@Test
public void should_parse_view() {
SchemaRows rows = rows(VIEW_ROW_3_0, COLUMN_ROWS_3_0);
ViewParser parser = new ViewParser(rows, context);
ViewMetadata view = parser.parseView(VIEW_ROW_3_0, KEYSPACE_ID, Collections.emptyMap());
assertThat(view.getKeyspace().asInternal()).isEqualTo("ks");
assertThat(view.getName().asInternal()).isEqualTo("alltimehigh");
assertThat(view.getBaseTable().asInternal()).isEqualTo("scores");
assertThat(view.getPartitionKey()).hasSize(1);
ColumnMetadata pk0 = view.getPartitionKey().get(0);
assertThat(pk0.getName().asInternal()).isEqualTo("game");
assertThat(pk0.getType()).isEqualTo(DataTypes.TEXT);
assertThat(view.getClusteringColumns().entrySet()).hasSize(5);
Iterator<ColumnMetadata> clusteringColumnsIterator = view.getClusteringColumns().keySet().iterator();
assertThat(clusteringColumnsIterator.next().getName().asInternal()).isEqualTo("score");
assertThat(clusteringColumnsIterator.next().getName().asInternal()).isEqualTo("user");
assertThat(clusteringColumnsIterator.next().getName().asInternal()).isEqualTo("year");
assertThat(clusteringColumnsIterator.next().getName().asInternal()).isEqualTo("month");
assertThat(clusteringColumnsIterator.next().getName().asInternal()).isEqualTo("day");
assertThat(view.getColumns()).containsOnlyKeys(CqlIdentifier.fromInternal("game"), CqlIdentifier.fromInternal("score"), CqlIdentifier.fromInternal("user"), CqlIdentifier.fromInternal("year"), CqlIdentifier.fromInternal("month"), CqlIdentifier.fromInternal("day"));
}
use of com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata in project calcite by apache.
the class CassandraSchema method addMaterializedViews.
/**
* Adds all materialized views defined in the schema to this column family.
*/
private void addMaterializedViews() {
// Close the hook used to get us here
hook.close();
for (ViewMetadata view : getKeyspace().getViews().values()) {
String tableName = view.getBaseTable().asInternal();
StringBuilder queryBuilder = new StringBuilder("SELECT ");
// Add all the selected columns to the query
String columnsList = view.getColumns().values().stream().map(c -> c.getName().asInternal()).collect(Collectors.joining(", "));
queryBuilder.append(columnsList);
queryBuilder.append(" FROM ").append(tableName);
// Get the where clause from the system schema
String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName().asInternal() + "'";
Row whereClauseRow = Objects.requireNonNull(session.execute(whereQuery).one());
queryBuilder.append(" WHERE ").append(whereClauseRow.getString(0));
// Parse and unparse the view query to get properly quoted field names
String query = queryBuilder.toString();
SqlParser.Config parserConfig = SqlParser.config().withUnquotedCasing(Casing.UNCHANGED);
SqlSelect parsedQuery;
try {
parsedQuery = (SqlSelect) SqlParser.create(query, parserConfig).parseQuery();
} catch (SqlParseException e) {
LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName().asInternal());
continue;
}
final StringBuilder buf = new StringBuilder(query.length());
final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(true);
final SqlWriter writer = new SqlPrettyWriter(config, buf);
parsedQuery.unparse(writer, 0, 0);
query = buf.toString();
// Add the view for this query
String viewName = "$" + getTableNames().size();
SchemaPlus schema = parentSchema.getSubSchema(name);
if (schema == null) {
throw new IllegalStateException("Cannot find schema " + name + " in parent schema " + parentSchema.getName());
}
CalciteSchema calciteSchema = CalciteSchema.from(schema);
List<String> viewPath = calciteSchema.path(viewName);
schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName().asInternal(), true));
}
}
use of com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata in project java-driver by datastax.
the class ViewParserTest method should_skip_when_no_column_rows.
@Test
public void should_skip_when_no_column_rows() {
SchemaRows rows = rows(VIEW_ROW_3_0, Collections.emptyList());
ViewParser parser = new ViewParser(rows, context);
ViewMetadata view = parser.parseView(VIEW_ROW_3_0, KEYSPACE_ID, Collections.emptyMap());
assertThat(view).isNull();
}
Aggregations