Elffuss BraidHub

kikocis/java-driver public

JAVA-918: Allow MV's select statements to be more complex.
Changesete725ee58fbda8b72030c4d7b80cd7ac14cccf8cdfe68500edca74137c0b3eac0
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-09-16 19:21 +02:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-09-18 18:20 -05:00
Parentsa18d9fe5715a

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/MaterializedViewMetadata.java +11 −24
@@ -18,14 +18,11 @@
1818
1919 import java.util.*;
2020
21-import com.google.common.base.Function;
22-import com.google.common.base.Joiner;
2321 import com.google.common.base.Objects;
22+
2423 import org.slf4j.Logger;
2524 import org.slf4j.LoggerFactory;
2625
27-import static com.google.common.collect.Iterables.transform;
28-
2926 /**
3027 * An immutable representation of a materialized view.
3128 * Materialized views are available starting from Cassandra 3.0.
@@ -38,6 +35,8 @@
3835
3936 private final boolean includeAllColumns;
4037
38+ private final String whereClause;
39+
4140 private MaterializedViewMetadata(
4241 KeyspaceMetadata keyspace,
4342 TableMetadata baseTable,
@@ -47,12 +46,14 @@
4746 List<ColumnMetadata> clusteringColumns,
4847 Map<String, ColumnMetadata> columns,
4948 boolean includeAllColumns,
49+ String whereClause,
5050 Options options,
5151 List<Order> clusteringOrder,
5252 VersionNumber cassandraVersion) {
5353 super(keyspace, name, id, partitionKey, clusteringColumns, columns, options, clusteringOrder, cassandraVersion);
5454 this.baseTable = baseTable;
5555 this.includeAllColumns = includeAllColumns;
56+ this.whereClause = whereClause;
5657 }
5758
5859 static MaterializedViewMetadata build(KeyspaceMetadata keyspace, Row row, Map<String, ColumnMetadata.Raw> rawCols, VersionNumber cassandraVersion) {
@@ -69,6 +70,7 @@
6970
7071 UUID id = row.getUUID("id");
7172 boolean includeAllColumns = row.getBool("include_all_columns");
73+ String whereClause = row.getString("where_clause");
7274
7375 int partitionKeySize = findCollectionSize(rawCols.values(), ColumnMetadata.Raw.Kind.PARTITION_KEY);
7476 int clusteringSize = findCollectionSize(rawCols.values(), ColumnMetadata.Raw.Kind.CLUSTERING_COLUMN);
@@ -92,7 +94,7 @@
9294
9395 MaterializedViewMetadata view = new MaterializedViewMetadata(
9496 keyspace, baseTable, name, id, partitionKey, clusteringColumns, columns,
95- includeAllColumns, options, clusteringOrder, cassandraVersion);
97+ includeAllColumns, whereClause, options, clusteringOrder, cassandraVersion);
9698
9799 // We use this temporary set just so non PK columns are added in lexicographical order, which is the one of a
98100 // 'SELECT * FROM ...'
@@ -147,13 +149,6 @@
147149 String keyspaceName = Metadata.escapeId(keyspace.getName());
148150 String baseTableName = Metadata.escapeId(baseTable.getName());
149151 String viewName = Metadata.escapeId(name);
150- String whereClause = Joiner.on(" AND ").join(transform(getPartitionKey(), new Function<ColumnMetadata, String>() {
151- @Override
152- public String apply(ColumnMetadata input) {
153- return Metadata.escapeId(input.getName()) + " IS NOT NULL";
154- }
155- }));
156-
157152
158153 StringBuilder sb = new StringBuilder();
159154 sb.append("CREATE MATERIALIZED VIEW ")
@@ -180,21 +175,14 @@
180175 newLine(sb.append("FROM ").append(keyspaceName).append('.').append(baseTableName).append(" "), formatted);
181176
182177 // WHERE
183- newLine(sb.append("WHERE "), formatted);
184- Iterator<ColumnMetadata> it = getPrimaryKey().iterator();
185- while(it.hasNext()) {
186- ColumnMetadata column = it.next();
187- sb.append(spaces(4, formatted)).append(Metadata.escapeId(column.getName()));
188- sb.append(" IS NOT NULL");
189- if(it.hasNext()) sb.append(" AND");
190- sb.append(" ");
191- newLine(sb, formatted);
192- }
178+ // the CQL grammar allows missing WHERE clauses, although C* currently disallows it
179+ if (whereClause != null && !whereClause.isEmpty())
180+ newLine(sb.append("WHERE ").append(whereClause).append(' '), formatted);
193181
194182 // PK
195183 sb.append("PRIMARY KEY (");
196184 if (partitionKey.size() == 1) {
197- sb.append(partitionKey.get(0).getName());
185+ sb.append(Metadata.escapeId(partitionKey.get(0).getName()));
198186 } else {
199187 sb.append('(');
200188 boolean first = true;
@@ -210,7 +198,6 @@
210198 for (ColumnMetadata cm : clusteringColumns)
211199 sb.append(", ").append(Metadata.escapeId(cm.getName()));
212200 sb.append(')');
213- newLine(sb, formatted);
214201
215202 appendOptions(sb, formatted);
216203 return sb.toString();
modified driver-core/src/test/java/com/datastax/driver/core/MaterializedViewMetadataTest.java +24 −13
@@ -29,6 +29,12 @@
2929 @CassandraVersion(major = 3)
3030 public class MaterializedViewMetadataTest extends CCMBridge.PerClassSingleNodeCluster {
3131
32+ /**
33+ * Validates that a materialized view is properly retrieved and parsed.
34+ *
35+ * @test_category metadata,materialized_view
36+ * @jira_ticket JAVA-825
37+ */
3238 @Test(groups = "short")
3339 public void should_create_view_metadata() {
3440
@@ -75,27 +81,31 @@
7581 assertThat(mv.getColumns().get(3)).isNotNull().hasName("score").isClusteringColumn().hasClusteringOrder(DESC);
7682 assertThat(mv.getColumns().get(4)).isNotNull().hasName("user").isClusteringColumn();
7783 assertThat(mv.getColumns().get(5)).isNotNull().hasName("day").isClusteringColumn();
78-
7984 assertThat(mv.asCQLQuery(false)).contains(createMV);
80-
8185 }
8286
87+ /**
88+ * Validates that a materialized view is properly retrieved and parsed when using quoted identifiers.
89+ *
90+ * @test_category metadata,materialized_view
91+ * @jira_ticket JAVA-825
92+ */
8393 @Test(groups = "short")
84- public void should_create_view_metadata_with_case_sensitive_column_names() {
94+ public void should_create_view_metadata_with_quoted_identifiers() {
8595 // given
8696 String createTable = String.format(
8797 "CREATE TABLE %s.t1 ("
8898 + "\"theKey\" int, "
89- + "\"theClustering\" int, "
90- + "\"theValue\" int, "
91- + "PRIMARY KEY (\"theKey\", \"theClustering\"))",
99+ + "\"the;Clustering\" int, "
100+ + "\"the Value\" int, "
101+ + "PRIMARY KEY (\"theKey\", \"the;Clustering\"))",
92102 keyspace);
93103 String createMV = String.format(
94104 "CREATE MATERIALIZED VIEW %s.mv1 AS "
95- + "SELECT \"theKey\", \"theClustering\", \"theValue\" "
105+ + "SELECT \"theKey\", \"the;Clustering\", \"the Value\" "
96106 + "FROM %s.t1 "
97- + "WHERE \"theKey\" IS NOT NULL AND \"theClustering\" IS NOT NULL AND \"theValue\" IS NOT NULL "
98- + "PRIMARY KEY (\"theKey\", \"theClustering\")",
107+ + "WHERE \"theKey\" IS NOT NULL AND \"the;Clustering\" IS NOT NULL AND \"the Value\" IS NOT NULL "
108+ + "PRIMARY KEY (\"theKey\", \"the;Clustering\")",
99109 keyspace, keyspace);
100110 // when
101111 session.execute(createTable);
@@ -105,12 +115,13 @@
105115 MaterializedViewMetadata mv = cluster.getMetadata().getKeyspace(keyspace).getMaterializedView("mv1");
106116 assertThat(table).isNotNull().hasName("t1").hasMaterializedView(mv).hasNumberOfColumns(3);
107117 assertThat(table.getColumns().get(0)).isNotNull().hasName("theKey").isPartitionKey().hasType(cint());
108- assertThat(table.getColumns().get(1)).isNotNull().hasName("theClustering").isClusteringColumn().hasType(cint());
109- assertThat(table.getColumns().get(2)).isNotNull().hasName("theValue").isRegularColumn().hasType(cint());
118+ assertThat(table.getColumns().get(1)).isNotNull().hasName("the;Clustering").isClusteringColumn().hasType(cint());
119+ assertThat(table.getColumns().get(2)).isNotNull().hasName("the Value").isRegularColumn().hasType(cint());
110120 assertThat(mv).isNotNull().hasName("mv1").hasBaseTable(table).hasNumberOfColumns(3);
111121 assertThat(mv.getColumns().get(0)).isNotNull().hasName("theKey").isPartitionKey().hasType(cint());
112- assertThat(mv.getColumns().get(1)).isNotNull().hasName("theClustering").isClusteringColumn().hasType(cint());
113- assertThat(mv.getColumns().get(2)).isNotNull().hasName("theValue").isRegularColumn().hasType(cint());
122+ assertThat(mv.getColumns().get(1)).isNotNull().hasName("the;Clustering").isClusteringColumn().hasType(cint());
123+ assertThat(mv.getColumns().get(2)).isNotNull().hasName("the Value").isRegularColumn().hasType(cint());
124+ assertThat(mv.asCQLQuery(false)).contains(createMV);
114125 }
115126
116127 @Override