kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 56dd40c2701d3384047c27f4a45d93b076fe823e27fafbe388cf5b55ab2b6483 |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-07-15 00:15 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-07-14 15:15 -07:00 |
| Parents | 2c0ff9778e8c |
10 files changed
modified changelog/README.md +3 −0
| @@ -2,6 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | ### 3.1.0 (in progress) |
| 4 | 4 | |
| 5 | +- [new feature] JAVA-1153: Add PER PARTITION LIMIT to Select QueryBuilder. | |
| 6 | + | |
| 7 | + | |
| 5 | 8 | Merged from 3.0.x branch: |
| 6 | 9 | |
| 7 | 10 | - [bug] JAVA-1179: Request objects should be copied when executed. |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +1 −1
| @@ -110,7 +110,7 @@ | ||
| 110 | 110 | this.nonBuiltStatementValues += ((SimpleStatement) statement).valuesCount(); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - checkForBindMarkers(null); | |
| 113 | + setDirty(); | |
| 114 | 114 | |
| 115 | 115 | return this; |
| 116 | 116 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +4 −0
| @@ -210,6 +210,10 @@ | ||
| 210 | 210 | hasNonIdempotentOps = true; |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | + void setDirty() { | |
| 214 | + dirty = true; | |
| 215 | + } | |
| 216 | + | |
| 213 | 217 | void checkForBindMarkers(Object value) { |
| 214 | 218 | dirty = true; |
| 215 | 219 | if (Utils.containsBindMarker(value)) |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +106 −15
| @@ -38,6 +38,7 @@ | ||
| 38 | 38 | private final Where where; |
| 39 | 39 | private List<Ordering> orderings; |
| 40 | 40 | private Object limit; |
| 41 | + private Object perPartitionLimit; | |
| 41 | 42 | private boolean allowFiltering; |
| 42 | 43 | |
| 43 | 44 | Select(String keyspace, String table, List<Object> columnNames, boolean isDistinct) { |
| @@ -93,6 +94,10 @@ | ||
| 93 | 94 | Utils.joinAndAppend(builder, codecRegistry, ",", orderings, variables); |
| 94 | 95 | } |
| 95 | 96 | |
| 97 | + if (perPartitionLimit != null) { | |
| 98 | + builder.append(" PER PARTITION LIMIT ").append(perPartitionLimit); | |
| 99 | + } | |
| 100 | + | |
| 96 | 101 | if (limit != null) { |
| 97 | 102 | builder.append(" LIMIT ").append(limit); |
| 98 | 103 | } |
| @@ -144,12 +149,12 @@ | ||
| 144 | 149 | } |
| 145 | 150 | |
| 146 | 151 | /** |
| 147 | - * Adds a LIMIT clause to this statement. | |
| 152 | + * Adds a {@code LIMIT} clause to this statement. | |
| 148 | 153 | * |
| 149 | 154 | * @param limit the limit to set. |
| 150 | 155 | * @return this statement. |
| 151 | - * @throws IllegalArgumentException if {@code limit >e; 0}. | |
| 152 | - * @throws IllegalStateException if a LIMIT clause has already been | |
| 156 | + * @throws IllegalArgumentException if {@code limit <= 0}. | |
| 157 | + * @throws IllegalStateException if a {@code LIMIT} clause has already been | |
| 153 | 158 | * provided. |
| 154 | 159 | */ |
| 155 | 160 | public Select limit(int limit) { |
| @@ -160,16 +165,16 @@ | ||
| 160 | 165 | throw new IllegalStateException("A LIMIT value has already been provided"); |
| 161 | 166 | |
| 162 | 167 | this.limit = limit; |
| 163 | - checkForBindMarkers(null); | |
| 168 | + setDirty(); | |
| 164 | 169 | return this; |
| 165 | 170 | } |
| 166 | 171 | |
| 167 | 172 | /** |
| 168 | - * Adds a prepared LIMIT clause to this statement. | |
| 173 | + * Adds a prepared {@code LIMIT} clause to this statement. | |
| 169 | 174 | * |
| 170 | 175 | * @param marker the marker to use for the limit. |
| 171 | 176 | * @return this statement. |
| 172 | - * @throws IllegalStateException if a LIMIT clause has already been | |
| 177 | + * @throws IllegalStateException if a {@code LIMIT} clause has already been | |
| 173 | 178 | * provided. |
| 174 | 179 | */ |
| 175 | 180 | public Select limit(BindMarker marker) { |
| @@ -181,6 +186,56 @@ | ||
| 181 | 186 | return this; |
| 182 | 187 | } |
| 183 | 188 | |
| 189 | + /** | |
| 190 | + * Adds a {@code PER PARTITION LIMIT} clause to this statement. | |
| 191 | + * <p> | |
| 192 | + * Note: support for {@code PER PARTITION LIMIT} clause is only available from | |
| 193 | + * Cassandra 3.6 onwards. | |
| 194 | + * | |
| 195 | + * @param perPartitionLimit the limit to set per partition. | |
| 196 | + * @return this statement. | |
| 197 | + * @throws IllegalArgumentException if {@code perPartitionLimit <= 0}. | |
| 198 | + * @throws IllegalStateException if a {@code PER PARTITION LIMIT} clause has already been | |
| 199 | + * provided. | |
| 200 | + * @throws IllegalStateException if this statement is a {@code SELECT DISTINCT} statement. | |
| 201 | + */ | |
| 202 | + public Select perPartitionLimit(int perPartitionLimit) { | |
| 203 | + if (perPartitionLimit <= 0) | |
| 204 | + throw new IllegalArgumentException("Invalid PER PARTITION LIMIT value, must be strictly positive"); | |
| 205 | + | |
| 206 | + if (this.perPartitionLimit != null) | |
| 207 | + throw new IllegalStateException("A PER PARTITION LIMIT value has already been provided"); | |
| 208 | + if (isDistinct) | |
| 209 | + throw new IllegalStateException("PER PARTITION LIMIT is not allowed with SELECT DISTINCT queries"); | |
| 210 | + | |
| 211 | + this.perPartitionLimit = perPartitionLimit; | |
| 212 | + setDirty(); | |
| 213 | + return this; | |
| 214 | + } | |
| 215 | + | |
| 216 | + /** | |
| 217 | + * Adds a prepared {@code PER PARTITION LIMIT} clause to this statement. | |
| 218 | + * <p> | |
| 219 | + * Note: support for {@code PER PARTITION LIMIT} clause is only available from | |
| 220 | + * Cassandra 3.6 onwards. | |
| 221 | + * | |
| 222 | + * @param marker the marker to use for the limit per partition. | |
| 223 | + * @return this statement. | |
| 224 | + * @throws IllegalStateException if a {@code PER PARTITION LIMIT} clause has already been | |
| 225 | + * provided. | |
| 226 | + * @throws IllegalStateException if this statement is a {@code SELECT DISTINCT} statement. | |
| 227 | + */ | |
| 228 | + public Select perPartitionLimit(BindMarker marker) { | |
| 229 | + if (this.perPartitionLimit != null) | |
| 230 | + throw new IllegalStateException("A PER PARTITION LIMIT value has already been provided"); | |
| 231 | + if (isDistinct) | |
| 232 | + throw new IllegalStateException("PER PARTITION LIMIT is not allowed with SELECT DISTINCT queries"); | |
| 233 | + | |
| 234 | + this.perPartitionLimit = marker; | |
| 235 | + checkForBindMarkers(marker); | |
| 236 | + return this; | |
| 237 | + } | |
| 238 | + | |
| 184 | 239 | /** |
| 185 | 240 | * Adds an ALLOW FILTERING directive to this statement. |
| 186 | 241 | * |
| @@ -229,13 +284,13 @@ | ||
| 229 | 284 | } |
| 230 | 285 | |
| 231 | 286 | /** |
| 232 | - * Adds a LIMIT clause to the SELECT statement this Where clause if | |
| 233 | - * part of. | |
| 287 | + * Adds a {@code LIMIT} clause to the {@code SELECT} statement this | |
| 288 | + * {@code WHERE} clause if part of. | |
| 234 | 289 | * |
| 235 | 290 | * @param limit the limit to set. |
| 236 | - * @return the select statement this Where clause if part of. | |
| 237 | - * @throws IllegalArgumentException if {@code limit >e; 0}. | |
| 238 | - * @throws IllegalStateException if a LIMIT clause has already been | |
| 291 | + * @return the {@code SELECT} statement this {@code WHERE} clause if part of. | |
| 292 | + * @throws IllegalArgumentException if {@code limit <= 0}. | |
| 293 | + * @throws IllegalStateException if a {@code LIMIT} clause has already been | |
| 239 | 294 | * provided. |
| 240 | 295 | */ |
| 241 | 296 | public Select limit(int limit) { |
| @@ -243,17 +298,53 @@ | ||
| 243 | 298 | } |
| 244 | 299 | |
| 245 | 300 | /** |
| 246 | - * Adds a bind marker for the LIMIT clause to the SELECT statement this | |
| 247 | - * Where clause if part of. | |
| 301 | + * Adds a bind marker for the {@code LIMIT} clause to the {@code SELECT} statement this | |
| 302 | + * {@code WHERE} clause if part of. | |
| 248 | 303 | * |
| 249 | 304 | * @param limit the bind marker to use as limit. |
| 250 | - * @return the select statement this Where clause if part of. | |
| 251 | - * @throws IllegalStateException if a LIMIT clause has already been | |
| 305 | + * @return the {@code SELECT} statement this {@code WHERE} clause if part of. | |
| 306 | + * @throws IllegalStateException if a {@code LIMIT} clause has already been | |
| 252 | 307 | * provided. |
| 253 | 308 | */ |
| 254 | 309 | public Select limit(BindMarker limit) { |
| 255 | 310 | return statement.limit(limit); |
| 256 | 311 | } |
| 312 | + | |
| 313 | + /** | |
| 314 | + * Adds a {@code PER PARTITION LIMIT} clause to the {@code SELECT} statement this | |
| 315 | + * {@code WHERE} clause if part of. | |
| 316 | + * <p> | |
| 317 | + * Note: support for {@code PER PARTITION LIMIT} clause is only available from | |
| 318 | + * Cassandra 3.6 onwards. | |
| 319 | + * | |
| 320 | + * @param perPartitionLimit the limit to set per partition. | |
| 321 | + * @return the {@code SELECT} statement this {@code WHERE} clause if part of. | |
| 322 | + * @throws IllegalArgumentException if {@code perPartitionLimit <= 0}. | |
| 323 | + * @throws IllegalStateException if a {@code PER PARTITION LIMIT} clause has already been | |
| 324 | + * provided. | |
| 325 | + * @throws IllegalStateException if this statement is a {@code SELECT DISTINCT} statement. | |
| 326 | + */ | |
| 327 | + public Select perPartitionLimit(int perPartitionLimit) { | |
| 328 | + return statement.perPartitionLimit(perPartitionLimit); | |
| 329 | + } | |
| 330 | + | |
| 331 | + /** | |
| 332 | + * Adds a bind marker for the {@code PER PARTITION LIMIT} clause to the {@code SELECT} statement this | |
| 333 | + * {@code WHERE} clause if part of. | |
| 334 | + * <p> | |
| 335 | + * Note: support for {@code PER PARTITION LIMIT} clause is only available from | |
| 336 | + * Cassandra 3.6 onwards. | |
| 337 | + * | |
| 338 | + * @param limit the bind marker to use as limit per partition. | |
| 339 | + * @return the {@code SELECT} statement this {@code WHERE} clause if part of. | |
| 340 | + * @throws IllegalStateException if a {@code PER PARTITION LIMIT} clause has already been | |
| 341 | + * provided. | |
| 342 | + * @throws IllegalStateException if this statement is a {@code SELECT DISTINCT} statement. | |
| 343 | + */ | |
| 344 | + public Select perPartitionLimit(BindMarker limit) { | |
| 345 | + return statement.perPartitionLimit(limit); | |
| 346 | + } | |
| 347 | + | |
| 257 | 348 | } |
| 258 | 349 | |
| 259 | 350 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/Assertions.java +4 −0
| @@ -87,4 +87,8 @@ | ||
| 87 | 87 | public static VersionNumberAssert assertThat(VersionNumber actual) { |
| 88 | 88 | return new VersionNumberAssert(actual); |
| 89 | 89 | } |
| 90 | + public static ResultSetAssert assertThat(ResultSet rows) { | |
| 91 | + return new ResultSetAssert(rows); | |
| 92 | + } | |
| 93 | + | |
| 90 | 94 | } |
added driver-core/src/test/java/com/datastax/driver/core/ResultSetAssert.java +144 −0
| @@ -0,0 +1,144 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.google.common.base.Function; | |
| 19 | +import com.google.common.base.Joiner; | |
| 20 | +import com.google.common.collect.Lists; | |
| 21 | +import org.assertj.core.api.AbstractListAssert; | |
| 22 | +import org.assertj.core.data.Index; | |
| 23 | + | |
| 24 | +import java.util.Arrays; | |
| 25 | +import java.util.List; | |
| 26 | + | |
| 27 | +// better use ListAssert than IterableAssert to avoid attempting to consume the resultset more than once | |
| 28 | +public class ResultSetAssert extends AbstractListAssert<ResultSetAssert, List<Row>, Row> { | |
| 29 | + | |
| 30 | + // a helper assert object to simplify assertions on row contents | |
| 31 | + // by considering a row as a mere tuple | |
| 32 | + private final TupleListAssert helper; | |
| 33 | + | |
| 34 | + public ResultSetAssert(ResultSet actual) { | |
| 35 | + super(actual.all(), ResultSetAssert.class); | |
| 36 | + helper = new TupleListAssert(Lists.transform(this.actual, ROW_TO_TUPLE)); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public static Tuple row(Object... cols) { | |
| 40 | + return new Tuple(cols); | |
| 41 | + } | |
| 42 | + | |
| 43 | + public ResultSetAssert contains(Tuple value, Index index) { | |
| 44 | + helper.contains(value, index); | |
| 45 | + return this; | |
| 46 | + } | |
| 47 | + | |
| 48 | + public ResultSetAssert containsSubsequence(Tuple... sequence) { | |
| 49 | + helper.containsSubsequence(sequence); | |
| 50 | + return this; | |
| 51 | + } | |
| 52 | + | |
| 53 | + public ResultSetAssert containsOnly(Tuple... values) { | |
| 54 | + helper.containsOnly(values); | |
| 55 | + return this; | |
| 56 | + } | |
| 57 | + | |
| 58 | + public ResultSetAssert endsWith(Tuple... sequence) { | |
| 59 | + helper.endsWith(sequence); | |
| 60 | + return this; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public ResultSetAssert startsWith(Tuple... sequence) { | |
| 64 | + helper.startsWith(sequence); | |
| 65 | + return this; | |
| 66 | + } | |
| 67 | + | |
| 68 | + public ResultSetAssert doesNotContain(Tuple value, Index index) { | |
| 69 | + helper.doesNotContain(value, index); | |
| 70 | + return this; | |
| 71 | + } | |
| 72 | + | |
| 73 | + public ResultSetAssert doesNotContain(Tuple... values) { | |
| 74 | + helper.doesNotContain(values); | |
| 75 | + return this; | |
| 76 | + } | |
| 77 | + | |
| 78 | + public ResultSetAssert containsExactly(Tuple... values) { | |
| 79 | + helper.containsExactly(values); | |
| 80 | + return this; | |
| 81 | + } | |
| 82 | + | |
| 83 | + public ResultSetAssert containsOnlyOnce(Tuple... values) { | |
| 84 | + helper.containsOnlyOnce(values); | |
| 85 | + return this; | |
| 86 | + } | |
| 87 | + | |
| 88 | + public ResultSetAssert contains(Tuple... values) { | |
| 89 | + helper.contains(values); | |
| 90 | + return this; | |
| 91 | + } | |
| 92 | + | |
| 93 | + public ResultSetAssert containsSequence(Tuple... sequence) { | |
| 94 | + helper.containsSequence(sequence); | |
| 95 | + return this; | |
| 96 | + } | |
| 97 | + | |
| 98 | + public static class Tuple { | |
| 99 | + | |
| 100 | + final Object[] cols; | |
| 101 | + | |
| 102 | + public Tuple(Object... cols) { | |
| 103 | + this.cols = cols; | |
| 104 | + } | |
| 105 | + | |
| 106 | + @Override | |
| 107 | + public boolean equals(Object o) { | |
| 108 | + if (this == o) return true; | |
| 109 | + if (getClass() != o.getClass()) return false; | |
| 110 | + Tuple tuple = (Tuple) o; | |
| 111 | + return Arrays.equals(cols, tuple.cols); | |
| 112 | + } | |
| 113 | + | |
| 114 | + @Override | |
| 115 | + public int hashCode() { | |
| 116 | + return Arrays.hashCode(cols); | |
| 117 | + } | |
| 118 | + | |
| 119 | + @Override | |
| 120 | + public String toString() { | |
| 121 | + return '(' + Joiner.on(',').join(cols) + ')'; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + private static final Function<Row, Tuple> ROW_TO_TUPLE = new Function<Row, Tuple>() { | |
| 126 | + @Override | |
| 127 | + public Tuple apply(Row input) { | |
| 128 | + Object[] cols = new Object[input.getColumnDefinitions().size()]; | |
| 129 | + for (int i = 0; i < input.getColumnDefinitions().size(); i++) { | |
| 130 | + cols[i] = input.getObject(i); | |
| 131 | + } | |
| 132 | + return new Tuple(cols); | |
| 133 | + } | |
| 134 | + }; | |
| 135 | + | |
| 136 | + private static class TupleListAssert extends AbstractListAssert<TupleListAssert, List<Tuple>, Tuple> { | |
| 137 | + | |
| 138 | + private TupleListAssert(List<Tuple> rows) { | |
| 139 | + super(rows, TupleListAssert.class); | |
| 140 | + } | |
| 141 | + | |
| 142 | + } | |
| 143 | + | |
| 144 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +86 −1
| @@ -25,9 +25,10 @@ | ||
| 25 | 25 | import java.util.Map; |
| 26 | 26 | import java.util.Set; |
| 27 | 27 | |
| 28 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 29 | +import static com.datastax.driver.core.ResultSetAssert.row; | |
| 28 | 30 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 29 | 31 | import static com.datastax.driver.core.schemabuilder.SchemaBuilder.createTable; |
| 30 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 31 | 32 | import static org.assertj.core.data.MapEntry.entry; |
| 32 | 33 | import static org.testng.Assert.*; |
| 33 | 34 | |
| @@ -43,11 +44,18 @@ | ||
| 43 | 44 | String.format("CREATE TABLE %s (k text, t text, i int, f float, PRIMARY KEY (k, t))", TABLE2), |
| 44 | 45 | "CREATE TABLE dateTest (t timestamp PRIMARY KEY)", |
| 45 | 46 | "CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>, c set<text>)", |
| 47 | + "CREATE TABLE test_ppl (a int, b int, c int, PRIMARY KEY (a, b))", | |
| 46 | 48 | insertInto(TABLE2).value("k", "cast_t").value("t", "a").value("i", 1).value("f", 1.1).toString(), |
| 47 | 49 | insertInto(TABLE2).value("k", "cast_t").value("t", "b").value("i", 2).value("f", 2.5).toString(), |
| 48 | 50 | insertInto(TABLE2).value("k", "cast_t").value("t", "c").value("i", 3).value("f", 3.7).toString(), |
| 49 | 51 | insertInto(TABLE2).value("k", "cast_t").value("t", "d").value("i", 4).value("f", 5.0).toString() |
| 50 | 52 | ); |
| 53 | + // for per partition limit tests | |
| 54 | + for (int i = 0; i < 5; i++) { | |
| 55 | + for (int j = 0; j < 5; j++) { | |
| 56 | + session().execute(String.format("INSERT INTO test_ppl (a, b, c) VALUES (%d, %d, %d)", i, j, j)); | |
| 57 | + } | |
| 58 | + } | |
| 51 | 59 | } |
| 52 | 60 | |
| 53 | 61 | @Test(groups = "short") |
| @@ -300,4 +308,81 @@ | ||
| 300 | 308 | } |
| 301 | 309 | }).containsOnly("Hello World", "Hello Moon"); |
| 302 | 310 | } |
| 311 | + | |
| 312 | + /** | |
| 313 | + * Validates that {@link QueryBuilder} can construct a query using the 'PER PARTITION LIMIT' operator to restrict | |
| 314 | + * the number of rows returned per partition in a query, i.e.: | |
| 315 | + * <p/> | |
| 316 | + * <code>SELECT * FROM test_ppl PER PARTITION LIMIT 2</code> | |
| 317 | + * <p/> | |
| 318 | + * | |
| 319 | + * @test_category queries:builder | |
| 320 | + * @jira_ticket JAVA-1153 | |
| 321 | + * @since 3.1.0 | |
| 322 | + */ | |
| 323 | + @CassandraVersion(major = 3.6, description = "Support for PER PARTITION LIMIT was added to C* 3.6 (CASSANDRA-7017)") | |
| 324 | + @Test(groups = "short") | |
| 325 | + public void should_support_per_partition_limit() throws Exception { | |
| 326 | + assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2))) | |
| 327 | + .contains( | |
| 328 | + row(0, 0, 0), | |
| 329 | + row(0, 1, 1), | |
| 330 | + row(1, 0, 0), | |
| 331 | + row(1, 1, 1), | |
| 332 | + row(2, 0, 0), | |
| 333 | + row(2, 1, 1), | |
| 334 | + row(3, 0, 0), | |
| 335 | + row(3, 1, 1), | |
| 336 | + row(4, 0, 0), | |
| 337 | + row(4, 1, 1)); | |
| 338 | + // Combined Per Partition and "global" limit | |
| 339 | + assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2).limit(6))).hasSize(6); | |
| 340 | + // odd amount of results | |
| 341 | + assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2).limit(5))) | |
| 342 | + .contains( | |
| 343 | + row(0, 0, 0), | |
| 344 | + row(0, 1, 1), | |
| 345 | + row(1, 0, 0), | |
| 346 | + row(1, 1, 1), | |
| 347 | + row(2, 0, 0)); | |
| 348 | + // IN query | |
| 349 | + assertThat(session().execute(select().all().from("test_ppl").where(in("a", 2, 3)).perPartitionLimit(2))) | |
| 350 | + .contains( | |
| 351 | + row(2, 0, 0), | |
| 352 | + row(2, 1, 1), | |
| 353 | + row(3, 0, 0), | |
| 354 | + row(3, 1, 1)); | |
| 355 | + assertThat(session().execute(select().all().from("test_ppl").where(in("a", 2, 3)) | |
| 356 | + .perPartitionLimit(bindMarker()).limit(3).getQueryString(), 2)) | |
| 357 | + .hasSize(3); | |
| 358 | + assertThat(session().execute(select().all().from("test_ppl").where(in("a", 1, 2, 3)) | |
| 359 | + .perPartitionLimit(bindMarker()).limit(3).getQueryString(), 2)) | |
| 360 | + .hasSize(3); | |
| 361 | + // with restricted partition key | |
| 362 | + assertThat(session().execute(select().all().from("test_ppl").where(eq("a", bindMarker())) | |
| 363 | + .perPartitionLimit(bindMarker()).getQueryString(), 2, 3)) | |
| 364 | + .containsExactly( | |
| 365 | + row(2, 0, 0), | |
| 366 | + row(2, 1, 1), | |
| 367 | + row(2, 2, 2)); | |
| 368 | + // with ordering | |
| 369 | + assertThat(session().execute(select().all().from("test_ppl").where(eq("a", bindMarker())) | |
| 370 | + .orderBy(desc("b")).perPartitionLimit(bindMarker()).getQueryString(), 2, 3)) | |
| 371 | + .containsExactly( | |
| 372 | + row(2, 4, 4), | |
| 373 | + row(2, 3, 3), | |
| 374 | + row(2, 2, 2)); | |
| 375 | + // with filtering | |
| 376 | + assertThat(session().execute(select().all().from("test_ppl").where(eq("a", bindMarker())) | |
| 377 | + .and(gt("b", bindMarker())).perPartitionLimit(bindMarker()).allowFiltering().getQueryString(), 2, 0, 2)) | |
| 378 | + .containsExactly( | |
| 379 | + row(2, 1, 1), | |
| 380 | + row(2, 2, 2)); | |
| 381 | + assertThat(session().execute(select().all().from("test_ppl").where(eq("a", bindMarker())) | |
| 382 | + .and(gt("b", bindMarker())).orderBy(desc("b")).perPartitionLimit(bindMarker()).allowFiltering().getQueryString(), 2, 2, 2)) | |
| 383 | + .containsExactly( | |
| 384 | + row(2, 4, 4), | |
| 385 | + row(2, 3, 3)); | |
| 386 | + } | |
| 387 | + | |
| 303 | 388 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +2 −138
| @@ -20,9 +20,6 @@ | ||
| 20 | 20 | import com.datastax.driver.core.utils.CassandraVersion; |
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | 22 | |
| 23 | -import java.util.Arrays; | |
| 24 | -import java.util.TreeSet; | |
| 25 | - | |
| 26 | 23 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 27 | 24 | import static org.testng.Assert.*; |
| 28 | 25 | |
| @@ -64,147 +61,14 @@ | ||
| 64 | 61 | PreparedStatement ps; |
| 65 | 62 | BoundStatement bs; |
| 66 | 63 | |
| 67 | - session().execute("CREATE KEYSPACE foo WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};"); | |
| 68 | - session().execute("CREATE TABLE foo.foo ( k ascii PRIMARY KEY , i int, s ascii )"); | |
| 69 | - session().execute("USE foo"); | |
| 64 | + session().execute("CREATE TABLE foo ( k ascii PRIMARY KEY , i int, s ascii )"); | |
| 70 | 65 | |
| 71 | 66 | query = "SELECT * FROM foo WHERE k=?;"; |
| 72 | - select = select().all().from("foo").where(eq("k", QueryBuilder.bindMarker())); | |
| 67 | + select = select().all().from("foo").where(eq("k", bindMarker())); | |
| 73 | 68 | ps = session().prepare(select.toString()); |
| 74 | 69 | bs = ps.bind(); |
| 75 | 70 | assertEquals(select.toString(), query); |
| 76 | 71 | session().execute(bs.setString("k", "4 AND c=5")); |
| 77 | - | |
| 78 | - query = "SELECT * FROM foo WHERE k='4'' AND c=''5';"; | |
| 79 | - select = select().all().from("foo").where(eq("k", "4' AND c='5")); | |
| 80 | - assertEquals(select.toString(), query); | |
| 81 | - | |
| 82 | - query = "SELECT * FROM foo WHERE k='4'' OR ''1''=''1';"; | |
| 83 | - select = select().all().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 84 | - assertEquals(select.toString(), query); | |
| 85 | - | |
| 86 | - query = "SELECT * FROM foo WHERE k='4; --test comment;';"; | |
| 87 | - select = select().all().from("foo").where(eq("k", "4; --test comment;")); | |
| 88 | - assertEquals(select.toString(), query); | |
| 89 | - | |
| 90 | - query = "SELECT \"*\" FROM foo;"; | |
| 91 | - select = select("*").from("foo"); | |
| 92 | - assertEquals(select.toString(), query); | |
| 93 | - | |
| 94 | - query = "SELECT a,b FROM foo WHERE a IN ('b','c''); --comment');"; | |
| 95 | - select = select("a", "b").from("foo") | |
| 96 | - .where(in("a", "b", "c'); --comment")); | |
| 97 | - assertEquals(select.toString(), query); | |
| 98 | - | |
| 99 | - // User Injection? | |
| 100 | - query = "SELECT * FROM bar; --(b) FROM foo;"; | |
| 101 | - select = select().fcall("* FROM bar; --", column("b")).from("foo"); | |
| 102 | - assertEquals(select.toString(), query); | |
| 103 | - | |
| 104 | - query = "SELECT writetime(\"a) FROM bar; --\"),ttl(a) FROM foo ALLOW FILTERING;"; | |
| 105 | - select = select().writeTime("a) FROM bar; --").ttl("a").from("foo").allowFiltering(); | |
| 106 | - assertEquals(select.toString(), query); | |
| 107 | - | |
| 108 | - query = "SELECT writetime(a),ttl(\"a) FROM bar; --\") FROM foo ALLOW FILTERING;"; | |
| 109 | - select = select().writeTime("a").ttl("a) FROM bar; --").from("foo").allowFiltering(); | |
| 110 | - assertEquals(select.toString(), query); | |
| 111 | - | |
| 112 | - query = "SELECT * FROM foo WHERE \"k=1 OR k\">42 LIMIT 42;"; | |
| 113 | - select = select().all().from("foo").where(gt("k=1 OR k", 42)).limit(42); | |
| 114 | - assertEquals(select.toString(), query); | |
| 115 | - | |
| 116 | - query = "SELECT * FROM foo WHERE token(\"k)>0 OR token(k\")>token(42);"; | |
| 117 | - select = select().all().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 118 | - assertEquals(select.toString(), query); | |
| 119 | - } | |
| 120 | - | |
| 121 | - @Test(groups = "short") | |
| 122 | - @SuppressWarnings("serial") | |
| 123 | - public void insertInjectionTest() throws Exception { | |
| 124 | - | |
| 125 | - String query; | |
| 126 | - Statement insert; | |
| 127 | - | |
| 128 | - query = "INSERT INTO foo (a) VALUES ('123); --comment');"; | |
| 129 | - insert = insertInto("foo") | |
| 130 | - .value("a", "123); --comment"); | |
| 131 | - assertEquals(insert.toString(), query); | |
| 132 | - | |
| 133 | - query = "INSERT INTO foo (\"a,b\") VALUES (123);"; | |
| 134 | - insert = insertInto("foo") | |
| 135 | - .value("a,b", 123); | |
| 136 | - assertEquals(insert.toString(), query); | |
| 137 | - | |
| 138 | - query = "INSERT INTO foo (a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;"; | |
| 139 | - insert = insertInto("foo").values(new String[]{"a", "b"}, new Object[]{new TreeSet<String>() {{ | |
| 140 | - add("2'} space"); | |
| 141 | - add("3"); | |
| 142 | - add("4"); | |
| 143 | - }}, 3.4}).using(ttl(24)).and(timestamp(42)); | |
| 144 | - assertEquals(insert.toString(), query); | |
| 145 | - } | |
| 146 | - | |
| 147 | - @Test(groups = "short") | |
| 148 | - public void updateInjectionTest() throws Exception { | |
| 149 | - | |
| 150 | - String query; | |
| 151 | - Statement update; | |
| 152 | - | |
| 153 | - query = "UPDATE foo.bar USING TIMESTAMP 42 SET a=12 WHERE k='2 OR 1=1';"; | |
| 154 | - update = update("foo", "bar").using(timestamp(42)).with(set("a", 12)).where(eq("k", "2 OR 1=1")); | |
| 155 | - assertEquals(update.toString(), query); | |
| 156 | - | |
| 157 | - query = "UPDATE foo SET b='null WHERE k=1; --comment' WHERE k=2;"; | |
| 158 | - update = update("foo").where().and(eq("k", 2)).with(set("b", "null WHERE k=1; --comment")); | |
| 159 | - assertEquals(update.toString(), query); | |
| 160 | - | |
| 161 | - query = "UPDATE foo USING TIMESTAMP 42 SET \"b WHERE k=1; --comment\"=[3,2,1]+\"b WHERE k=1; --comment\" WHERE k=2;"; | |
| 162 | - update = update("foo").where().and(eq("k", 2)).with(prependAll("b WHERE k=1; --comment", Arrays.asList(3, 2, 1))).using(timestamp(42)); | |
| 163 | - assertEquals(update.toString(), query); | |
| 164 | - } | |
| 165 | - | |
| 166 | - @Test(groups = "short") | |
| 167 | - public void deleteInjectionTests() throws Exception { | |
| 168 | - | |
| 169 | - String query; | |
| 170 | - Statement delete; | |
| 171 | - | |
| 172 | - query = "DELETE FROM \"foo WHERE k=4\";"; | |
| 173 | - delete = delete().from("foo WHERE k=4"); | |
| 174 | - assertEquals(delete.toString(), query); | |
| 175 | - | |
| 176 | - query = "DELETE FROM foo WHERE k='4 AND c=5';"; | |
| 177 | - delete = delete().from("foo").where(eq("k", "4 AND c=5")); | |
| 178 | - assertEquals(delete.toString(), query); | |
| 179 | - | |
| 180 | - query = "DELETE FROM foo WHERE k='4'' AND c=''5';"; | |
| 181 | - delete = delete().from("foo").where(eq("k", "4' AND c='5")); | |
| 182 | - assertEquals(delete.toString(), query); | |
| 183 | - | |
| 184 | - query = "DELETE FROM foo WHERE k='4'' OR ''1''=''1';"; | |
| 185 | - delete = delete().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 186 | - assertEquals(delete.toString(), query); | |
| 187 | - | |
| 188 | - query = "DELETE FROM foo WHERE k='4; --test comment;';"; | |
| 189 | - delete = delete().from("foo").where(eq("k", "4; --test comment;")); | |
| 190 | - assertEquals(delete.toString(), query); | |
| 191 | - | |
| 192 | - query = "DELETE \"*\" FROM foo;"; | |
| 193 | - delete = delete("*").from("foo"); | |
| 194 | - assertEquals(delete.toString(), query); | |
| 195 | - | |
| 196 | - query = "DELETE a,b FROM foo WHERE a IN ('b','c''); --comment');"; | |
| 197 | - delete = delete("a", "b").from("foo") | |
| 198 | - .where(in("a", "b", "c'); --comment")); | |
| 199 | - assertEquals(delete.toString(), query); | |
| 200 | - | |
| 201 | - query = "DELETE FROM foo WHERE \"k=1 OR k\">42;"; | |
| 202 | - delete = delete().from("foo").where(gt("k=1 OR k", 42)); | |
| 203 | - assertEquals(delete.toString(), query); | |
| 204 | - | |
| 205 | - query = "DELETE FROM foo WHERE token(\"k)>0 OR token(k\")>token(42);"; | |
| 206 | - delete = delete().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 207 | - assertEquals(delete.toString(), query); | |
| 208 | 72 | } |
| 209 | 73 | |
| 210 | 74 | @Test(groups = "short") |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +46 −0
| @@ -999,4 +999,50 @@ | ||
| 999 | 999 | } |
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | + @Test(groups = "unit") | |
| 1003 | + public void should_handle_per_partition_limit_clause() { | |
| 1004 | + assertThat( | |
| 1005 | + select().all().from("foo").perPartitionLimit(2).toString()) | |
| 1006 | + .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT 2;"); | |
| 1007 | + assertThat( | |
| 1008 | + select().all().from("foo").perPartitionLimit(bindMarker()).toString()) | |
| 1009 | + .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT ?;"); | |
| 1010 | + assertThat( | |
| 1011 | + select().all().from("foo").perPartitionLimit(bindMarker("limit")).toString()) | |
| 1012 | + .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT :limit;"); | |
| 1013 | + assertThat( | |
| 1014 | + select().all().from("foo").perPartitionLimit(2).limit(bindMarker()).toString()) | |
| 1015 | + .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT 2 LIMIT ?;"); | |
| 1016 | + assertThat( | |
| 1017 | + select().all().from("foo").where(in("a", 2, 4)).perPartitionLimit(2).limit(3).toString()) | |
| 1018 | + .isEqualTo("SELECT * FROM foo WHERE a IN (2,4) PER PARTITION LIMIT 2 LIMIT 3;"); | |
| 1019 | + assertThat( | |
| 1020 | + select().all().from("foo").where(eq("a", bindMarker())).perPartitionLimit(bindMarker()).limit(3).toString()) | |
| 1021 | + .isEqualTo("SELECT * FROM foo WHERE a=? PER PARTITION LIMIT ? LIMIT 3;"); | |
| 1022 | + assertThat( | |
| 1023 | + select().all().from("foo").where(eq("a", bindMarker())).orderBy(desc("b")).perPartitionLimit(2).limit(3).toString()) | |
| 1024 | + .isEqualTo("SELECT * FROM foo WHERE a=? ORDER BY b DESC PER PARTITION LIMIT 2 LIMIT 3;"); | |
| 1025 | + assertThat( | |
| 1026 | + select().all().from("foo").where(eq("a", bindMarker())).and(gt("b", bindMarker())) | |
| 1027 | + .orderBy(desc("b")).perPartitionLimit(bindMarker()).limit(3).allowFiltering().toString()) | |
| 1028 | + .isEqualTo("SELECT * FROM foo WHERE a=? AND b>? ORDER BY b DESC PER PARTITION LIMIT ? LIMIT 3 ALLOW FILTERING;"); | |
| 1029 | + try { | |
| 1030 | + select().distinct().all().from("foo").perPartitionLimit(3); | |
| 1031 | + fail("Should not allow DISTINCT + PER PARTITION LIMIT"); | |
| 1032 | + } catch (Exception e) { | |
| 1033 | + assertThat(e).hasMessage("PER PARTITION LIMIT is not allowed with SELECT DISTINCT queries"); | |
| 1034 | + } | |
| 1035 | + try { | |
| 1036 | + select().all().from("foo").perPartitionLimit(-1); | |
| 1037 | + fail("Should not allow negative limit"); | |
| 1038 | + } catch (IllegalArgumentException e) { | |
| 1039 | + assertThat(e).hasMessage("Invalid PER PARTITION LIMIT value, must be strictly positive"); | |
| 1040 | + } | |
| 1041 | + try { | |
| 1042 | + select().all().from("foo").perPartitionLimit(1).perPartitionLimit(bindMarker()); | |
| 1043 | + fail("Should not allow to set limit twice"); | |
| 1044 | + } catch (IllegalStateException e) { | |
| 1045 | + assertThat(e).hasMessage("A PER PARTITION LIMIT value has already been provided"); | |
| 1046 | + } | |
| 1047 | + } | |
| 1002 | 1048 | } |
modified pom.xml +1 −1
| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | |
| 48 | 48 | <properties> |
| 49 | 49 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 50 | - <cassandra.version>3.2.1</cassandra.version> | |
| 50 | + <cassandra.version>3.6</cassandra.version> | |
| 51 | 51 | <java.version>1.6</java.version> |
| 52 | 52 | <log4j.version>1.2.17</log4j.version> |
| 53 | 53 | <slf4j-log4j12.version>1.7.6</slf4j-log4j12.version> |