cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sn...@apache.org
Subject cassandra git commit: Aggregate UDFs allow SFUNC return type to differ from STYPE if FFUNC specified
Date Fri, 08 May 2015 20:43:24 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 2da468d9b -> 40a7e8606


Aggregate UDFs allow SFUNC return type to differ from STYPE if FFUNC specified

Patch by Robert Stupp; Reviewed by Tyler Hobbs for CASSANDRA-9321


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

Branch: refs/heads/trunk
Commit: 40a7e8606eed7664de07653eb962cee3fa77d72b
Parents: 2da468d
Author: Robert Stupp <snazy@snazy.de>
Authored: Fri May 8 22:40:36 2015 +0200
Committer: Robert Stupp <snazy@snazy.de>
Committed: Fri May 8 22:40:36 2015 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../cassandra/cql3/functions/UDAggregate.java   |  2 ++
 .../statements/CreateAggregateStatement.java    | 14 +++++-----
 .../apache/cassandra/cql3/AggregationTest.java  | 28 ++++++++++++++++++++
 4 files changed, 39 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/40a7e860/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 0dbc281..bf98e7e 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 3.0
+ * Aggregate UDFs allow SFUNC return type to differ from STYPE if FFUNC specified (CASSANDRA-9321)
  * Failure detector detects and ignores local pauses (CASSANDRA-9183)
  * Remove Thrift dependencies in bundled tools (CASSANDRA-8358)
  * Disable memory mapping of hsperfdata file for JVM statistics (CASSANDRA-9242)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/40a7e860/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java b/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
index 7eed4f0..e4cbd55 100644
--- a/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
+++ b/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
@@ -100,6 +100,8 @@ public class UDAggregate extends AbstractFunction implements AggregateFunction
 
     public Iterable<Function> getFunctions()
     {
+        if (stateFunction == null)
+            return Collections.emptySet();
         if (finalFunction != null)
             return ImmutableSet.of(this, stateFunction, finalFunction);
         else

http://git-wip-us.apache.org/repos/asf/cassandra/blob/40a7e860/src/java/org/apache/cassandra/cql3/statements/CreateAggregateStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/CreateAggregateStatement.java b/src/java/org/apache/cassandra/cql3/statements/CreateAggregateStatement.java
index 021077e..74cc521 100644
--- a/src/java/org/apache/cassandra/cql3/statements/CreateAggregateStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/CreateAggregateStatement.java
@@ -36,7 +36,7 @@ import org.apache.cassandra.thrift.ThriftValidation;
 import org.apache.cassandra.transport.Event;
 
 /**
- * A <code>CREATE AGGREGATE</code> statement parsed from a CQL query.
+ * A {@code CREATE AGGREGATE} statement parsed from a CQL query.
  */
 public final class CreateAggregateStatement extends SchemaAlteringStatement
 {
@@ -91,20 +91,22 @@ public final class CreateAggregateStatement extends SchemaAlteringStatement
             throw new InvalidRequestException("State function " + stateFuncSig(stateFuncName,
stateTypeRaw, argRawTypes) + " does not exist or is not a scalar function");
         stateFunction = (ScalarFunction)f;
 
+        AbstractType<?> stateReturnType = stateFunction.returnType();
+        if (!stateReturnType.equals(stateType))
+            throw new InvalidRequestException("State function " + stateFuncSig(stateFunction.name(),
stateTypeRaw, argRawTypes) + " return type must be the same as the first argument type - check
STYPE, argument and return types");
+
         if (finalFunc != null)
         {
             FunctionName finalFuncName = new FunctionName(functionName.keyspace, finalFunc);
             f = Functions.find(finalFuncName, Collections.<AbstractType<?>>singletonList(stateType));
             if (!(f instanceof ScalarFunction))
-                throw new InvalidRequestException("Final function " + finalFuncName + "("
+ stateTypeRaw + ") does not exist or is not a scalar function");
+                throw new InvalidRequestException("Final function " + finalFuncName + '('
+ stateTypeRaw + ") does not exist or is not a scalar function");
             finalFunction = (ScalarFunction) f;
             returnType = finalFunction.returnType();
         }
         else
         {
-            returnType = stateFunction.returnType();
-            if (!returnType.equals(stateType))
-                throw new InvalidRequestException("State function " + stateFuncSig(stateFunction.name(),
stateTypeRaw, argRawTypes) + " return type must be the same as the first argument type (if
no final function is used)");
+            returnType = stateReturnType;
         }
 
         if (ival != null)
@@ -208,7 +210,7 @@ public final class CreateAggregateStatement extends SchemaAlteringStatement
         return true;
     }
 
-    private String stateFuncSig(FunctionName stateFuncName, CQL3Type.Raw stateTypeRaw, List<CQL3Type.Raw>
argRawTypes)
+    private static String stateFuncSig(FunctionName stateFuncName, CQL3Type.Raw stateTypeRaw,
List<CQL3Type.Raw> argRawTypes)
     {
         StringBuilder sb = new StringBuilder();
         sb.append(stateFuncName.toString()).append('(').append(stateTypeRaw);

http://git-wip-us.apache.org/repos/asf/cassandra/blob/40a7e860/test/unit/org/apache/cassandra/cql3/AggregationTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/AggregationTest.java b/test/unit/org/apache/cassandra/cql3/AggregationTest.java
index 86bd8f2..7fe665d 100644
--- a/test/unit/org/apache/cassandra/cql3/AggregationTest.java
+++ b/test/unit/org/apache/cassandra/cql3/AggregationTest.java
@@ -846,4 +846,32 @@ public class AggregationTest extends CQLTester
                                   "SELECT " + a + "(val) FROM %s");
     }
 
+    @Test
+    public void testWrongStateType() throws Throwable
+    {
+        createTable("CREATE TABLE %s (key int primary key, val int)");
+        execute("INSERT INTO %s (key, val) VALUES (?, ?)", 1, 1);
+
+        String fState = createFunction(KEYSPACE,
+                                       "int, int",
+                                       "CREATE FUNCTION %s(a int, b int) " +
+                                       "RETURNS double " +
+                                       "LANGUAGE java " +
+                                       "AS 'return Double.valueOf(1.0);'");
+
+        String fFinal = createFunction(KEYSPACE,
+                                       "int",
+                                       "CREATE FUNCTION %s(a int) " +
+                                       "RETURNS int " +
+                                       "LANGUAGE java " +
+                                       "AS 'return Integer.valueOf(1);';");
+
+        assertInvalidMessage("return type must be the same as the first argument type - check
STYPE, argument and return types",
+                                   "CREATE AGGREGATE %s(int) " +
+                                   "SFUNC " + shortFunctionName(fState) + ' ' +
+                                   "STYPE int " +
+                                   "FINALFUNC " + shortFunctionName(fFinal) + ' ' +
+                                   "INITCOND 1");
+    }
+
 }


Mime
View raw message