cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ble...@apache.org
Subject [1/4] cassandra git commit: Add support for arithmetic operators
Date Mon, 21 Nov 2016 17:50:16 GMT
Repository: cassandra
Updated Branches:
  refs/heads/cassandra-3.X 075539a5b -> 8b3de2f49


http://git-wip-us.apache.org/repos/asf/cassandra/blob/8b3de2f4/test/unit/org/apache/cassandra/cql3/selection/SelectionColumnMappingTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/selection/SelectionColumnMappingTest.java
b/test/unit/org/apache/cassandra/cql3/selection/SelectionColumnMappingTest.java
index ece2d1d..975eb8e 100644
--- a/test/unit/org/apache/cassandra/cql3/selection/SelectionColumnMappingTest.java
+++ b/test/unit/org/apache/cassandra/cql3/selection/SelectionColumnMappingTest.java
@@ -39,6 +39,7 @@ import org.apache.cassandra.service.ClientState;
 import org.apache.cassandra.service.QueryState;
 import org.apache.cassandra.utils.ByteBufferUtil;
 
+import static java.util.Arrays.asList;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
@@ -102,6 +103,14 @@ public class SelectionColumnMappingTest extends CQLTester
         testMixedColumnTypes();
         testMultipleUnaliasedSelectionOfSameColumn();
         testUserDefinedAggregate();
+        testListLitteral();
+        testEmptyListLitteral();
+        testSetLitteral();
+        testEmptySetLitteral();
+        testMapLitteral();
+        testEmptyMapLitteral();
+        testUDTLitteral();
+        testTupleLitteral();
     }
 
     @Test
@@ -407,6 +416,91 @@ public class SelectionColumnMappingTest extends CQLTester
         verify(expected, "SELECT v1, v1 FROM %s");
     }
 
