You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by bl...@apache.org on 2017/01/27 14:51:38 UTC
[1/7] cassandra git commit: Fix handling of nulls and unsets in IN
conditions
Repository: cassandra
Updated Branches:
refs/heads/trunk 80728df56 -> e71a49e81
Fix handling of nulls and unsets in IN conditions
patch by Benjamin Lerer; reviewed by Alex Petrov for CASSANDRA-12981
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/70e33d96
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/70e33d96
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/70e33d96
Branch: refs/heads/trunk
Commit: 70e33d96e1f1236788afb50c1f02fbc64d760281
Parents: e5a5533
Author: Benjamin Lerer <b....@gmail.com>
Authored: Fri Jan 27 15:17:53 2017 +0100
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Fri Jan 27 15:17:53 2017 +0100
----------------------------------------------------------------------
CHANGES.txt | 1 +
.../apache/cassandra/cql3/ColumnCondition.java | 62 ++++++-
.../operations/InsertUpdateIfConditionTest.java | 161 ++++++++++++++++++-
3 files changed, 214 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/70e33d96/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 4f769a1..c5e5335 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
2.2.9
+ * Fix handling of nulls and unsets in IN conditions (CASSANDRA-12981)
* Remove support for non-JavaScript UDFs (CASSANDRA-12883)
* Fix DynamicEndpointSnitch noop in multi-datacenter situations (CASSANDRA-13074)
* cqlsh copy-from: encode column names to avoid primary key parsing errors (CASSANDRA-12909)
http://git-wip-us.apache.org/repos/asf/cassandra/blob/70e33d96/src/java/org/apache/cassandra/cql3/ColumnCondition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ColumnCondition.java b/src/java/org/apache/cassandra/cql3/ColumnCondition.java
index c7b5ddb..3412e71 100644
--- a/src/java/org/apache/cassandra/cql3/ColumnCondition.java
+++ b/src/java/org/apache/cassandra/cql3/ColumnCondition.java
@@ -25,6 +25,7 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.Term.Terminal;
import org.apache.cassandra.cql3.functions.Function;
import org.apache.cassandra.db.Cell;
import org.apache.cassandra.db.ColumnFamily;
@@ -267,12 +268,26 @@ public class ColumnCondition
assert !(column.type instanceof CollectionType) && condition.collectionElement == null;
assert condition.operator == Operator.IN;
if (condition.inValues == null)
- this.inValues = ((Lists.Value) condition.value.bind(options)).getElements();
+ {
+ Terminal terminal = condition.value.bind(options);
+
+ if (terminal == null)
+ throw new InvalidRequestException("Invalid null list in IN condition");
+
+ if (terminal == Constants.UNSET_VALUE)
+ throw new InvalidRequestException("Invalid 'unset' value in condition");
+
+ this.inValues = ((Lists.Value) terminal).getElements();
+ }
else
{
this.inValues = new ArrayList<>(condition.inValues.size());
for (Term value : condition.inValues)
- this.inValues.add(value.bindAndGet(options));
+ {
+ ByteBuffer buffer = value.bindAndGet(options);
+ if (buffer != ByteBufferUtil.UNSET_BYTE_BUFFER)
+ this.inValues.add(value.bindAndGet(options));
+ }
}
}
@@ -378,12 +393,22 @@ public class ColumnCondition
this.collectionElement = condition.collectionElement.bindAndGet(options);
if (condition.inValues == null)
- this.inValues = ((Lists.Value) condition.value.bind(options)).getElements();
+ {
+ Terminal terminal = condition.value.bind(options);
+ if (terminal == Constants.UNSET_VALUE)
+ throw new InvalidRequestException("Invalid 'unset' value in condition");
+ this.inValues = ((Lists.Value) terminal).getElements();
+ }
else
{
this.inValues = new ArrayList<>(condition.inValues.size());
for (Term value : condition.inValues)
- this.inValues.add(value.bindAndGet(options));
+ {
+ ByteBuffer buffer = value.bindAndGet(options);
+ // We want to ignore unset values
+ if (buffer != ByteBufferUtil.UNSET_BYTE_BUFFER)
+ this.inValues.add(buffer);
+ }
}
}
@@ -642,11 +667,22 @@ public class ColumnCondition
// We have a list of serialized collections that need to be deserialized for later comparisons
CollectionType collectionType = (CollectionType) column.type;
Lists.Marker inValuesMarker = (Lists.Marker) condition.value;
+ Terminal terminal = inValuesMarker.bind(options);
+
+ if (terminal == null)
+ throw new InvalidRequestException("Invalid null list in IN condition");
+
+ if (terminal == Constants.UNSET_VALUE)
+ throw new InvalidRequestException("Invalid 'unset' value in condition");
+
if (column.type instanceof ListType)
{
ListType deserializer = ListType.getInstance(collectionType.valueComparator(), false);
- for (ByteBuffer buffer : ((Lists.Value)inValuesMarker.bind(options)).elements)
+ for (ByteBuffer buffer : ((Lists.Value) terminal).elements)
{
+ if (buffer == ByteBufferUtil.UNSET_BYTE_BUFFER)
+ continue;
+
if (buffer == null)
this.inValues.add(null);
else
@@ -656,8 +692,11 @@ public class ColumnCondition
else if (column.type instanceof MapType)
{
MapType deserializer = MapType.getInstance(collectionType.nameComparator(), collectionType.valueComparator(), false);
- for (ByteBuffer buffer : ((Lists.Value)inValuesMarker.bind(options)).elements)
+ for (ByteBuffer buffer : ((Lists.Value) terminal).elements)
{
+ if (buffer == ByteBufferUtil.UNSET_BYTE_BUFFER)
+ continue;
+
if (buffer == null)
this.inValues.add(null);
else
@@ -667,8 +706,11 @@ public class ColumnCondition
else if (column.type instanceof SetType)
{
SetType deserializer = SetType.getInstance(collectionType.valueComparator(), false);
- for (ByteBuffer buffer : ((Lists.Value)inValuesMarker.bind(options)).elements)
+ for (ByteBuffer buffer : ((Lists.Value) terminal).elements)
{
+ if (buffer == ByteBufferUtil.UNSET_BYTE_BUFFER)
+ continue;
+
if (buffer == null)
this.inValues.add(null);
else
@@ -679,7 +721,11 @@ public class ColumnCondition
else
{
for (Term value : condition.inValues)
- this.inValues.add(value.bind(options));
+ {
+ Terminal terminal = value.bind(options);
+ if (terminal != Constants.UNSET_VALUE)
+ this.inValues.add(terminal);
+ }
}
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/70e33d96/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
index 4a209e6..6396727 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
@@ -18,6 +18,9 @@
package org.apache.cassandra.cql3.validation.operations;
+import java.nio.ByteBuffer;
+import java.util.List;
+
import org.junit.Test;
import org.apache.cassandra.cql3.CQLTester;
@@ -135,6 +138,7 @@ public class InsertUpdateIfConditionTest extends CQLTester
assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?, ?)", 0, 1, 2), row(true));
assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN ?", list(142, 276)), row(false, 2));
assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN ()"), row(false, 2));
+ assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?)", unset(), 1), row(false, 2));
assertInvalidMessage("Invalid 'unset' value in condition",
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 < ?", unset());
@@ -146,8 +150,6 @@ public class InsertUpdateIfConditionTest extends CQLTester
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 >= ?", unset());
assertInvalidMessage("Invalid 'unset' value in condition",
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 != ?", unset());
- assertInvalidMessage("Invalid 'unset' value in condition",
- "UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?)", unset(), 1);
}
/**
@@ -1303,4 +1305,159 @@ public class InsertUpdateIfConditionTest extends CQLTester
assertRows(execute("SELECT * FROM %s WHERE a = 7"),
row(7, 7, null, null, 7));
}
+
+ @Test
+ public void testInMarkerWithUDTs() throws Throwable
+ {
+ String typename = createType("CREATE TYPE %s (a int, b text)");
+ String myType = KEYSPACE + '.' + typename;
+
+ createTable("CREATE TABLE %s (k int PRIMARY KEY, v frozen<" + myType + "> )");
+
+ Object v = userType(0, "abc");
+ execute("INSERT INTO %s (k, v) VALUES (?, ?)", 0, v);
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(1, "abc"), userType(0, "ac")),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(1, "abc"), null),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(1, "abc"), unset()),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", null, null),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", unset(), unset()),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", list(userType(1, "abc"), userType(0, "ac"))),
+ row(false, v));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(0, "abc"), userType(0, "ac")),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 1, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(0, "bc"), null),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 1, b: 'ac'} WHERE k = 0 IF v IN (?, ?, ?)", userType(0, "bc"), unset(), userType(1, "bc")),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'abc'} WHERE k = 0 IF v IN ?", list(userType(1, "ac"), userType(0, "ac"))),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", unset());
+ }
+
+ @Test
+ public void testInMarkerWithLists() throws Throwable
+ {
+ for (boolean frozen : new boolean[]{false, true})
+ {
+ createTable(String.format("CREATE TABLE %%s (k int PRIMARY KEY, l %s)",
+ frozen
+ ? "frozen<list<text>>"
+ : "list<text>"));
+
+ execute("INSERT INTO %s(k, l) VALUES (0, ['foo', 'bar', 'foobar'])");
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), list("bar", "foobar")),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), unset()),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", null, null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", list(list("foo", "foobar"), list("bar", "foobar"))),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, list("foo", "foobar")),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", "foobar"),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", unset()),
+ row(false, list("foo", "bar", "foobar")));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "bar", "foobar"), list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l IN (?, ?, ?)", list("foo", "bar", "foobar"), null, list("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?, ?)", list("foo", "bar", "foobar"), unset(), list("foo", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l IN (?, ?)", list("bar", "foobar"), list("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l[?] IN (?, ?, ?)", 1, "bar", null, "foobar"),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l[?] IN (?, ?, ?)", 1, "bar", unset(), "foobar"),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", unset());
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, unset());
+ }
+ }
+
+ @Test
+ public void testInMarkerWithMaps() throws Throwable
+ {
+ for (boolean frozen : new boolean[] {false, true})
+ {
+ createTable(String.format("CREATE TABLE %%s (k int PRIMARY KEY, m %s)",
+ frozen
+ ? "frozen<map<text, text>>"
+ : "map<text, text>"));
+
+ execute("INSERT INTO %s (k, m) VALUES (0, {'foo' : 'bar'})");
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), map("bar", "foobar")),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), unset()),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", null, null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", list(map("foo", "foobar"), map("bar", "foobar"))),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN ?", "foo", list("foo", "foobar")),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", "foobar"),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", unset()),
+ row(false, map("foo", "bar")));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), map("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m IN (?, ?, ?)", map("bar", "foobar"), null, map("foo", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m IN (?, ?, ?)", map("bar", "foobar"), unset(), map("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", list(map("foo", "bar"), map("bar", "foobar"))),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m[?] IN ?", "foo", list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?, ?)", "foo", "bar", null, "foobar"),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?, ?)", "foo", "bar", unset(), "foobar"),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", unset());
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN ?", "foo", unset());
+ }
+ }
}
[7/7] cassandra git commit: Refactor ColumnCondition
Posted by bl...@apache.org.
Refactor ColumnCondition
patch by Benjamin Lerer; reviewed by Alex Petrov for CASSANDRA-12981
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/e71a49e8
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/e71a49e8
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/e71a49e8
Branch: refs/heads/trunk
Commit: e71a49e81f97864641f406461425a74ca4c56df1
Parents: 36375f9
Author: Benjamin Lerer <b....@gmail.com>
Authored: Fri Jan 27 15:50:06 2017 +0100
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Fri Jan 27 15:50:06 2017 +0100
----------------------------------------------------------------------
CHANGES.txt | 4 +-
src/antlr/Cql.g | 1 +
.../cassandra/cql3/AbstractConditions.java | 64 --
.../apache/cassandra/cql3/AbstractMarker.java | 4 +-
.../apache/cassandra/cql3/ColumnCondition.java | 1042 ------------------
.../apache/cassandra/cql3/ColumnConditions.java | 164 ---
.../org/apache/cassandra/cql3/Conditions.java | 102 --
.../cassandra/cql3/IfExistsCondition.java | 36 -
.../cassandra/cql3/IfNotExistsCondition.java | 36 -
.../org/apache/cassandra/cql3/Operator.java | 180 ++-
src/java/org/apache/cassandra/cql3/Terms.java | 241 +++-
.../cql3/conditions/AbstractConditions.java | 64 ++
.../cql3/conditions/ColumnCondition.java | 825 ++++++++++++++
.../cql3/conditions/ColumnConditions.java | 165 +++
.../cassandra/cql3/conditions/Conditions.java | 103 ++
.../cql3/conditions/IfExistsCondition.java | 37 +
.../cql3/conditions/IfNotExistsCondition.java | 37 +
.../cql3/statements/CQL3CasRequest.java | 1 +
.../cql3/statements/DeleteStatement.java | 2 +
.../cql3/statements/ModificationStatement.java | 3 +
.../cql3/statements/UpdateStatement.java | 2 +
.../cassandra/cql3/ColumnConditionTest.java | 589 ----------
.../cql3/conditions/ColumnConditionTest.java | 557 ++++++++++
.../operations/InsertUpdateIfConditionTest.java | 192 +++-
24 files changed, 2356 insertions(+), 2095 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index ee2196d..d113645 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,6 @@
4.0
- * Parallelize streaming of different keyspaces (4663)
+ * Refactor ColumnCondition (CASSANDRA-12981)
+ * Parallelize streaming of different keyspaces (CASSANDRA-4663)
* Improved compactions metrics (CASSANDRA-13015)
* Speed-up start-up sequence by avoiding un-needed flushes (CASSANDRA-13031)
* Use Caffeine (W-TinyLFU) for on-heap caches (CASSANDRA-10855)
@@ -216,6 +217,7 @@ Merged from 3.0:
* Correct log message for statistics of offheap memtable flush (CASSANDRA-12776)
* Explicitly set locale for string validation (CASSANDRA-12541,CASSANDRA-12542,CASSANDRA-12543,CASSANDRA-12545)
Merged from 2.2:
+ * Fix handling of nulls and unsets in IN conditions (CASSANDRA-12981)
* Fix race causing infinite loop if Thrift server is stopped before it starts listening (CASSANDRA-12856)
* CompactionTasks now correctly drops sstables out of compaction when not enough disk space is available (CASSANDRA-12979)
* Fix DynamicEndpointSnitch noop in multi-datacenter situations (CASSANDRA-13074)
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/antlr/Cql.g
----------------------------------------------------------------------
diff --git a/src/antlr/Cql.g b/src/antlr/Cql.g
index a11f2fd..8b26426 100644
--- a/src/antlr/Cql.g
+++ b/src/antlr/Cql.g
@@ -46,6 +46,7 @@ import Parser,Lexer;
import org.apache.cassandra.cql3.statements.*;
import org.apache.cassandra.cql3.selection.*;
import org.apache.cassandra.cql3.functions.*;
+ import org.apache.cassandra.cql3.conditions.*;
import org.apache.cassandra.db.marshal.CollectionType;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.exceptions.InvalidRequestException;
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/AbstractConditions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/AbstractConditions.java b/src/java/org/apache/cassandra/cql3/AbstractConditions.java
deleted file mode 100644
index 530d2b1..0000000
--- a/src/java/org/apache/cassandra/cql3/AbstractConditions.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3;
-
-import java.util.List;
-
-import org.apache.cassandra.config.ColumnDefinition;
-import org.apache.cassandra.cql3.functions.Function;
-
-/**
- * Base class for <code>Conditions</code> classes.
- *
- */
-abstract class AbstractConditions implements Conditions
-{
- public void addFunctionsTo(List<Function> functions)
- {
- }
-
- public Iterable<ColumnDefinition> getColumns()
- {
- return null;
- }
-
- public boolean isEmpty()
- {
- return false;
- }
-
- public boolean appliesToStaticColumns()
- {
- return false;
- }
-
- public boolean appliesToRegularColumns()
- {
- return false;
- }
-
- public boolean isIfExists()
- {
- return false;
- }
-
- public boolean isIfNotExists()
- {
- return false;
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/AbstractMarker.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/AbstractMarker.java b/src/java/org/apache/cassandra/cql3/AbstractMarker.java
index 7ffedef..19ce26c 100644
--- a/src/java/org/apache/cassandra/cql3/AbstractMarker.java
+++ b/src/java/org/apache/cassandra/cql3/AbstractMarker.java
@@ -140,7 +140,7 @@ public abstract class AbstractMarker extends Term.NonTerminal
*
* Because a single type is used, a List is used to represent the values.
*/
- public static class INRaw extends Raw
+ public static final class INRaw extends Raw
{
public INRaw(int bindIndex)
{
@@ -154,7 +154,7 @@ public abstract class AbstractMarker extends Term.NonTerminal
}
@Override
- public AbstractMarker prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
+ public Lists.Marker prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
return new Lists.Marker(bindIndex, makeInReceiver(receiver));
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/ColumnCondition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ColumnCondition.java b/src/java/org/apache/cassandra/cql3/ColumnCondition.java
deleted file mode 100644
index 07f9f60..0000000
--- a/src/java/org/apache/cassandra/cql3/ColumnCondition.java
+++ /dev/null
@@ -1,1042 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3;
-
-import java.nio.ByteBuffer;
-import java.util.*;
-
-import com.google.common.collect.Iterators;
-
-import org.apache.cassandra.config.CFMetaData;
-import org.apache.cassandra.config.ColumnDefinition;
-import org.apache.cassandra.cql3.functions.Function;
-import org.apache.cassandra.db.rows.*;
-import org.apache.cassandra.db.marshal.*;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.transport.ProtocolVersion;
-import org.apache.cassandra.utils.ByteBufferUtil;
-
-/**
- * A CQL3 condition on the value of a column or collection element. For example, "UPDATE .. IF a = 0".
- */
-public class ColumnCondition
-{
- public final ColumnDefinition column;
-
- // For collection, when testing the equality of a specific element, null otherwise.
- private final Term collectionElement;
-
- // For UDT, when testing the equality of a specific field, null otherwise.
- private final FieldIdentifier field;
-
- private final Term value; // a single value or a marker for a list of IN values
- private final List<Term> inValues;
-
- public final Operator operator;
-
- private ColumnCondition(ColumnDefinition column, Term collectionElement, FieldIdentifier field, Term value, List<Term> inValues, Operator op)
- {
- this.column = column;
- this.collectionElement = collectionElement;
- this.field = field;
- this.value = value;
- this.inValues = inValues;
- this.operator = op;
-
- assert field == null || collectionElement == null;
- if (operator != Operator.IN)
- assert this.inValues == null;
- }
-
- public static ColumnCondition condition(ColumnDefinition column, Term value, Operator op)
- {
- return new ColumnCondition(column, null, null, value, null, op);
- }
-
- public static ColumnCondition condition(ColumnDefinition column, Term collectionElement, Term value, Operator op)
- {
- return new ColumnCondition(column, collectionElement, null, value, null, op);
- }
-
- public static ColumnCondition condition(ColumnDefinition column, FieldIdentifier udtField, Term value, Operator op)
- {
- return new ColumnCondition(column, null, udtField, value, null, op);
- }
-
- public static ColumnCondition inCondition(ColumnDefinition column, List<Term> inValues)
- {
- return new ColumnCondition(column, null, null, null, inValues, Operator.IN);
- }
-
- public static ColumnCondition inCondition(ColumnDefinition column, Term collectionElement, List<Term> inValues)
- {
- return new ColumnCondition(column, collectionElement, null, null, inValues, Operator.IN);
- }
-
- public static ColumnCondition inCondition(ColumnDefinition column, FieldIdentifier udtField, List<Term> inValues)
- {
- return new ColumnCondition(column, null, udtField, null, inValues, Operator.IN);
- }
-
- public static ColumnCondition inCondition(ColumnDefinition column, Term inMarker)
- {
- return new ColumnCondition(column, null, null, inMarker, null, Operator.IN);
- }
-
- public static ColumnCondition inCondition(ColumnDefinition column, Term collectionElement, Term inMarker)
- {
- return new ColumnCondition(column, collectionElement, null, inMarker, null, Operator.IN);
- }
-
- public static ColumnCondition inCondition(ColumnDefinition column, FieldIdentifier udtField, Term inMarker)
- {
- return new ColumnCondition(column, null, udtField, inMarker, null, Operator.IN);
- }
-
- public void addFunctionsTo(List<Function> functions)
- {
- if (collectionElement != null)
- collectionElement.addFunctionsTo(functions);
- if (value != null)
- value.addFunctionsTo(functions);
- if (inValues != null)
- for (Term value : inValues)
- if (value != null)
- value.addFunctionsTo(functions);
- }
-
- /**
- * Collects the column specification for the bind variables of this operation.
- *
- * @param boundNames the list of column specification where to collect the
- * bind variables of this term in.
- */
- public void collectMarkerSpecification(VariableSpecifications boundNames)
- {
- if (collectionElement != null)
- collectionElement.collectMarkerSpecification(boundNames);
-
- if ((operator == Operator.IN) && inValues != null)
- {
- for (Term value : inValues)
- value.collectMarkerSpecification(boundNames);
- }
- else
- {
- value.collectMarkerSpecification(boundNames);
- }
- }
-
- public ColumnCondition.Bound bind(QueryOptions options) throws InvalidRequestException
- {
- boolean isInCondition = operator == Operator.IN;
- if (column.type instanceof CollectionType)
- {
- if (collectionElement != null)
- return isInCondition ? new ElementAccessInBound(this, options) : new ElementAccessBound(this, options);
- else
- return isInCondition ? new CollectionInBound(this, options) : new CollectionBound(this, options);
- }
- else if (column.type.isUDT())
- {
- if (field != null)
- return isInCondition ? new UDTFieldAccessInBound(this, options) : new UDTFieldAccessBound(this, options);
- else
- return isInCondition ? new UDTInBound(this, options) : new UDTBound(this, options);
- }
-
- return isInCondition ? new SimpleInBound(this, options) : new SimpleBound(this, options);
- }
-
- public static abstract class Bound
- {
- public final ColumnDefinition column;
- public final Operator operator;
-
- protected Bound(ColumnDefinition column, Operator operator)
- {
- this.column = column;
- this.operator = operator;
- }
-
- /**
- * Validates whether this condition applies to {@code current}.
- */
- public abstract boolean appliesTo(Row row) throws InvalidRequestException;
-
- public ByteBuffer getCollectionElementValue()
- {
- return null;
- }
-
- protected boolean isSatisfiedByValue(ByteBuffer value, Cell c, AbstractType<?> type, Operator operator) throws InvalidRequestException
- {
- return compareWithOperator(operator, type, value, c == null ? null : c.value());
- }
-
- /** Returns true if the operator is satisfied (i.e. "otherValue operator value == true"), false otherwise. */
- protected boolean compareWithOperator(Operator operator, AbstractType<?> type, ByteBuffer value, ByteBuffer otherValue) throws InvalidRequestException
- {
- if (value == ByteBufferUtil.UNSET_BYTE_BUFFER)
- throw new InvalidRequestException("Invalid 'unset' value in condition");
- if (value == null)
- {
- switch (operator)
- {
- case EQ:
- return otherValue == null;
- case NEQ:
- return otherValue != null;
- default:
- throw new InvalidRequestException(String.format("Invalid comparison with null for operator \"%s\"", operator));
- }
- }
- else if (otherValue == null)
- {
- // the condition value is not null, so only NEQ can return true
- return operator == Operator.NEQ;
- }
- return operator.isSatisfiedBy(type, otherValue, value);
- }
- }
-
- private static Cell getCell(Row row, ColumnDefinition column)
- {
- // If we're asking for a given cell, and we didn't got any row from our read, it's
- // the same as not having said cell.
- return row == null ? null : row.getCell(column);
- }
-
- private static Cell getCell(Row row, ColumnDefinition column, CellPath path)
- {
- // If we're asking for a given cell, and we didn't got any row from our read, it's
- // the same as not having said cell.
- return row == null ? null : row.getCell(column, path);
- }
-
- private static Iterator<Cell> getCells(Row row, ColumnDefinition column)
- {
- // If we're asking for a complex cells, and we didn't got any row from our read, it's
- // the same as not having any cells for that column.
- if (row == null)
- return Collections.<Cell>emptyIterator();
-
- ComplexColumnData complexData = row.getComplexColumnData(column);
- return complexData == null ? Collections.<Cell>emptyIterator() : complexData.iterator();
- }
-
- private static boolean evaluateComparisonWithOperator(int comparison, Operator operator)
- {
- // called when comparison != 0
- switch (operator)
- {
- case EQ:
- return false;
- case LT:
- case LTE:
- return comparison < 0;
- case GT:
- case GTE:
- return comparison > 0;
- case NEQ:
- return true;
- default:
- throw new AssertionError();
- }
- }
-
- private static ByteBuffer cellValueAtIndex(Iterator<Cell> iter, int index)
- {
- int adv = Iterators.advance(iter, index);
- if (adv == index && iter.hasNext())
- return iter.next().value();
- else
- return null;
- }
-
- /**
- * A condition on a single non-collection column. This does not support IN operators (see SimpleInBound).
- */
- static class SimpleBound extends Bound
- {
- public final ByteBuffer value;
-
- private SimpleBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert !(column.type instanceof CollectionType) && condition.field == null;
- assert condition.operator != Operator.IN;
- this.value = condition.value.bindAndGet(options);
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- return isSatisfiedByValue(value, getCell(row, column), column.type, operator);
- }
- }
-
- /**
- * An IN condition on a single non-collection column.
- */
- static class SimpleInBound extends Bound
- {
- public final List<ByteBuffer> inValues;
-
- private SimpleInBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert !(column.type instanceof CollectionType) && condition.field == null;
- assert condition.operator == Operator.IN;
- if (condition.inValues == null)
- this.inValues = ((Lists.Value) condition.value.bind(options)).getElements();
- else
- {
- this.inValues = new ArrayList<>(condition.inValues.size());
- for (Term value : condition.inValues)
- this.inValues.add(value.bindAndGet(options));
- }
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- Cell c = getCell(row, column);
- for (ByteBuffer value : inValues)
- {
- if (isSatisfiedByValue(value, c, column.type, Operator.EQ))
- return true;
- }
- return false;
- }
- }
-
- /** A condition on an element of a collection column. IN operators are not supported here, see ElementAccessInBound. */
- static class ElementAccessBound extends Bound
- {
- public final ByteBuffer collectionElement;
- public final ByteBuffer value;
-
- private ElementAccessBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type instanceof CollectionType && condition.collectionElement != null;
- assert condition.operator != Operator.IN;
- this.collectionElement = condition.collectionElement.bindAndGet(options);
- this.value = condition.value.bindAndGet(options);
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- if (collectionElement == null)
- throw new InvalidRequestException("Invalid null value for " + (column.type instanceof MapType ? "map" : "list") + " element access");
-
- if (column.type instanceof MapType)
- {
- MapType mapType = (MapType) column.type;
- if (column.type.isMultiCell())
- {
- Cell cell = getCell(row, column, CellPath.create(collectionElement));
- return isSatisfiedByValue(value, cell, ((MapType) column.type).getValuesType(), operator);
- }
- else
- {
- Cell cell = getCell(row, column);
- ByteBuffer mapElementValue = mapType.getSerializer().getSerializedValue(cell.value(), collectionElement, mapType.getKeysType());
- return compareWithOperator(operator, mapType.getValuesType(), value, mapElementValue);
- }
- }
-
- // sets don't have element access, so it's a list
- ListType listType = (ListType) column.type;
- if (column.type.isMultiCell())
- {
- ByteBuffer columnValue = cellValueAtIndex(getCells(row, column), getListIndex(collectionElement));
- return compareWithOperator(operator, ((ListType)column.type).getElementsType(), value, columnValue);
- }
- else
- {
- Cell cell = getCell(row, column);
- ByteBuffer listElementValue = listType.getSerializer().getElement(cell.value(), getListIndex(collectionElement));
- return compareWithOperator(operator, listType.getElementsType(), value, listElementValue);
- }
- }
-
- static int getListIndex(ByteBuffer collectionElement) throws InvalidRequestException
- {
- int idx = ByteBufferUtil.toInt(collectionElement);
- if (idx < 0)
- throw new InvalidRequestException(String.format("Invalid negative list index %d", idx));
- return idx;
- }
-
- public ByteBuffer getCollectionElementValue()
- {
- return collectionElement;
- }
- }
-
- static class ElementAccessInBound extends Bound
- {
- public final ByteBuffer collectionElement;
- public final List<ByteBuffer> inValues;
-
- private ElementAccessInBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type instanceof CollectionType && condition.collectionElement != null;
- this.collectionElement = condition.collectionElement.bindAndGet(options);
-
- if (condition.inValues == null)
- this.inValues = ((Lists.Value) condition.value.bind(options)).getElements();
- else
- {
- this.inValues = new ArrayList<>(condition.inValues.size());
- for (Term value : condition.inValues)
- this.inValues.add(value.bindAndGet(options));
- }
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- if (collectionElement == null)
- throw new InvalidRequestException("Invalid null value for " + (column.type instanceof MapType ? "map" : "list") + " element access");
-
- ByteBuffer cellValue;
- AbstractType<?> valueType;
- if (column.type instanceof MapType)
- {
- MapType mapType = (MapType) column.type;
- valueType = mapType.getValuesType();
- if (column.type.isMultiCell())
- {
- Cell cell = getCell(row, column, CellPath.create(collectionElement));
- cellValue = cell == null ? null : cell.value();
- }
- else
- {
- Cell cell = getCell(row, column);
- cellValue = cell == null
- ? null
- : mapType.getSerializer().getSerializedValue(cell.value(), collectionElement, mapType.getKeysType());
- }
- }
- else // ListType
- {
- ListType listType = (ListType) column.type;
- valueType = listType.getElementsType();
- if (column.type.isMultiCell())
- {
- cellValue = cellValueAtIndex(getCells(row, column), ElementAccessBound.getListIndex(collectionElement));
- }
- else
- {
- Cell cell = getCell(row, column);
- cellValue = cell == null
- ? null
- : listType.getSerializer().getElement(cell.value(), ElementAccessBound.getListIndex(collectionElement));
- }
- }
-
- for (ByteBuffer value : inValues)
- {
- if (compareWithOperator(Operator.EQ, valueType, value, cellValue))
- return true;
- }
- return false;
- }
- }
-
- /** A condition on an entire collection column. IN operators are not supported here, see CollectionInBound. */
- static class CollectionBound extends Bound
- {
- private final Term.Terminal value;
-
- private CollectionBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type.isCollection() && condition.collectionElement == null;
- assert condition.operator != Operator.IN;
- this.value = condition.value.bind(options);
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- CollectionType type = (CollectionType)column.type;
-
- if (type.isMultiCell())
- {
- Iterator<Cell> iter = getCells(row, column);
- if (value == null)
- {
- if (operator == Operator.EQ)
- return !iter.hasNext();
- else if (operator == Operator.NEQ)
- return iter.hasNext();
- else
- throw new InvalidRequestException(String.format("Invalid comparison with null for operator \"%s\"", operator));
- }
-
- return valueAppliesTo(type, iter, value, operator);
- }
-
- // frozen collections
- Cell cell = getCell(row, column);
- if (value == null)
- {
- if (operator == Operator.EQ)
- return cell == null;
- else if (operator == Operator.NEQ)
- return cell != null;
- else
- throw new InvalidRequestException(String.format("Invalid comparison with null for operator \"%s\"", operator));
- }
-
- // make sure we use v3 serialization format for comparison
- ByteBuffer conditionValue;
- if (type.kind == CollectionType.Kind.LIST)
- conditionValue = ((Lists.Value) value).get(ProtocolVersion.V3);
- else if (type.kind == CollectionType.Kind.SET)
- conditionValue = ((Sets.Value) value).get(ProtocolVersion.V3);
- else
- conditionValue = ((Maps.Value) value).get(ProtocolVersion.V3);
-
- return compareWithOperator(operator, type, conditionValue, cell.value());
- }
-
- static boolean valueAppliesTo(CollectionType type, Iterator<Cell> iter, Term.Terminal value, Operator operator)
- {
- if (value == null)
- return !iter.hasNext();
-
- switch (type.kind)
- {
- case LIST:
- List<ByteBuffer> valueList = ((Lists.Value) value).elements;
- return listAppliesTo((ListType)type, iter, valueList, operator);
- case SET:
- Set<ByteBuffer> valueSet = ((Sets.Value) value).elements;
- return setAppliesTo((SetType)type, iter, valueSet, operator);
- case MAP:
- Map<ByteBuffer, ByteBuffer> valueMap = ((Maps.Value) value).map;
- return mapAppliesTo((MapType)type, iter, valueMap, operator);
- }
- throw new AssertionError();
- }
-
- private static boolean setOrListAppliesTo(AbstractType<?> type, Iterator<Cell> iter, Iterator<ByteBuffer> conditionIter, Operator operator, boolean isSet)
- {
- while(iter.hasNext())
- {
- if (!conditionIter.hasNext())
- return (operator == Operator.GT) || (operator == Operator.GTE) || (operator == Operator.NEQ);
-
- // for lists we use the cell value; for sets we use the cell name
- ByteBuffer cellValue = isSet ? iter.next().path().get(0) : iter.next().value();
- int comparison = type.compare(cellValue, conditionIter.next());
- if (comparison != 0)
- return evaluateComparisonWithOperator(comparison, operator);
- }
-
- if (conditionIter.hasNext())
- return (operator == Operator.LT) || (operator == Operator.LTE) || (operator == Operator.NEQ);
-
- // they're equal
- return operator == Operator.EQ || operator == Operator.LTE || operator == Operator.GTE;
- }
-
- static boolean listAppliesTo(ListType type, Iterator<Cell> iter, List<ByteBuffer> elements, Operator operator)
- {
- return setOrListAppliesTo(type.getElementsType(), iter, elements.iterator(), operator, false);
- }
-
- static boolean setAppliesTo(SetType type, Iterator<Cell> iter, Set<ByteBuffer> elements, Operator operator)
- {
- ArrayList<ByteBuffer> sortedElements = new ArrayList<>(elements.size());
- sortedElements.addAll(elements);
- Collections.sort(sortedElements, type.getElementsType());
- return setOrListAppliesTo(type.getElementsType(), iter, sortedElements.iterator(), operator, true);
- }
-
- static boolean mapAppliesTo(MapType type, Iterator<Cell> iter, Map<ByteBuffer, ByteBuffer> elements, Operator operator)
- {
- Iterator<Map.Entry<ByteBuffer, ByteBuffer>> conditionIter = elements.entrySet().iterator();
- while(iter.hasNext())
- {
- if (!conditionIter.hasNext())
- return (operator == Operator.GT) || (operator == Operator.GTE) || (operator == Operator.NEQ);
-
- Map.Entry<ByteBuffer, ByteBuffer> conditionEntry = conditionIter.next();
- Cell c = iter.next();
-
- // compare the keys
- int comparison = type.getKeysType().compare(c.path().get(0), conditionEntry.getKey());
- if (comparison != 0)
- return evaluateComparisonWithOperator(comparison, operator);
-
- // compare the values
- comparison = type.getValuesType().compare(c.value(), conditionEntry.getValue());
- if (comparison != 0)
- return evaluateComparisonWithOperator(comparison, operator);
- }
-
- if (conditionIter.hasNext())
- return (operator == Operator.LT) || (operator == Operator.LTE) || (operator == Operator.NEQ);
-
- // they're equal
- return operator == Operator.EQ || operator == Operator.LTE || operator == Operator.GTE;
- }
- }
-
- public static class CollectionInBound extends Bound
- {
- private final List<Term.Terminal> inValues;
-
- private CollectionInBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type instanceof CollectionType && condition.collectionElement == null;
- assert condition.operator == Operator.IN;
- inValues = new ArrayList<>();
- if (condition.inValues == null)
- {
- // We have a list of serialized collections that need to be deserialized for later comparisons
- CollectionType collectionType = (CollectionType) column.type;
- Lists.Marker inValuesMarker = (Lists.Marker) condition.value;
- if (column.type instanceof ListType)
- {
- ListType deserializer = ListType.getInstance(collectionType.valueComparator(), false);
- for (ByteBuffer buffer : ((Lists.Value)inValuesMarker.bind(options)).elements)
- {
- if (buffer == null)
- this.inValues.add(null);
- else
- this.inValues.add(Lists.Value.fromSerialized(buffer, deserializer, options.getProtocolVersion()));
- }
- }
- else if (column.type instanceof MapType)
- {
- MapType deserializer = MapType.getInstance(collectionType.nameComparator(), collectionType.valueComparator(), false);
- for (ByteBuffer buffer : ((Lists.Value)inValuesMarker.bind(options)).elements)
- {
- if (buffer == null)
- this.inValues.add(null);
- else
- this.inValues.add(Maps.Value.fromSerialized(buffer, deserializer, options.getProtocolVersion()));
- }
- }
- else if (column.type instanceof SetType)
- {
- SetType deserializer = SetType.getInstance(collectionType.valueComparator(), false);
- for (ByteBuffer buffer : ((Lists.Value)inValuesMarker.bind(options)).elements)
- {
- if (buffer == null)
- this.inValues.add(null);
- else
- this.inValues.add(Sets.Value.fromSerialized(buffer, deserializer, options.getProtocolVersion()));
- }
- }
- }
- else
- {
- for (Term value : condition.inValues)
- this.inValues.add(value.bind(options));
- }
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- CollectionType type = (CollectionType)column.type;
- if (type.isMultiCell())
- {
- // copy iterator contents so that we can properly reuse them for each comparison with an IN value
- for (Term.Terminal value : inValues)
- {
- if (CollectionBound.valueAppliesTo(type, getCells(row, column), value, Operator.EQ))
- return true;
- }
- return false;
- }
- else
- {
- Cell cell = getCell(row, column);
- for (Term.Terminal value : inValues)
- {
- if (value == null)
- {
- if (cell == null)
- return true;
- }
- else if (type.compare(value.get(ProtocolVersion.V3), cell.value()) == 0)
- {
- return true;
- }
- }
- return false;
- }
- }
- }
-
- /** A condition on a UDT field. IN operators are not supported here, see UDTFieldAccessInBound. */
- static class UDTFieldAccessBound extends Bound
- {
- public final FieldIdentifier field;
- public final ByteBuffer value;
-
- private UDTFieldAccessBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type.isUDT() && condition.field != null;
- assert condition.operator != Operator.IN;
- this.field = condition.field;
- this.value = condition.value.bindAndGet(options);
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- UserType userType = (UserType) column.type;
- int fieldPosition = userType.fieldPosition(field);
- assert fieldPosition >= 0;
-
- ByteBuffer cellValue;
- if (column.type.isMultiCell())
- {
- Cell cell = getCell(row, column, userType.cellPathForField(field));
- cellValue = cell == null ? null : cell.value();
- }
- else
- {
- Cell cell = getCell(row, column);
- cellValue = cell == null
- ? null
- : userType.split(cell.value())[fieldPosition];
- }
- return compareWithOperator(operator, userType.fieldType(fieldPosition), value, cellValue);
- }
- }
-
- /** An IN condition on a UDT field. For example: IF user.name IN ('a', 'b') */
- static class UDTFieldAccessInBound extends Bound
- {
- public final FieldIdentifier field;
- public final List<ByteBuffer> inValues;
-
- private UDTFieldAccessInBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type.isUDT() && condition.field != null;
- this.field = condition.field;
-
- if (condition.inValues == null)
- this.inValues = ((Lists.Value) condition.value.bind(options)).getElements();
- else
- {
- this.inValues = new ArrayList<>(condition.inValues.size());
- for (Term value : condition.inValues)
- this.inValues.add(value.bindAndGet(options));
- }
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- UserType userType = (UserType) column.type;
- int fieldPosition = userType.fieldPosition(field);
- assert fieldPosition >= 0;
-
- ByteBuffer cellValue;
- if (column.type.isMultiCell())
- {
- Cell cell = getCell(row, column, userType.cellPathForField(field));
- cellValue = cell == null ? null : cell.value();
- }
- else
- {
- Cell cell = getCell(row, column);
- cellValue = cell == null ? null : userType.split(getCell(row, column).value())[fieldPosition];
- }
-
- AbstractType<?> valueType = userType.fieldType(fieldPosition);
- for (ByteBuffer value : inValues)
- {
- if (compareWithOperator(Operator.EQ, valueType, value, cellValue))
- return true;
- }
- return false;
- }
- }
-
- /** A non-IN condition on an entire UDT. For example: IF user = {name: 'joe', age: 42}). */
- static class UDTBound extends Bound
- {
- private final ByteBuffer value;
- private final ProtocolVersion protocolVersion;
-
- private UDTBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type.isUDT() && condition.field == null;
- assert condition.operator != Operator.IN;
- protocolVersion = options.getProtocolVersion();
- value = condition.value.bindAndGet(options);
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- UserType userType = (UserType) column.type;
- ByteBuffer rowValue;
- if (userType.isMultiCell())
- {
- Iterator<Cell> iter = getCells(row, column);
- rowValue = iter.hasNext() ? userType.serializeForNativeProtocol(iter, protocolVersion) : null;
- }
- else
- {
- Cell cell = getCell(row, column);
- rowValue = cell == null ? null : cell.value();
- }
-
- if (value == null)
- {
- if (operator == Operator.EQ)
- return rowValue == null;
- else if (operator == Operator.NEQ)
- return rowValue != null;
- else
- throw new InvalidRequestException(String.format("Invalid comparison with null for operator \"%s\"", operator));
- }
-
- return compareWithOperator(operator, userType, value, rowValue);
- }
- }
-
- /** An IN condition on an entire UDT. For example: IF user IN ({name: 'joe', age: 42}, {name: 'bob', age: 23}). */
- public static class UDTInBound extends Bound
- {
- private final List<ByteBuffer> inValues;
- private final ProtocolVersion protocolVersion;
-
- private UDTInBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
- {
- super(condition.column, condition.operator);
- assert column.type.isUDT() && condition.field == null;
- assert condition.operator == Operator.IN;
- protocolVersion = options.getProtocolVersion();
- inValues = new ArrayList<>();
- if (condition.inValues == null)
- {
- Lists.Marker inValuesMarker = (Lists.Marker) condition.value;
- for (ByteBuffer buffer : ((Lists.Value)inValuesMarker.bind(options)).elements)
- this.inValues.add(buffer);
- }
- else
- {
- for (Term value : condition.inValues)
- this.inValues.add(value.bindAndGet(options));
- }
- }
-
- public boolean appliesTo(Row row) throws InvalidRequestException
- {
- UserType userType = (UserType) column.type;
- ByteBuffer rowValue;
- if (userType.isMultiCell())
- {
- Iterator<Cell> cells = getCells(row, column);
- rowValue = cells.hasNext() ? userType.serializeForNativeProtocol(cells, protocolVersion) : null;
- }
- else
- {
- Cell cell = getCell(row, column);
- rowValue = cell == null ? null : cell.value();
- }
-
- for (ByteBuffer value : inValues)
- {
- if (value == null || rowValue == null)
- {
- if (value == rowValue) // both null
- return true;
- }
- else if (userType.compare(value, rowValue) == 0)
- {
- return true;
- }
- }
- return false;
- }
- }
-
- public static class Raw
- {
- private final Term.Raw value;
- private final List<Term.Raw> inValues;
- private final AbstractMarker.INRaw inMarker;
-
- // Can be null, only used with the syntax "IF m[e] = ..." (in which case it's 'e')
- private final Term.Raw collectionElement;
-
- // Can be null, only used with the syntax "IF udt.field = ..." (in which case it's 'field')
- private final FieldIdentifier udtField;
-
- private final Operator operator;
-
- private Raw(Term.Raw value, List<Term.Raw> inValues, AbstractMarker.INRaw inMarker, Term.Raw collectionElement,
- FieldIdentifier udtField, Operator op)
- {
- this.value = value;
- this.inValues = inValues;
- this.inMarker = inMarker;
- this.collectionElement = collectionElement;
- this.udtField = udtField;
- this.operator = op;
- }
-
- /** A condition on a column. For example: "IF col = 'foo'" */
- public static Raw simpleCondition(Term.Raw value, Operator op)
- {
- return new Raw(value, null, null, null, null, op);
- }
-
- /** An IN condition on a column. For example: "IF col IN ('foo', 'bar', ...)" */
- public static Raw simpleInCondition(List<Term.Raw> inValues)
- {
- return new Raw(null, inValues, null, null, null, Operator.IN);
- }
-
- /** An IN condition on a column with a single marker. For example: "IF col IN ?" */
- public static Raw simpleInCondition(AbstractMarker.INRaw inMarker)
- {
- return new Raw(null, null, inMarker, null, null, Operator.IN);
- }
-
- /** A condition on a collection element. For example: "IF col['key'] = 'foo'" */
- public static Raw collectionCondition(Term.Raw value, Term.Raw collectionElement, Operator op)
- {
- return new Raw(value, null, null, collectionElement, null, op);
- }
-
- /** An IN condition on a collection element. For example: "IF col['key'] IN ('foo', 'bar', ...)" */
- public static Raw collectionInCondition(Term.Raw collectionElement, List<Term.Raw> inValues)
- {
- return new Raw(null, inValues, null, collectionElement, null, Operator.IN);
- }
-
- /** An IN condition on a collection element with a single marker. For example: "IF col['key'] IN ?" */
- public static Raw collectionInCondition(Term.Raw collectionElement, AbstractMarker.INRaw inMarker)
- {
- return new Raw(null, null, inMarker, collectionElement, null, Operator.IN);
- }
-
- /** A condition on a UDT field. For example: "IF col.field = 'foo'" */
- public static Raw udtFieldCondition(Term.Raw value, FieldIdentifier udtField, Operator op)
- {
- return new Raw(value, null, null, null, udtField, op);
- }
-
- /** An IN condition on a collection element. For example: "IF col.field IN ('foo', 'bar', ...)" */
- public static Raw udtFieldInCondition(FieldIdentifier udtField, List<Term.Raw> inValues)
- {
- return new Raw(null, inValues, null, null, udtField, Operator.IN);
- }
-
- /** An IN condition on a collection element with a single marker. For example: "IF col.field IN ?" */
- public static Raw udtFieldInCondition(FieldIdentifier udtField, AbstractMarker.INRaw inMarker)
- {
- return new Raw(null, null, inMarker, null, udtField, Operator.IN);
- }
-
- public ColumnCondition prepare(String keyspace, ColumnDefinition receiver, CFMetaData cfm) throws InvalidRequestException
- {
- if (receiver.type instanceof CounterColumnType)
- throw new InvalidRequestException("Conditions on counters are not supported");
-
- if (collectionElement != null)
- {
- if (!(receiver.type.isCollection()))
- throw new InvalidRequestException(String.format("Invalid element access syntax for non-collection column %s", receiver.name));
-
- ColumnSpecification elementSpec, valueSpec;
- switch ((((CollectionType) receiver.type).kind))
- {
- case LIST:
- elementSpec = Lists.indexSpecOf(receiver);
- valueSpec = Lists.valueSpecOf(receiver);
- break;
- case MAP:
- elementSpec = Maps.keySpecOf(receiver);
- valueSpec = Maps.valueSpecOf(receiver);
- break;
- case SET:
- throw new InvalidRequestException(String.format("Invalid element access syntax for set column %s", receiver.name));
- default:
- throw new AssertionError();
- }
- if (operator == Operator.IN)
- {
- if (inValues == null)
- return ColumnCondition.inCondition(receiver, collectionElement.prepare(keyspace, elementSpec), inMarker.prepare(keyspace, valueSpec));
- List<Term> terms = new ArrayList<>(inValues.size());
- for (Term.Raw value : inValues)
- terms.add(value.prepare(keyspace, valueSpec));
- return ColumnCondition.inCondition(receiver, collectionElement.prepare(keyspace, elementSpec), terms);
- }
- else
- {
- return ColumnCondition.condition(receiver, collectionElement.prepare(keyspace, elementSpec), value.prepare(keyspace, valueSpec), operator);
- }
- }
- else if (udtField != null)
- {
- UserType userType = (UserType) receiver.type;
- int fieldPosition = userType.fieldPosition(udtField);
- if (fieldPosition == -1)
- throw new InvalidRequestException(String.format("Unknown field %s for column %s", udtField, receiver.name));
-
- ColumnSpecification fieldReceiver = UserTypes.fieldSpecOf(receiver, fieldPosition);
- if (operator == Operator.IN)
- {
- if (inValues == null)
- return ColumnCondition.inCondition(receiver, udtField, inMarker.prepare(keyspace, fieldReceiver));
-
- List<Term> terms = new ArrayList<>(inValues.size());
- for (Term.Raw value : inValues)
- terms.add(value.prepare(keyspace, fieldReceiver));
- return ColumnCondition.inCondition(receiver, udtField, terms);
- }
- else
- {
- return ColumnCondition.condition(receiver, udtField, value.prepare(keyspace, fieldReceiver), operator);
- }
- }
- else
- {
- if (operator == Operator.IN)
- {
- if (inValues == null)
- return ColumnCondition.inCondition(receiver, inMarker.prepare(keyspace, receiver));
- List<Term> terms = new ArrayList<>(inValues.size());
- for (Term.Raw value : inValues)
- terms.add(value.prepare(keyspace, receiver));
- return ColumnCondition.inCondition(receiver, terms);
- }
- else
- {
- return ColumnCondition.condition(receiver, value.prepare(keyspace, receiver), operator);
- }
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/ColumnConditions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ColumnConditions.java b/src/java/org/apache/cassandra/cql3/ColumnConditions.java
deleted file mode 100644
index cb09b1a..0000000
--- a/src/java/org/apache/cassandra/cql3/ColumnConditions.java
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import org.apache.cassandra.config.ColumnDefinition;
-import org.apache.cassandra.cql3.functions.Function;
-import org.apache.cassandra.cql3.statements.CQL3CasRequest;
-import org.apache.cassandra.db.Clustering;
-
-/**
- * A set of <code>ColumnCondition</code>s.
- *
- */
-public final class ColumnConditions extends AbstractConditions
-{
- /**
- * The conditions on regular columns.
- */
- private final List<ColumnCondition> columnConditions;
-
- /**
- * The conditions on static columns
- */
- private final List<ColumnCondition> staticConditions;
-
- /**
- * Creates a new <code>ColumnConditions</code> instance for the specified builder.
- */
- private ColumnConditions(Builder builder)
- {
- this.columnConditions = builder.columnConditions;
- this.staticConditions = builder.staticConditions;
- }
-
- @Override
- public boolean appliesToStaticColumns()
- {
- return !staticConditions.isEmpty();
- }
-
- @Override
- public boolean appliesToRegularColumns()
- {
- return !columnConditions.isEmpty();
- }
-
- @Override
- public Collection<ColumnDefinition> getColumns()
- {
- return Stream.concat(columnConditions.stream(), staticConditions.stream())
- .map(e -> e.column)
- .collect(Collectors.toList());
- }
-
- @Override
- public boolean isEmpty()
- {
- return columnConditions.isEmpty() && staticConditions.isEmpty();
- }
-
- /**
- * Adds the conditions to the specified CAS request.
- *
- * @param request the request
- * @param clustering the clustering prefix
- * @param options the query options
- */
- public void addConditionsTo(CQL3CasRequest request,
- Clustering clustering,
- QueryOptions options)
- {
- if (!columnConditions.isEmpty())
- request.addConditions(clustering, columnConditions, options);
- if (!staticConditions.isEmpty())
- request.addConditions(Clustering.STATIC_CLUSTERING, staticConditions, options);
- }
-
- @Override
- public void addFunctionsTo(List<Function> functions)
- {
- columnConditions.forEach(p -> p.addFunctionsTo(functions));
- staticConditions.forEach(p -> p.addFunctionsTo(functions));
- }
-
- /**
- * Creates a new <code>Builder</code> for <code>ColumnConditions</code>.
- * @return a new <code>Builder</code> for <code>ColumnConditions</code>
- */
- public static Builder newBuilder()
- {
- return new Builder();
- }
-
- /**
- * A <code>Builder</code> for <code>ColumnConditions</code>.
- *
- */
- public static final class Builder
- {
- /**
- * The conditions on regular columns.
- */
- private List<ColumnCondition> columnConditions = Collections.emptyList();
-
- /**
- * The conditions on static columns
- */
- private List<ColumnCondition> staticConditions = Collections.emptyList();
-
- /**
- * Adds the specified <code>ColumnCondition</code> to this set of conditions.
- * @param condition the condition to add
- */
- public Builder add(ColumnCondition condition)
- {
- List<ColumnCondition> conds = null;
- if (condition.column.isStatic())
- {
- if (staticConditions.isEmpty())
- staticConditions = new ArrayList<>();
- conds = staticConditions;
- }
- else
- {
- if (columnConditions.isEmpty())
- columnConditions = new ArrayList<>();
- conds = columnConditions;
- }
- conds.add(condition);
- return this;
- }
-
- public ColumnConditions build()
- {
- return new ColumnConditions(this);
- }
-
- private Builder()
- {
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/Conditions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/Conditions.java b/src/java/org/apache/cassandra/cql3/Conditions.java
deleted file mode 100644
index 16fa4aa..0000000
--- a/src/java/org/apache/cassandra/cql3/Conditions.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3;
-
-import java.util.List;
-
-import org.apache.cassandra.config.ColumnDefinition;
-import org.apache.cassandra.cql3.functions.Function;
-import org.apache.cassandra.cql3.statements.CQL3CasRequest;
-import org.apache.cassandra.db.Clustering;
-
-/**
- * Conditions that can be applied to a mutation statement.
- *
- */
-public interface Conditions
-{
- /**
- * An EMPTY condition
- */
- static final Conditions EMPTY_CONDITION = ColumnConditions.newBuilder().build();
-
- /**
- * IF EXISTS condition
- */
- static final Conditions IF_EXISTS_CONDITION = new IfExistsCondition();
-
- /**
- * IF NOT EXISTS condition
- */
- static final Conditions IF_NOT_EXISTS_CONDITION = new IfNotExistsCondition();
-
- /**
- * Adds the functions used by the conditions to the specified list.
- * @param functions the list to add to
- */
- void addFunctionsTo(List<Function> functions);
-
- /**
- * Returns the column definitions to which apply the conditions.
- * @return the column definitions to which apply the conditions.
- */
- Iterable<ColumnDefinition> getColumns();
-
- /**
- * Checks if this <code>Conditions</code> is empty.
- * @return <code>true</code> if this <code>Conditions</code> is empty, <code>false</code> otherwise.
- */
- boolean isEmpty();
-
- /**
- * Checks if this is a IF EXIST condition.
- * @return <code>true</code> if this is a IF EXIST condition, <code>false</code> otherwise.
- */
- boolean isIfExists();
-
- /**
- * Checks if this is a IF NOT EXIST condition.
- * @return <code>true</code> if this is a IF NOT EXIST condition, <code>false</code> otherwise.
- */
- boolean isIfNotExists();
-
- /**
- * Checks if some of the conditions apply to static columns.
- *
- * @return <code>true</code> if some of the conditions apply to static columns, <code>false</code> otherwise.
- */
- boolean appliesToStaticColumns();
-
- /**
- * Checks if some of the conditions apply to regular columns.
- *
- * @return <code>true</code> if some of the conditions apply to regular columns, <code>false</code> otherwise.
- */
- boolean appliesToRegularColumns();
-
- /**
- * Adds the conditions to the specified CAS request.
- *
- * @param request the request
- * @param clustering the clustering prefix
- * @param options the query options
- */
- public void addConditionsTo(CQL3CasRequest request,
- Clustering clustering,
- QueryOptions options);
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/IfExistsCondition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/IfExistsCondition.java b/src/java/org/apache/cassandra/cql3/IfExistsCondition.java
deleted file mode 100644
index a24d8c0..0000000
--- a/src/java/org/apache/cassandra/cql3/IfExistsCondition.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3;
-
-import org.apache.cassandra.cql3.statements.CQL3CasRequest;
-import org.apache.cassandra.db.Clustering;
-
-final class IfExistsCondition extends AbstractConditions
-{
- @Override
- public void addConditionsTo(CQL3CasRequest request, Clustering clustering, QueryOptions options)
- {
- request.addExist(clustering);
- }
-
- @Override
- public boolean isIfExists()
- {
- return true;
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/IfNotExistsCondition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/IfNotExistsCondition.java b/src/java/org/apache/cassandra/cql3/IfNotExistsCondition.java
deleted file mode 100644
index 05cb864..0000000
--- a/src/java/org/apache/cassandra/cql3/IfNotExistsCondition.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3;
-
-import org.apache.cassandra.cql3.statements.CQL3CasRequest;
-import org.apache.cassandra.db.Clustering;
-
-final class IfNotExistsCondition extends AbstractConditions
-{
- @Override
- public void addConditionsTo(CQL3CasRequest request, Clustering clustering, QueryOptions options)
- {
- request.addNotExist(clustering);
- }
-
- @Override
- public boolean isIfNotExists()
- {
- return true;
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/Operator.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/Operator.java b/src/java/org/apache/cassandra/cql3/Operator.java
index 07c92f0..f2a6c6b 100644
--- a/src/java/org/apache/cassandra/cql3/Operator.java
+++ b/src/java/org/apache/cassandra/cql3/Operator.java
@@ -37,6 +37,12 @@ public enum Operator
{
return "=";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return type.compareForCQL(leftOperand, rightOperand) == 0;
+ }
},
LT(4)
{
@@ -45,6 +51,12 @@ public enum Operator
{
return "<";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return type.compareForCQL(leftOperand, rightOperand) < 0;
+ }
},
LTE(3)
{
@@ -53,6 +65,12 @@ public enum Operator
{
return "<=";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return type.compareForCQL(leftOperand, rightOperand) <= 0;
+ }
},
GTE(1)
{
@@ -61,6 +79,12 @@ public enum Operator
{
return ">=";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return type.compareForCQL(leftOperand, rightOperand) >= 0;
+ }
},
GT(2)
{
@@ -69,12 +93,56 @@ public enum Operator
{
return ">";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return type.compareForCQL(leftOperand, rightOperand) > 0;
+ }
},
IN(7)
{
+ @Override
+ public String toString()
+ {
+ return "IN";
+ }
+
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ List<?> inValues = ListType.getInstance(type, false).getSerializer().deserialize(rightOperand);
+ return inValues.contains(type.getSerializer().deserialize(leftOperand));
+ }
},
CONTAINS(5)
{
+ @Override
+ public String toString()
+ {
+ return "CONTAINS";
+ }
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ switch(((CollectionType<?>) type).kind)
+ {
+ case LIST :
+ ListType<?> listType = (ListType<?>) type;
+ List<?> list = listType.getSerializer().deserialize(leftOperand);
+ return list.contains(listType.getElementsType().getSerializer().deserialize(rightOperand));
+ case SET:
+ SetType<?> setType = (SetType<?>) type;
+ Set<?> set = setType.getSerializer().deserialize(leftOperand);
+ return set.contains(setType.getElementsType().getSerializer().deserialize(rightOperand));
+ case MAP:
+ MapType<?, ?> mapType = (MapType<?, ?>) type;
+ Map<?, ?> map = mapType.getSerializer().deserialize(leftOperand);
+ return map.containsValue(mapType.getValuesType().getSerializer().deserialize(rightOperand));
+ default:
+ throw new AssertionError();
+ }
+ }
},
CONTAINS_KEY(6)
{
@@ -83,6 +151,14 @@ public enum Operator
{
return "CONTAINS KEY";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ MapType<?, ?> mapType = (MapType<?, ?>) type;
+ Map<?, ?> map = mapType.getSerializer().deserialize(leftOperand);
+ return map.containsKey(mapType.getKeysType().getSerializer().deserialize(rightOperand));
+ }
},
NEQ(8)
{
@@ -91,6 +167,13 @@ public enum Operator
{
return "!=";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return type.compareForCQL(leftOperand, rightOperand) != 0;
+
+ }
},
IS_NOT(9)
{
@@ -99,6 +182,12 @@ public enum Operator
{
return "IS NOT";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ throw new UnsupportedOperationException();
+ }
},
LIKE_PREFIX(10)
{
@@ -107,6 +196,12 @@ public enum Operator
{
return "LIKE '<term>%'";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return ByteBufferUtil.startsWith(leftOperand, rightOperand);
+ }
},
LIKE_SUFFIX(11)
{
@@ -115,6 +210,12 @@ public enum Operator
{
return "LIKE '%<term>'";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return ByteBufferUtil.endsWith(leftOperand, rightOperand);
+ }
},
LIKE_CONTAINS(12)
{
@@ -123,6 +224,12 @@ public enum Operator
{
return "LIKE '%<term>%'";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return ByteBufferUtil.contains(leftOperand, rightOperand);
+ }
},
LIKE_MATCHES(13)
{
@@ -131,6 +238,11 @@ public enum Operator
{
return "LIKE '<term>'";
}
+
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ return ByteBufferUtil.contains(leftOperand, rightOperand);
+ }
},
LIKE(14)
{
@@ -139,6 +251,12 @@ public enum Operator
{
return "LIKE";
}
+
+ @Override
+ public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
+ {
+ throw new UnsupportedOperationException();
+ }
};
/**
@@ -190,59 +308,8 @@ public enum Operator
/**
* Whether 2 values satisfy this operator (given the type they should be compared with).
- *
- * @throws AssertionError for CONTAINS and CONTAINS_KEY as this doesn't support those operators yet
*/
- public boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand)
- {
- switch (this)
- {
- case EQ:
- return type.compareForCQL(leftOperand, rightOperand) == 0;
- case LT:
- return type.compareForCQL(leftOperand, rightOperand) < 0;
- case LTE:
- return type.compareForCQL(leftOperand, rightOperand) <= 0;
- case GT:
- return type.compareForCQL(leftOperand, rightOperand) > 0;
- case GTE:
- return type.compareForCQL(leftOperand, rightOperand) >= 0;
- case NEQ:
- return type.compareForCQL(leftOperand, rightOperand) != 0;
- case IN:
- List inValues = ((List) ListType.getInstance(type, false).getSerializer().deserialize(rightOperand));
- return inValues.contains(type.getSerializer().deserialize(leftOperand));
- case CONTAINS:
- if (type instanceof ListType)
- {
- List list = (List) type.getSerializer().deserialize(leftOperand);
- return list.contains(((ListType) type).getElementsType().getSerializer().deserialize(rightOperand));
- }
- else if (type instanceof SetType)
- {
- Set set = (Set) type.getSerializer().deserialize(leftOperand);
- return set.contains(((SetType) type).getElementsType().getSerializer().deserialize(rightOperand));
- }
- else // MapType
- {
- Map map = (Map) type.getSerializer().deserialize(leftOperand);
- return map.containsValue(((MapType) type).getValuesType().getSerializer().deserialize(rightOperand));
- }
- case CONTAINS_KEY:
- Map map = (Map) type.getSerializer().deserialize(leftOperand);
- return map.containsKey(((MapType) type).getKeysType().getSerializer().deserialize(rightOperand));
- case LIKE_PREFIX:
- return ByteBufferUtil.startsWith(leftOperand, rightOperand);
- case LIKE_SUFFIX:
- return ByteBufferUtil.endsWith(leftOperand, rightOperand);
- case LIKE_MATCHES:
- case LIKE_CONTAINS:
- return ByteBufferUtil.contains(leftOperand, rightOperand);
- default:
- // we shouldn't get LIKE, CONTAINS, CONTAINS KEY, or IS NOT here
- throw new AssertionError();
- }
- }
+ public abstract boolean isSatisfiedBy(AbstractType<?> type, ByteBuffer leftOperand, ByteBuffer rightOperand);
public int serializedSize()
{
@@ -254,4 +321,13 @@ public enum Operator
{
return this.name();
}
+
+ /**
+ * Checks if this operator is an IN operator.
+ * @return {@code true} if this operator is an IN operator, {@code false} otherwise.
+ */
+ public boolean isIN()
+ {
+ return this == IN;
+ }
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/Terms.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/Terms.java b/src/java/org/apache/cassandra/cql3/Terms.java
index 7d3948a..7478366 100644
--- a/src/java/org/apache/cassandra/cql3/Terms.java
+++ b/src/java/org/apache/cassandra/cql3/Terms.java
@@ -18,17 +18,248 @@
package org.apache.cassandra.cql3;
import java.nio.ByteBuffer;
-import java.util.List;
+import java.util.*;
+import org.apache.cassandra.cql3.Term.MultiItemTerminal;
+import org.apache.cassandra.cql3.Term.Terminal;
import org.apache.cassandra.cql3.functions.Function;
-import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.*;
+import org.apache.cassandra.transport.ProtocolVersion;
-public class Terms
+/**
+ * A set of {@code Terms}
+ */
+public interface Terms
{
+ /**
+ * The {@code List} returned when the list was not set.
+ */
+ @SuppressWarnings("rawtypes")
+ public static final List UNSET_LIST = new AbstractList()
+ {
+ @Override
+ public Object get(int index)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int size()
+ {
+ return 0;
+ }
+ };
+
+ /**
+ * Adds all functions (native and user-defined) used by any of the terms to the specified list.
+ * @param functions the list to add to
+ */
+ public void addFunctionsTo(List<Function> functions);
+
+ /**
+ * Collects the column specifications for the bind variables in the terms.
+ * This is obviously a no-op if the terms are Terminal.
+ *
+ * @param boundNames the variables specification where to collect the
+ * bind variables of the terms in.
+ */
+ public void collectMarkerSpecification(VariableSpecifications boundNames);
+
+ /**
+ * Bind the values in these terms to the values contained in {@code options}.
+ * This is obviously a no-op if the term is Terminal.
+ *
+ * @param options the values to bind markers to.
+ * @return the result of binding all the variables of these NonTerminals or an {@code UNSET_LIST} if the term
+ * was unset.
+ */
+ public List<Terminal> bind(QueryOptions options);
+
+
+ public List<ByteBuffer> bindAndGet(QueryOptions options);
+
+ /**
+ * Creates a {@code Terms} for the specified list marker.
+ *
+ * @param marker the list marker
+ * @param type the element type
+ * @return a {@code Terms} for the specified list marker
+ */
+ public static Terms ofListMarker(final Lists.Marker marker, final AbstractType<?> type)
+ {
+ return new Terms()
+ {
+ @Override
+ public void addFunctionsTo(List<Function> functions)
+ {
+ }
+
+ @Override
+ public void collectMarkerSpecification(VariableSpecifications boundNames)
+ {
+ marker.collectMarkerSpecification(boundNames);
+ }
+
+ @Override
+ public List<ByteBuffer> bindAndGet(QueryOptions options)
+ {
+ Terminal terminal = marker.bind(options);
+
+ if (terminal == null)
+ return null;
+
+ if (terminal == Constants.UNSET_VALUE)
+ return UNSET_LIST;
+
+ return ((MultiItemTerminal) terminal).getElements();
+ }
+
+ @Override
+ public List<Terminal> bind(QueryOptions options)
+ {
+ Terminal terminal = marker.bind(options);
+
+ if (terminal == null)
+ return null;
+
+ if (terminal == Constants.UNSET_VALUE)
+ return UNSET_LIST;
+
+ java.util.function.Function<ByteBuffer, Term.Terminal> deserializer = deserializer(options.getProtocolVersion());
+
+ List<ByteBuffer> boundValues = ((MultiItemTerminal) terminal).getElements();
+ List<Term.Terminal> values = new ArrayList<>(boundValues.size());
+ for (int i = 0, m = boundValues.size(); i < m; i++)
+ {
+ ByteBuffer buffer = boundValues.get(i);
+ Term.Terminal value = buffer == null ? null : deserializer.apply(buffer);
+ values.add(value);
+ }
+ return values;
+ }
+
+ public java.util.function.Function<ByteBuffer, Term.Terminal> deserializer(ProtocolVersion version)
+ {
+ if (type.isCollection())
+ {
+ switch (((CollectionType<?>) type).kind)
+ {
+ case LIST:
+ return e -> Lists.Value.fromSerialized(e, (ListType<?>) type, version);
+ case SET:
+ return e -> Sets.Value.fromSerialized(e, (SetType<?>) type, version);
+ case MAP:
+ return e -> Maps.Value.fromSerialized(e, (MapType<?, ?>) type, version);
+ }
+ throw new AssertionError();
+ }
+ return e -> new Constants.Value(e);
+ }
+ };
+ }
+
+ /**
+ * Creates a {@code Terms} containing a single {@code Term}.
+ *
+ * @param term the {@code Term}
+ * @return a {@code Terms} containing a single {@code Term}.
+ */
+ public static Terms of(final Term term)
+ {
+ assert !(term instanceof Lists.Marker);
+ return new Terms()
+ {
+ @Override
+ public void addFunctionsTo(List<Function> functions)
+ {
+ term.addFunctionsTo(functions);
+ }
+
+ @Override
+ public void collectMarkerSpecification(VariableSpecifications boundNames)
+ {
+ term.collectMarkerSpecification(boundNames);
+ }
+
+ @Override
+ public List<ByteBuffer> bindAndGet(QueryOptions options)
+ {
+ return Collections.singletonList(term.bindAndGet(options));
+ }
+
+ @Override
+ public List<Terminal> bind(QueryOptions options)
+ {
+ return Collections.singletonList(term.bind(options));
+ }
+ };
+ }
+
+ /**
+ * Creates a {@code Terms} containing a set of {@code Term}.
+ *
+ * @param term the {@code Term}
+ * @return a {@code Terms} containing a set of {@code Term}.
+ */
+ public static Terms of(final List<Term> terms)
+ {
+ return new Terms()
+ {
+ @Override
+ public void addFunctionsTo(List<Function> functions)
+ {
+ addFunctions(terms, functions);
+ }
+
+ @Override
+ public void collectMarkerSpecification(VariableSpecifications boundNames)
+ {
+ for (int i = 0, m = terms.size(); i <m; i++)
+ {
+ Term term = terms.get(i);
+ term.collectMarkerSpecification(boundNames);
+ }
+ }
+
+ @Override
+ public List<Terminal> bind(QueryOptions options)
+ {
+ int size = terms.size();
+ List<Terminal> terminals = new ArrayList<>(size);
+ for (int i = 0; i < size; i++)
+ {
+ Term term = terms.get(i);
+ terminals.add(term.bind(options));
+ }
+ return terminals;
+ }
+
+ @Override
+ public List<ByteBuffer> bindAndGet(QueryOptions options)
+ {
+ int size = terms.size();
+ List<ByteBuffer> buffers = new ArrayList<>(size);
+ for (int i = 0; i < size; i++)
+ {
+ Term term = terms.get(i);
+ buffers.add(term.bindAndGet(options));
+ }
+ return buffers;
+ }
+ };
+ }
+
+ /**
+ * Adds all functions (native and user-defined) of the specified terms to the list.
+ * @param functions the list to add to
+ */
public static void addFunctions(Iterable<Term> terms, List<Function> functions)
{
- if (terms != null)
- terms.forEach(t -> t.addFunctionsTo(functions));
+ for (Term term : terms)
+ {
+ if (term != null)
+ term.addFunctionsTo(functions);
+ }
}
public static ByteBuffer asBytes(String keyspace, String term, AbstractType type)
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/conditions/AbstractConditions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/conditions/AbstractConditions.java b/src/java/org/apache/cassandra/cql3/conditions/AbstractConditions.java
new file mode 100644
index 0000000..9b6dd7e
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/conditions/AbstractConditions.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.cql3.conditions;
+
+import java.util.List;
+
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.functions.Function;
+
+/**
+ * Base class for <code>Conditions</code> classes.
+ *
+ */
+abstract class AbstractConditions implements Conditions
+{
+ public void addFunctionsTo(List<Function> functions)
+ {
+ }
+
+ public Iterable<ColumnDefinition> getColumns()
+ {
+ return null;
+ }
+
+ public boolean isEmpty()
+ {
+ return false;
+ }
+
+ public boolean appliesToStaticColumns()
+ {
+ return false;
+ }
+
+ public boolean appliesToRegularColumns()
+ {
+ return false;
+ }
+
+ public boolean isIfExists()
+ {
+ return false;
+ }
+
+ public boolean isIfNotExists()
+ {
+ return false;
+ }
+}
[4/7] cassandra git commit: Merge branch cassandra-3.11 into trunk
Posted by bl...@apache.org.
Merge branch cassandra-3.11 into trunk
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/36375f9a
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/36375f9a
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/36375f9a
Branch: refs/heads/trunk
Commit: 36375f9a08a13b658ead66634fd6a27957e8e02c
Parents: 80728df 078a841
Author: Benjamin Lerer <b....@gmail.com>
Authored: Fri Jan 27 15:42:47 2017 +0100
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Fri Jan 27 15:42:58 2017 +0100
----------------------------------------------------------------------
----------------------------------------------------------------------
[6/7] cassandra git commit: Refactor ColumnCondition
Posted by bl...@apache.org.
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/conditions/ColumnCondition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/conditions/ColumnCondition.java b/src/java/org/apache/cassandra/cql3/conditions/ColumnCondition.java
new file mode 100644
index 0000000..cfd62f5
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/conditions/ColumnCondition.java
@@ -0,0 +1,825 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.cql3.conditions;
+
+import java.nio.ByteBuffer;
+import java.util.*;
+
+import com.google.common.collect.Iterators;
+
+import org.apache.cassandra.config.CFMetaData;
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.cql3.Term.Terminal;
+import org.apache.cassandra.cql3.functions.Function;
+import org.apache.cassandra.db.rows.*;
+import org.apache.cassandra.db.marshal.*;
+import org.apache.cassandra.transport.ProtocolVersion;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+import static org.apache.cassandra.cql3.statements.RequestValidations.*;
+
+/**
+ * A CQL3 condition on the value of a column or collection element. For example, "UPDATE .. IF a = 0".
+ */
+public abstract class ColumnCondition
+{
+ public final ColumnDefinition column;
+ public final Operator operator;
+ private final Terms terms;
+
+ private ColumnCondition(ColumnDefinition column, Operator op, Terms terms)
+ {
+ this.column = column;
+ this.operator = op;
+ this.terms = terms;
+ }
+
+ /**
+ * Adds functions for the bind variables of this operation.
+ *
+ * @param functions the list of functions to get add
+ */
+ public void addFunctionsTo(List<Function> functions)
+ {
+ terms.addFunctionsTo(functions);
+ }
+
+ /**
+ * Collects the column specification for the bind variables of this operation.
+ *
+ * @param boundNames the list of column specification where to collect the
+ * bind variables of this term in.
+ */
+ public void collectMarkerSpecification(VariableSpecifications boundNames)
+ {
+ terms.collectMarkerSpecification(boundNames);
+ }
+
+ public abstract ColumnCondition.Bound bind(QueryOptions options);
+
+ protected final List<ByteBuffer> bindAndGetTerms(QueryOptions options)
+ {
+ return filterUnsetValuesIfNeeded(checkValues(terms.bindAndGet(options)));
+ }
+
+ protected final List<Terminal> bindTerms(QueryOptions options)
+ {
+ return filterUnsetValuesIfNeeded(checkValues(terms.bind(options)));
+ }
+
+ /**
+ * Checks that the output of a bind operations on {@code Terms} is a valid one.
+ * @param values the list to check
+ * @return the input list
+ */
+ private <T> List<T> checkValues(List<T> values)
+ {
+ checkFalse(values == null && operator.isIN(), "Invalid null list in IN condition");
+ checkFalse(values == Terms.UNSET_LIST, "Invalid 'unset' value in condition");
+ return values;
+ }
+
+ private <T> List<T> filterUnsetValuesIfNeeded(List<T> values)
+ {
+ if (!operator.isIN())
+ return values;
+
+ List<T> filtered = new ArrayList<>(values.size());
+ for (int i = 0, m = values.size(); i < m; i++)
+ {
+ T value = values.get(i);
+ // The value can be ByteBuffer or Constants.Value so we need to check the 2 type of UNSET
+ if (value != ByteBufferUtil.UNSET_BYTE_BUFFER && value != Constants.UNSET_VALUE)
+ filtered.add(value);
+ }
+ return filtered;
+ }
+
+ /**
+ * Simple condition (e.g. <pre>IF v = 1</pre>).
+ */
+ private static final class SimpleColumnCondition extends ColumnCondition
+ {
+ public SimpleColumnCondition(ColumnDefinition column, Operator op, Terms values)
+ {
+ super(column, op, values);
+ }
+
+ public Bound bind(QueryOptions options)
+ {
+ if (column.type.isCollection() && column.type.isMultiCell())
+ return new MultiCellCollectionBound(column, operator, bindTerms(options));
+
+ if (column.type.isUDT() && column.type.isMultiCell())
+ return new MultiCellUdtBound(column, operator, bindAndGetTerms(options), options.getProtocolVersion());
+
+ return new SimpleBound(column, operator, bindAndGetTerms(options));
+ }
+ }
+
+ /**
+ * A condition on a collection element (e.g. <pre>IF l[1] = 1</pre>).
+ */
+ private static class CollectionElementCondition extends ColumnCondition
+ {
+ private final Term collectionElement;
+
+ public CollectionElementCondition(ColumnDefinition column, Term collectionElement, Operator op, Terms values)
+ {
+ super(column, op, values);
+ this.collectionElement = collectionElement;
+ }
+
+ public void addFunctionsTo(List<Function> functions)
+ {
+ collectionElement.addFunctionsTo(functions);
+ super.addFunctionsTo(functions);
+ }
+
+ public void collectMarkerSpecification(VariableSpecifications boundNames)
+ {
+ collectionElement.collectMarkerSpecification(boundNames);
+ super.collectMarkerSpecification(boundNames);
+ }
+
+ public Bound bind(QueryOptions options)
+ {
+ return new ElementAccessBound(column, collectionElement.bindAndGet(options), operator, bindAndGetTerms(options));
+ }
+ }
+
+ /**
+ * A condition on a UDT field (e.g. <pre>IF v.a = 1</pre>).
+ */
+ private final static class UDTFieldCondition extends ColumnCondition
+ {
+ private final FieldIdentifier udtField;
+
+ public UDTFieldCondition(ColumnDefinition column, FieldIdentifier udtField, Operator op, Terms values)
+ {
+ super(column, op, values);
+ assert udtField != null;
+ this.udtField = udtField;
+ }
+
+ public Bound bind(QueryOptions options)
+ {
+ return new UDTFieldAccessBound(column, udtField, operator, bindAndGetTerms(options));
+ }
+ }
+
+ /**
+ * A regular column, simple condition.
+ */
+ public static ColumnCondition condition(ColumnDefinition column, Operator op, Terms terms)
+ {
+ return new SimpleColumnCondition(column, op, terms);
+ }
+
+ /**
+ * A collection column, simple condition.
+ */
+ public static ColumnCondition condition(ColumnDefinition column, Term collectionElement, Operator op, Terms terms)
+ {
+ return new CollectionElementCondition(column, collectionElement, op, terms);
+ }
+
+ /**
+ * A UDT column, simple condition.
+ */
+ public static ColumnCondition condition(ColumnDefinition column, FieldIdentifier udtField, Operator op, Terms terms)
+ {
+ return new UDTFieldCondition(column, udtField, op, terms);
+ }
+
+ public static abstract class Bound
+ {
+ public final ColumnDefinition column;
+ public final Operator comparisonOperator;
+
+ protected Bound(ColumnDefinition column, Operator operator)
+ {
+ this.column = column;
+ // If the operator is an IN we want to compare the value using an EQ.
+ this.comparisonOperator = operator.isIN() ? Operator.EQ : operator;
+ }
+
+ /**
+ * Validates whether this condition applies to {@code current}.
+ */
+ public abstract boolean appliesTo(Row row);
+
+ public ByteBuffer getCollectionElementValue()
+ {
+ return null;
+ }
+
+ /** Returns true if the operator is satisfied (i.e. "otherValue operator value == true"), false otherwise. */
+ protected static boolean compareWithOperator(Operator operator, AbstractType<?> type, ByteBuffer value, ByteBuffer otherValue)
+ {
+ if (value == ByteBufferUtil.UNSET_BYTE_BUFFER)
+ throw invalidRequest("Invalid 'unset' value in condition");
+
+ if (value == null)
+ {
+ switch (operator)
+ {
+ case EQ:
+ return otherValue == null;
+ case NEQ:
+ return otherValue != null;
+ default:
+ throw invalidRequest("Invalid comparison with null for operator \"%s\"", operator);
+ }
+ }
+ else if (otherValue == null)
+ {
+ // the condition value is not null, so only NEQ can return true
+ return operator == Operator.NEQ;
+ }
+ return operator.isSatisfiedBy(type, otherValue, value);
+ }
+ }
+
+ protected static final Cell getCell(Row row, ColumnDefinition column)
+ {
+ // If we're asking for a given cell, and we didn't got any row from our read, it's
+ // the same as not having said cell.
+ return row == null ? null : row.getCell(column);
+ }
+
+ protected static final Cell getCell(Row row, ColumnDefinition column, CellPath path)
+ {
+ // If we're asking for a given cell, and we didn't got any row from our read, it's
+ // the same as not having said cell.
+ return row == null ? null : row.getCell(column, path);
+ }
+
+ protected static final Iterator<Cell> getCells(Row row, ColumnDefinition column)
+ {
+ // If we're asking for a complex cells, and we didn't got any row from our read, it's
+ // the same as not having any cells for that column.
+ if (row == null)
+ return Collections.<Cell>emptyIterator();
+
+ ComplexColumnData complexData = row.getComplexColumnData(column);
+ return complexData == null ? Collections.<Cell>emptyIterator() : complexData.iterator();
+ }
+
+ protected static final boolean evaluateComparisonWithOperator(int comparison, Operator operator)
+ {
+ // called when comparison != 0
+ switch (operator)
+ {
+ case EQ:
+ return false;
+ case LT:
+ case LTE:
+ return comparison < 0;
+ case GT:
+ case GTE:
+ return comparison > 0;
+ case NEQ:
+ return true;
+ default:
+ throw new AssertionError();
+ }
+ }
+
+ /**
+ * A condition on a single non-collection column.
+ */
+ private static final class SimpleBound extends Bound
+ {
+ /**
+ * The condition values
+ */
+ private final List<ByteBuffer> values;
+
+ private SimpleBound(ColumnDefinition column, Operator operator, List<ByteBuffer> values)
+ {
+ super(column, operator);
+ this.values = values;
+ }
+
+ @Override
+ public boolean appliesTo(Row row)
+ {
+ return isSatisfiedBy(rowValue(row));
+ }
+
+ private ByteBuffer rowValue(Row row)
+ {
+ Cell c = getCell(row, column);
+ return c == null ? null : c.value();
+ }
+
+ private boolean isSatisfiedBy(ByteBuffer rowValue)
+ {
+ for (ByteBuffer value : values)
+ {
+ if (compareWithOperator(comparisonOperator, column.type, value, rowValue))
+ return true;
+ }
+ return false;
+ }
+ }
+
+ /**
+ * A condition on an element of a collection column.
+ */
+ private static final class ElementAccessBound extends Bound
+ {
+ /**
+ * The collection element
+ */
+ private final ByteBuffer collectionElement;
+
+ /**
+ * The conditions values.
+ */
+ private final List<ByteBuffer> values;
+
+ private ElementAccessBound(ColumnDefinition column,
+ ByteBuffer collectionElement,
+ Operator operator,
+ List<ByteBuffer> values)
+ {
+ super(column, operator);
+
+ this.collectionElement = collectionElement;
+ this.values = values;
+ }
+
+ @Override
+ public boolean appliesTo(Row row)
+ {
+ boolean isMap = column.type instanceof MapType;
+
+ if (collectionElement == null)
+ throw invalidRequest("Invalid null value for %s element access", isMap ? "map" : "list");
+
+ if (isMap)
+ {
+ MapType<?, ?> mapType = (MapType<?, ?>) column.type;
+ ByteBuffer rowValue = rowMapValue(mapType, row);
+ return isSatisfiedBy(mapType.getKeysType(), rowValue);
+ }
+
+ ListType<?> listType = (ListType<?>) column.type;
+ ByteBuffer rowValue = rowListValue(listType, row);
+ return isSatisfiedBy(listType.getElementsType(), rowValue);
+ }
+
+ private ByteBuffer rowMapValue(MapType<?, ?> type, Row row)
+ {
+ if (column.type.isMultiCell())
+ {
+ Cell cell = getCell(row, column, CellPath.create(collectionElement));
+ return cell == null ? null : cell.value();
+ }
+
+ Cell cell = getCell(row, column);
+ return cell == null
+ ? null
+ : type.getSerializer().getSerializedValue(cell.value(), collectionElement, type.getKeysType());
+ }
+
+ private ByteBuffer rowListValue(ListType<?> type, Row row)
+ {
+ if (column.type.isMultiCell())
+ return cellValueAtIndex(getCells(row, column), getListIndex(collectionElement));
+
+ Cell cell = getCell(row, column);
+ return cell == null
+ ? null
+ : type.getSerializer().getElement(cell.value(), getListIndex(collectionElement));
+ }
+
+ private static ByteBuffer cellValueAtIndex(Iterator<Cell> iter, int index)
+ {
+ int adv = Iterators.advance(iter, index);
+ if (adv == index && iter.hasNext())
+ return iter.next().value();
+
+ return null;
+ }
+
+ private boolean isSatisfiedBy(AbstractType<?> valueType, ByteBuffer rowValue)
+ {
+ for (ByteBuffer value : values)
+ {
+ if (compareWithOperator(comparisonOperator, valueType, value, rowValue))
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public ByteBuffer getCollectionElementValue()
+ {
+ return collectionElement;
+ }
+
+ private static int getListIndex(ByteBuffer collectionElement)
+ {
+ int idx = ByteBufferUtil.toInt(collectionElement);
+ checkFalse(idx < 0, "Invalid negative list index %d", idx);
+ return idx;
+ }
+ }
+
+ /**
+ * A condition on an entire collection column.
+ */
+ private static final class MultiCellCollectionBound extends Bound
+ {
+ private final List<Term.Terminal> values;
+
+ public MultiCellCollectionBound(ColumnDefinition column, Operator operator, List<Term.Terminal> values)
+ {
+ super(column, operator);
+ assert column.type.isMultiCell();
+ this.values = values;
+ }
+
+ public boolean appliesTo(Row row)
+ {
+ CollectionType<?> type = (CollectionType<?>)column.type;
+
+ // copy iterator contents so that we can properly reuse them for each comparison with an IN value
+ for (Term.Terminal value : values)
+ {
+ Iterator<Cell> iter = getCells(row, column);
+ if (value == null)
+ {
+ if (comparisonOperator == Operator.EQ)
+ {
+ if (!iter.hasNext())
+ return true;
+ continue;
+ }
+
+ if (comparisonOperator == Operator.NEQ)
+ return iter.hasNext();
+
+ throw invalidRequest("Invalid comparison with null for operator \"%s\"", comparisonOperator);
+ }
+
+ if (valueAppliesTo(type, iter, value, comparisonOperator))
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean valueAppliesTo(CollectionType<?> type, Iterator<Cell> iter, Term.Terminal value, Operator operator)
+ {
+ if (value == null)
+ return !iter.hasNext();
+
+ switch (type.kind)
+ {
+ case LIST:
+ List<ByteBuffer> valueList = ((Lists.Value) value).elements;
+ return listAppliesTo((ListType<?>)type, iter, valueList, operator);
+ case SET:
+ Set<ByteBuffer> valueSet = ((Sets.Value) value).elements;
+ return setAppliesTo((SetType<?>)type, iter, valueSet, operator);
+ case MAP:
+ Map<ByteBuffer, ByteBuffer> valueMap = ((Maps.Value) value).map;
+ return mapAppliesTo((MapType<?, ?>)type, iter, valueMap, operator);
+ }
+ throw new AssertionError();
+ }
+
+ private static boolean setOrListAppliesTo(AbstractType<?> type, Iterator<Cell> iter, Iterator<ByteBuffer> conditionIter, Operator operator, boolean isSet)
+ {
+ while(iter.hasNext())
+ {
+ if (!conditionIter.hasNext())
+ return (operator == Operator.GT) || (operator == Operator.GTE) || (operator == Operator.NEQ);
+
+ // for lists we use the cell value; for sets we use the cell name
+ ByteBuffer cellValue = isSet ? iter.next().path().get(0) : iter.next().value();
+ int comparison = type.compare(cellValue, conditionIter.next());
+ if (comparison != 0)
+ return evaluateComparisonWithOperator(comparison, operator);
+ }
+
+ if (conditionIter.hasNext())
+ return (operator == Operator.LT) || (operator == Operator.LTE) || (operator == Operator.NEQ);
+
+ // they're equal
+ return operator == Operator.EQ || operator == Operator.LTE || operator == Operator.GTE;
+ }
+
+ private static boolean listAppliesTo(ListType<?> type, Iterator<Cell> iter, List<ByteBuffer> elements, Operator operator)
+ {
+ return setOrListAppliesTo(type.getElementsType(), iter, elements.iterator(), operator, false);
+ }
+
+ private static boolean setAppliesTo(SetType<?> type, Iterator<Cell> iter, Set<ByteBuffer> elements, Operator operator)
+ {
+ ArrayList<ByteBuffer> sortedElements = new ArrayList<>(elements);
+ Collections.sort(sortedElements, type.getElementsType());
+ return setOrListAppliesTo(type.getElementsType(), iter, sortedElements.iterator(), operator, true);
+ }
+
+ private static boolean mapAppliesTo(MapType<?, ?> type, Iterator<Cell> iter, Map<ByteBuffer, ByteBuffer> elements, Operator operator)
+ {
+ Iterator<Map.Entry<ByteBuffer, ByteBuffer>> conditionIter = elements.entrySet().iterator();
+ while(iter.hasNext())
+ {
+ if (!conditionIter.hasNext())
+ return (operator == Operator.GT) || (operator == Operator.GTE) || (operator == Operator.NEQ);
+
+ Map.Entry<ByteBuffer, ByteBuffer> conditionEntry = conditionIter.next();
+ Cell c = iter.next();
+
+ // compare the keys
+ int comparison = type.getKeysType().compare(c.path().get(0), conditionEntry.getKey());
+ if (comparison != 0)
+ return evaluateComparisonWithOperator(comparison, operator);
+
+ // compare the values
+ comparison = type.getValuesType().compare(c.value(), conditionEntry.getValue());
+ if (comparison != 0)
+ return evaluateComparisonWithOperator(comparison, operator);
+ }
+
+ if (conditionIter.hasNext())
+ return (operator == Operator.LT) || (operator == Operator.LTE) || (operator == Operator.NEQ);
+
+ // they're equal
+ return operator == Operator.EQ || operator == Operator.LTE || operator == Operator.GTE;
+ }
+ }
+
+ /**
+ * A condition on a UDT field
+ */
+ private static final class UDTFieldAccessBound extends Bound
+ {
+ /**
+ * The UDT field.
+ */
+ private final FieldIdentifier field;
+
+ /**
+ * The conditions values.
+ */
+ private final List<ByteBuffer> values;
+
+ private UDTFieldAccessBound(ColumnDefinition column, FieldIdentifier field, Operator operator, List<ByteBuffer> values)
+ {
+ super(column, operator);
+ assert column.type.isUDT() && field != null;
+ this.field = field;
+ this.values = values;
+ }
+
+ @Override
+ public boolean appliesTo(Row row)
+ {
+ return isSatisfiedBy(rowValue(row));
+ }
+
+ private ByteBuffer rowValue(Row row)
+ {
+ UserType userType = (UserType) column.type;
+
+ if (column.type.isMultiCell())
+ {
+ Cell cell = getCell(row, column, userType.cellPathForField(field));
+ return cell == null ? null : cell.value();
+ }
+
+ Cell cell = getCell(row, column);
+ return cell == null
+ ? null
+ : userType.split(cell.value())[userType.fieldPosition(field)];
+ }
+
+ private boolean isSatisfiedBy(ByteBuffer rowValue)
+ {
+ UserType userType = (UserType) column.type;
+ int fieldPosition = userType.fieldPosition(field);
+ AbstractType<?> valueType = userType.fieldType(fieldPosition);
+ for (ByteBuffer value : values)
+ {
+ if (compareWithOperator(comparisonOperator, valueType, value, rowValue))
+ return true;
+ }
+ return false;
+ }
+ }
+
+ /**
+ * A condition on an entire UDT.
+ */
+ private static final class MultiCellUdtBound extends Bound
+ {
+ /**
+ * The conditions values.
+ */
+ private final List<ByteBuffer> values;
+
+ /**
+ * The protocol version
+ */
+ private final ProtocolVersion protocolVersion;
+
+ private MultiCellUdtBound(ColumnDefinition column, Operator op, List<ByteBuffer> values, ProtocolVersion protocolVersion)
+ {
+ super(column, op);
+ assert column.type.isMultiCell();
+ this.values = values;
+ this.protocolVersion = protocolVersion;
+ }
+
+ @Override
+ public boolean appliesTo(Row row)
+ {
+ return isSatisfiedBy(rowValue(row));
+ }
+
+ private final ByteBuffer rowValue(Row row)
+ {
+ UserType userType = (UserType) column.type;
+ Iterator<Cell> iter = getCells(row, column);
+ return iter.hasNext() ? userType.serializeForNativeProtocol(iter, protocolVersion) : null;
+ }
+
+ private boolean isSatisfiedBy(ByteBuffer rowValue)
+ {
+ for (ByteBuffer value : values)
+ {
+ if (compareWithOperator(comparisonOperator, column.type, value, rowValue))
+ return true;
+ }
+ return false;
+ }
+ }
+
+ public static class Raw
+ {
+ private final Term.Raw value;
+ private final List<Term.Raw> inValues;
+ private final AbstractMarker.INRaw inMarker;
+
+ // Can be null, only used with the syntax "IF m[e] = ..." (in which case it's 'e')
+ private final Term.Raw collectionElement;
+
+ // Can be null, only used with the syntax "IF udt.field = ..." (in which case it's 'field')
+ private final FieldIdentifier udtField;
+
+ private final Operator operator;
+
+ private Raw(Term.Raw value, List<Term.Raw> inValues, AbstractMarker.INRaw inMarker, Term.Raw collectionElement,
+ FieldIdentifier udtField, Operator op)
+ {
+ this.value = value;
+ this.inValues = inValues;
+ this.inMarker = inMarker;
+ this.collectionElement = collectionElement;
+ this.udtField = udtField;
+ this.operator = op;
+ }
+
+ /** A condition on a column. For example: "IF col = 'foo'" */
+ public static Raw simpleCondition(Term.Raw value, Operator op)
+ {
+ return new Raw(value, null, null, null, null, op);
+ }
+
+ /** An IN condition on a column. For example: "IF col IN ('foo', 'bar', ...)" */
+ public static Raw simpleInCondition(List<Term.Raw> inValues)
+ {
+ return new Raw(null, inValues, null, null, null, Operator.IN);
+ }
+
+ /** An IN condition on a column with a single marker. For example: "IF col IN ?" */
+ public static Raw simpleInCondition(AbstractMarker.INRaw inMarker)
+ {
+ return new Raw(null, null, inMarker, null, null, Operator.IN);
+ }
+
+ /** A condition on a collection element. For example: "IF col['key'] = 'foo'" */
+ public static Raw collectionCondition(Term.Raw value, Term.Raw collectionElement, Operator op)
+ {
+ return new Raw(value, null, null, collectionElement, null, op);
+ }
+
+ /** An IN condition on a collection element. For example: "IF col['key'] IN ('foo', 'bar', ...)" */
+ public static Raw collectionInCondition(Term.Raw collectionElement, List<Term.Raw> inValues)
+ {
+ return new Raw(null, inValues, null, collectionElement, null, Operator.IN);
+ }
+
+ /** An IN condition on a collection element with a single marker. For example: "IF col['key'] IN ?" */
+ public static Raw collectionInCondition(Term.Raw collectionElement, AbstractMarker.INRaw inMarker)
+ {
+ return new Raw(null, null, inMarker, collectionElement, null, Operator.IN);
+ }
+
+ /** A condition on a UDT field. For example: "IF col.field = 'foo'" */
+ public static Raw udtFieldCondition(Term.Raw value, FieldIdentifier udtField, Operator op)
+ {
+ return new Raw(value, null, null, null, udtField, op);
+ }
+
+ /** An IN condition on a collection element. For example: "IF col.field IN ('foo', 'bar', ...)" */
+ public static Raw udtFieldInCondition(FieldIdentifier udtField, List<Term.Raw> inValues)
+ {
+ return new Raw(null, inValues, null, null, udtField, Operator.IN);
+ }
+
+ /** An IN condition on a collection element with a single marker. For example: "IF col.field IN ?" */
+ public static Raw udtFieldInCondition(FieldIdentifier udtField, AbstractMarker.INRaw inMarker)
+ {
+ return new Raw(null, null, inMarker, null, udtField, Operator.IN);
+ }
+
+ public ColumnCondition prepare(String keyspace, ColumnDefinition receiver, CFMetaData cfm)
+ {
+ if (receiver.type instanceof CounterColumnType)
+ throw invalidRequest("Conditions on counters are not supported");
+
+ if (collectionElement != null)
+ {
+ if (!(receiver.type.isCollection()))
+ throw invalidRequest("Invalid element access syntax for non-collection column %s", receiver.name);
+
+ ColumnSpecification elementSpec, valueSpec;
+ switch ((((CollectionType<?>) receiver.type).kind))
+ {
+ case LIST:
+ elementSpec = Lists.indexSpecOf(receiver);
+ valueSpec = Lists.valueSpecOf(receiver);
+ break;
+ case MAP:
+ elementSpec = Maps.keySpecOf(receiver);
+ valueSpec = Maps.valueSpecOf(receiver);
+ break;
+ case SET:
+ throw invalidRequest("Invalid element access syntax for set column %s", receiver.name);
+ default:
+ throw new AssertionError();
+ }
+
+ return condition(receiver, collectionElement.prepare(keyspace, elementSpec), operator, prepareTerms(keyspace, valueSpec));
+ }
+
+ if (udtField != null)
+ {
+ UserType userType = (UserType) receiver.type;
+ int fieldPosition = userType.fieldPosition(udtField);
+ if (fieldPosition == -1)
+ throw invalidRequest("Unknown field %s for column %s", udtField, receiver.name);
+
+ ColumnSpecification fieldReceiver = UserTypes.fieldSpecOf(receiver, fieldPosition);
+ return condition(receiver, udtField, operator, prepareTerms(keyspace, fieldReceiver));
+ }
+
+ return condition(receiver, operator, prepareTerms(keyspace, receiver));
+ }
+
+ private Terms prepareTerms(String keyspace, ColumnSpecification receiver)
+ {
+ if (operator.isIN())
+ {
+ return inValues == null ? Terms.ofListMarker(inMarker.prepare(keyspace, receiver), receiver.type)
+ : Terms.of(prepareTerms(keyspace, receiver, inValues));
+ }
+
+ return Terms.of(value.prepare(keyspace, receiver));
+ }
+
+ private static List<Term> prepareTerms(String keyspace, ColumnSpecification receiver, List<Term.Raw> raws)
+ {
+ List<Term> terms = new ArrayList<>(raws.size());
+ for (int i = 0, m = raws.size(); i < m; i++)
+ {
+ Term.Raw raw = raws.get(i);
+ terms.add(raw.prepare(keyspace, receiver));
+ }
+ return terms;
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/conditions/ColumnConditions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/conditions/ColumnConditions.java b/src/java/org/apache/cassandra/cql3/conditions/ColumnConditions.java
new file mode 100644
index 0000000..b0378b7
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/conditions/ColumnConditions.java
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.cql3.conditions;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.QueryOptions;
+import org.apache.cassandra.cql3.functions.Function;
+import org.apache.cassandra.cql3.statements.CQL3CasRequest;
+import org.apache.cassandra.db.Clustering;
+
+/**
+ * A set of <code>ColumnCondition</code>s.
+ *
+ */
+public final class ColumnConditions extends AbstractConditions
+{
+ /**
+ * The conditions on regular columns.
+ */
+ private final List<ColumnCondition> columnConditions;
+
+ /**
+ * The conditions on static columns
+ */
+ private final List<ColumnCondition> staticConditions;
+
+ /**
+ * Creates a new <code>ColumnConditions</code> instance for the specified builder.
+ */
+ private ColumnConditions(Builder builder)
+ {
+ this.columnConditions = builder.columnConditions;
+ this.staticConditions = builder.staticConditions;
+ }
+
+ @Override
+ public boolean appliesToStaticColumns()
+ {
+ return !staticConditions.isEmpty();
+ }
+
+ @Override
+ public boolean appliesToRegularColumns()
+ {
+ return !columnConditions.isEmpty();
+ }
+
+ @Override
+ public Collection<ColumnDefinition> getColumns()
+ {
+ return Stream.concat(columnConditions.stream(), staticConditions.stream())
+ .map(e -> e.column)
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ return columnConditions.isEmpty() && staticConditions.isEmpty();
+ }
+
+ /**
+ * Adds the conditions to the specified CAS request.
+ *
+ * @param request the request
+ * @param clustering the clustering prefix
+ * @param options the query options
+ */
+ public void addConditionsTo(CQL3CasRequest request,
+ Clustering clustering,
+ QueryOptions options)
+ {
+ if (!columnConditions.isEmpty())
+ request.addConditions(clustering, columnConditions, options);
+ if (!staticConditions.isEmpty())
+ request.addConditions(Clustering.STATIC_CLUSTERING, staticConditions, options);
+ }
+
+ @Override
+ public void addFunctionsTo(List<Function> functions)
+ {
+ columnConditions.forEach(p -> p.addFunctionsTo(functions));
+ staticConditions.forEach(p -> p.addFunctionsTo(functions));
+ }
+
+ /**
+ * Creates a new <code>Builder</code> for <code>ColumnConditions</code>.
+ * @return a new <code>Builder</code> for <code>ColumnConditions</code>
+ */
+ public static Builder newBuilder()
+ {
+ return new Builder();
+ }
+
+ /**
+ * A <code>Builder</code> for <code>ColumnConditions</code>.
+ *
+ */
+ public static final class Builder
+ {
+ /**
+ * The conditions on regular columns.
+ */
+ private List<ColumnCondition> columnConditions = Collections.emptyList();
+
+ /**
+ * The conditions on static columns
+ */
+ private List<ColumnCondition> staticConditions = Collections.emptyList();
+
+ /**
+ * Adds the specified <code>ColumnCondition</code> to this set of conditions.
+ * @param condition the condition to add
+ */
+ public Builder add(ColumnCondition condition)
+ {
+ List<ColumnCondition> conds = null;
+ if (condition.column.isStatic())
+ {
+ if (staticConditions.isEmpty())
+ staticConditions = new ArrayList<>();
+ conds = staticConditions;
+ }
+ else
+ {
+ if (columnConditions.isEmpty())
+ columnConditions = new ArrayList<>();
+ conds = columnConditions;
+ }
+ conds.add(condition);
+ return this;
+ }
+
+ public ColumnConditions build()
+ {
+ return new ColumnConditions(this);
+ }
+
+ private Builder()
+ {
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/conditions/Conditions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/conditions/Conditions.java b/src/java/org/apache/cassandra/cql3/conditions/Conditions.java
new file mode 100644
index 0000000..0214fcf
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/conditions/Conditions.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.cql3.conditions;
+
+import java.util.List;
+
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.QueryOptions;
+import org.apache.cassandra.cql3.functions.Function;
+import org.apache.cassandra.cql3.statements.CQL3CasRequest;
+import org.apache.cassandra.db.Clustering;
+
+/**
+ * Conditions that can be applied to a mutation statement.
+ *
+ */
+public interface Conditions
+{
+ /**
+ * An EMPTY condition
+ */
+ static final Conditions EMPTY_CONDITION = ColumnConditions.newBuilder().build();
+
+ /**
+ * IF EXISTS condition
+ */
+ static final Conditions IF_EXISTS_CONDITION = new IfExistsCondition();
+
+ /**
+ * IF NOT EXISTS condition
+ */
+ static final Conditions IF_NOT_EXISTS_CONDITION = new IfNotExistsCondition();
+
+ /**
+ * Adds the functions used by the conditions to the specified list.
+ * @param functions the list to add to
+ */
+ void addFunctionsTo(List<Function> functions);
+
+ /**
+ * Returns the column definitions to which apply the conditions.
+ * @return the column definitions to which apply the conditions.
+ */
+ Iterable<ColumnDefinition> getColumns();
+
+ /**
+ * Checks if this <code>Conditions</code> is empty.
+ * @return <code>true</code> if this <code>Conditions</code> is empty, <code>false</code> otherwise.
+ */
+ boolean isEmpty();
+
+ /**
+ * Checks if this is a IF EXIST condition.
+ * @return <code>true</code> if this is a IF EXIST condition, <code>false</code> otherwise.
+ */
+ boolean isIfExists();
+
+ /**
+ * Checks if this is a IF NOT EXIST condition.
+ * @return <code>true</code> if this is a IF NOT EXIST condition, <code>false</code> otherwise.
+ */
+ boolean isIfNotExists();
+
+ /**
+ * Checks if some of the conditions apply to static columns.
+ *
+ * @return <code>true</code> if some of the conditions apply to static columns, <code>false</code> otherwise.
+ */
+ boolean appliesToStaticColumns();
+
+ /**
+ * Checks if some of the conditions apply to regular columns.
+ *
+ * @return <code>true</code> if some of the conditions apply to regular columns, <code>false</code> otherwise.
+ */
+ boolean appliesToRegularColumns();
+
+ /**
+ * Adds the conditions to the specified CAS request.
+ *
+ * @param request the request
+ * @param clustering the clustering prefix
+ * @param options the query options
+ */
+ public void addConditionsTo(CQL3CasRequest request,
+ Clustering clustering,
+ QueryOptions options);
+}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/conditions/IfExistsCondition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/conditions/IfExistsCondition.java b/src/java/org/apache/cassandra/cql3/conditions/IfExistsCondition.java
new file mode 100644
index 0000000..fa8822d
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/conditions/IfExistsCondition.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.cql3.conditions;
+
+import org.apache.cassandra.cql3.QueryOptions;
+import org.apache.cassandra.cql3.statements.CQL3CasRequest;
+import org.apache.cassandra.db.Clustering;
+
+final class IfExistsCondition extends AbstractConditions
+{
+ @Override
+ public void addConditionsTo(CQL3CasRequest request, Clustering clustering, QueryOptions options)
+ {
+ request.addExist(clustering);
+ }
+
+ @Override
+ public boolean isIfExists()
+ {
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/conditions/IfNotExistsCondition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/conditions/IfNotExistsCondition.java b/src/java/org/apache/cassandra/cql3/conditions/IfNotExistsCondition.java
new file mode 100644
index 0000000..ede9119
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/conditions/IfNotExistsCondition.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.cql3.conditions;
+
+import org.apache.cassandra.cql3.QueryOptions;
+import org.apache.cassandra.cql3.statements.CQL3CasRequest;
+import org.apache.cassandra.db.Clustering;
+
+final class IfNotExistsCondition extends AbstractConditions
+{
+ @Override
+ public void addConditionsTo(CQL3CasRequest request, Clustering clustering, QueryOptions options)
+ {
+ request.addNotExist(clustering);
+ }
+
+ @Override
+ public boolean isIfNotExists()
+ {
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java b/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java
index c203ace..4cf2baf 100644
--- a/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java
+++ b/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java
@@ -24,6 +24,7 @@ import com.google.common.collect.*;
import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.cql3.conditions.ColumnCondition;
import org.apache.cassandra.db.*;
import org.apache.cassandra.db.filter.*;
import org.apache.cassandra.db.rows.Row;
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java b/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
index 26b25de..10f3d6f 100644
--- a/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
@@ -22,6 +22,8 @@ import java.util.List;
import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.config.ColumnDefinition;
import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.cql3.conditions.ColumnCondition;
+import org.apache.cassandra.cql3.conditions.Conditions;
import org.apache.cassandra.cql3.restrictions.StatementRestrictions;
import org.apache.cassandra.db.Clustering;
import org.apache.cassandra.db.Slice;
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java b/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java
index 6f2ab44..601ddf7 100644
--- a/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java
@@ -30,6 +30,9 @@ import org.apache.cassandra.config.ColumnDefinition;
import org.apache.cassandra.config.ColumnDefinition.Raw;
import org.apache.cassandra.config.ViewDefinition;
import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.cql3.conditions.ColumnCondition;
+import org.apache.cassandra.cql3.conditions.ColumnConditions;
+import org.apache.cassandra.cql3.conditions.Conditions;
import org.apache.cassandra.cql3.functions.Function;
import org.apache.cassandra.cql3.restrictions.StatementRestrictions;
import org.apache.cassandra.cql3.selection.Selection;
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java
index 86d7171..01d47bd 100644
--- a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java
@@ -24,6 +24,8 @@ import java.util.List;
import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.config.ColumnDefinition;
import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.cql3.conditions.ColumnCondition;
+import org.apache.cassandra.cql3.conditions.Conditions;
import org.apache.cassandra.cql3.restrictions.StatementRestrictions;
import org.apache.cassandra.db.Clustering;
import org.apache.cassandra.db.CompactTables;
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/test/unit/org/apache/cassandra/cql3/ColumnConditionTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/ColumnConditionTest.java b/test/unit/org/apache/cassandra/cql3/ColumnConditionTest.java
deleted file mode 100644
index ca0c182..0000000
--- a/test/unit/org/apache/cassandra/cql3/ColumnConditionTest.java
+++ /dev/null
@@ -1,589 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3;
-
-import java.nio.ByteBuffer;
-import java.util.*;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import org.apache.cassandra.config.CFMetaData;
-import org.apache.cassandra.config.ColumnDefinition;
-import org.apache.cassandra.config.DatabaseDescriptor;
-import org.apache.cassandra.db.rows.BufferCell;
-import org.apache.cassandra.db.rows.Cell;
-import org.apache.cassandra.db.rows.CellPath;
-import org.apache.cassandra.db.marshal.*;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.serializers.Int32Serializer;
-import org.apache.cassandra.utils.ByteBufferUtil;
-import org.apache.cassandra.utils.UUIDGen;
-
-import static org.junit.Assert.*;
-
-public class ColumnConditionTest
-{
- private static final CellPath LIST_PATH = CellPath.create(ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes()));
-
- public static final ByteBuffer ZERO = Int32Type.instance.fromString("0");
- public static final ByteBuffer ONE = Int32Type.instance.fromString("1");
- public static final ByteBuffer TWO = Int32Type.instance.fromString("2");
-
- public static final ByteBuffer A = AsciiType.instance.fromString("a");
- public static final ByteBuffer B = AsciiType.instance.fromString("b");
-
- @BeforeClass
- public static void setupDD()
- {
- DatabaseDescriptor.daemonInitialization();
- }
-
- private static boolean isSatisfiedBy(ColumnCondition.Bound bound, ByteBuffer conditionValue, ByteBuffer columnValue) throws InvalidRequestException
- {
- Cell cell = null;
- if (columnValue != null)
- {
- ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", ListType.getInstance(Int32Type.instance, true));
- cell = testCell(definition, columnValue, LIST_PATH);
- }
- return bound.isSatisfiedByValue(conditionValue, cell, Int32Type.instance, bound.operator);
- }
-
- private static Cell testCell(ColumnDefinition column, ByteBuffer value, CellPath path)
- {
- return new BufferCell(column, 0L, Cell.NO_TTL, Cell.NO_DELETION_TIME, value, path);
- }
-
- private static void assertThrowsIRE(ColumnCondition.Bound bound, ByteBuffer conditionValue, ByteBuffer columnValue)
- {
- try
- {
- isSatisfiedBy(bound, conditionValue, columnValue);
- fail("Expected InvalidRequestException was not thrown");
- } catch (InvalidRequestException e) { }
- }
-
- @Test
- public void testSimpleBoundIsSatisfiedByValue() throws InvalidRequestException
- {
- ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", ListType.getInstance(Int32Type.instance, true));
-
- // EQ
- ColumnCondition condition = ColumnCondition.condition(definition, new Constants.Value(ONE), Operator.EQ);
- ColumnCondition.Bound bound = condition.bind(QueryOptions.DEFAULT);
- assertTrue(isSatisfiedBy(bound, ONE, ONE));
- assertFalse(isSatisfiedBy(bound, ZERO, ONE));
- assertFalse(isSatisfiedBy(bound, TWO, ONE));
- assertFalse(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE));
- assertFalse(isSatisfiedBy(bound, ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertTrue(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertTrue(isSatisfiedBy(bound, null, null));
- assertFalse(isSatisfiedBy(bound, ONE, null));
- assertFalse(isSatisfiedBy(bound, null, ONE));
-
- // NEQ
- condition = ColumnCondition.condition(definition, new Constants.Value(ONE), Operator.NEQ);
- bound = condition.bind(QueryOptions.DEFAULT);
- assertFalse(isSatisfiedBy(bound, ONE, ONE));
- assertTrue(isSatisfiedBy(bound, ZERO, ONE));
- assertTrue(isSatisfiedBy(bound, TWO, ONE));
- assertTrue(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE));
- assertTrue(isSatisfiedBy(bound, ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertFalse(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertFalse(isSatisfiedBy(bound, null, null));
- assertTrue(isSatisfiedBy(bound, ONE, null));
- assertTrue(isSatisfiedBy(bound, null, ONE));
-
- // LT
- condition = ColumnCondition.condition(definition, new Constants.Value(ONE), Operator.LT);
- bound = condition.bind(QueryOptions.DEFAULT);
- assertFalse(isSatisfiedBy(bound, ONE, ONE));
- assertFalse(isSatisfiedBy(bound, ZERO, ONE));
- assertTrue(isSatisfiedBy(bound, TWO, ONE));
- assertFalse(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE));
- assertTrue(isSatisfiedBy(bound, ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertFalse(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertThrowsIRE(bound, null, ONE);
- assertFalse(isSatisfiedBy(bound, ONE, null));
-
- // LTE
- condition = ColumnCondition.condition(definition, new Constants.Value(ONE), Operator.LTE);
- bound = condition.bind(QueryOptions.DEFAULT);
- assertTrue(isSatisfiedBy(bound, ONE, ONE));
- assertFalse(isSatisfiedBy(bound, ZERO, ONE));
- assertTrue(isSatisfiedBy(bound, TWO, ONE));
- assertFalse(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE));
- assertTrue(isSatisfiedBy(bound, ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertTrue(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertThrowsIRE(bound, null, ONE);
- assertFalse(isSatisfiedBy(bound, ONE, null));
-
- // GT
- condition = ColumnCondition.condition(definition, new Constants.Value(ONE), Operator.GT);
- bound = condition.bind(QueryOptions.DEFAULT);
- assertFalse(isSatisfiedBy(bound, ONE, ONE));
- assertTrue(isSatisfiedBy(bound, ZERO, ONE));
- assertFalse(isSatisfiedBy(bound, TWO, ONE));
- assertTrue(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE));
- assertFalse(isSatisfiedBy(bound, ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertFalse(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertThrowsIRE(bound, null, ONE);
- assertFalse(isSatisfiedBy(bound, ONE, null));
-
- // GT
- condition = ColumnCondition.condition(definition, new Constants.Value(ONE), Operator.GTE);
- bound = condition.bind(QueryOptions.DEFAULT);
- assertTrue(isSatisfiedBy(bound, ONE, ONE));
- assertTrue(isSatisfiedBy(bound, ZERO, ONE));
- assertFalse(isSatisfiedBy(bound, TWO, ONE));
- assertTrue(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE));
- assertFalse(isSatisfiedBy(bound, ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertTrue(isSatisfiedBy(bound, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- assertThrowsIRE(bound, null, ONE);
- assertFalse(isSatisfiedBy(bound, ONE, null));
- }
-
- private static List<ByteBuffer> list(ByteBuffer... values)
- {
- return Arrays.asList(values);
- }
-
- private static boolean listAppliesTo(ColumnCondition.CollectionBound bound, List<ByteBuffer> conditionValues, List<ByteBuffer> columnValues)
- {
- CFMetaData cfm = CFMetaData.compile("create table foo(a int PRIMARY KEY, b int, c list<int>)", "ks");
- Map<ByteBuffer, CollectionType> typeMap = new HashMap<>();
- typeMap.put(ByteBufferUtil.bytes("c"), ListType.getInstance(Int32Type.instance, true));
-
- ColumnDefinition definition = ColumnDefinition.regularDef(cfm, ByteBufferUtil.bytes("c"), ListType.getInstance(Int32Type.instance, true));
-
- List<Cell> cells = new ArrayList<>(columnValues.size());
- if (columnValues != null)
- {
- for (int i = 0; i < columnValues.size(); i++)
- {
- ByteBuffer key = Int32Serializer.instance.serialize(i);
- ByteBuffer value = columnValues.get(i);
- cells.add(testCell(definition, value, CellPath.create(key)));
- };
- }
-
- return bound.listAppliesTo(ListType.getInstance(Int32Type.instance, true), cells == null ? null : cells.iterator(), conditionValues, bound.operator);
- }
-
- @Test
- // sets use the same check as lists
- public void testListCollectionBoundAppliesTo() throws InvalidRequestException
- {
- ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", ListType.getInstance(Int32Type.instance, true));
-
- // EQ
- ColumnCondition condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.EQ);
- ColumnCondition.CollectionBound bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertTrue(listAppliesTo(bound, list(ONE), list(ONE)));
- assertTrue(listAppliesTo(bound, list(), list()));
- assertFalse(listAppliesTo(bound, list(ZERO), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ZERO)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ONE, ONE)));
- assertFalse(listAppliesTo(bound, list(ONE, ONE), list(ONE)));
- assertFalse(listAppliesTo(bound, list(), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list()));
-
- assertFalse(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // NEQ
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.NEQ);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertFalse(listAppliesTo(bound, list(ONE), list(ONE)));
- assertFalse(listAppliesTo(bound, list(), list()));
- assertTrue(listAppliesTo(bound, list(ZERO), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ZERO)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ONE, ONE)));
- assertTrue(listAppliesTo(bound, list(ONE, ONE), list(ONE)));
- assertTrue(listAppliesTo(bound, list(), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list()));
-
- assertTrue(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // LT
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.LT);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertFalse(listAppliesTo(bound, list(ONE), list(ONE)));
- assertFalse(listAppliesTo(bound, list(), list()));
- assertFalse(listAppliesTo(bound, list(ZERO), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ZERO)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ONE, ONE)));
- assertTrue(listAppliesTo(bound, list(ONE, ONE), list(ONE)));
- assertFalse(listAppliesTo(bound, list(), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list()));
-
- assertFalse(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // LTE
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.LTE);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertTrue(listAppliesTo(bound, list(ONE), list(ONE)));
- assertTrue(listAppliesTo(bound, list(), list()));
- assertFalse(listAppliesTo(bound, list(ZERO), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ZERO)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ONE, ONE)));
- assertTrue(listAppliesTo(bound, list(ONE, ONE), list(ONE)));
- assertFalse(listAppliesTo(bound, list(), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list()));
-
- assertFalse(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // GT
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.GT);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertFalse(listAppliesTo(bound, list(ONE), list(ONE)));
- assertFalse(listAppliesTo(bound, list(), list()));
- assertTrue(listAppliesTo(bound, list(ZERO), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ZERO)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ONE, ONE)));
- assertFalse(listAppliesTo(bound, list(ONE, ONE), list(ONE)));
- assertTrue(listAppliesTo(bound, list(), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list()));
-
- assertTrue(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // GTE
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.GTE);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertTrue(listAppliesTo(bound, list(ONE), list(ONE)));
- assertTrue(listAppliesTo(bound, list(), list()));
- assertTrue(listAppliesTo(bound, list(ZERO), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ZERO)));
- assertTrue(listAppliesTo(bound, list(ONE), list(ONE, ONE)));
- assertFalse(listAppliesTo(bound, list(ONE, ONE), list(ONE)));
- assertTrue(listAppliesTo(bound, list(), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list()));
-
- assertTrue(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertFalse(listAppliesTo(bound, list(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(listAppliesTo(bound, list(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- }
-
- private static SortedSet<ByteBuffer> set(ByteBuffer... values)
- {
- SortedSet<ByteBuffer> results = new TreeSet<>(Int32Type.instance);
- results.addAll(Arrays.asList(values));
- return results;
- }
-
- private static boolean setAppliesTo(ColumnCondition.CollectionBound bound, Set<ByteBuffer> conditionValues, List<ByteBuffer> columnValues)
- {
- CFMetaData cfm = CFMetaData.compile("create table foo(a int PRIMARY KEY, b int, c set<int>)", "ks");
- Map<ByteBuffer, CollectionType> typeMap = new HashMap<>();
- typeMap.put(ByteBufferUtil.bytes("c"), SetType.getInstance(Int32Type.instance, true));
- ColumnDefinition definition = ColumnDefinition.regularDef(cfm, ByteBufferUtil.bytes("c"), SetType.getInstance(Int32Type.instance, true));
-
- List<Cell> cells = new ArrayList<>(columnValues.size());
- if (columnValues != null)
- {
- for (int i = 0; i < columnValues.size(); i++)
- {
- ByteBuffer key = columnValues.get(i);
- cells.add(testCell(definition, ByteBufferUtil.EMPTY_BYTE_BUFFER, CellPath.create(key)));
- };
- }
-
- return bound.setAppliesTo(SetType.getInstance(Int32Type.instance, true), cells == null ? null : cells.iterator(), conditionValues, bound.operator);
- }
-
- @Test
- public void testSetCollectionBoundAppliesTo() throws InvalidRequestException
- {
- ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", ListType.getInstance(Int32Type.instance, true));
-
- // EQ
- ColumnCondition condition = ColumnCondition.condition(definition, new Sets.Value(set(ONE)), Operator.EQ);
- ColumnCondition.CollectionBound bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertTrue(setAppliesTo(bound, set(ONE), list(ONE)));
- assertTrue(setAppliesTo(bound, set(), list()));
- assertFalse(setAppliesTo(bound, set(ZERO), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ZERO)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ONE, TWO)));
- assertFalse(setAppliesTo(bound, set(ONE, TWO), list(ONE)));
- assertFalse(setAppliesTo(bound, set(), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list()));
-
- assertFalse(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // NEQ
- condition = ColumnCondition.condition(definition, new Sets.Value(set(ONE)), Operator.NEQ);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertFalse(setAppliesTo(bound, set(ONE), list(ONE)));
- assertFalse(setAppliesTo(bound, set(), list()));
- assertTrue(setAppliesTo(bound, set(ZERO), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ZERO)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ONE, TWO)));
- assertTrue(setAppliesTo(bound, set(ONE, TWO), list(ONE)));
- assertTrue(setAppliesTo(bound, set(), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list()));
-
- assertTrue(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // LT
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.LT);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertFalse(setAppliesTo(bound, set(ONE), list(ONE)));
- assertFalse(setAppliesTo(bound, set(), list()));
- assertFalse(setAppliesTo(bound, set(ZERO), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ZERO)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ONE, TWO)));
- assertTrue(setAppliesTo(bound, set(ONE, TWO), list(ONE)));
- assertFalse(setAppliesTo(bound, set(), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list()));
-
- assertFalse(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // LTE
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.LTE);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertTrue(setAppliesTo(bound, set(ONE), list(ONE)));
- assertTrue(setAppliesTo(bound, set(), list()));
- assertFalse(setAppliesTo(bound, set(ZERO), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ZERO)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ONE, TWO)));
- assertTrue(setAppliesTo(bound, set(ONE, TWO), list(ONE)));
- assertFalse(setAppliesTo(bound, set(), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list()));
-
- assertFalse(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // GT
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.GT);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertFalse(setAppliesTo(bound, set(ONE), list(ONE)));
- assertFalse(setAppliesTo(bound, set(), list()));
- assertTrue(setAppliesTo(bound, set(ZERO), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ZERO)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ONE, TWO)));
- assertFalse(setAppliesTo(bound, set(ONE, TWO), list(ONE)));
- assertTrue(setAppliesTo(bound, set(), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list()));
-
- assertTrue(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // GTE
- condition = ColumnCondition.condition(definition, new Lists.Value(Arrays.asList(ONE)), Operator.GTE);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
- assertTrue(setAppliesTo(bound, set(ONE), list(ONE)));
- assertTrue(setAppliesTo(bound, set(), list()));
- assertTrue(setAppliesTo(bound, set(ZERO), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ZERO)));
- assertTrue(setAppliesTo(bound, set(ONE), list(ONE, TWO)));
- assertFalse(setAppliesTo(bound, set(ONE, TWO), list(ONE)));
- assertTrue(setAppliesTo(bound, set(), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list()));
-
- assertTrue(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ONE)));
- assertFalse(setAppliesTo(bound, set(ONE), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(setAppliesTo(bound, set(ByteBufferUtil.EMPTY_BYTE_BUFFER), list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- }
-
- // values should be a list of key, value, key, value, ...
- private static Map<ByteBuffer, ByteBuffer> map(ByteBuffer... values)
- {
- Map<ByteBuffer, ByteBuffer> map = new TreeMap<>();
- for (int i = 0; i < values.length; i += 2)
- map.put(values[i], values[i + 1]);
-
- return map;
- }
-
- private static boolean mapAppliesTo(ColumnCondition.CollectionBound bound, Map<ByteBuffer, ByteBuffer> conditionValues, Map<ByteBuffer, ByteBuffer> columnValues)
- {
- CFMetaData cfm = CFMetaData.compile("create table foo(a int PRIMARY KEY, b map<int, int>)", "ks");
- Map<ByteBuffer, CollectionType> typeMap = new HashMap<>();
- typeMap.put(ByteBufferUtil.bytes("b"), MapType.getInstance(Int32Type.instance, Int32Type.instance, true));
- ColumnDefinition definition = ColumnDefinition.regularDef(cfm, ByteBufferUtil.bytes("b"), MapType.getInstance(Int32Type.instance, Int32Type.instance, true));
-
- List<Cell> cells = new ArrayList<>(columnValues.size());
- if (columnValues != null)
- {
- for (Map.Entry<ByteBuffer, ByteBuffer> entry : columnValues.entrySet())
- cells.add(testCell(definition, entry.getValue(), CellPath.create(entry.getKey())));
- }
-
- return bound.mapAppliesTo(MapType.getInstance(Int32Type.instance, Int32Type.instance, true), cells.iterator(), conditionValues, bound.operator);
- }
-
- @Test
- public void testMapCollectionBoundIsSatisfiedByValue() throws InvalidRequestException
- {
- ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", ListType.getInstance(Int32Type.instance, true));
-
- Map<ByteBuffer, ByteBuffer> placeholderMap = new TreeMap<>();
- placeholderMap.put(ONE, ONE);
- Maps.Value placeholder = new Maps.Value(placeholderMap);
-
- // EQ
- ColumnCondition condition = ColumnCondition.condition(definition, placeholder, Operator.EQ);
- ColumnCondition.CollectionBound bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
-
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(), map()));
- assertFalse(mapAppliesTo(bound, map(ZERO, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ZERO, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ZERO), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ZERO)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE, TWO, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE, TWO, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map()));
-
- assertFalse(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // NEQ
- condition = ColumnCondition.condition(definition, placeholder, Operator.NEQ);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
-
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(), map()));
- assertTrue(mapAppliesTo(bound, map(ZERO, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ZERO, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ZERO), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ZERO)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE, TWO, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE, TWO, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map()));
-
- assertTrue(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // LT
- condition = ColumnCondition.condition(definition, placeholder, Operator.LT);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
-
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(), map()));
- assertFalse(mapAppliesTo(bound, map(ZERO, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ZERO, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ZERO), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ZERO)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE, TWO, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE, TWO, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map()));
-
- assertFalse(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // LTE
- condition = ColumnCondition.condition(definition, placeholder, Operator.LTE);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
-
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(), map()));
- assertFalse(mapAppliesTo(bound, map(ZERO, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ZERO, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ZERO), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ZERO)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE, TWO, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE, TWO, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map()));
-
- assertFalse(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // GT
- condition = ColumnCondition.condition(definition, placeholder, Operator.GT);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
-
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(), map()));
- assertTrue(mapAppliesTo(bound, map(ZERO, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ZERO, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ZERO), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ZERO)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE, TWO, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE, TWO, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map()));
-
- assertTrue(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertFalse(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
-
- // GTE
- condition = ColumnCondition.condition(definition, placeholder, Operator.GTE);
- bound = (ColumnCondition.CollectionBound) condition.bind(QueryOptions.DEFAULT);
-
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(), map()));
- assertTrue(mapAppliesTo(bound, map(ZERO, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ZERO, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ZERO), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ZERO)));
- assertTrue(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ONE, TWO, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE, TWO, ONE), map(ONE, ONE)));
- assertTrue(mapAppliesTo(bound, map(), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map()));
-
- assertTrue(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ONE)));
- assertFalse(mapAppliesTo(bound, map(ONE, ONE), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- assertTrue(mapAppliesTo(bound, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
- assertTrue(mapAppliesTo(bound, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
- }
-}
[5/7] cassandra git commit: Refactor ColumnCondition
Posted by bl...@apache.org.
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/test/unit/org/apache/cassandra/cql3/conditions/ColumnConditionTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/conditions/ColumnConditionTest.java b/test/unit/org/apache/cassandra/cql3/conditions/ColumnConditionTest.java
new file mode 100644
index 0000000..5822027
--- /dev/null
+++ b/test/unit/org/apache/cassandra/cql3/conditions/ColumnConditionTest.java
@@ -0,0 +1,557 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.cql3.conditions;
+
+import java.nio.ByteBuffer;
+import java.util.*;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.cql3.Constants.Value;
+import org.apache.cassandra.cql3.conditions.ColumnCondition;
+import org.apache.cassandra.db.Clustering;
+import org.apache.cassandra.db.marshal.Int32Type;
+import org.apache.cassandra.db.marshal.ListType;
+import org.apache.cassandra.db.marshal.MapType;
+import org.apache.cassandra.db.marshal.SetType;
+import org.apache.cassandra.db.rows.*;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.serializers.TimeUUIDSerializer;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.apache.cassandra.utils.UUIDGen;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import static org.apache.cassandra.cql3.Operator.*;
+import static org.apache.cassandra.utils.ByteBufferUtil.EMPTY_BYTE_BUFFER;
+
+
+public class ColumnConditionTest
+{
+ public static final ByteBuffer ZERO = Int32Type.instance.fromString("0");
+ public static final ByteBuffer ONE = Int32Type.instance.fromString("1");
+ public static final ByteBuffer TWO = Int32Type.instance.fromString("2");
+
+ private static Row newRow(ColumnDefinition definition, ByteBuffer value)
+ {
+ BufferCell cell = new BufferCell(definition, 0L, Cell.NO_TTL, Cell.NO_DELETION_TIME, value, null);
+ return BTreeRow.singleCellRow(Clustering.EMPTY, cell);
+ }
+
+ private static Row newRow(ColumnDefinition definition, List<ByteBuffer> values)
+ {
+ Row.Builder builder = BTreeRow.sortedBuilder();
+ builder.newRow(Clustering.EMPTY);
+ long now = System.currentTimeMillis();
+ if (values != null)
+ {
+ for (int i = 0, m = values.size(); i < m; i++)
+ {
+ UUID uuid = UUIDGen.getTimeUUID(now, i);
+ ByteBuffer key = TimeUUIDSerializer.instance.serialize(uuid);
+ ByteBuffer value = values.get(i);
+ BufferCell cell = new BufferCell(definition,
+ 0L,
+ Cell.NO_TTL,
+ Cell.NO_DELETION_TIME,
+ value,
+ CellPath.create(key));
+ builder.addCell(cell);
+ }
+ }
+ return builder.build();
+ }
+
+ private static Row newRow(ColumnDefinition definition, SortedSet<ByteBuffer> values)
+ {
+ Row.Builder builder = BTreeRow.sortedBuilder();
+ builder.newRow(Clustering.EMPTY);
+ if (values != null)
+ {
+ for (ByteBuffer value : values)
+ {
+ BufferCell cell = new BufferCell(definition,
+ 0L,
+ Cell.NO_TTL,
+ Cell.NO_DELETION_TIME,
+ ByteBufferUtil.EMPTY_BYTE_BUFFER,
+ CellPath.create(value));
+ builder.addCell(cell);
+ }
+ }
+ return builder.build();
+ }
+
+ private static Row newRow(ColumnDefinition definition, Map<ByteBuffer, ByteBuffer> values)
+ {
+ Row.Builder builder = BTreeRow.sortedBuilder();
+ builder.newRow(Clustering.EMPTY);
+ if (values != null)
+ {
+ for (Map.Entry<ByteBuffer, ByteBuffer> entry : values.entrySet())
+ {
+ BufferCell cell = new BufferCell(definition,
+ 0L,
+ Cell.NO_TTL,
+ Cell.NO_DELETION_TIME,
+ entry.getValue(),
+ CellPath.create(entry.getKey()));
+ builder.addCell(cell);
+ }
+ }
+ return builder.build();
+ }
+
+ private static boolean conditionApplies(ByteBuffer rowValue, Operator op, ByteBuffer conditionValue)
+ {
+ ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", Int32Type.instance);
+ ColumnCondition condition = ColumnCondition.condition(definition, op, Terms.of(new Constants.Value(conditionValue)));
+ ColumnCondition.Bound bound = condition.bind(QueryOptions.DEFAULT);
+ return bound.appliesTo(newRow(definition, rowValue));
+ }
+
+ private static boolean conditionApplies(List<ByteBuffer> rowValue, Operator op, List<ByteBuffer> conditionValue)
+ {
+ ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", ListType.getInstance(Int32Type.instance, true));
+ ColumnCondition condition = ColumnCondition.condition(definition, op, Terms.of(new Lists.Value(conditionValue)));
+ ColumnCondition.Bound bound = condition.bind(QueryOptions.DEFAULT);
+ return bound.appliesTo(newRow(definition, rowValue));
+ }
+
+ private static boolean conditionApplies(SortedSet<ByteBuffer> rowValue, Operator op, SortedSet<ByteBuffer> conditionValue)
+ {
+ ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", SetType.getInstance(Int32Type.instance, true));
+ ColumnCondition condition = ColumnCondition.condition(definition, op, Terms.of(new Sets.Value(conditionValue)));
+ ColumnCondition.Bound bound = condition.bind(QueryOptions.DEFAULT);
+ return bound.appliesTo(newRow(definition, rowValue));
+ }
+
+ private static boolean conditionApplies(Map<ByteBuffer, ByteBuffer> rowValue, Operator op, Map<ByteBuffer, ByteBuffer> conditionValue)
+ {
+ ColumnDefinition definition = ColumnDefinition.regularDef("ks", "cf", "c", MapType.getInstance(Int32Type.instance, Int32Type.instance, true));
+ ColumnCondition condition = ColumnCondition.condition(definition, op, Terms.of(new Maps.Value(conditionValue)));
+ ColumnCondition.Bound bound = condition.bind(QueryOptions.DEFAULT);
+ return bound.appliesTo(newRow(definition, rowValue));
+ }
+
+ @FunctionalInterface
+ public interface CheckedFunction {
+ void apply();
+ }
+
+ private static void assertThrowsIRE(CheckedFunction runnable, String errorMessage)
+ {
+ try
+ {
+ runnable.apply();
+ fail("Expected InvalidRequestException was not thrown");
+ } catch (InvalidRequestException e)
+ {
+ Assert.assertTrue("Expected error message to contain '" + errorMessage + "', but got '" + e.getMessage() + "'",
+ e.getMessage().contains(errorMessage));
+ }
+ }
+
+ @Test
+ public void testSimpleBoundIsSatisfiedByValue() throws InvalidRequestException
+ {
+ // EQ
+ assertTrue(conditionApplies(ONE, EQ, ONE));
+ assertFalse(conditionApplies(TWO, EQ, ONE));
+ assertFalse(conditionApplies(ONE, EQ, TWO));
+ assertFalse(conditionApplies(ONE, EQ, EMPTY_BYTE_BUFFER));
+ assertFalse(conditionApplies(EMPTY_BYTE_BUFFER, EQ, ONE));
+ assertTrue(conditionApplies(EMPTY_BYTE_BUFFER, EQ, EMPTY_BYTE_BUFFER));
+ assertFalse(conditionApplies(ONE, EQ, null));
+ assertFalse(conditionApplies(null, EQ, ONE));
+ assertTrue(conditionApplies((ByteBuffer) null, EQ, (ByteBuffer) null));
+
+ // NEQ
+ assertFalse(conditionApplies(ONE, NEQ, ONE));
+ assertTrue(conditionApplies(TWO, NEQ, ONE));
+ assertTrue(conditionApplies(ONE, NEQ, TWO));
+ assertTrue(conditionApplies(ONE, NEQ, EMPTY_BYTE_BUFFER));
+ assertTrue(conditionApplies(EMPTY_BYTE_BUFFER, NEQ, ONE));
+ assertFalse(conditionApplies(EMPTY_BYTE_BUFFER, NEQ, EMPTY_BYTE_BUFFER));
+ assertTrue(conditionApplies(ONE, NEQ, null));
+ assertTrue(conditionApplies(null, NEQ, ONE));
+ assertFalse(conditionApplies((ByteBuffer) null, NEQ, (ByteBuffer) null));
+
+ // LT
+ assertFalse(conditionApplies(ONE, LT, ONE));
+ assertFalse(conditionApplies(TWO, LT, ONE));
+ assertTrue(conditionApplies(ONE, LT, TWO));
+ assertFalse(conditionApplies(ONE, LT, EMPTY_BYTE_BUFFER));
+ assertTrue(conditionApplies(EMPTY_BYTE_BUFFER, LT, ONE));
+ assertFalse(conditionApplies(EMPTY_BYTE_BUFFER, LT, EMPTY_BYTE_BUFFER));
+ assertThrowsIRE(() -> conditionApplies(ONE, LT, null), "Invalid comparison with null for operator \"<\"");
+ assertFalse(conditionApplies(null, LT, ONE));
+
+ // LTE
+ assertTrue(conditionApplies(ONE, LTE, ONE));
+ assertFalse(conditionApplies(TWO, LTE, ONE));
+ assertTrue(conditionApplies(ONE, LTE, TWO));
+ assertFalse(conditionApplies(ONE, LTE, EMPTY_BYTE_BUFFER));
+ assertTrue(conditionApplies(EMPTY_BYTE_BUFFER, LTE, ONE));
+ assertTrue(conditionApplies(EMPTY_BYTE_BUFFER, LTE, EMPTY_BYTE_BUFFER));
+ assertThrowsIRE(() -> conditionApplies(ONE, LTE, null), "Invalid comparison with null for operator \"<=\"");
+ assertFalse(conditionApplies(null, LTE, ONE));
+
+ // GT
+ assertFalse(conditionApplies(ONE, GT, ONE));
+ assertTrue(conditionApplies(TWO, GT, ONE));
+ assertFalse(conditionApplies(ONE, GT, TWO));
+ assertTrue(conditionApplies(ONE, GT, EMPTY_BYTE_BUFFER));
+ assertFalse(conditionApplies(EMPTY_BYTE_BUFFER, GT, ONE));
+ assertFalse(conditionApplies(EMPTY_BYTE_BUFFER, GT, EMPTY_BYTE_BUFFER));
+ assertThrowsIRE(() -> conditionApplies(ONE, GT, null), "Invalid comparison with null for operator \">\"");
+ assertFalse(conditionApplies(null, GT, ONE));
+
+ // GTE
+ assertTrue(conditionApplies(ONE, GTE, ONE));
+ assertTrue(conditionApplies(TWO, GTE, ONE));
+ assertFalse(conditionApplies(ONE, GTE, TWO));
+ assertTrue(conditionApplies(ONE, GTE, EMPTY_BYTE_BUFFER));
+ assertFalse(conditionApplies(EMPTY_BYTE_BUFFER, GTE, ONE));
+ assertTrue(conditionApplies(EMPTY_BYTE_BUFFER, GTE, EMPTY_BYTE_BUFFER));
+ assertThrowsIRE(() -> conditionApplies(ONE, GTE, null), "Invalid comparison with null for operator \">=\"");
+ assertFalse(conditionApplies(null, GTE, ONE));
+ }
+
+ private static List<ByteBuffer> list(ByteBuffer... values)
+ {
+ return Arrays.asList(values);
+ }
+
+ @Test
+ // sets use the same check as lists
+ public void testListCollectionBoundAppliesTo() throws InvalidRequestException
+ {
+ // EQ
+ assertTrue(conditionApplies(list(ONE), EQ, list(ONE)));
+ assertTrue(conditionApplies(list(), EQ, list()));
+ assertFalse(conditionApplies(list(ONE), EQ, list(ZERO)));
+ assertFalse(conditionApplies(list(ZERO), EQ, list(ONE)));
+ assertFalse(conditionApplies(list(ONE, ONE), EQ, list(ONE)));
+ assertFalse(conditionApplies(list(ONE), EQ, list(ONE, ONE)));
+ assertFalse(conditionApplies(list(ONE), EQ, list()));
+ assertFalse(conditionApplies(list(), EQ, list(ONE)));
+
+ assertFalse(conditionApplies(list(ONE), EQ, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), EQ, list(ONE)));
+ assertTrue(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), EQ, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // NEQ
+ assertFalse(conditionApplies(list(ONE), NEQ, list(ONE)));
+ assertFalse(conditionApplies(list(), NEQ, list()));
+ assertTrue(conditionApplies(list(ONE), NEQ, list(ZERO)));
+ assertTrue(conditionApplies(list(ZERO), NEQ, list(ONE)));
+ assertTrue(conditionApplies(list(ONE, ONE), NEQ, list(ONE)));
+ assertTrue(conditionApplies(list(ONE), NEQ, list(ONE, ONE)));
+ assertTrue(conditionApplies(list(ONE), NEQ, list()));
+ assertTrue(conditionApplies(list(), NEQ, list(ONE)));
+
+ assertTrue(conditionApplies(list(ONE), NEQ, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), NEQ, list(ONE)));
+ assertFalse(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), NEQ, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // LT
+ assertFalse(conditionApplies(list(ONE), LT, list(ONE)));
+ assertFalse(conditionApplies(list(), LT, list()));
+ assertFalse(conditionApplies(list(ONE), LT, list(ZERO)));
+ assertTrue(conditionApplies(list(ZERO), LT, list(ONE)));
+ assertFalse(conditionApplies(list(ONE, ONE), LT, list(ONE)));
+ assertTrue(conditionApplies(list(ONE), LT, list(ONE, ONE)));
+ assertFalse(conditionApplies(list(ONE), LT, list()));
+ assertTrue(conditionApplies(list(), LT, list(ONE)));
+
+ assertFalse(conditionApplies(list(ONE), LT, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), LT, list(ONE)));
+ assertFalse(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), LT, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // LTE
+ assertTrue(conditionApplies(list(ONE), LTE, list(ONE)));
+ assertTrue(conditionApplies(list(), LTE, list()));
+ assertFalse(conditionApplies(list(ONE), LTE, list(ZERO)));
+ assertTrue(conditionApplies(list(ZERO), LTE, list(ONE)));
+ assertFalse(conditionApplies(list(ONE, ONE), LTE, list(ONE)));
+ assertTrue(conditionApplies(list(ONE), LTE, list(ONE, ONE)));
+ assertFalse(conditionApplies(list(ONE), LTE, list()));
+ assertTrue(conditionApplies(list(), LTE, list(ONE)));
+
+ assertFalse(conditionApplies(list(ONE), LTE, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), LTE, list(ONE)));
+ assertTrue(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), LTE, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // GT
+ assertFalse(conditionApplies(list(ONE), GT, list(ONE)));
+ assertFalse(conditionApplies(list(), GT, list()));
+ assertTrue(conditionApplies(list(ONE), GT, list(ZERO)));
+ assertFalse(conditionApplies(list(ZERO), GT, list(ONE)));
+ assertTrue(conditionApplies(list(ONE, ONE), GT, list(ONE)));
+ assertFalse(conditionApplies(list(ONE), GT, list(ONE, ONE)));
+ assertTrue(conditionApplies(list(ONE), GT, list()));
+ assertFalse(conditionApplies(list(), GT, list(ONE)));
+
+ assertTrue(conditionApplies(list(ONE), GT, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), GT, list(ONE)));
+ assertFalse(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), GT, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // GTE
+ assertTrue(conditionApplies(list(ONE), GTE, list(ONE)));
+ assertTrue(conditionApplies(list(), GTE, list()));
+ assertTrue(conditionApplies(list(ONE), GTE, list(ZERO)));
+ assertFalse(conditionApplies(list(ZERO), GTE, list(ONE)));
+ assertTrue(conditionApplies(list(ONE, ONE), GTE, list(ONE)));
+ assertFalse(conditionApplies(list(ONE), GTE, list(ONE, ONE)));
+ assertTrue(conditionApplies(list(ONE), GTE, list()));
+ assertFalse(conditionApplies(list(), GTE, list(ONE)));
+
+ assertTrue(conditionApplies(list(ONE), GTE, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), GTE, list(ONE)));
+ assertTrue(conditionApplies(list(ByteBufferUtil.EMPTY_BYTE_BUFFER), GTE, list(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ }
+
+ private static SortedSet<ByteBuffer> set(ByteBuffer... values)
+ {
+ SortedSet<ByteBuffer> results = new TreeSet<>(Int32Type.instance);
+ results.addAll(Arrays.asList(values));
+ return results;
+ }
+
+ @Test
+ public void testSetCollectionBoundAppliesTo() throws InvalidRequestException
+ {
+ // EQ
+ assertTrue(conditionApplies(set(ONE), EQ, set(ONE)));
+ assertTrue(conditionApplies(set(), EQ, set()));
+ assertFalse(conditionApplies(set(ONE), EQ, set(ZERO)));
+ assertFalse(conditionApplies(set(ZERO), EQ, set(ONE)));
+ assertFalse(conditionApplies(set(ONE, TWO), EQ, set(ONE)));
+ assertFalse(conditionApplies(set(ONE), EQ, set(ONE, TWO)));
+ assertFalse(conditionApplies(set(ONE), EQ, set()));
+ assertFalse(conditionApplies(set(), EQ, set(ONE)));
+
+ assertFalse(conditionApplies(set(ONE), EQ, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), EQ, set(ONE)));
+ assertTrue(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), EQ, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // NEQ
+ assertFalse(conditionApplies(set(ONE), NEQ, set(ONE)));
+ assertFalse(conditionApplies(set(), NEQ, set()));
+ assertTrue(conditionApplies(set(ONE), NEQ, set(ZERO)));
+ assertTrue(conditionApplies(set(ZERO), NEQ, set(ONE)));
+ assertTrue(conditionApplies(set(ONE, TWO), NEQ, set(ONE)));
+ assertTrue(conditionApplies(set(ONE), NEQ, set(ONE, TWO)));
+ assertTrue(conditionApplies(set(ONE), NEQ, set()));
+ assertTrue(conditionApplies(set(), NEQ, set(ONE)));
+
+ assertTrue(conditionApplies(set(ONE), NEQ, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), NEQ, set(ONE)));
+ assertFalse(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), NEQ, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // LT
+ assertFalse(conditionApplies(set(ONE), LT, set(ONE)));
+ assertFalse(conditionApplies(set(), LT, set()));
+ assertFalse(conditionApplies(set(ONE), LT, set(ZERO)));
+ assertTrue(conditionApplies(set(ZERO), LT, set(ONE)));
+ assertFalse(conditionApplies(set(ONE, TWO), LT, set(ONE)));
+ assertTrue(conditionApplies(set(ONE), LT, set(ONE, TWO)));
+ assertFalse(conditionApplies(set(ONE), LT, set()));
+ assertTrue(conditionApplies(set(), LT, set(ONE)));
+
+ assertFalse(conditionApplies(set(ONE), LT, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), LT, set(ONE)));
+ assertFalse(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), LT, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // LTE
+ assertTrue(conditionApplies(set(ONE), LTE, set(ONE)));
+ assertTrue(conditionApplies(set(), LTE, set()));
+ assertFalse(conditionApplies(set(ONE), LTE, set(ZERO)));
+ assertTrue(conditionApplies(set(ZERO), LTE, set(ONE)));
+ assertFalse(conditionApplies(set(ONE, TWO), LTE, set(ONE)));
+ assertTrue(conditionApplies(set(ONE), LTE, set(ONE, TWO)));
+ assertFalse(conditionApplies(set(ONE), LTE, set()));
+ assertTrue(conditionApplies(set(), LTE, set(ONE)));
+
+ assertFalse(conditionApplies(set(ONE), LTE, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), LTE, set(ONE)));
+ assertTrue(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), LTE, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // GT
+ assertFalse(conditionApplies(set(ONE), GT, set(ONE)));
+ assertFalse(conditionApplies(set(), GT, set()));
+ assertTrue(conditionApplies(set(ONE), GT, set(ZERO)));
+ assertFalse(conditionApplies(set(ZERO), GT, set(ONE)));
+ assertTrue(conditionApplies(set(ONE, TWO), GT, set(ONE)));
+ assertFalse(conditionApplies(set(ONE), GT, set(ONE, TWO)));
+ assertTrue(conditionApplies(set(ONE), GT, set()));
+ assertFalse(conditionApplies(set(), GT, set(ONE)));
+
+ assertTrue(conditionApplies(set(ONE), GT, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), GT, set(ONE)));
+ assertFalse(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), GT, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // GTE
+ assertTrue(conditionApplies(set(ONE), GTE, set(ONE)));
+ assertTrue(conditionApplies(set(), GTE, set()));
+ assertTrue(conditionApplies(set(ONE), GTE, set(ZERO)));
+ assertFalse(conditionApplies(set(ZERO), GTE, set(ONE)));
+ assertTrue(conditionApplies(set(ONE, TWO), GTE, set(ONE)));
+ assertFalse(conditionApplies(set(ONE), GTE, set(ONE, TWO)));
+ assertTrue(conditionApplies(set(ONE), GTE, set()));
+ assertFalse(conditionApplies(set(), GTE, set(ONE)));
+
+ assertTrue(conditionApplies(set(ONE), GTE, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), GTE, set(ONE)));
+ assertTrue(conditionApplies(set(ByteBufferUtil.EMPTY_BYTE_BUFFER), GTE, set(ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ }
+
+ // values should be a list of key, value, key, value, ...
+ private static Map<ByteBuffer, ByteBuffer> map(ByteBuffer... values)
+ {
+ Map<ByteBuffer, ByteBuffer> map = new TreeMap<>();
+ for (int i = 0; i < values.length; i += 2)
+ map.put(values[i], values[i + 1]);
+
+ return map;
+ }
+
+ @Test
+ public void testMapCollectionBoundIsSatisfiedByValue() throws InvalidRequestException
+ {
+ // EQ
+ assertTrue(conditionApplies(map(ONE, ONE), EQ, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(), EQ, map()));
+ assertFalse(conditionApplies(map(ONE, ONE), EQ, map(ZERO, ONE)));
+ assertFalse(conditionApplies(map(ZERO, ONE), EQ, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), EQ, map(ONE, ZERO)));
+ assertFalse(conditionApplies(map(ONE, ZERO), EQ, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE, TWO, ONE), EQ, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), EQ, map(ONE, ONE, TWO, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), EQ, map()));
+ assertFalse(conditionApplies(map(), EQ, map(ONE, ONE)));
+
+ assertFalse(conditionApplies(map(ONE, ONE), EQ, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertFalse(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), EQ, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), EQ, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), EQ, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), EQ, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertTrue(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), EQ, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // NEQ
+ assertFalse(conditionApplies(map(ONE, ONE), NEQ, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(), NEQ, map()));
+ assertTrue(conditionApplies(map(ONE, ONE), NEQ, map(ZERO, ONE)));
+ assertTrue(conditionApplies(map(ZERO, ONE), NEQ, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), NEQ, map(ONE, ZERO)));
+ assertTrue(conditionApplies(map(ONE, ZERO), NEQ, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE, TWO, ONE), NEQ, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), NEQ, map(ONE, ONE, TWO, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), NEQ, map()));
+ assertTrue(conditionApplies(map(), NEQ, map(ONE, ONE)));
+
+ assertTrue(conditionApplies(map(ONE, ONE), NEQ, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertTrue(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), NEQ, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), NEQ, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), NEQ, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), NEQ, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertFalse(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), NEQ, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // LT
+ assertFalse(conditionApplies(map(ONE, ONE), LT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(), LT, map()));
+ assertFalse(conditionApplies(map(ONE, ONE), LT, map(ZERO, ONE)));
+ assertTrue(conditionApplies(map(ZERO, ONE), LT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), LT, map(ONE, ZERO)));
+ assertTrue(conditionApplies(map(ONE, ZERO), LT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE, TWO, ONE), LT, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), LT, map(ONE, ONE, TWO, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), LT, map()));
+ assertTrue(conditionApplies(map(), LT, map(ONE, ONE)));
+
+ assertFalse(conditionApplies(map(ONE, ONE), LT, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertTrue(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), LT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), LT, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), LT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), LT, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertFalse(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), LT, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // LTE
+ assertTrue(conditionApplies(map(ONE, ONE), LTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(), LTE, map()));
+ assertFalse(conditionApplies(map(ONE, ONE), LTE, map(ZERO, ONE)));
+ assertTrue(conditionApplies(map(ZERO, ONE), LTE, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), LTE, map(ONE, ZERO)));
+ assertTrue(conditionApplies(map(ONE, ZERO), LTE, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE, TWO, ONE), LTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), LTE, map(ONE, ONE, TWO, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), LTE, map()));
+ assertTrue(conditionApplies(map(), LTE, map(ONE, ONE)));
+
+ assertFalse(conditionApplies(map(ONE, ONE), LTE, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertTrue(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), LTE, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), LTE, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertTrue(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), LTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), LTE, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertTrue(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), LTE, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // GT
+ assertFalse(conditionApplies(map(ONE, ONE), GT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(), GT, map()));
+ assertTrue(conditionApplies(map(ONE, ONE), GT, map(ZERO, ONE)));
+ assertFalse(conditionApplies(map(ZERO, ONE), GT, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), GT, map(ONE, ZERO)));
+ assertFalse(conditionApplies(map(ONE, ZERO), GT, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE, TWO, ONE), GT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), GT, map(ONE, ONE, TWO, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), GT, map()));
+ assertFalse(conditionApplies(map(), GT, map(ONE, ONE)));
+
+ assertTrue(conditionApplies(map(ONE, ONE), GT, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertFalse(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), GT, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), GT, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), GT, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), GT, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertFalse(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), GT, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+
+ // GTE
+ assertTrue(conditionApplies(map(ONE, ONE), GTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(), GTE, map()));
+ assertTrue(conditionApplies(map(ONE, ONE), GTE, map(ZERO, ONE)));
+ assertFalse(conditionApplies(map(ZERO, ONE), GTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), GTE, map(ONE, ZERO)));
+ assertFalse(conditionApplies(map(ONE, ZERO), GTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE, TWO, ONE), GTE, map(ONE, ONE)));
+ assertFalse(conditionApplies(map(ONE, ONE), GTE, map(ONE, ONE, TWO, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), GTE, map()));
+ assertFalse(conditionApplies(map(), GTE, map(ONE, ONE)));
+
+ assertTrue(conditionApplies(map(ONE, ONE), GTE, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertFalse(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), GTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ONE, ONE), GTE, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ assertFalse(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), GTE, map(ONE, ONE)));
+ assertTrue(conditionApplies(map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE), GTE, map(ByteBufferUtil.EMPTY_BYTE_BUFFER, ONE)));
+ assertTrue(conditionApplies(map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER), GTE, map(ONE, ByteBufferUtil.EMPTY_BYTE_BUFFER)));
+ }
+}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/e71a49e8/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
index 1f01931..b5f8b6c 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
@@ -18,6 +18,9 @@
package org.apache.cassandra.cql3.validation.operations;
+import java.nio.ByteBuffer;
+import java.util.List;
+
import org.junit.Test;
import org.apache.cassandra.config.SchemaConstants;
@@ -61,7 +64,10 @@ public class InsertUpdateIfConditionTest extends CQLTester
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 = ?", unset());
// Shouldn't apply
+
assertRows(execute("UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 = ?", 4), row(false));
+ assertRows(execute("UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?)", 1, 2), row(false));
+ assertRows(execute("UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 IN ?", list(1, 2)), row(false));
assertRows(execute("UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF EXISTS"), row(false));
// Should apply
@@ -77,11 +83,15 @@ public class InsertUpdateIfConditionTest extends CQLTester
// Should apply (note: we want v2 before v1 in the statement order to exercise #5786)
assertRows(execute("UPDATE %s SET v2 = 'bar', v1 = 3 WHERE k = 0 IF v1 = ?", 2), row(true));
+ assertRows(execute("UPDATE %s SET v2 = 'bar', v1 = 2 WHERE k = 0 IF v1 IN (?, ?)", 2, 3), row(true));
+ assertRows(execute("UPDATE %s SET v2 = 'bar', v1 = 3 WHERE k = 0 IF v1 IN ?", list(2, 3)), row(true));
+ assertRows(execute("UPDATE %s SET v2 = 'bar', v1 = 3 WHERE k = 0 IF v1 IN ?", list(1, null, 3)), row(true));
assertRows(execute("UPDATE %s SET v2 = 'bar', v1 = 3 WHERE k = 0 IF EXISTS"), row(true));
assertRows(execute("SELECT * FROM %s"), row(0, 3, "bar", null));
// Shouldn't apply, only one condition is ok
assertRows(execute("UPDATE %s SET v1 = 5, v2 = 'foobar' WHERE k = 0 IF v1 = ? AND v2 = ?", 3, "foo"), row(false, 3, "bar"));
+ assertRows(execute("UPDATE %s SET v1 = 5, v2 = 'foobar' WHERE k = 0 IF v1 = 3 AND v2 = 'foo'"), row(false, 3, "bar"));
assertRows(execute("SELECT * FROM %s"), row(0, 3, "bar", null));
// Should apply
@@ -145,6 +155,7 @@ public class InsertUpdateIfConditionTest extends CQLTester
assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?, ?)", 0, 1, 2), row(true));
assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN ?", list(142, 276)), row(false, 2));
assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN ()"), row(false, 2));
+ assertRows(execute("UPDATE %s SET v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?)", unset(), 1), row(false, 2));
assertInvalidMessage("Invalid 'unset' value in condition",
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 < ?", unset());
@@ -156,8 +167,6 @@ public class InsertUpdateIfConditionTest extends CQLTester
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 >= ?", unset());
assertInvalidMessage("Invalid 'unset' value in condition",
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 != ?", unset());
- assertInvalidMessage("Invalid 'unset' value in condition",
- "UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?)", unset(), 1);
}
/**
@@ -1957,4 +1966,183 @@ public class InsertUpdateIfConditionTest extends CQLTester
+ "UPDATE %1$s SET v2 = 'bar' WHERE k = 1 AND t = 0 IF v1 = 'foo'; "
+ "APPLY BATCH");
}
+
+ @Test
+ public void testInMarkerWithUDTs() throws Throwable
+ {
+ String typename = createType("CREATE TYPE %s (a int, b text)");
+ String myType = KEYSPACE + '.' + typename;
+
+ for (boolean frozen : new boolean[] {false, true})
+ {
+ createTable(String.format("CREATE TABLE %%s (k int PRIMARY KEY, v %s)",
+ frozen
+ ? "frozen<" + myType + ">"
+ : myType));
+
+ Object v = userType("a", 0, "b", "abc");
+ execute("INSERT INTO %s (k, v) VALUES (0, ?)", v);
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType("a", 1, "b", "abc"), userType("a", 0, "b", "ac")),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType("a", 1, "b", "abc"), null),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", null, null),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType("a", 1, "b", "abc"), unset()),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", unset(), unset()),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", list(userType("a", 1, "b", "abc"), userType("a", 0, "b", "ac"))),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v.a IN (?, ?)", 1, 2),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v.a IN (?, ?)", 1, null),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v.a IN ?", list(1, 2)),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v.a IN (?, ?)", 1, unset()),
+ row(false, v));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType("a", 0, "b", "abc"), userType("a", 0, "b", "ac")),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 1, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType("a", 0, "b", "bc"), null),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'abc'} WHERE k = 0 IF v IN ?", list(userType("a", 1, "b", "bc"), userType("a", 0, "b", "ac"))),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 1, b: 'bc'} WHERE k = 0 IF v IN (?, ?, ?)", userType("a", 1, "b", "bc"), unset(), userType("a", 0, "b", "abc")),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v.a IN (?, ?)", 1, 0),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'abc'} WHERE k = 0 IF v.a IN (?, ?)", 0, null),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v.a IN ?", list(0, 1)),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'abc'} WHERE k = 0 IF v.a IN (?, ?, ?)", 1, unset(), 0),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", unset());
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v.a IN ?", unset());
+ }
+ }
+
+ @Test
+ public void testInMarkerWithLists() throws Throwable
+ {
+ for (boolean frozen : new boolean[]{false, true})
+ {
+ createTable(String.format("CREATE TABLE %%s (k int PRIMARY KEY, l %s)",
+ frozen
+ ? "frozen<list<text>>"
+ : "list<text>"));
+
+ execute("INSERT INTO %s(k, l) VALUES (0, ['foo', 'bar', 'foobar'])");
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), list("bar", "foobar")),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), unset()),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", null, null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", list(list("foo", "foobar"), list("bar", "foobar"))),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, list("foo", "foobar")),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", "foobar"),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", unset()),
+ row(false, list("foo", "bar", "foobar")));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "bar", "foobar"), list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l IN (?, ?, ?)", list("foo", "bar", "foobar"), null, list("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?, ?)", list("foo", "bar", "foobar"), unset(), list("foo", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l IN (?, ?)", list("bar", "foobar"), list("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l[?] IN (?, ?, ?)", 1, "bar", null, "foobar"),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l[?] IN (?, ?, ?)", 1, "bar", unset(), "foobar"),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", unset());
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, unset());
+ }
+ }
+
+ @Test
+ public void testInMarkerWithMaps() throws Throwable
+ {
+ for (boolean frozen : new boolean[] {false, true})
+ {
+ createTable(String.format("CREATE TABLE %%s (k int PRIMARY KEY, m %s)",
+ frozen
+ ? "frozen<map<text, text>>"
+ : "map<text, text>"));
+
+ execute("INSERT INTO %s (k, m) VALUES (0, {'foo' : 'bar'})");
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), map("bar", "foobar")),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), unset()),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", null, null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", list(map("foo", "foobar"), map("bar", "foobar"))),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN ?", "foo", list("foo", "foobar")),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", "foobar"),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", unset()),
+ row(false, map("foo", "bar")));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), map("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m IN (?, ?, ?)", map("bar", "foobar"), null, map("foo", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m IN (?, ?, ?)", map("bar", "foobar"), unset(), map("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", list(map("foo", "bar"), map("bar", "foobar"))),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m[?] IN ?", "foo", list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?, ?)", "foo", "bar", null, "foobar"),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?, ?)", "foo", "bar", unset(), "foobar"),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", unset());
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN ?", "foo", unset());
+ }
+ }
}
[2/7] cassandra git commit: Merge branch cassandra-2.2 into
cassandra-3.0
Posted by bl...@apache.org.
Merge branch cassandra-2.2 into cassandra-3.0
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/714edbce
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/714edbce
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/714edbce
Branch: refs/heads/trunk
Commit: 714edbce9a38c70a0f031a73d3c1a20f7f2b4bb3
Parents: e1da99a 70e33d9
Author: Benjamin Lerer <b....@gmail.com>
Authored: Fri Jan 27 15:26:18 2017 +0100
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Fri Jan 27 15:29:40 2017 +0100
----------------------------------------------------------------------
CHANGES.txt | 1 +
.../apache/cassandra/cql3/ColumnCondition.java | 62 ++++++-
.../operations/InsertUpdateIfConditionTest.java | 162 ++++++++++++++++++-
3 files changed, 215 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/714edbce/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 3796a8d,c5e5335..547fc07
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -1,32 -1,5 +1,33 @@@
-2.2.9
+3.0.11
+ * Better error when modifying function permissions without explicit keyspace (CASSANDRA-12925)
+ * Indexer is not correctly invoked when building indexes over sstables (CASSANDRA-13075)
+ * Read repair is not blocking repair to finish in foreground repair (CASSANDRA-13115)
+ * Stress daemon help is incorrect (CASSANDRA-12563)
+ * Remove ALTER TYPE support (CASSANDRA-12443)
+ * Fix assertion for certain legacy range tombstone pattern (CASSANDRA-12203)
+ * Set javac encoding to utf-8 (CASSANDRA-11077)
+ * Replace empty strings with null values if they cannot be converted (CASSANDRA-12794)
+ * Fixed flacky SSTableRewriterTest: check file counts before calling validateCFS (CASSANDRA-12348)
+ * Fix deserialization of 2.x DeletedCells (CASSANDRA-12620)
+ * Add parent repair session id to anticompaction log message (CASSANDRA-12186)
+ * Improve contention handling on failure to acquire MV lock for streaming and hints (CASSANDRA-12905)
+ * Fix DELETE and UPDATE queries with empty IN restrictions (CASSANDRA-12829)
+ * Mark MVs as built after successful bootstrap (CASSANDRA-12984)
+ * Estimated TS drop-time histogram updated with Cell.NO_DELETION_TIME (CASSANDRA-13040)
+ * Nodetool compactionstats fails with NullPointerException (CASSANDRA-13021)
+ * Thread local pools never cleaned up (CASSANDRA-13033)
+ * Set RPC_READY to false when draining or if a node is marked as shutdown (CASSANDRA-12781)
+ * Make sure sstables only get committed when it's safe to discard commit log records (CASSANDRA-12956)
+ * Reject default_time_to_live option when creating or altering MVs (CASSANDRA-12868)
+ * Nodetool should use a more sane max heap size (CASSANDRA-12739)
+ * LocalToken ensures token values are cloned on heap (CASSANDRA-12651)
+ * AnticompactionRequestSerializer serializedSize is incorrect (CASSANDRA-12934)
+ * Prevent reloading of logback.xml from UDF sandbox (CASSANDRA-12535)
+ * Reenable HeapPool (CASSANDRA-12900)
+Merged from 2.2:
+ * Fix handling of nulls and unsets in IN conditions (CASSANDRA-12981)
+ * Fix race causing infinite loop if Thrift server is stopped before it starts listening (CASSANDRA-12856)
+ * CompactionTasks now correctly drops sstables out of compaction when not enough disk space is available (CASSANDRA-12979)
* Remove support for non-JavaScript UDFs (CASSANDRA-12883)
* Fix DynamicEndpointSnitch noop in multi-datacenter situations (CASSANDRA-13074)
* cqlsh copy-from: encode column names to avoid primary key parsing errors (CASSANDRA-12909)
http://git-wip-us.apache.org/repos/asf/cassandra/blob/714edbce/src/java/org/apache/cassandra/cql3/ColumnCondition.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/ColumnCondition.java
index b13e534,3412e71..60e67f3
--- a/src/java/org/apache/cassandra/cql3/ColumnCondition.java
+++ b/src/java/org/apache/cassandra/cql3/ColumnCondition.java
@@@ -23,8 -23,16 +23,9 @@@ import java.util.*
import com.google.common.collect.Iterators;
import org.apache.cassandra.config.ColumnDefinition;
+ import org.apache.cassandra.cql3.Term.Terminal;
import org.apache.cassandra.cql3.functions.Function;
-import org.apache.cassandra.db.Cell;
-import org.apache.cassandra.db.ColumnFamily;
-import org.apache.cassandra.db.composites.CellName;
-import org.apache.cassandra.db.composites.CellNameType;
-import org.apache.cassandra.db.composites.Composite;
-import org.apache.cassandra.db.filter.ColumnSlice;
+import org.apache.cassandra.db.rows.*;
import org.apache.cassandra.db.marshal.*;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.transport.Server;
http://git-wip-us.apache.org/repos/asf/cassandra/blob/714edbce/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
----------------------------------------------------------------------
diff --cc test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
index fd02a69,6396727..8adce7a
--- a/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
@@@ -155,8 -150,6 +159,7 @@@ public class InsertUpdateIfConditionTes
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 >= ?", unset());
assertInvalidMessage("Invalid 'unset' value in condition",
"UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 != ?", unset());
- assertInvalidMessage("Invalid 'unset' value in condition",
- "UPDATE %s SET v1 = 3, v2 = 'bar' WHERE k = 0 IF v1 IN (?, ?)", unset(), 1);
++
}
/**
@@@ -1482,107 -1306,158 +1485,262 @@@
row(7, 7, null, null, 7));
}
+ /**
+ * Test for CASSANDRA-12060, using a table without clustering.
+ */
+ @Test
+ public void testMultiExistConditionOnSameRowNoClustering() throws Throwable
+ {
+ createTable("CREATE TABLE %s (k int PRIMARY KEY, v1 text, v2 text)");
+
+ // Multiple inserts on the same row with not exist conditions
+ assertRows(execute("BEGIN BATCH "
+ + "INSERT INTO %1$s (k, v1) values (0, 'foo') IF NOT EXISTS; "
+ + "INSERT INTO %1$s (k, v2) values (0, 'bar') IF NOT EXISTS; "
+ + "APPLY BATCH"),
+ row(true));
+
+ assertRows(execute("SELECT * FROM %s WHERE k = 0"), row(0, "foo", "bar"));
+
+ // Same, but both insert on the same column: doing so would almost surely be a user error, but that's the
+ // original case reported in #12867, so being thorough.
+ assertRows(execute("BEGIN BATCH "
+ + "INSERT INTO %1$s (k, v1) values (1, 'foo') IF NOT EXISTS; "
+ + "INSERT INTO %1$s (k, v1) values (1, 'bar') IF NOT EXISTS; "
+ + "APPLY BATCH"),
+ row(true));
+
+ // As all statement gets the same timestamp, the biggest value ends up winning, so that's "foo"
+ assertRows(execute("SELECT * FROM %s WHERE k = 1"), row(1, "foo", null));
+
+ // Multiple deletes on the same row with exists conditions (note that this is somewhat non-sensical, one of the
+ // delete is redundant, we're just checking it doesn't break something)
+ assertRows(execute("BEGIN BATCH "
+ + "DELETE FROM %1$s WHERE k = 0 IF EXISTS; "
+ + "DELETE FROM %1$s WHERE k = 0 IF EXISTS; "
+ + "APPLY BATCH"),
+ row(true));
+
+ assertEmpty(execute("SELECT * FROM %s WHERE k = 0"));
+
+ // Validate we can't mix different type of conditions however
+ assertInvalidMessage("Cannot mix IF EXISTS and IF NOT EXISTS conditions for the same row",
+ "BEGIN BATCH "
+ + "INSERT INTO %1$s (k, v1) values (1, 'foo') IF NOT EXISTS; "
+ + "DELETE FROM %1$s WHERE k = 1 IF EXISTS; "
+ + "APPLY BATCH");
+
+ assertInvalidMessage("Cannot mix IF conditions and IF NOT EXISTS for the same row",
+ "BEGIN BATCH "
+ + "INSERT INTO %1$s (k, v1) values (1, 'foo') IF NOT EXISTS; "
+ + "UPDATE %1$s SET v2 = 'bar' WHERE k = 1 IF v1 = 'foo'; "
+ + "APPLY BATCH");
+ }
+
+ /**
+ * Test for CASSANDRA-12060, using a table with clustering.
+ */
+ @Test
+ public void testMultiExistConditionOnSameRowClustering() throws Throwable
+ {
+ createTable("CREATE TABLE %s (k int, t int, v1 text, v2 text, PRIMARY KEY (k, t))");
+
+ // Multiple inserts on the same row with not exist conditions
+ assertRows(execute("BEGIN BATCH "
+ + "INSERT INTO %1$s (k, t, v1) values (0, 0, 'foo') IF NOT EXISTS; "
+ + "INSERT INTO %1$s (k, t, v2) values (0, 0, 'bar') IF NOT EXISTS; "
+ + "APPLY BATCH"),
+ row(true));
+
+ assertRows(execute("SELECT * FROM %s WHERE k = 0"), row(0, 0, "foo", "bar"));
+
+ // Same, but both insert on the same column: doing so would almost surely be a user error, but that's the
+ // original case reported in #12867, so being thorough.
+ assertRows(execute("BEGIN BATCH "
+ + "INSERT INTO %1$s (k, t, v1) values (1, 0, 'foo') IF NOT EXISTS; "
+ + "INSERT INTO %1$s (k, t, v1) values (1, 0, 'bar') IF NOT EXISTS; "
+ + "APPLY BATCH"),
+ row(true));
+
+ // As all statement gets the same timestamp, the biggest value ends up winning, so that's "foo"
+ assertRows(execute("SELECT * FROM %s WHERE k = 1"), row(1, 0, "foo", null));
+
+ // Multiple deletes on the same row with exists conditions (note that this is somewhat non-sensical, one of the
+ // delete is redundant, we're just checking it doesn't break something)
+ assertRows(execute("BEGIN BATCH "
+ + "DELETE FROM %1$s WHERE k = 0 AND t = 0 IF EXISTS; "
+ + "DELETE FROM %1$s WHERE k = 0 AND t = 0 IF EXISTS; "
+ + "APPLY BATCH"),
+ row(true));
+
+ assertEmpty(execute("SELECT * FROM %s WHERE k = 0"));
+
+ // Validate we can't mix different type of conditions however
+ assertInvalidMessage("Cannot mix IF EXISTS and IF NOT EXISTS conditions for the same row",
+ "BEGIN BATCH "
+ + "INSERT INTO %1$s (k, t, v1) values (1, 0, 'foo') IF NOT EXISTS; "
+ + "DELETE FROM %1$s WHERE k = 1 AND t = 0 IF EXISTS; "
+ + "APPLY BATCH");
+
+ assertInvalidMessage("Cannot mix IF conditions and IF NOT EXISTS for the same row",
+ "BEGIN BATCH "
+ + "INSERT INTO %1$s (k, t, v1) values (1, 0, 'foo') IF NOT EXISTS; "
+ + "UPDATE %1$s SET v2 = 'bar' WHERE k = 1 AND t = 0 IF v1 = 'foo'; "
+ + "APPLY BATCH");
+ }
++
+ @Test
+ public void testInMarkerWithUDTs() throws Throwable
+ {
+ String typename = createType("CREATE TYPE %s (a int, b text)");
+ String myType = KEYSPACE + '.' + typename;
+
+ createTable("CREATE TABLE %s (k int PRIMARY KEY, v frozen<" + myType + "> )");
+
+ Object v = userType(0, "abc");
+ execute("INSERT INTO %s (k, v) VALUES (?, ?)", 0, v);
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(1, "abc"), userType(0, "ac")),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(1, "abc"), null),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(1, "abc"), unset()),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", null, null),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", unset(), unset()),
+ row(false, v));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", list(userType(1, "abc"), userType(0, "ac"))),
+ row(false, v));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(0, "abc"), userType(0, "ac")),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 1, b: 'bc'} WHERE k = 0 IF v IN (?, ?)", userType(0, "bc"), null),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 1, b: 'ac'} WHERE k = 0 IF v IN (?, ?, ?)", userType(0, "bc"), unset(), userType(1, "bc")),
+ row(true));
+ assertRows(execute("UPDATE %s SET v = {a: 0, b: 'abc'} WHERE k = 0 IF v IN ?", list(userType(1, "ac"), userType(0, "ac"))),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET v = {a: 0, b: 'bc'} WHERE k = 0 IF v IN ?", unset());
+ }
+
+ @Test
+ public void testInMarkerWithLists() throws Throwable
+ {
+ for (boolean frozen : new boolean[]{false, true})
+ {
+ createTable(String.format("CREATE TABLE %%s (k int PRIMARY KEY, l %s)",
+ frozen
+ ? "frozen<list<text>>"
+ : "list<text>"));
+
+ execute("INSERT INTO %s(k, l) VALUES (0, ['foo', 'bar', 'foobar'])");
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), list("bar", "foobar")),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "foobar"), unset()),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", null, null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", list(list("foo", "foobar"), list("bar", "foobar"))),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, list("foo", "foobar")),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", "foobar"),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", null),
+ row(false, list("foo", "bar", "foobar")));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN (?, ?)", 1, "foo", unset()),
+ row(false, list("foo", "bar", "foobar")));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?)", list("foo", "bar", "foobar"), list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l IN (?, ?, ?)", list("foo", "bar", "foobar"), null, list("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN (?, ?, ?)", list("foo", "bar", "foobar"), unset(), list("foo", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l IN (?, ?)", list("bar", "foobar"), list("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l[?] IN (?, ?, ?)", 1, "bar", null, "foobar"),
+ row(true));
+ assertRows(execute("UPDATE %s SET l = ['foo', 'foobar'] WHERE k = 0 IF l[?] IN (?, ?, ?)", 1, "bar", unset(), "foobar"),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l IN ?", unset());
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET l = ['foo', 'bar'] WHERE k = 0 IF l[?] IN ?", 1, unset());
+ }
+ }
+
+ @Test
+ public void testInMarkerWithMaps() throws Throwable
+ {
+ for (boolean frozen : new boolean[] {false, true})
+ {
+ createTable(String.format("CREATE TABLE %%s (k int PRIMARY KEY, m %s)",
+ frozen
+ ? "frozen<map<text, text>>"
+ : "map<text, text>"));
+
+ execute("INSERT INTO %s (k, m) VALUES (0, {'foo' : 'bar'})");
+
+ // Does not apply
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), map("bar", "foobar")),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), unset()),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", null, null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", list(map("foo", "foobar"), map("bar", "foobar"))),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN ?", "foo", list("foo", "foobar")),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", "foobar"),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", null),
+ row(false, map("foo", "bar")));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?)", "foo", "foo", unset()),
+ row(false, map("foo", "bar")));
+
+ // Does apply
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN (?, ?)", map("foo", "foobar"), map("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m IN (?, ?, ?)", map("bar", "foobar"), null, map("foo", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m IN (?, ?, ?)", map("bar", "foobar"), unset(), map("foo", "bar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", list(map("foo", "bar"), map("bar", "foobar"))),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'bar'} WHERE k = 0 IF m[?] IN ?", "foo", list("bar", "foobar")),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?, ?)", "foo", "bar", null, "foobar"),
+ row(true));
+ assertRows(execute("UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN (?, ?, ?)", "foo", "bar", unset(), "foobar"),
+ row(true));
+
+ assertInvalidMessage("Invalid null list in IN condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", (List<ByteBuffer>) null);
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m IN ?", unset());
+ assertInvalidMessage("Invalid 'unset' value in condition",
+ "UPDATE %s SET m = {'foo' : 'foobar'} WHERE k = 0 IF m[?] IN ?", "foo", unset());
+ }
+ }
}
[3/7] cassandra git commit: Merge branch cassandra-3.0 into
cassandra-3.11
Posted by bl...@apache.org.
Merge branch cassandra-3.0 into cassandra-3.11
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/078a8415
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/078a8415
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/078a8415
Branch: refs/heads/trunk
Commit: 078a8415441ce965d5844cb8f246e0e61da6c397
Parents: 84d8361 714edbc
Author: Benjamin Lerer <b....@gmail.com>
Authored: Fri Jan 27 15:36:15 2017 +0100
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Fri Jan 27 15:36:15 2017 +0100
----------------------------------------------------------------------
CHANGES.txt | 1 +
.../apache/cassandra/cql3/ColumnCondition.java | 62 ++++++-
.../operations/InsertUpdateIfConditionTest.java | 162 ++++++++++++++++++-
3 files changed, 215 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/078a8415/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 66e17a2,547fc07..6f7b5c2
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -186,18 -80,6 +186,19 @@@ Merged from 3.0
* Correct log message for statistics of offheap memtable flush (CASSANDRA-12776)
* Explicitly set locale for string validation (CASSANDRA-12541,CASSANDRA-12542,CASSANDRA-12543,CASSANDRA-12545)
Merged from 2.2:
++ * Fix handling of nulls and unsets in IN conditions (CASSANDRA-12981)
+ * Fix race causing infinite loop if Thrift server is stopped before it starts listening (CASSANDRA-12856)
+ * CompactionTasks now correctly drops sstables out of compaction when not enough disk space is available (CASSANDRA-12979)
+ * Remove support for non-JavaScript UDFs (CASSANDRA-12883)
+ * Fix DynamicEndpointSnitch noop in multi-datacenter situations (CASSANDRA-13074)
+ * cqlsh copy-from: encode column names to avoid primary key parsing errors (CASSANDRA-12909)
+ * Temporarily fix bug that creates commit log when running offline tools (CASSANDRA-8616)
+ * Reduce granuality of OpOrder.Group during index build (CASSANDRA-12796)
+ * Test bind parameters and unset parameters in InsertUpdateIfConditionTest (CASSANDRA-12980)
+ * Use saved tokens when setting local tokens on StorageService.joinRing (CASSANDRA-12935)
+ * cqlsh: fix DESC TYPES errors (CASSANDRA-12914)
+ * Fix leak on skipped SSTables in sstableupgrade (CASSANDRA-12899)
+ * Avoid blocking gossip during pending range calculation (CASSANDRA-12281)
* Fix purgeability of tombstones with max timestamp (CASSANDRA-12792)
* Fail repair if participant dies during sync or anticompaction (CASSANDRA-12901)
* cqlsh COPY: unprotected pk values before converting them if not using prepared statements (CASSANDRA-12863)
http://git-wip-us.apache.org/repos/asf/cassandra/blob/078a8415/src/java/org/apache/cassandra/cql3/ColumnCondition.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/ColumnCondition.java
index 07f9f60,60e67f3..75a988e
--- a/src/java/org/apache/cassandra/cql3/ColumnCondition.java
+++ b/src/java/org/apache/cassandra/cql3/ColumnCondition.java
@@@ -22,8 -22,8 +22,9 @@@ import java.util.*
import com.google.common.collect.Iterators;
+import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.config.ColumnDefinition;
+ import org.apache.cassandra.cql3.Term.Terminal;
import org.apache.cassandra.cql3.functions.Function;
import org.apache.cassandra.db.rows.*;
import org.apache.cassandra.db.marshal.*;
@@@ -300,10 -245,20 +301,20 @@@ public class ColumnConditio
private SimpleInBound(ColumnCondition condition, QueryOptions options) throws InvalidRequestException
{
super(condition.column, condition.operator);
- assert !(column.type instanceof CollectionType) && condition.collectionElement == null;
+ assert !(column.type instanceof CollectionType) && condition.field == null;
assert condition.operator == Operator.IN;
if (condition.inValues == null)
- this.inValues = ((Lists.Value) condition.value.bind(options)).getElements();
+ {
+ Terminal terminal = condition.value.bind(options);
+
+ if (terminal == null)
+ throw new InvalidRequestException("Invalid null list in IN condition");
+
+ if (terminal == Constants.UNSET_VALUE)
+ throw new InvalidRequestException("Invalid 'unset' value in condition");
+
+ this.inValues = ((Lists.Value) terminal).getElements();
+ }
else
{
this.inValues = new ArrayList<>(condition.inValues.size());
http://git-wip-us.apache.org/repos/asf/cassandra/blob/078a8415/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
----------------------------------------------------------------------
diff --cc test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
index 1f01931,8adce7a..2729b83
--- a/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/operations/InsertUpdateIfConditionTest.java
@@@ -18,9 -18,11 +18,12 @@@
package org.apache.cassandra.cql3.validation.operations;
+ import java.nio.ByteBuffer;
+ import java.util.List;
+
import org.junit.Test;
+import org.apache.cassandra.config.SchemaConstants;
import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.exceptions.SyntaxException;