Elffuss BraidHub

kikocis/java-driver public

JAVA-866: Support tuple notation in QueryBuilder.eq/in (#766)

Re-opened because the multi-column IN restrictions were incorrectly implemented.

This commit contains the following contributions by Andrew Tolbert (@tolbertam):
- Add more negative unit tests for clause
Changeseta6c13e17062ec8af8bfe61b0981059d745a8eb80c058ba442b3587669fa9172a
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2016-12-28 18:21 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-12-28 18:21 +01:00
Parentsb42c6090f073

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -5,6 +5,7 @@
55 - [bug] JAVA-1330: Add un/register for SchemaChangeListener in DelegatingCluster
66 - [bug] JAVA-1351: Include Custom Payload in Request.copy.
77 - [bug] JAVA-1346: Reset heartbeat only on client reads (not writes).
8+- [improvement] JAVA-866: Support tuple notation in QueryBuilder.eq/in.
89
910
1011 ### 3.0.5
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +28 −13
@@ -227,9 +227,23 @@
227227
228228 static class CompoundInClause extends Clause {
229229 private final List<String> names;
230- private final List<List<?>> valueLists;
230+ private final List<?> valueLists;
231231
232- public CompoundInClause(List<String> names, List<List<?>> valueLists) {
232+ public CompoundInClause(List<String> names, List<?> valueLists) {
233+ if (valueLists == null)
234+ throw new IllegalArgumentException("Missing values for IN clause");
235+ if (valueLists.size() > 65535)
236+ throw new IllegalArgumentException("Too many values for IN clause, the maximum allowed is 65535");
237+ for (Object value : valueLists) {
238+ if (value instanceof List) {
239+ List<?> tuple = (List<?>) value;
240+ if (tuple.size() != names.size()) {
241+ throw new IllegalArgumentException(String.format("The number of names (%d) and values (%d) don't match", names.size(), tuple.size()));
242+ }
243+ } else if (!(value instanceof BindMarker)) {
244+ throw new IllegalArgumentException(String.format("Wrong element type for values list, expected List or BindMarker, got %s", value.getClass().getName()));
245+ }
246+ }
233247 this.names = names;
234248 this.valueLists = valueLists;
235249 }
@@ -250,11 +264,7 @@
250264
251265 @Override
252266 boolean containsBindMarker() {
253- for (List<?> values : valueLists)
254- for (Object value : values)
255- if (Utils.containsBindMarker(value))
256- return true;
257- return false;
267+ return Utils.containsBindMarker(valueLists);
258268 }
259269
260270 @Override
@@ -270,14 +280,19 @@
270280 if (i > 0)
271281 sb.append(",");
272282
273- List<?> values = valueLists.get(i);
283+ Object elt = valueLists.get(i);
274284
275- // Special case when there is only one bind marker: "IN ?" instead of "IN (?)"
276- if (values.size() == 1 && values.get(0) instanceof BindMarker) {
277- sb.append(values.get(0));
285+ if (elt instanceof BindMarker) {
286+ sb.append(elt);
278287 } else {
279- sb.append("(");
280- Utils.joinAndAppendValues(sb, codecRegistry, ",", values, variables).append(')');
288+ List<?> tuple = (List<?>) elt;
289+ if (tuple.size() == 1 && tuple.get(0) instanceof BindMarker) {
290+ // Special case when there is only one bind marker: "IN ?" instead of "IN (?)"
291+ sb.append(tuple.get(0));
292+ } else {
293+ sb.append("(");
294+ Utils.joinAndAppendValues(sb, codecRegistry, ",", (List<?>) tuple, variables).append(')');
295+ }
281296 }
282297 }
283298 sb.append(")");
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +11 −8
@@ -333,22 +333,25 @@
333333 }
334334
335335 /**
336- * Creates an "in" where clause for a group of clustering columns.
336+ * Creates an "in" where clause for a group of clustering columns (a.k.a. "multi-column IN restriction").
337337 * <p/>
338- * For instance, {@code in(Arrays.asList("a", "b"), Arrays.asList(Arrays.asList(1, 2), Arrays.asList("foo", "bar")))}
339- * will generate the CQL WHERE clause {@code (a, b) IN ((1, 2), ('foo', 'bar')) }.
338+ * For instance, {@code in(Arrays.asList("a", "b"), Arrays.asList(Arrays.asList(1, "foo"), Arrays.asList(2, "bar")))}
339+ * will generate the CQL WHERE clause {@code (a, b) IN ((1, 'foo'), (2, 'bar'))}.
340+ * <p/>
341+ * Each element in {@code values} must be either a {@link List list} containing exactly as many values
342+ * as there are columns to match in {@code names},
343+ * or a {@link #bindMarker() bind marker} – in which case, that marker is to be considered as
344+ * a placeholder for one whole tuple of values to match.
340345 * <p/>
341346 * Please note that this variant is only supported starting with Cassandra 2.0.9.
342347 *
343348 * @param names the column names
344349 * @param values the values
345350 * @return the corresponding where clause.
346- * @throws IllegalArgumentException if {@code names.size() != values.size()}.
351+ * @throws IllegalArgumentException if the size of any tuple in {@code values} is not equal to {@code names.size()},
352+ * or if {@code values} contains elements that are neither {@link List lists} nor {@link #bindMarker() bind markers}.
347353 */
348- public static Clause in(List<String> names, List<List<?>> values) {
349- if (names.size() != values.size())
350- throw new IllegalArgumentException(String.format("The number of names (%d) and values (%d) don't match", names.size(), values.size()));
351-
354+ public static Clause in(List<String> names, List<?> values) {
352355 return new Clause.CompoundInClause(names, values);
353356 }
354357
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +48 −8
@@ -783,21 +783,61 @@
783783 select = select().all().from("foo").where(eq("k", 4)).and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2)));
784784 assertEquals(select.toString(), query);
785785
786- query = "SELECT * FROM foo WHERE k=4 AND (c1,c2) IN ((1,2),('foo','bar'));";
786+ query = "SELECT * FROM foo WHERE k=4 AND (c1,c2) IN ((1,'foo'),(2,'bar'),(3,'qix'));";
787787 List<String> names = ImmutableList.of("c1", "c2");
788- List<List<?>> values = ImmutableList.<List<?>>of(
789- ImmutableList.of(1, 2),
790- ImmutableList.of("foo", "bar"));
788+ List<?> values = ImmutableList.<List<?>>of(
789+ ImmutableList.of(1, "foo"),
790+ ImmutableList.of(2, "bar"),
791+ ImmutableList.of(3, "qix"));
791792 select = select().all().from("foo").where(eq("k", 4)).and(in(names, values));
792793 assertEquals(select.toString(), query);
793794
794- query = "SELECT * FROM foo WHERE k=4 AND (c1,c2) IN ((1,2),?);";
795+ query = "SELECT * FROM foo WHERE k=4 AND (c1,c2) IN ((1,'foo'),(2,?),?);";
795796 names = ImmutableList.of("c1", "c2");
796- values = ImmutableList.<List<?>>of(
797- ImmutableList.of(1, 2),
798- ImmutableList.of(bindMarker()));
797+ values = ImmutableList.of(
798+ ImmutableList.of(1, "foo"),
799+ ImmutableList.of(2, bindMarker()),
800+ bindMarker());
799801 select = select().all().from("foo").where(eq("k", 4)).and(in(names, values));
800802 assertEquals(select.toString(), query);
803+
804+ // special case, single element list with bind marker should be (?) instead of ((?))
805+ query = "SELECT * FROM foo WHERE k=4 AND (c1) IN (?);";
806+ names = ImmutableList.of("c1");
807+ values = ImmutableList.of(ImmutableList.of(bindMarker()));
808+ select = select().all().from("foo").where(eq("k", 4)).and(in(names, values));
809+ assertEquals(select.toString(), query);
810+ }
811+
812+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Too many values for IN clause, the maximum allowed is 65535")
813+ public void should_fail_if_compound_in_clause_has_too_many_values() {
814+ List<Object> values = Collections.<Object>nCopies(65536, "a");
815+ select().all().from("foo").where(eq("k", 4)).and(in(ImmutableList.of("name"), values));
816+ }
817+
818+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Missing values for IN clause")
819+ public void should_fail_if_compound_in_clause_given_null_values() {
820+ select().all().from("foo").where(eq("k", 4)).and(in(ImmutableList.of("name"), null));
821+ }
822+
823+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The number of names \\(4\\) and values \\(3\\) don't match")
824+ public void should_fail_if_compound_in_clause_has_mismatch_of_names_and_values() {
825+ select().all().from("foo").where(eq("k", 4)).and(in(ImmutableList.of("a", "b", "c", "d"),
826+ ImmutableList.of(
827+ ImmutableList.of(1, 2, 3, 4), // Adequately sized (4)
828+ ImmutableList.of(1, 2, 3) // Inadequately sized (3)
829+ )));
830+ }
831+
832+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Wrong element type for values list, expected List or BindMarker, got java.lang.Integer")
833+ public void shoud_fail_if_compound_in_clause_has_value_pair_that_is_not_list_or_bind_marker() {
834+ select().all().from("foo").where(eq("k", 4)).and(in(ImmutableList.of("a", "b", "c", "d"),
835+ ImmutableList.of(1))); // Invalid value 1, must be list or bind marker.
836+ }
837+
838+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Missing values for IN clause")
839+ public void should_fail_if_in_clause_has_null_values() {
840+ select().all().from("foo").where(in("bar", (List<?>) null));
801841 }
802842
803843 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)