kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e725ee58fbda8b72030c4d7b80cd7ac14cccf8cdfe68500edca74137c0b3eac0 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-09-16 19:21 +02:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-09-18 18:20 -05:00 |
| Parents | a18d9fe5715a |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/MaterializedViewMetadata.java +11 −24
| @@ -18,14 +18,11 @@ | ||
| 18 | 18 | |
| 19 | 19 | import java.util.*; |
| 20 | 20 | |
| 21 | -import com.google.common.base.Function; | |
| 22 | -import com.google.common.base.Joiner; | |
| 23 | 21 | import com.google.common.base.Objects; |
| 22 | + | |
| 24 | 23 | import org.slf4j.Logger; |
| 25 | 24 | import org.slf4j.LoggerFactory; |
| 26 | 25 | |
| 27 | -import static com.google.common.collect.Iterables.transform; | |
| 28 | - | |
| 29 | 26 | /** |
| 30 | 27 | * An immutable representation of a materialized view. |
| 31 | 28 | * Materialized views are available starting from Cassandra 3.0. |
| @@ -38,6 +35,8 @@ | ||
| 38 | 35 | |
| 39 | 36 | private final boolean includeAllColumns; |
| 40 | 37 | |
| 38 | + private final String whereClause; | |
| 39 | + | |
| 41 | 40 | private MaterializedViewMetadata( |
| 42 | 41 | KeyspaceMetadata keyspace, |
| 43 | 42 | TableMetadata baseTable, |
| @@ -47,12 +46,14 @@ | ||
| 47 | 46 | List<ColumnMetadata> clusteringColumns, |
| 48 | 47 | Map<String, ColumnMetadata> columns, |
| 49 | 48 | boolean includeAllColumns, |
| 49 | + String whereClause, | |
| 50 | 50 | Options options, |
| 51 | 51 | List<Order> clusteringOrder, |
| 52 | 52 | VersionNumber cassandraVersion) { |
| 53 | 53 | super(keyspace, name, id, partitionKey, clusteringColumns, columns, options, clusteringOrder, cassandraVersion); |
| 54 | 54 | this.baseTable = baseTable; |
| 55 | 55 | this.includeAllColumns = includeAllColumns; |
| 56 | + this.whereClause = whereClause; | |
| 56 | 57 | } |
| 57 | 58 | |
| 58 | 59 | static MaterializedViewMetadata build(KeyspaceMetadata keyspace, Row row, Map<String, ColumnMetadata.Raw> rawCols, VersionNumber cassandraVersion) { |
| @@ -69,6 +70,7 @@ | ||
| 69 | 70 | |
| 70 | 71 | UUID id = row.getUUID("id"); |
| 71 | 72 | boolean includeAllColumns = row.getBool("include_all_columns"); |
| 73 | + String whereClause = row.getString("where_clause"); | |
| 72 | 74 | |
| 73 | 75 | int partitionKeySize = findCollectionSize(rawCols.values(), ColumnMetadata.Raw.Kind.PARTITION_KEY); |
| 74 | 76 | int clusteringSize = findCollectionSize(rawCols.values(), ColumnMetadata.Raw.Kind.CLUSTERING_COLUMN); |
| @@ -92,7 +94,7 @@ | ||
| 92 | 94 | |
| 93 | 95 | MaterializedViewMetadata view = new MaterializedViewMetadata( |
| 94 | 96 | keyspace, baseTable, name, id, partitionKey, clusteringColumns, columns, |
| 95 | - includeAllColumns, options, clusteringOrder, cassandraVersion); | |
| 97 | + includeAllColumns, whereClause, options, clusteringOrder, cassandraVersion); | |
| 96 | 98 | |
| 97 | 99 | // We use this temporary set just so non PK columns are added in lexicographical order, which is the one of a |
| 98 | 100 | // 'SELECT * FROM ...' |
| @@ -147,13 +149,6 @@ | ||
| 147 | 149 | String keyspaceName = Metadata.escapeId(keyspace.getName()); |
| 148 | 150 | String baseTableName = Metadata.escapeId(baseTable.getName()); |
| 149 | 151 | 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 | - | |
| 157 | 152 | |
| 158 | 153 | StringBuilder sb = new StringBuilder(); |
| 159 | 154 | sb.append("CREATE MATERIALIZED VIEW ") |
| @@ -180,21 +175,14 @@ | ||
| 180 | 175 | newLine(sb.append("FROM ").append(keyspaceName).append('.').append(baseTableName).append(" "), formatted); |
| 181 | 176 | |
| 182 | 177 | // 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); | |
| 193 | 181 | |
| 194 | 182 | // PK |
| 195 | 183 | sb.append("PRIMARY KEY ("); |
| 196 | 184 | if (partitionKey.size() == 1) { |
| 197 | - sb.append(partitionKey.get(0).getName()); | |
| 185 | + sb.append(Metadata.escapeId(partitionKey.get(0).getName())); | |
| 198 | 186 | } else { |
| 199 | 187 | sb.append('('); |
| 200 | 188 | boolean first = true; |
| @@ -210,7 +198,6 @@ | ||
| 210 | 198 | for (ColumnMetadata cm : clusteringColumns) |
| 211 | 199 | sb.append(", ").append(Metadata.escapeId(cm.getName())); |
| 212 | 200 | sb.append(')'); |
| 213 | - newLine(sb, formatted); | |
| 214 | 201 | |
| 215 | 202 | appendOptions(sb, formatted); |
| 216 | 203 | return sb.toString(); |
modified driver-core/src/test/java/com/datastax/driver/core/MaterializedViewMetadataTest.java +24 −13
| @@ -29,6 +29,12 @@ | ||
| 29 | 29 | @CassandraVersion(major = 3) |
| 30 | 30 | public class MaterializedViewMetadataTest extends CCMBridge.PerClassSingleNodeCluster { |
| 31 | 31 | |
| 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 | + */ | |
| 32 | 38 | @Test(groups = "short") |
| 33 | 39 | public void should_create_view_metadata() { |
| 34 | 40 | |
| @@ -75,27 +81,31 @@ | ||
| 75 | 81 | assertThat(mv.getColumns().get(3)).isNotNull().hasName("score").isClusteringColumn().hasClusteringOrder(DESC); |
| 76 | 82 | assertThat(mv.getColumns().get(4)).isNotNull().hasName("user").isClusteringColumn(); |
| 77 | 83 | assertThat(mv.getColumns().get(5)).isNotNull().hasName("day").isClusteringColumn(); |
| 78 | - | |
| 79 | 84 | assertThat(mv.asCQLQuery(false)).contains(createMV); |
| 80 | - | |
| 81 | 85 | } |
| 82 | 86 | |
| 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 | + */ | |
| 83 | 93 | @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() { | |
| 85 | 95 | // given |
| 86 | 96 | String createTable = String.format( |
| 87 | 97 | "CREATE TABLE %s.t1 (" |
| 88 | 98 | + "\"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\"))", | |
| 92 | 102 | keyspace); |
| 93 | 103 | String createMV = String.format( |
| 94 | 104 | "CREATE MATERIALIZED VIEW %s.mv1 AS " |
| 95 | - + "SELECT \"theKey\", \"theClustering\", \"theValue\" " | |
| 105 | + + "SELECT \"theKey\", \"the;Clustering\", \"the Value\" " | |
| 96 | 106 | + "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\")", | |
| 99 | 109 | keyspace, keyspace); |
| 100 | 110 | // when |
| 101 | 111 | session.execute(createTable); |
| @@ -105,12 +115,13 @@ | ||
| 105 | 115 | MaterializedViewMetadata mv = cluster.getMetadata().getKeyspace(keyspace).getMaterializedView("mv1"); |
| 106 | 116 | assertThat(table).isNotNull().hasName("t1").hasMaterializedView(mv).hasNumberOfColumns(3); |
| 107 | 117 | 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()); | |
| 110 | 120 | assertThat(mv).isNotNull().hasName("mv1").hasBaseTable(table).hasNumberOfColumns(3); |
| 111 | 121 | 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); | |
| 114 | 125 | } |
| 115 | 126 | |
| 116 | 127 | @Override |