+    private void testListLitteral() throws Throwable
+    {
+        ColumnSpecification listSpec = columnSpecification("[k, v1]", ListType.getInstance(Int32Type.instance,
false));
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(listSpec, asList(columnDefinition("k"),
+                                                                                        
    columnDefinition("v1")));
+
+        verify(expected, "SELECT [k, v1] FROM %s");
+    }
+
+    private void testEmptyListLitteral() throws Throwable
+    {
+        ColumnSpecification listSpec = columnSpecification("(list<int>)[]", ListType.getInstance(Int32Type.instance,
false));
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(listSpec, (ColumnDefinition)
null);
+
+        verify(expected, "SELECT (list<int>)[] FROM %s");
+    }
+
+    private void testSetLitteral() throws Throwable
+    {
+        ColumnSpecification setSpec = columnSpecification("{k, v1}", SetType.getInstance(Int32Type.instance,
false));
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(setSpec, asList(columnDefinition("k"),
+                                                                                        
    columnDefinition("v1")));
+
+        verify(expected, "SELECT {k, v1} FROM %s");
+    }
+
+    private void testEmptySetLitteral() throws Throwable
+    {
+        ColumnSpecification setSpec = columnSpecification("(set<int>){}", SetType.getInstance(Int32Type.instance,
false));
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(setSpec, (ColumnDefinition)
null);
+
+        verify(expected, "SELECT (set<int>){} FROM %s");
+    }
+
+    private void testMapLitteral() throws Throwable
+    {
+        ColumnSpecification mapSpec = columnSpecification("(map<text, int>){'min':
system.min(v1), 'max': system.max(v1)}", MapType.getInstance(UTF8Type.instance, Int32Type.instance,
false));
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(mapSpec, asList(columnDefinition("v1")));
+
+        verify(expected, "SELECT (map<text, int>){'min': min(v1), 'max': max(v1)} FROM
%s");
+    }
+
+    private void testEmptyMapLitteral() throws Throwable
+    {
+        ColumnSpecification mapSpec = columnSpecification("(map<text, int>){}", MapType.getInstance(UTF8Type.instance,
Int32Type.instance, false));
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(mapSpec, (ColumnDefinition)
null);
+
+        verify(expected, "SELECT (map<text, int>){} FROM %s");
+    }
+
+    private void testUDTLitteral() throws Throwable
+    {
+        UserType type = new UserType(KEYSPACE, ByteBufferUtil.bytes(typeName),
+                                      asList(FieldIdentifier.forUnquoted("f1"),
+                                             FieldIdentifier.forUnquoted("f2")),
+                                      asList(Int32Type.instance,
+                                             UTF8Type.instance),
+                                      false);
+
+        ColumnSpecification spec = columnSpecification("(" + KEYSPACE + "." + typeName +
"){f1: v1, f2: v2}", type);
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(spec, asList(columnDefinition("v1"),
+                                                                                        
columnDefinition("v2")));
+
+        verify(expected, "SELECT ("+ typeName + "){f1: v1, f2: v2} FROM %s");
+    }
+
+    private void testTupleLitteral() throws Throwable
+    {
+        TupleType type = new TupleType(asList(Int32Type.instance, UTF8Type.instance));
+
+        ColumnSpecification setSpec = columnSpecification("(tuple<int, text>)(v1, v2)",
type);
+        SelectionColumnMapping expected = SelectionColumnMapping.newMapping()
+                                                                .addMapping(setSpec, asList(columnDefinition("v1"),
+                                                                                        
   columnDefinition("v2")));
+
+        verify(expected, "SELECT (tuple<int, text>)(v1, v2) FROM %s");
+    }
+
     private void testMixedColumnTypes() throws Throwable
     {
         ColumnSpecification kSpec = columnSpecification("k_alias", Int32Type.instance);

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8b3de2f4/test/unit/org/apache/cassandra/cql3/selection/TermSelectionTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/selection/TermSelectionTest.java b/test/unit/org/apache/cassandra/cql3/selection/TermSelectionTest.java
index a07f8f9..fb46809 100644
--- a/test/unit/org/apache/cassandra/cql3/selection/TermSelectionTest.java
+++ b/test/unit/org/apache/cassandra/cql3/selection/TermSelectionTest.java
@@ -43,20 +43,26 @@ public class TermSelectionTest extends CQLTester
     @Test
     public void testSelectLiteral() throws Throwable
     {
+        long timestampInMicros = System.currentTimeMillis() * 1000;
         createTable("CREATE TABLE %s (pk int, ck int, t text, PRIMARY KEY (pk, ck) )");
-        execute("INSERT INTO %s (pk, ck, t) VALUES (1, 1, 'one')");
-        execute("INSERT INTO %s (pk, ck, t) VALUES (1, 2, 'two')");
-        execute("INSERT INTO %s (pk, ck, t) VALUES (1, 3, 'three')");
+        execute("INSERT INTO %s (pk, ck, t) VALUES (?, ?, ?) USING TIMESTAMP ?", 1, 1, "one",
timestampInMicros);
+        execute("INSERT INTO %s (pk, ck, t) VALUES (?, ?, ?) USING TIMESTAMP ?", 1, 2, "two",
timestampInMicros);
+        execute("INSERT INTO %s (pk, ck, t) VALUES (?, ?, ?) USING TIMESTAMP ?", 1, 3, "three",
timestampInMicros);
 
         assertInvalidMessage("Cannot infer type for term", "SELECT ck, t, 'a const' FROM
%s");
         assertConstantResult(execute("SELECT ck, t, (text)'a const' FROM %s"), "a const");
 
         assertInvalidMessage("Cannot infer type for term", "SELECT ck, t, 42 FROM %s");
-        assertConstantResult(execute("SELECT ck, t, (int)42 FROM %s"), 42);
+        assertConstantResult(execute("SELECT ck, t, (smallint)42 FROM %s"), (short) 42);
 
         assertInvalidMessage("Cannot infer type for term", "SELECT ck, t, (1, 'foo') FROM
%s");
         assertConstantResult(execute("SELECT ck, t, (tuple<int, text>)(1, 'foo') FROM
%s"), tuple(1, "foo"));
 
+        assertInvalidMessage("Cannot infer type for term ((1)) in selection clause", "SELECT
ck, t, ((1)) FROM %s");
+        // We cannot differentiate a tuple containing a tuple from a tuple between parentheses.
+        assertInvalidMessage("Cannot infer type for term ((tuple<int>)(1))", "SELECT
ck, t, ((tuple<int>)(1)) FROM %s");
+        assertConstantResult(execute("SELECT ck, t, (tuple<tuple<int>>)((1))
FROM %s"), tuple(tuple(1)));
+
         assertInvalidMessage("Cannot infer type for term", "SELECT ck, t, [1, 2, 3] FROM
%s");
         assertConstantResult(execute("SELECT ck, t, (list<int>)[1, 2, 3] FROM %s"),
list(1, 2, 3));
 
@@ -66,11 +72,343 @@ public class TermSelectionTest extends CQLTester
         assertInvalidMessage("Cannot infer type for term", "SELECT ck, t, {1: 'foo', 2: 'bar',
3: 'baz'} FROM %s");
         assertConstantResult(execute("SELECT ck, t, (map<int, text>){1: 'foo', 2: 'bar',
3: 'baz'} FROM %s"), map(1, "foo", 2, "bar", 3, "baz"));
 
+        assertInvalidMessage("Cannot infer type for term", "SELECT ck, t, {} FROM %s");
+        assertConstantResult(execute("SELECT ck, t, (map<int, text>){} FROM %s"), map());
+        assertConstantResult(execute("SELECT ck, t, (set<int>){} FROM %s"), set());
+
         assertColumnNames(execute("SELECT ck, t, (int)42, (int)43 FROM %s"), "ck", "t", "(int)42",
"(int)43");
         assertRows(execute("SELECT ck, t, (int) 42, (int) 43 FROM %s"),
                    row(1, "one", 42, 43),
                    row(2, "two", 42, 43),
                    row(3, "three", 42, 43));
+
+        assertRows(execute("SELECT min(ck), max(ck), [min(ck), max(ck)] FROM %s"), row(1,
3, list(1, 3)));
+        assertRows(execute("SELECT [min(ck), max(ck)] FROM %s"), row(list(1, 3)));
+        assertRows(execute("SELECT {min(ck), max(ck)} FROM %s"), row(set(1, 3)));
+
+        // We need to use a cast to differentiate between a map and an UDT
+        assertInvalidMessage("Cannot infer type for term {'min': system.min(ck), 'max': system.max(ck)}",
+                             "SELECT {'min' : min(ck), 'max' : max(ck)} FROM %s");
+        assertRows(execute("SELECT (map<text, int>){'min' : min(ck), 'max' : max(ck)}
FROM %s"), row(map("min", 1, "max", 3)));
+
+        assertRows(execute("SELECT [1, min(ck), max(ck)] FROM %s"), row(list(1, 1, 3)));
+        assertRows(execute("SELECT {1, min(ck), max(ck)} FROM %s"), row(set(1, 1, 3)));
+        assertRows(execute("SELECT (map<text, int>) {'litteral' : 1, 'min' : min(ck),
'max' : max(ck)} FROM %s"), row(map("litteral", 1, "min", 1, "max", 3)));
+
+        // Test List nested within Lists
+        assertRows(execute("SELECT [[], [min(ck), max(ck)]] FROM %s"),
+                   row(list(list(), list(1, 3))));
+        assertRows(execute("SELECT [[], [CAST(pk AS BIGINT), CAST(ck AS BIGINT), WRITETIME(t)]]
FROM %s"),
+                   row(list(list(), list(1L, 1L, timestampInMicros))),
+                   row(list(list(), list(1L, 2L, timestampInMicros))),
+                   row(list(list(), list(1L, 3L, timestampInMicros))));
+        assertRows(execute("SELECT [[min(ck)], [max(ck)]] FROM %s"),
+                   row(list(list(1), list(3))));
+        assertRows(execute("SELECT [[min(ck)], ([max(ck)])] FROM %s"),
+                   row(list(list(1), list(3))));
+        assertRows(execute("SELECT [[pk], [ck]] FROM %s"),
+                   row(list(list(1), list(1))),
+                   row(list(list(1), list(2))),
+                   row(list(list(1), list(3))));
+        assertRows(execute("SELECT [[pk], [ck]] FROM %s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(list(list(1), list(3))),
+                   row(list(list(1), list(2))),
+                   row(list(list(1), list(1))));
+
+        // Test Sets nested within Lists
+        assertRows(execute("SELECT [{}, {min(ck), max(ck)}] FROM %s"),
+                   row(list(set(), set(1, 3))));
+        assertRows(execute("SELECT [{}, {CAST(pk AS BIGINT), CAST(ck AS BIGINT), WRITETIME(t)}]
FROM %s"),
+                   row(list(set(), set(1L, 1L, timestampInMicros))),
+                   row(list(set(), set(1L, 2L, timestampInMicros))),
+                   row(list(set(), set(1L, 3L, timestampInMicros))));
+        assertRows(execute("SELECT [{min(ck)}, {max(ck)}] FROM %s"),
+                   row(list(set(1), set(3))));
+        assertRows(execute("SELECT [{min(ck)}, ({max(ck)})] FROM %s"),
+                   row(list(set(1), set(3))));
+        assertRows(execute("SELECT [{pk}, {ck}] FROM %s"),
+                   row(list(set(1), set(1))),
+                   row(list(set(1), set(2))),
+                   row(list(set(1), set(3))));
+        assertRows(execute("SELECT [{pk}, {ck}] FROM %s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(list(set(1), set(3))),
+                   row(list(set(1), set(2))),
+                   row(list(set(1), set(1))));
+
+        // Test Maps nested within Lists
+        assertRows(execute("SELECT [{}, (map<text, int>){'min' : min(ck), 'max' : max(ck)}]
FROM %s"),
+                   row(list(map(), map("min", 1, "max", 3))));
+        assertRows(execute("SELECT [{}, (map<text, bigint>){'pk' : CAST(pk AS BIGINT),
'ck' : CAST(ck AS BIGINT), 'writetime' : WRITETIME(t)}] FROM %s"),
+                   row(list(map(), map("pk", 1L, "ck", 1L, "writetime", timestampInMicros))),
+                   row(list(map(), map("pk", 1L, "ck", 2L, "writetime", timestampInMicros))),
+                   row(list(map(), map("pk", 1L, "ck", 3L, "writetime", timestampInMicros))));
+        assertRows(execute("SELECT [{}, (map<text, int>){'pk' : pk, 'ck' : ck}] FROM
%s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(list(map(), map("pk", 1, "ck", 3))),
+                   row(list(map(), map("pk", 1, "ck", 2))),
+                   row(list(map(), map("pk", 1, "ck", 1))));
+
+        // Test Tuples nested within Lists
+        assertRows(execute("SELECT [(pk, ck, WRITETIME(t))] FROM %s"),
+                   row(list(tuple(1, 1, timestampInMicros))),
+                   row(list(tuple(1, 2, timestampInMicros))),
+                   row(list(tuple(1, 3, timestampInMicros))));
+        assertRows(execute("SELECT [(min(ck), max(ck))] FROM %s"),
+                   row(list(tuple(1, 3))));
+        assertRows(execute("SELECT [(CAST(pk AS BIGINT), CAST(ck AS BIGINT)), (t, WRITETIME(t))]
FROM %s"),
+                   row(list(tuple(1L, 1L), tuple("one", timestampInMicros))),
+                   row(list(tuple(1L, 2L), tuple("two", timestampInMicros))),
+                   row(list(tuple(1L, 3L), tuple("three", timestampInMicros))));
+
+        // Test UDTs nested within Lists
+        String type = createType("CREATE TYPE %s(a int, b int, c bigint)");
+        assertRows(execute("SELECT [(" + type + "){a : min(ck), b: max(ck)}] FROM %s"),
+                   row(list(userType("a", 1, "b", 3, "c", null))));
+        assertRows(execute("SELECT [(" + type + "){a : pk, b : ck, c : WRITETIME(t)}] FROM
%s"),
+                   row(list(userType("a", 1, "b", 1, "c", timestampInMicros))),
+                   row(list(userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(list(userType("a", 1, "b", 3, "c", timestampInMicros))));
+        assertRows(execute("SELECT [(" + type + "){a : pk, b : ck, c : WRITETIME(t)}] FROM
%s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(list(userType("a", 1, "b", 3, "c", timestampInMicros))),
+                   row(list(userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(list(userType("a", 1, "b", 1, "c", timestampInMicros))));
+
+        // Test Lists nested within Sets
+        assertRows(execute("SELECT {[], [min(ck), max(ck)]} FROM %s"),
+                   row(set(list(), list(1, 3))));
+        assertRows(execute("SELECT {[], [pk, ck]} FROM %s LIMIT 2"),
+                   row(set(list(), list(1, 1))),
+                   row(set(list(), list(1, 2))));
+        assertRows(execute("SELECT {[], [pk, ck]} FROM %s WHERE pk = 1 ORDER BY ck DESC LIMIT
2"),
+                   row(set(list(), list(1, 3))),
+                   row(set(list(), list(1, 2))));
+        assertRows(execute("SELECT {[min(ck)], ([max(ck)])} FROM %s"),
+                   row(set(list(1), list(3))));
+        assertRows(execute("SELECT {[pk], ([ck])} FROM %s"),
+                   row(set(list(1), list(1))),
+                   row(set(list(1), list(2))),
+                   row(set(list(1), list(3))));
+        assertRows(execute("SELECT {([min(ck)]), [max(ck)]} FROM %s"),
+                   row(set(list(1), list(3))));
+
+        // Test Sets nested within Sets
+        assertRows(execute("SELECT {{}, {min(ck), max(ck)}} FROM %s"),
+                   row(set(set(), set(1, 3))));
+        assertRows(execute("SELECT {{}, {pk, ck}} FROM %s LIMIT 2"),
+                   row(set(set(), set(1, 1))),
+                   row(set(set(), set(1, 2))));
+        assertRows(execute("SELECT {{}, {pk, ck}} FROM %s WHERE pk = 1 ORDER BY ck DESC LIMIT
2"),
+                   row(set(set(), set(1, 3))),
+                   row(set(set(), set(1, 2))));
+        assertRows(execute("SELECT {{min(ck)}, ({max(ck)})} FROM %s"),
+                   row(set(set(1), set(3))));
+        assertRows(execute("SELECT {{pk}, ({ck})} FROM %s"),
+                   row(set(set(1), set(1))),
+                   row(set(set(1), set(2))),
+                   row(set(set(1), set(3))));
+        assertRows(execute("SELECT {({min(ck)}), {max(ck)}} FROM %s"),
+                   row(set(set(1), set(3))));
+
+        // Test Maps nested within Sets
+        assertRows(execute("SELECT {{}, (map<text, int>){'min' : min(ck), 'max' : max(ck)}}
FROM %s"),
+                   row(set(map(), map("min", 1, "max", 3))));
+        assertRows(execute("SELECT {{}, (map<text, int>){'pk' : pk, 'ck' : ck}} FROM
%s"),
+                   row(set(map(), map("pk", 1, "ck", 1))),
+                   row(set(map(), map("pk", 1, "ck", 2))),
+                   row(set(map(), map("pk", 1, "ck", 3))));
+
+        // Test Tuples nested within Sets
+        assertRows(execute("SELECT {(pk, ck, WRITETIME(t))} FROM %s"),
+                   row(set(tuple(1, 1, timestampInMicros))),
+                   row(set(tuple(1, 2, timestampInMicros))),
+                   row(set(tuple(1, 3, timestampInMicros))));
+        assertRows(execute("SELECT {(min(ck), max(ck))} FROM %s"),
+                   row(set(tuple(1, 3))));
+
+        // Test UDTs nested within Sets
+        assertRows(execute("SELECT {(" + type + "){a : min(ck), b: max(ck)}} FROM %s"),
+                   row(set(userType("a", 1, "b", 3, "c", null))));
+        assertRows(execute("SELECT {(" + type + "){a : pk, b : ck, c : WRITETIME(t)}} FROM
%s"),
+                   row(set(userType("a", 1, "b", 1, "c", timestampInMicros))),
+                   row(set(userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(set(userType("a", 1, "b", 3, "c", timestampInMicros))));
+        assertRows(execute("SELECT {(" + type + "){a : pk, b : ck, c : WRITETIME(t)}} FROM
%s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(set(userType("a", 1, "b", 3, "c", timestampInMicros))),
+                   row(set(userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(set(userType("a", 1, "b", 1, "c", timestampInMicros))));
+
+        // Test Lists nested within Maps
+        assertRows(execute("SELECT (map<frozen<list<int>>, frozen<list<int>>>){[min(ck)]:[max(ck)]}
FROM %s"),
+                   row(map(list(1), list(3))));
+        assertRows(execute("SELECT (map<frozen<list<int>>, frozen<list<int>>>){[pk]:
[ck]} FROM %s"),
+                   row(map(list(1), list(1))),
+                   row(map(list(1), list(2))),
+                   row(map(list(1), list(3))));
+
+        // Test Sets nested within Maps
+        assertRows(execute("SELECT (map<frozen<set<int>>, frozen<set<int>>>){{min(ck)}
: {max(ck)}} FROM %s"),
+                   row(map(set(1), set(3))));
+        assertRows(execute("SELECT (map<frozen<set<int>>, frozen<set<int>>>){{pk}
: {ck}} FROM %s"),
+                   row(map(set(1), set(1))),
+                   row(map(set(1), set(2))),
+                   row(map(set(1), set(3))));
+
+        // Test Maps nested within Maps
+        assertRows(execute("SELECT (map<frozen<map<text, int>>, frozen<map<text,
int>>>){{'min' : min(ck)} : {'max' : max(ck)}} FROM %s"),
+                   row(map(map("min", 1), map("max", 3))));
+        assertRows(execute("SELECT (map<frozen<map<text, int>>, frozen<map<text,
int>>>){{'pk' : pk} : {'ck' : ck}} FROM %s"),
+                   row(map(map("pk", 1), map("ck", 1))),
+                   row(map(map("pk", 1), map("ck", 2))),
+                   row(map(map("pk", 1), map("ck", 3))));
+
+        // Test Tuples nested within Maps
+        assertRows(execute("SELECT (map<frozen<tuple<int, int>>, frozen<tuple<bigint>>>){(pk,
ck) : (WRITETIME(t))} FROM %s"),
+                   row(map(tuple(1, 1), tuple(timestampInMicros))),
+                   row(map(tuple(1, 2), tuple(timestampInMicros))),
+                   row(map(tuple(1, 3), tuple(timestampInMicros))));
+        assertRows(execute("SELECT (map<frozen<tuple<int>> , frozen<tuple<int>>>){(min(ck))
: (max(ck))} FROM %s"),
+                   row(map(tuple(1), tuple(3))));
+
+        // Test UDTs nested within Maps
+        assertRows(execute("SELECT (map<int, frozen<" + type + ">>){ck : {a :
min(ck), b: max(ck)}} FROM %s"),
+                   row(map(1, userType("a", 1, "b", 3, "c", null))));
+        assertRows(execute("SELECT (map<int, frozen<" + type + ">>){ck : {a :
pk, b : ck, c : WRITETIME(t)}} FROM %s"),
+                   row(map(1, userType("a", 1, "b", 1, "c", timestampInMicros))),
+                   row(map(2, userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(map(3, userType("a", 1, "b", 3, "c", timestampInMicros))));
+        assertRows(execute("SELECT (map<int, frozen<" + type + ">>){ck : {a :
pk, b : ck, c : WRITETIME(t)}} FROM %s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(map(3, userType("a", 1, "b", 3, "c", timestampInMicros))),
+                   row(map(2, userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(map(1, userType("a", 1, "b", 1, "c", timestampInMicros))));
+
+        // Test Lists nested within Tuples
+        assertRows(execute("SELECT ([min(ck)], [max(ck)]) FROM %s"),
+                   row(tuple(list(1), list(3))));
+        assertRows(execute("SELECT ([pk], [ck]) FROM %s"),
+                   row(tuple(list(1), list(1))),
+                   row(tuple(list(1), list(2))),
+                   row(tuple(list(1), list(3))));
+
+        // Test Sets nested within Tuples
+        assertRows(execute("SELECT ({min(ck)}, {max(ck)}) FROM %s"),
+                   row(tuple(set(1), set(3))));
+        assertRows(execute("SELECT ({pk}, {ck}) FROM %s"),
+                   row(tuple(set(1), set(1))),
+                   row(tuple(set(1), set(2))),
+                   row(tuple(set(1), set(3))));
+
+        // Test Maps nested within Tuples
+        assertRows(execute("SELECT ((map<text, int>){'min' : min(ck)}, (map<text,
int>){'max' : max(ck)}) FROM %s"),
+                   row(tuple(map("min", 1), map("max", 3))));
+        assertRows(execute("SELECT ((map<text, int>){'pk' : pk}, (map<text, int>){'ck'
: ck}) FROM %s"),
+                   row(tuple(map("pk", 1), map("ck", 1))),
+                   row(tuple(map("pk", 1), map("ck", 2))),
+                   row(tuple(map("pk", 1), map("ck", 3))));
+
+        // Test Tuples nested within Tuples
+        assertRows(execute("SELECT (tuple<tuple<int, int, bigint>>)((pk, ck,
WRITETIME(t))) FROM %s"),
+                   row(tuple(tuple(1, 1, timestampInMicros))),
+                   row(tuple(tuple(1, 2, timestampInMicros))),
+                   row(tuple(tuple(1, 3, timestampInMicros))));
+        assertRows(execute("SELECT (tuple<tuple<int, int, bigint>>)((min(ck),
max(ck))) FROM %s"),
+                   row(tuple(tuple(1, 3))));
+
+        assertRows(execute("SELECT ((t, WRITETIME(t)), (CAST(pk AS BIGINT), CAST(ck AS BIGINT)))
FROM %s"),
+                   row(tuple(tuple("one", timestampInMicros), tuple(1L, 1L))),
+                   row(tuple(tuple("two", timestampInMicros), tuple(1L, 2L))),
+                   row(tuple(tuple("three", timestampInMicros), tuple(1L, 3L))));
+
+        // Test UDTs nested within Tuples
+        assertRows(execute("SELECT (tuple<" + type + ">)({a : min(ck), b: max(ck)})
FROM %s"),
+                   row(tuple(userType("a", 1, "b", 3, "c", null))));
+        assertRows(execute("SELECT (tuple<" + type + ">)({a : pk, b : ck, c : WRITETIME(t)})
FROM %s"),
+                   row(tuple(userType("a", 1, "b", 1, "c", timestampInMicros))),
+                   row(tuple(userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(tuple(userType("a", 1, "b", 3, "c", timestampInMicros))));
+        assertRows(execute("SELECT (tuple<" + type + ">)({a : pk, b : ck, c : WRITETIME(t)})
FROM %s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(tuple(userType("a", 1, "b", 3, "c", timestampInMicros))),
+                   row(tuple(userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(tuple(userType("a", 1, "b", 1, "c", timestampInMicros))));
+
+        // Test Lists nested within UDTs
+        String containerType = createType("CREATE TYPE %s(l list<int>)");
+        assertRows(execute("SELECT (" + containerType + "){l : [min(ck), max(ck)]} FROM %s"),
+                   row(userType("l", list(1, 3))));
+        assertRows(execute("SELECT (" + containerType + "){l : [pk, ck]} FROM %s"),
+                   row(userType("l", list(1, 1))),
+                   row(userType("l", list(1, 2))),
+                   row(userType("l", list(1, 3))));
+
+        // Test Sets nested within UDTs
+        containerType = createType("CREATE TYPE %s(s set<int>)");
+        assertRows(execute("SELECT (" + containerType + "){s : {min(ck), max(ck)}} FROM %s"),
+                   row(userType("s", set(1, 3))));
+        assertRows(execute("SELECT (" + containerType + "){s : {pk, ck}} FROM %s"),
+                   row(userType("s", set(1))),
+                   row(userType("s", set(1, 2))),
+                   row(userType("s", set(1, 3))));
+
+        // Test Maps nested within UDTs
+        containerType = createType("CREATE TYPE %s(m map<text, int>)");
+        assertRows(execute("SELECT (" + containerType + "){m : {'min' : min(ck), 'max' :
max(ck)}} FROM %s"),
+                   row(userType("m", map("min", 1, "max", 3))));
+        assertRows(execute("SELECT (" + containerType + "){m : {'pk' : pk, 'ck' : ck}} FROM
%s"),
+                   row(userType("m", map("pk", 1, "ck", 1))),
+                   row(userType("m", map("pk", 1, "ck", 2))),
+                   row(userType("m", map("pk", 1, "ck", 3))));
+
+        // Test Tuples nested within UDTs
+        containerType = createType("CREATE TYPE %s(t tuple<int, int>, w tuple<bigint>)");
+        assertRows(execute("SELECT (" + containerType + "){t : (pk, ck), w : (WRITETIME(t))}
FROM %s"),
+                   row(userType("t", tuple(1, 1), "w", tuple(timestampInMicros))),
+                   row(userType("t", tuple(1, 2), "w", tuple(timestampInMicros))),
+                   row(userType("t", tuple(1, 3), "w", tuple(timestampInMicros))));
+
+        // Test UDTs nested within Maps
+        containerType = createType("CREATE TYPE %s(t frozen<" + type + ">)");
+        assertRows(execute("SELECT (" + containerType + "){t : {a : min(ck), b: max(ck)}}
FROM %s"),
+                   row(userType("t", userType("a", 1, "b", 3, "c", null))));
+        assertRows(execute("SELECT (" + containerType + "){t : {a : pk, b : ck, c : WRITETIME(t)}}
FROM %s"),
+                   row(userType("t", userType("a", 1, "b", 1, "c", timestampInMicros))),
+                   row(userType("t", userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(userType("t", userType("a", 1, "b", 3, "c", timestampInMicros))));
+        assertRows(execute("SELECT (" + containerType + "){t : {a : pk, b : ck, c : WRITETIME(t)}}
FROM %s WHERE pk = 1 ORDER BY ck DESC"),
+                   row(userType("t", userType("a", 1, "b", 3, "c", timestampInMicros))),
+                   row(userType("t", userType("a", 1, "b", 2, "c", timestampInMicros))),
+                   row(userType("t", userType("a", 1, "b", 1, "c", timestampInMicros))));
+
+
+        // Test Litteral Set with Duration elements
+        assertInvalidMessage("Durations are not allowed inside sets: set<duration>",
+                             "SELECT pk, ck, (set<duration>){2d, 1mo} FROM %s");
+
+        assertInvalidMessage("Invalid field selection: system.min(ck) of type int is not
a user type",
+                             "SELECT min(ck).min FROM %s");
+        assertInvalidMessage("Invalid field selection: (map<text, int>){'min': system.min(ck),
'max': system.max(ck)} of type frozen<map<text, int>> is not a user type",
+                             "SELECT (map<text, int>) {'min' : min(ck), 'max' : max(ck)}.min
FROM %s");
+    }
+
+    @Test
+    public void testCollectionLiteralsWithDurations() throws Throwable
+    {
+        createTable("CREATE TABLE %s (pk int, ck int, d1 duration, d2 duration, PRIMARY KEY
(pk, ck) )");
+        execute("INSERT INTO %s (pk, ck, d1, d2) VALUES (1, 1, 15h, 13h)");
+        execute("INSERT INTO %s (pk, ck, d1, d2) VALUES (1, 2, 10h, 12h)");
+        execute("INSERT INTO %s (pk, ck, d1, d2) VALUES (1, 3, 11h, 13h)");
+
+        assertRows(execute("SELECT [d1, d2] FROM %s"),
+                   row(list(Duration.from("15h"), Duration.from("13h"))),
+                   row(list(Duration.from("10h"), Duration.from("12h"))),
+                   row(list(Duration.from("11h"), Duration.from("13h"))));
+
+        assertInvalidMessage("Durations are not allowed inside sets: frozen<set<duration>>",
"SELECT {d1, d2} FROM %s");
+
+        assertRows(execute("SELECT (map<int, duration>){ck : d1} FROM %s"),
+                   row(map(1, Duration.from("15h"))),
+                   row(map(2, Duration.from("10h"))),
+                   row(map(3, Duration.from("11h"))));
+
+        assertInvalidMessage("Durations are not allowed as map keys: map<duration, int>",
+                             "SELECT (map<duration, int>){d1 : ck, d2 :ck} FROM %s");
     }
 
     @Test
@@ -81,11 +419,41 @@ public class TermSelectionTest extends CQLTester
 
         execute("INSERT INTO %s(k, v) VALUES (?, ?)", 0, userType("a", 3, "b", "foo"));
 
-        assertInvalidMessage("Cannot infer type for term", "SELECT k, v, { a: 4, b: 'bar'}
FROM %s");
+        assertInvalidMessage("Cannot infer type for term", "SELECT { a: 4, b: 'bar'} FROM
%s");
 
         assertRows(execute("SELECT k, v, (" + type + "){ a: 4, b: 'bar'} FROM %s"),
             row(0, userType("a", 3, "b", "foo"), userType("a", 4, "b", "bar"))
         );
+
+        assertRows(execute("SELECT k, v, (" + type + ")({ a: 4, b: 'bar'}) FROM %s"),
+            row(0, userType("a", 3, "b", "foo"), userType("a", 4, "b", "bar"))
+        );
+
+        assertRows(execute("SELECT k, v, ((" + type + "){ a: 4, b: 'bar'}).a FROM %s"),
+                   row(0, userType("a", 3, "b", "foo"), 4)
+        );
+
+        assertRows(execute("SELECT k, v, (" + type + "){ a: 4, b: 'bar'}.a FROM %s"),
+                   row(0, userType("a", 3, "b", "foo"), 4)
+        );
+
+        assertInvalidMessage("Cannot infer type for term", "SELECT { a: 4} FROM %s");
+
+        assertRows(execute("SELECT k, v, (" + type + "){ a: 4} FROM %s"),
+            row(0, userType("a", 3, "b", "foo"), userType("a", 4, "b", null))
+        );
+
+        assertRows(execute("SELECT k, v, (" + type + "){ b: 'bar'} FROM %s"),
+                   row(0, userType("a", 3, "b", "foo"), userType("a", null, "b", "bar"))
+        );
+
+        execute("INSERT INTO %s(k, v) VALUES (?, ?)", 1, userType("a", 5, "b", "foo"));
+        assertRows(execute("SELECT (" + type + "){ a: max(v.a) , b: 'max'} FROM %s"),
+                   row(userType("a", 5, "b", "max"))
+        );
+        assertRows(execute("SELECT (" + type + "){ a: min(v.a) , b: 'min'} FROM %s"),
+                   row(userType("a", 3, "b", "min"))
+        );
     }
 
     @Test
@@ -221,11 +589,11 @@ public class TermSelectionTest extends CQLTester
         createFunctionOverload(fAmbiguousFunc1, "int,int",
                                                 "CREATE FUNCTION %s (val1 int, val2 int)
" +
                                                 "CALLED ON NULL INPUT " +
-                                                "RETURNS bigint " +
+                                                "RETURNS int " +
                                                 "LANGUAGE java\n" +
-                                                "AS 'return (long)Math.max(val1, val2);';");
-        assertInvalidMessage("Ambiguous call to function cql_test_keyspace.function_",
-                             "SELECT pk, " + fAmbiguousFunc1 + "(valInt, 100) FROM %s");
+                                                "AS 'return Math.max(val1, val2);';");
+        assertRows(execute("SELECT pk, " + fAmbiguousFunc1 + "(valInt, 100) FROM %s"),
+                   row(1, 100));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8b3de2f4/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java
index f167955..23a5a45 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java
@@ -4478,4 +4478,14 @@ public class SelectTest extends CQLTester
             });
         }
     }
+
+    @Test
+    public void testWithDistinctAndJsonAsColumnName() throws Throwable
+    {
+        createTable("CREATE TABLE %s (distinct int, json int, value int, PRIMARY KEY(distinct,
json))");
+        execute("INSERT INTO %s (distinct, json, value) VALUES (0, 0, 0)");
+
+        assertRows(execute("SELECT distinct, json FROM %s"), row(0, 0));
+        assertRows(execute("SELECT distinct distinct FROM %s"), row(0));
+    }
 }


Mime
View raw message