cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tylerho...@apache.org
Subject [1/3] cassandra git commit: JSON support for smallint, tinyint
Date Fri, 05 Aug 2016 21:36:28 GMT
Repository: cassandra
Updated Branches:
  refs/heads/cassandra-3.9 aab33f117 -> 5a902babc


JSON support for smallint, tinyint

Patch by Paweł Rychlik; reviewed by Tyler Hobbs for CASSANDRA-12371


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/dc8a017c
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/dc8a017c
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/dc8a017c

Branch: refs/heads/cassandra-3.9
Commit: dc8a017c7909abc4086ecbb0540c75c88991ab73
Parents: 3cda682
Author: Paweł Rychlik <pawel.rychlik@rspective.pl>
Authored: Thu Aug 4 12:22:24 2016 -0500
Committer: Tyler Hobbs <tylerlhobbs@gmail.com>
Committed: Fri Aug 5 16:33:27 2016 -0500

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../apache/cassandra/db/marshal/ByteType.java   | 19 ++---
 .../apache/cassandra/db/marshal/ShortType.java  | 19 ++---
 .../cql3/validation/entities/JsonTest.java      | 76 ++++++++++++++++++++
 4 files changed, 85 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/dc8a017c/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 67c79de..5ac79cf 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.8
+ * Fix INSERT JSON, fromJson() support of smallint, tinyint types (CASSANDRA-12371)
  * Restore JVM metric export for metric reporters (CASSANDRA-12312)
  * Release sstables of failed stream sessions only when outgoing transfers are finished (CASSANDRA-11345)
  * Revert CASSANDRA-11427 (CASSANDRA-12351)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/dc8a017c/src/java/org/apache/cassandra/db/marshal/ByteType.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/marshal/ByteType.java b/src/java/org/apache/cassandra/db/marshal/ByteType.java
