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