Elffuss BraidHub

kikocis/java-driver public

Also check number of values on BuiltStatements (JAVA-515).
Changeset437ad5b67816ce0a3e3505637a158ddeaa08f91b3a813d690aabe2b5b7b8c52b
Authorolim7t <omichallat+github@gmail.com> · 2014-11-12 18:58 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-11-12 19:19 +01:00
Parents22c9a3c32b30

Browse the files at this changeset

3 files changed

modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +4 −0
@@ -82,6 +82,10 @@
8282 } else {
8383 List<ByteBuffer> l = new ArrayList<ByteBuffer>();
8484 sb = buildQueryString(l);
85+
86+ if (l.size() > 65535)
87+ throw new IllegalArgumentException("Too many values for built statement, the maximum allowed is 65535");
88+
8589 if (!l.isEmpty())
8690 values = l.toArray(new ByteBuffer[l.size()]);
8791 }
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +2 −0
@@ -74,6 +74,8 @@
7474
7575 if (values == null)
7676 throw new IllegalArgumentException("Missing values for IN clause");
77+ if (values.size() > 65535)
78+ throw new IllegalArgumentException("Too many values for IN clause, the maximum allowed is 65535");
7779 }
7880
7981 @Override
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +21 −3
@@ -18,9 +18,7 @@
1818 import java.math.BigDecimal;
1919 import java.math.BigInteger;
2020 import java.net.InetAddress;
21-import java.util.Arrays;
22-import java.util.TreeMap;
23-import java.util.TreeSet;
21+import java.util.*;
2422
2523 import org.testng.annotations.Test;
2624 import static org.testng.Assert.*;
@@ -680,4 +678,24 @@
680678 select = select().all().from("foo").where(eq("k", 4)).and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2)));
681679 assertEquals(select.toString(), query);
682680 }
681+
682+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)
683+ public void should_fail_if_in_clause_has_too_many_values() {
684+ List<Object> values = Collections.<Object>nCopies(65536, "a");
685+ select().all().from("foo").where(in("bar", values.toArray()));
686+ }
687+
688+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)
689+ public void should_fail_if_built_statement_has_too_many_values() {
690+ List<Object> values = Collections.<Object>nCopies(65535, "a");
691+
692+ // If the excessive count results from successive DSL calls, we don't check it on the fly so this statement works:
693+ BuiltStatement statement = select().all().from("foo")
694+ .where(eq("bar", "a"))
695+ .and(in("baz", values.toArray()));
696+
697+ // But we still want to check it client-side, to fail fast instead of sending a bad query to Cassandra.
698+ // getValues() is called on any RegularStatement before we send it (see SessionManager.makeRequestMessage).
699+ statement.getValues();
700+ }
683701 }