Search in sources :

Example 11 with ColumnMetadata

use of com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata in project java-driver by datastax.

the class EntityHelperBaseTest method typesProvider.

@DataProvider
public static Object[][] typesProvider() {
    ColumnMetadata columnMetadataText = mock(ColumnMetadata.class);
    when(columnMetadataText.getType()).thenReturn(DataTypes.TEXT);
    ColumnMetadata columnMetadataInt = mock(ColumnMetadata.class);
    when(columnMetadataInt.getType()).thenReturn(DataTypes.INT);
    CqlIdentifier c1 = CqlIdentifier.fromCql("c1");
    CqlIdentifier c2 = CqlIdentifier.fromCql("c2");
    return new Object[][] { { ImmutableMap.of(c1, GenericType.of(String.class)), ImmutableMap.of(c1, columnMetadataText), Collections.emptyList() }, { ImmutableMap.of(c1, GenericType.of(Integer.class)), ImmutableMap.of(c1, columnMetadataText), ImmutableList.of("Field: c1, Entity Type: java.lang.Integer, CQL type: TEXT") }, { ImmutableMap.of(c1, GenericType.of(String.class), c2, GenericType.of(Integer.class)), ImmutableMap.of(c1, columnMetadataText, c2, columnMetadataInt), Collections.emptyList() }, { ImmutableMap.of(c1, GenericType.of(String.class), c2, GenericType.of(Integer.class)), ImmutableMap.of(c1, columnMetadataInt, c2, columnMetadataText), ImmutableList.of("Field: c1, Entity Type: java.lang.String, CQL type: INT", "Field: c2, Entity Type: java.lang.Integer, CQL type: TEXT") } };
}
Also used : ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Example 12 with ColumnMetadata

use of com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata in project java-driver by datastax.

the class EntityHelperBaseTest method should_throw_if_there_is_not_matching_cql_column.

@Test
public void should_throw_if_there_is_not_matching_cql_column() {
    // given
    ImmutableMap<CqlIdentifier, GenericType<?>> entityColumns = ImmutableMap.of(CqlIdentifier.fromCql("c1"), GenericType.of(Integer.class));
    ColumnMetadata columnMetadataInt = mock(ColumnMetadata.class);
    when(columnMetadataInt.getType()).thenReturn(DataTypes.INT);
    ImmutableMap<CqlIdentifier, ColumnMetadata> cqlColumns = ImmutableMap.of(CqlIdentifier.fromCql("c2"), columnMetadataInt);
    // when, then
    assertThatThrownBy(() -> EntityHelperBase.findTypeMismatches(entityColumns, cqlColumns, CodecRegistry.DEFAULT)).isInstanceOf(AssertionError.class).hasMessageContaining("There is no cql column for entity column: c1");
}
Also used : GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 13 with ColumnMetadata

use of com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata in project java-driver by datastax.

the class DseViewParser method parseView.

public DseViewMetadata parseView(AdminRow viewRow, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userTypes) {
    // Cassandra 3.0 (no views in earlier versions):
    // CREATE TABLE system_schema.views (
    // keyspace_name text,
    // view_name text,
    // base_table_id uuid,
    // base_table_name text,
    // bloom_filter_fp_chance double,
    // caching frozen<map<text, text>>,
    // cdc boolean,
    // comment text,
    // compaction frozen<map<text, text>>,
    // compression frozen<map<text, text>>,
    // crc_check_chance double,
    // dclocal_read_repair_chance double,
    // default_time_to_live int,
    // extensions frozen<map<text, blob>>,
    // gc_grace_seconds int,
    // id uuid,
    // include_all_columns boolean,
    // max_index_interval int,
    // memtable_flush_period_in_ms int,
    // min_index_interval int,
    // read_repair_chance double,
    // speculative_retry text,
    // where_clause text,
    // PRIMARY KEY (keyspace_name, view_name)
    // ) WITH CLUSTERING ORDER BY (view_name ASC)
    CqlIdentifier viewId = CqlIdentifier.fromInternal(viewRow.getString("view_name"));
    UUID uuid = viewRow.getUuid("id");
    CqlIdentifier baseTableId = CqlIdentifier.fromInternal(viewRow.getString("base_table_name"));
    boolean includesAllColumns = MoreObjects.firstNonNull(viewRow.getBoolean("include_all_columns"), false);
    String whereClause = viewRow.getString("where_clause");
    List<RawColumn> rawColumns = RawColumn.toRawColumns(rows.columns().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(viewId));
    if (rawColumns.isEmpty()) {
        LOG.warn("[{}] Processing VIEW refresh for {}.{} but found no matching rows, skipping", logPrefix, keyspaceId, viewId);
        return null;
    }
    Collections.sort(rawColumns);
    ImmutableMap.Builder<CqlIdentifier, ColumnMetadata> allColumnsBuilder = ImmutableMap.builder();
    ImmutableList.Builder<ColumnMetadata> partitionKeyBuilder = ImmutableList.builder();
    ImmutableMap.Builder<ColumnMetadata, ClusteringOrder> clusteringColumnsBuilder = ImmutableMap.builder();
    for (RawColumn raw : rawColumns) {
        DataType dataType = rows.dataTypeParser().parse(keyspaceId, raw.dataType, userTypes, context);
        DseColumnMetadata column = new DefaultDseColumnMetadata(keyspaceId, viewId, raw.name, dataType, raw.kind.equals(RawColumn.KIND_STATIC));
        switch(raw.kind) {
            case RawColumn.KIND_PARTITION_KEY:
                partitionKeyBuilder.add(column);
                break;
            case RawColumn.KIND_CLUSTERING_COLUMN:
                clusteringColumnsBuilder.put(column, raw.reversed ? ClusteringOrder.DESC : ClusteringOrder.ASC);
                break;
            default:
        }
        allColumnsBuilder.put(column.getName(), column);
    }
    Map<CqlIdentifier, Object> options;
    try {
        options = parseOptions(viewRow);
    } catch (Exception e) {
        // Options change the most often, so be especially lenient if anything goes wrong.
        Loggers.warnWithException(LOG, "[{}] Error while parsing options for {}.{}, getOptions() will be empty", logPrefix, keyspaceId, viewId, e);
        options = Collections.emptyMap();
    }
    return new DefaultDseViewMetadata(keyspaceId, viewId, baseTableId, includesAllColumns, whereClause, uuid, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), options);
}
Also used : DefaultDseColumnMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseColumnMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) DseColumnMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseColumnMetadata) DefaultDseColumnMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseColumnMetadata) DseColumnMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseColumnMetadata) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) DefaultDseColumnMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseColumnMetadata) DefaultDseViewMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseViewMetadata) RawColumn(com.datastax.oss.driver.internal.core.metadata.schema.parsing.RawColumn) DataType(com.datastax.oss.driver.api.core.type.DataType) UUID(java.util.UUID) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)

