Search in sources :

Example 1 with ViewMetadata

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();
}
Also used : TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) MaterializedViewTable(org.apache.calcite.schema.impl.MaterializedViewTable) Table(org.apache.calcite.schema.Table) ImmutableMap(com.google.common.collect.ImmutableMap) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)

Example 2 with ViewMetadata

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());
}
Also used : TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MapType(com.datastax.oss.driver.api.core.type.MapType) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TupleType(com.datastax.oss.driver.api.core.type.TupleType) RelDataType(org.apache.calcite.rel.type.RelDataType) DataType(com.datastax.oss.driver.api.core.type.DataType) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)

Example 3 with ViewMetadata

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"));
}
Also used : ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) CassandraSchemaRows(com.datastax.oss.driver.internal.core.metadata.schema.queries.CassandraSchemaRows) SchemaRows(com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaRows) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata) Test(org.junit.Test)

Example 4 with ViewMetadata

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));
    }
}
Also used : RelDataTypeImpl(org.apache.calcite.rel.type.RelDataTypeImpl) IntStream(java.util.stream.IntStream) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ListType(com.datastax.oss.driver.api.core.type.ListType) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata) ArrayList(java.util.ArrayList) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Pair(org.apache.calcite.util.Pair) Map(java.util.Map) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) SqlSelect(org.apache.calcite.sql.SqlSelect) RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Row(com.datastax.oss.driver.api.core.cql.Row) RelDataType(org.apache.calcite.rel.type.RelDataType) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) MaterializedViewTable(org.apache.calcite.schema.impl.MaterializedViewTable) RelationMetadata(com.datastax.oss.driver.api.core.metadata.schema.RelationMetadata) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlWriter(org.apache.calcite.sql.SqlWriter) Logger(org.slf4j.Logger) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) MapType(com.datastax.oss.driver.api.core.type.MapType) ImmutableMap(com.google.common.collect.ImmutableMap) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Collection(java.util.Collection) Casing(org.apache.calcite.avatica.util.Casing) DataType(com.datastax.oss.driver.api.core.type.DataType) Table(org.apache.calcite.schema.Table) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) Collectors(java.util.stream.Collectors) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) Objects(java.util.Objects) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) List(java.util.List) SqlParser(org.apache.calcite.sql.parser.SqlParser) CalciteTrace(org.apache.calcite.util.trace.CalciteTrace) Optional(java.util.Optional) SetType(com.datastax.oss.driver.api.core.type.SetType) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) Hook(org.apache.calcite.runtime.Hook) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlParser(org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlSelect(org.apache.calcite.sql.SqlSelect) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Row(com.datastax.oss.driver.api.core.cql.Row) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)

Example 5 with ViewMetadata

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();
}
Also used : CassandraSchemaRows(com.datastax.oss.driver.internal.core.metadata.schema.queries.CassandraSchemaRows) SchemaRows(com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaRows) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata) Test(org.junit.Test)

Aggregations

ViewMetadata (com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)5 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)3 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 DataType (com.datastax.oss.driver.api.core.type.DataType)2 ListType (com.datastax.oss.driver.api.core.type.ListType)2 MapType (com.datastax.oss.driver.api.core.type.MapType)2 SetType (com.datastax.oss.driver.api.core.type.SetType)2 TupleType (com.datastax.oss.driver.api.core.type.TupleType)2 CassandraSchemaRows (com.datastax.oss.driver.internal.core.metadata.schema.queries.CassandraSchemaRows)2 SchemaRows (com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaRows)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2 RelProtoDataType (org.apache.calcite.rel.type.RelProtoDataType)2 Table (org.apache.calcite.schema.Table)2 MaterializedViewTable (org.apache.calcite.schema.impl.MaterializedViewTable)2