Elffuss BraidHub

kikocis/java-driver public

JAVA-1285: QueryBuilder routing key auto-discovery should handle case-sensitive column names. (#749)

Changeset9fa4ddba0db6d934a4f006f01cb201ce3a528f6d5c1a2184168fb7718f0a7ad9
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2016-09-16 00:29 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-09-15 15:29 -07:00
Parentscfd5e99fc305

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -8,6 +8,7 @@
88 - [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence
99 - [improvement] JAVA-1262: Use ParseUtils for quoting & unquoting.
1010 - [improvement] JAVA-1275: Use Netty's default thread factory
11+- [bug] JAVA-1285: QueryBuilder routing key auto-discovery should handle case-sensitive column names.
1112
1213
1314 ### 3.0.3
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +1 −2
@@ -21,7 +21,6 @@
2121
2222 import java.nio.ByteBuffer;
2323 import java.util.ArrayList;
24-import java.util.Arrays;
2524 import java.util.List;
2625 import java.util.Map;
2726 import java.util.regex.Pattern;
@@ -228,7 +227,7 @@
228227 return;
229228
230229 for (int i = 0; i < partitionKey.size(); i++) {
231- if (name.equals(partitionKey.get(i).getName())) {
230+ if (Utils.handleId(name).equals(partitionKey.get(i).getName())) {
232231 routingKeyValues.set(i, value);
233232 return;
234233 }
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +32 −0
@@ -32,8 +32,40 @@
3232 // Static utilities private to the query builder
3333 abstract class Utils {
3434
35+ private static final Pattern alphanumeric = Pattern.compile("\\w+"); // this includes _
3536 private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?");
3637
38+ /**
39+ * Deal with case sensitivity for a given element id (keyspace, table, column, etc.)
40+ *
41+ * This method is used to convert identifiers provided by the client (through methods such as getKeyspace(String)),
42+ * to the format used internally by the driver.
43+ *
44+ * We expect client-facing APIs to behave like cqlsh, that is:
45+ * - identifiers that are mixed-case or contain special characters should be quoted.
46+ * - unquoted identifiers will be lowercased: getKeyspace("Foo") will look for a keyspace named "foo"
47+ *
48+ * Copied from {@link Metadata#handleId(String)} to avoid making it public.
49+ */
50+ static String handleId(String id) {
51+ // Shouldn't really happen for this method, but no reason to fail here
52+ if (id == null)
53+ return null;
54+
55+ if (alphanumeric.matcher(id).matches())
56+ return id.toLowerCase();
57+
58+ // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes
59+ if (!id.isEmpty() && id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"')
60+ return id.substring(1, id.length() - 1).replaceAll("\"\"", "\"");
61+
62+ // Otherwise, just return the id.
63+ // Note that this is a bit at odds with the rules explained above, because the client can pass an
64+ // identifier that contains special characters, without the need to quote it.
65+ // Still it's better to be lenient here rather than throwing an exception.
66+ return id;
67+ }
68+
3769 static StringBuilder joinAndAppend(StringBuilder sb, CodecRegistry codecRegistry, String separator, List<? extends Appendeable> values, List<Object> variables) {
3870 for (int i = 0; i < values.size(); i++) {
3971 if (i > 0)
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +64 −1
@@ -23,17 +23,22 @@
2323 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
2424 import static org.testng.Assert.assertEquals;
2525 import static org.testng.Assert.assertNotNull;
26+import static org.testng.Assert.assertNull;
2627
2728 @CCMConfig(clusterProvider = "createClusterBuilderNoDebouncing")
2829 public class QueryBuilderRoutingKeyTest extends CCMTestsSupport {
2930
3031 private static final String TABLE_TEXT = "test_text";
3132 private static final String TABLE_INT = "test_int";
33+ private static final String TABLE_CASE = "test_case";
34+ private static final String TABLE_CASE_QUOTED = "test_case_quoted";
3235
3336 @Override
3437 public void onTestContextInitialized() {
3538 execute(String.format("CREATE TABLE %s (k text PRIMARY KEY, a int, b int)", TABLE_TEXT),
36- String.format("CREATE TABLE %s (k int PRIMARY KEY, a int, b int)", TABLE_INT));
39+ String.format("CREATE TABLE %s (k int PRIMARY KEY, a int, b int)", TABLE_INT),
40+ String.format("CREATE TABLE %s (theKey int PRIMARY KEY, a int, b int)", TABLE_CASE),
41+ String.format("CREATE TABLE %s (\"theKey\" int PRIMARY KEY, a int, b int, \"tHEkEY\" int)", TABLE_CASE_QUOTED));
3742 }
3843
3944 @Test(groups = "short")
@@ -58,6 +63,64 @@
5863 assertEquals(row.getInt("b"), 2);
5964 }
6065
66+ @Test(groups = "short")
67+ public void routingKeyColumnCaseSensitivityTest() throws Exception {
68+
69+ BuiltStatement query;
70+ TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_CASE);
71+ assertNotNull(table);
72+ ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
73+ CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE;
74+
75+ query = insertInto(table).values(new String[]{"theKey", "a", "b"}, new Object[]{42, 1, 2});
76+ ByteBuffer bb = ByteBuffer.allocate(4);
77+ bb.putInt(0, 42);
78+ assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb);
79+ session().execute(query);
80+
81+ query = select().from(table).where(eq("theKey", 42));
82+ assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb);
83+ Row row = session().execute(query).one();
84+ assertEquals(row.getInt("theKey"), 42);
85+ assertEquals(row.getInt("a"), 1);
86+ assertEquals(row.getInt("b"), 2);
87+
88+ query = insertInto(table).values(new String[]{"ThEkEy", "a", "b"}, new Object[]{42, 1, 2});
89+ bb = ByteBuffer.allocate(4);
90+ bb.putInt(0, 42);
91+ assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb);
92+ session().execute(query);
93+
94+ query = select().from(table).where(eq("ThEkEy", 42));
95+ assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb);
96+ row = session().execute(query).one();
97+ assertEquals(row.getInt("theKey"), 42);
98+ assertEquals(row.getInt("a"), 1);
99+ assertEquals(row.getInt("b"), 2);
100+ }
101+
102+ @Test(groups = "short")
103+ public void routingKeyColumnCaseSensitivityForQuotedIdentifiersTest() throws Exception {
104+
105+ BuiltStatement query;
106+ TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_CASE_QUOTED);
107+ assertNotNull(table);
108+ ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
109+ CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE;
110+
111+ query = insertInto(table).values(new String[]{"\"theKey\"", "a", "b", "\"tHEkEY\""}, new Object[]{42, 1, 2, 3});
112+ ByteBuffer bb = ByteBuffer.allocate(4);
113+ bb.putInt(0, 42);
114+ assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb);
115+
116+ query = insertInto(table).values(new String[]{"theKey", "a", "b", "\"tHEkEY\""}, new Object[]{42, 1, 2, 3});
117+ assertNull(query.getRoutingKey(protocolVersion, codecRegistry));
118+
119+ query = insertInto(table).values(new String[]{"theKey", "a", "b", "theKey"}, new Object[]{42, 1, 2, 3});
120+ assertNull(query.getRoutingKey(protocolVersion, codecRegistry));
121+
122+ }
123+
61124 @Test(groups = "short")
62125 public void intRoutingKeyTest() throws Exception {
63126