Example 14 with ColumnMetadata

use of com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata in project java-driver by datastax.

the class DseGraphTableMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    ScriptBuilder builder = new ScriptBuilder(pretty);
    if (isVirtual()) {
        builder.append("/* VIRTUAL ");
    } else {
        builder.append("CREATE ");
    }
    builder.append("TABLE ").append(getKeyspace()).append(".").append(getName()).append(" (").newLine().increaseIndent();
    for (ColumnMetadata column : getColumns().values()) {
        builder.append(column.getName()).append(" ").append(column.getType().asCql(true, pretty));
        if (column.isStatic()) {
            builder.append(" static");
        }
        builder.append(",").newLine();
    }
    // PK
    builder.append("PRIMARY KEY (");
    if (getPartitionKey().size() == 1) {
        // PRIMARY KEY (k
        builder.append(getPartitionKey().get(0).getName());
    } else {
        // PRIMARY KEY ((k1, k2)
        builder.append("(");
        boolean first = true;
        for (ColumnMetadata pkColumn : getPartitionKey()) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(pkColumn.getName());
        }
        builder.append(")");
    }
    // PRIMARY KEY (<pk portion>, cc1, cc2, cc3)
    for (ColumnMetadata clusteringColumn : getClusteringColumns().keySet()) {
        builder.append(", ").append(clusteringColumn.getName());
    }
    builder.append(")");
    builder.newLine().decreaseIndent().append(")");
    builder.increaseIndent();
    if (isCompactStorage()) {
        builder.andWith().append("COMPACT STORAGE");
    }
    if (getClusteringColumns().containsValue(ClusteringOrder.DESC)) {
        builder.andWith().append("CLUSTERING ORDER BY (");
        boolean first = true;
        for (Map.Entry<? extends ColumnMetadata, ClusteringOrder> entry : getClusteringColumns().entrySet()) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(entry.getKey().getName()).append(" ").append(entry.getValue().name());
        }
        builder.append(")");
    }
    getVertex().ifPresent(vertex -> {
        builder.andWith().append("VERTEX LABEL").append(" ").append(vertex.getLabelName());
    });
    getEdge().ifPresent(edge -> {
        builder.andWith().append("EDGE LABEL").append(" ").append(edge.getLabelName());
        ScriptHelper.appendEdgeSide(builder, edge.getFromTable(), edge.getFromLabel(), edge.getFromPartitionKeyColumns(), edge.getFromClusteringColumns(), "FROM");
        ScriptHelper.appendEdgeSide(builder, edge.getToTable(), edge.getToLabel(), edge.getToPartitionKeyColumns(), edge.getToClusteringColumns(), "TO");
    });
    Map<CqlIdentifier, Object> options = getOptions();
    RelationParser.appendOptions(options, builder);
    builder.append(";");
    if (isVirtual()) {
        builder.append(" */");
    }
    return builder.build();
}
Also used : ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) ScriptBuilder(com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder) Map(java.util.Map) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 15 with ColumnMetadata