index b63b92e..e1cacc3 100644
--- a/src/java/org/apache/cassandra/db/marshal/ByteType.java
+++ b/src/java/org/apache/cassandra/db/marshal/ByteType.java
@@ -62,22 +62,11 @@ public class ByteType extends AbstractType<Byte>
 
     public Term fromJSONObject(Object parsed) throws MarshalException
     {
-        try
-        {
-            if (parsed instanceof String)
-                return new Constants.Value(fromString((String) parsed));
-
-            Number parsedNumber = (Number) parsed;
-            if (!(parsedNumber instanceof Byte))
-                throw new MarshalException(String.format("Expected a byte value, but got
a %s: %s", parsed.getClass().getSimpleName(), parsed));
+        if (parsed instanceof String || parsed instanceof Number)
+            return new Constants.Value(fromString(String.valueOf(parsed)));
 
-            return new Constants.Value(getSerializer().serialize(parsedNumber.byteValue()));
-        }
-        catch (ClassCastException exc)
-        {
-            throw new MarshalException(String.format(
-                    "Expected a byte value, but got a %s: %s", parsed.getClass().getSimpleName(),
parsed));
-        }
+        throw new MarshalException(String.format(
+                "Expected a byte value, but got a %s: %s", parsed.getClass().getSimpleName(),
parsed));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cassandra/blob/dc8a017c/src/java/org/apache/cassandra/db/marshal/ShortType.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/marshal/ShortType.java b/src/java/org/apache/cassandra/db/marshal/ShortType.java
index 43e8f5e..2f9ec57 100644
--- a/src/java/org/apache/cassandra/db/marshal/ShortType.java
+++ b/src/java/org/apache/cassandra/db/marshal/ShortType.java
@@ -66,22 +66,11 @@ public class ShortType extends AbstractType<Short>
 
     public Term fromJSONObject(Object parsed) throws MarshalException
     {
-        try
-        {
-            if (parsed instanceof String)
-                return new Constants.Value(fromString((String) parsed));
-
-            Number parsedNumber = (Number) parsed;
-            if (!(parsedNumber instanceof Short))
-                throw new MarshalException(String.format("Expected a short value, but got
a %s: %s", parsed.getClass().getSimpleName(), parsed));
+        if (parsed instanceof String || parsed instanceof Number)
+            return new Constants.Value(fromString(String.valueOf(parsed)));
 
-            return new Constants.Value(getSerializer().serialize(parsedNumber.shortValue()));
-        }
-        catch (ClassCastException exc)
-        {
-            throw new MarshalException(String.format(
-                    "Expected a short value, but got a %s: %s", parsed.getClass().getSimpleName(),
parsed));
-        }
+        throw new MarshalException(String.format(
+                "Expected a short value, but got a %s: %s", parsed.getClass().getSimpleName(),
parsed));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cassandra/blob/dc8a017c/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index 4f49c3b..6892e79 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@ -65,10 +65,12 @@ public class JsonTest extends CQLTester
                 "floatval float, " +
                 "inetval inet, " +
                 "intval int, " +
+                "smallintval smallint, " +
                 "textval text, " +
                 "timeval time, " +
                 "timestampval timestamp, " +
                 "timeuuidval timeuuid, " +
+                "tinyintval tinyint, " +
                 "uuidval uuid," +
                 "varcharval varchar, " +
                 "varintval varint, " +
@@ -268,6 +270,48 @@ public class JsonTest extends CQLTester
         assertInvalidMessage("Expected an int value, but got a",
                 "INSERT INTO %s (k, intval) VALUES (?, fromJson(?))", 0, "true");
 
+        // ================ smallint ================
+        execute("INSERT INTO %s (k, smallintval) VALUES (?, fromJson(?))", 0, "32767");
+        assertRows(execute("SELECT k, smallintval FROM %s WHERE k = ?", 0), row(0, (short)
32767));
+
+        // strings are also accepted
+        execute("INSERT INTO %s (k, smallintval) VALUES (?, fromJson(?))", 0, "\"32767\"");
+        assertRows(execute("SELECT k, smallintval FROM %s WHERE k = ?", 0), row(0, (short)
32767));
+
+        // smallint overflow (Short.MAX_VALUE + 1)
+        assertInvalidMessage("Unable to make short from",
+                "INSERT INTO %s (k, smallintval) VALUES (?, fromJson(?))", 0, "32768");
+
+        assertInvalidMessage("Unable to make short from",
+                "INSERT INTO %s (k, smallintval) VALUES (?, fromJson(?))", 0, "123.456");
+
+        assertInvalidMessage("Unable to make short from",
+                "INSERT INTO %s (k, smallintval) VALUES (?, fromJson(?))", 0, "\"xyzz\"");
+
+        assertInvalidMessage("Expected a short value, but got a Boolean",
+                "INSERT INTO %s (k, smallintval) VALUES (?, fromJson(?))", 0, "true");
+
+        // ================ tinyint ================
+        execute("INSERT INTO %s (k, tinyintval) VALUES (?, fromJson(?))", 0, "127");
+        assertRows(execute("SELECT k, tinyintval FROM %s WHERE k = ?", 0), row(0, (byte)
127));
+
+        // strings are also accepted
+        execute("INSERT INTO %s (k, tinyintval) VALUES (?, fromJson(?))", 0, "\"127\"");
+        assertRows(execute("SELECT k, tinyintval FROM %s WHERE k = ?", 0), row(0, (byte)
127));
+
+        // tinyint overflow (Byte.MAX_VALUE + 1)
+        assertInvalidMessage("Unable to make byte from",
+                "INSERT INTO %s (k, tinyintval) VALUES (?, fromJson(?))", 0, "128");
+
+        assertInvalidMessage("Unable to make byte from",
+                "INSERT INTO %s (k, tinyintval) VALUES (?, fromJson(?))", 0, "123.456");
+
+        assertInvalidMessage("Unable to make byte from",
+                "INSERT INTO %s (k, tinyintval) VALUES (?, fromJson(?))", 0, "\"xyzz\"");
+
+        assertInvalidMessage("Expected a byte value, but got a Boolean",
+                "INSERT INTO %s (k, tinyintval) VALUES (?, fromJson(?))", 0, "true");
+
         // ================ text (varchar) ================
         execute("INSERT INTO %s (k, textval) VALUES (?, fromJson(?))", 0, "\"\"");
         assertRows(execute("SELECT k, textval FROM %s WHERE k = ?", 0), row(0, ""));
@@ -503,10 +547,12 @@ public class JsonTest extends CQLTester
                 "floatval float, " +
                 "inetval inet, " +
                 "intval int, " +
+                "smallintval smallint, " +
                 "textval text, " +
                 "timeval time, " +
                 "timestampval timestamp, " +
                 "timeuuidval timeuuid, " +
+                "tinyintval tinyint, " +
                 "uuidval uuid," +
                 "varcharval varchar, " +
                 "varintval varint, " +
@@ -600,6 +646,26 @@ public class JsonTest extends CQLTester
         execute("INSERT INTO %s (k, intval) VALUES (?, ?)", 0, -123123);
         assertRows(execute("SELECT k, toJson(intval) FROM %s WHERE k = ?", 0), row(0, "-123123"));
 
+        // ================ smallint ================
+        execute("INSERT INTO %s (k, smallintval) VALUES (?, ?)", 0, (short) 32767);
+        assertRows(execute("SELECT k, toJson(smallintval) FROM %s WHERE k = ?", 0), row(0,
"32767"));
+
+        execute("INSERT INTO %s (k, smallintval) VALUES (?, ?)", 0, (short) 0);
+        assertRows(execute("SELECT k, toJson(smallintval) FROM %s WHERE k = ?", 0), row(0,
"0"));
+
+        execute("INSERT INTO %s (k, smallintval) VALUES (?, ?)", 0, (short) -32768);
+        assertRows(execute("SELECT k, toJson(smallintval) FROM %s WHERE k = ?", 0), row(0,
"-32768"));
+
+        // ================ tinyint ================
+        execute("INSERT INTO %s (k, tinyintval) VALUES (?, ?)", 0, (byte) 127);
+        assertRows(execute("SELECT k, toJson(tinyintval) FROM %s WHERE k = ?", 0), row(0,
"127"));
+
+        execute("INSERT INTO %s (k, tinyintval) VALUES (?, ?)", 0, (byte) 0);
+        assertRows(execute("SELECT k, toJson(tinyintval) FROM %s WHERE k = ?", 0), row(0,
"0"));
+
+        execute("INSERT INTO %s (k, tinyintval) VALUES (?, ?)", 0, (byte) -128);
+        assertRows(execute("SELECT k, toJson(tinyintval) FROM %s WHERE k = ?", 0), row(0,
"-128"));
+
         // ================ text (varchar) ================
         execute("INSERT INTO %s (k, textval) VALUES (?, ?)", 0, "");
         assertRows(execute("SELECT k, toJson(textval) FROM %s WHERE k = ?", 0), row(0, "\"\""));
@@ -869,6 +935,8 @@ public class JsonTest extends CQLTester
                 "intmap map<int, boolean>, " +
                 "bigintmap map<bigint, boolean>, " +
                 "varintmap map<varint, boolean>, " +
+                "smallintmap map<smallint, boolean>, " +
+                "tinyintmap map<tinyint, boolean>, " +
                 "booleanmap map<boolean, boolean>, " +
                 "floatmap map<float, boolean>, " +
                 "doublemap map<double, boolean>, " +
@@ -893,6 +961,14 @@ public class JsonTest extends CQLTester
         execute("INSERT INTO %s JSON ?", "{\"k\": 0, \"varintmap\": {\"0\": true, \"1\":
false}}");
         assertRows(execute("SELECT JSON k, varintmap FROM %s"), row("{\"k\": 0, \"varintmap\":
{\"0\": true, \"1\": false}}"));
 
+        // smallint keys
+        execute("INSERT INTO %s JSON ?", "{\"k\": 0, \"smallintmap\": {\"0\": true, \"1\":
false}}");
+        assertRows(execute("SELECT JSON k, smallintmap FROM %s"), row("{\"k\": 0, \"smallintmap\":
{\"0\": true, \"1\": false}}"));
+
+        // tinyint keys
+        execute("INSERT INTO %s JSON ?", "{\"k\": 0, \"tinyintmap\": {\"0\": true, \"1\":
false}}");
+        assertRows(execute("SELECT JSON k, tinyintmap FROM %s"), row("{\"k\": 0, \"tinyintmap\":
{\"0\": true, \"1\": false}}"));
+
         // boolean keys
         execute("INSERT INTO %s JSON ?", "{\"k\": 0, \"booleanmap\": {\"true\": true, \"false\":
false}}");
         assertRows(execute("SELECT JSON k, booleanmap FROM %s"), row("{\"k\": 0, \"booleanmap\":
{\"false\": false, \"true\": true}}"));


Mime
View raw message