use of com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata in project java-driver by datastax.

the class SchemaIT method should_get_virtual_metadata.

@CassandraRequirement(min = "4.0", description = "virtual tables introduced in 4.0")
@Test
public void should_get_virtual_metadata() {
    skipIfDse60();
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withStringList(DefaultDriverOption.METADATA_SCHEMA_REFRESHED_KEYSPACES, Collections.singletonList("system_views")).build();
    try (CqlSession session = SessionUtils.newSession(ccmRule, loader)) {
        Metadata md = session.getMetadata();
        KeyspaceMetadata kmd = md.getKeyspace("system_views").get();
        // Keyspace name should be set, marked as virtual, and have at least sstable_tasks table.
        // All other values should be defaulted since they are not defined in the virtual schema
        // tables.
        assertThat(kmd.getTables().size()).isGreaterThanOrEqualTo(1);
        assertThat(kmd.isVirtual()).isTrue();
        assertThat(kmd.isDurableWrites()).isFalse();
        assertThat(kmd.getName().asCql(true)).isEqualTo("system_views");
        // Virtual tables lack User Types, Functions, Views and Aggregates
        assertThat(kmd.getUserDefinedTypes().size()).isEqualTo(0);
        assertThat(kmd.getFunctions().size()).isEqualTo(0);
        assertThat(kmd.getViews().size()).isEqualTo(0);
        assertThat(kmd.getAggregates().size()).isEqualTo(0);
        assertThat(kmd.describe(true)).isEqualTo("/* VIRTUAL KEYSPACE system_views WITH replication = { 'class' : 'null' } " + "AND durable_writes = false; */");
        // Table name should be set, marked as virtual, and it should have columns set.
        // indexes, views, clustering column, clustering order and id are not defined in the virtual
        // schema tables.
        TableMetadata tm = kmd.getTable("sstable_tasks").get();
        assertThat(tm).isNotNull();
        assertThat(tm.getName().toString()).isEqualTo("sstable_tasks");
        assertThat(tm.isVirtual()).isTrue();
        // DSE 6.8+ reports 7 columns, Cassandra 4+ reports 8 columns
        assertThat(tm.getColumns().size()).isGreaterThanOrEqualTo(7);
        assertThat(tm.getIndexes().size()).isEqualTo(0);
        assertThat(tm.getPartitionKey().size()).isEqualTo(1);
        assertThat(tm.getPartitionKey().get(0).getName().toString()).isEqualTo("keyspace_name");
        assertThat(tm.getClusteringColumns().size()).isEqualTo(2);
        assertThat(tm.getId().isPresent()).isFalse();
        assertThat(tm.getOptions().size()).isEqualTo(0);
        assertThat(tm.getKeyspace()).isEqualTo(kmd.getName());
        assertThat(tm.describe(true)).isIn(// DSE 6.8+
        "/* VIRTUAL TABLE system_views.sstable_tasks (\n" + "    keyspace_name text,\n" + "    table_name text,\n" + "    task_id uuid,\n" + "    kind text,\n" + "    progress bigint,\n" + "    total bigint,\n" + "    unit text,\n" + "    PRIMARY KEY (keyspace_name, table_name, task_id)\n" + "); */", // Cassandra 4.0
        "/* VIRTUAL TABLE system_views.sstable_tasks (\n" + "    keyspace_name text,\n" + "    table_name text,\n" + "    task_id uuid,\n" + "    completion_ratio double,\n" + "    kind text,\n" + "    progress bigint,\n" + "    total bigint,\n" + "    unit text,\n" + "    PRIMARY KEY (keyspace_name, table_name, task_id)\n" + "); */");
        // ColumnMetadata is as expected
        ColumnMetadata cm = tm.getColumn("progress").get();
        assertThat(cm).isNotNull();
        assertThat(cm.getParent()).isEqualTo(tm.getName());
        assertThat(cm.getType()).isEqualTo(DataTypes.BIGINT);
        assertThat(cm.getName().toString()).isEqualTo("progress");
    }
}
Also used : TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlSession(com.datastax.oss.driver.api.core.CqlSession) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Aggregations

ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)15 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)11 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)8 DataType (com.datastax.oss.driver.api.core.type.DataType)8 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)7 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)7 Map (java.util.Map)4 UUID (java.util.UUID)4 DseColumnMetadata (com.datastax.dse.driver.api.core.metadata.schema.DseColumnMetadata)3 DefaultDseColumnMetadata (com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseColumnMetadata)3 IndexMetadata (com.datastax.oss.driver.api.core.metadata.schema.IndexMetadata)3 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)3 DefaultColumnMetadata (com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata)3 RawColumn (com.datastax.oss.driver.internal.core.metadata.schema.parsing.RawColumn)3 Test (org.junit.Test)3 DefaultDseTableMetadata (com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseTableMetadata)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 KeyspaceMetadata (com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)2 ViewMetadata (com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)2 SetType (com.datastax.oss.driver.api.core.type.SetType)2