Repository: cassandra Updated Branches: refs/heads/cassandra-3.0 a1a260c9f -> 145583784
Fix CASSANDRA-9771 patch by Robert Stupp; reviewed by Aleksey Yeschenko for CASSANDRA-10040 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/fec40fd4 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/fec40fd4 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/fec40fd4 Branch: refs/heads/cassandra-3.0 Commit: fec40fd43b9a1fd694a88ff5186ff614a28ef895 Parents: 1960e5b Author: Robert Stupp <[email protected]> Authored: Tue Aug 11 22:24:41 2015 +0200 Committer: Robert Stupp <[email protected]> Committed: Tue Aug 11 22:24:41 2015 +0200 ---------------------------------------------------------------------- src/java/org/apache/cassandra/cql3/Cql.g | 4 +- .../statements/CreateAggregateStatement.java | 24 +++------ .../validation/operations/AggregationTest.java | 56 +++++++++++--------- 3 files changed, 40 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/fec40fd4/src/java/org/apache/cassandra/cql3/Cql.g ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/Cql.g b/src/java/org/apache/cassandra/cql3/Cql.g index 094b72e..0db09b8 100644 --- a/src/java/org/apache/cassandra/cql3/Cql.g +++ b/src/java/org/apache/cassandra/cql3/Cql.g @@ -557,10 +557,10 @@ createAggregateStatement returns [CreateAggregateStatement expr] ( ',' v=comparatorType { argsTypes.add(v); } )* )? ')' - K_SFUNC sfunc = functionName + K_SFUNC sfunc = allowedFunctionName K_STYPE stype = comparatorType ( - K_FINALFUNC ffunc = functionName + K_FINALFUNC ffunc = allowedFunctionName )? ( K_INITCOND ival = term http://git-wip-us.apache.org/repos/asf/cassandra/blob/fec40fd4/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 1d73e3f..5ee7e33 100644 --- a/src/java/org/apache/cassandra/cql3/statements/CreateAggregateStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/CreateAggregateStatement.java @@ -27,7 +27,6 @@ import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.config.Schema; import org.apache.cassandra.cql3.*; import org.apache.cassandra.cql3.functions.*; -import org.apache.cassandra.db.SystemKeyspace; import org.apache.cassandra.db.marshal.AbstractType; import org.apache.cassandra.exceptions.*; import org.apache.cassandra.service.ClientState; @@ -62,17 +61,17 @@ public final class CreateAggregateStatement extends SchemaAlteringStatement public CreateAggregateStatement(FunctionName functionName, List<CQL3Type.Raw> argRawTypes, - FunctionName stateFunc, + String stateFunc, CQL3Type.Raw stateType, - FunctionName finalFunc, + String finalFunc, Term.Raw ival, boolean orReplace, boolean ifNotExists) { this.functionName = functionName; this.argRawTypes = argRawTypes; - this.stateFunc = stateFunc; - this.finalFunc = finalFunc; + this.stateFunc = new FunctionName(functionName.keyspace, stateFunc); + this.finalFunc = finalFunc != null ? new FunctionName(functionName.keyspace, finalFunc) : null; this.stateTypeRaw = stateType; this.ival = ival; this.orReplace = orReplace; @@ -88,7 +87,6 @@ public final class CreateAggregateStatement extends SchemaAlteringStatement AbstractType<?> stateType = prepareType("state type", stateTypeRaw); List<AbstractType<?>> stateArgs = stateArguments(stateType, argTypes); - stateFunc = validateFunctionKeyspace(stateFunc); Function f = Functions.find(stateFunc, stateArgs); if (!(f instanceof ScalarFunction)) @@ -102,7 +100,6 @@ public final class CreateAggregateStatement extends SchemaAlteringStatement if (finalFunc != null) { List<AbstractType<?>> finalArgs = Collections.<AbstractType<?>>singletonList(stateType); - finalFunc = validateFunctionKeyspace(finalFunc); f = Functions.find(finalFunc, finalArgs); if (!(f instanceof ScalarFunction)) throw new InvalidRequestException("Final function " + finalFunc + '(' + stateTypeRaw + ") does not exist or is not a scalar function"); @@ -148,17 +145,10 @@ public final class CreateAggregateStatement extends SchemaAlteringStatement throw new InvalidRequestException("Functions must be fully qualified with a keyspace name if a keyspace is not set for the session"); ThriftValidation.validateKeyspaceNotSystem(functionName.keyspace); - } - private FunctionName validateFunctionKeyspace(FunctionName func) - { - if (!func.hasKeyspace()) - return new FunctionName(functionName.keyspace, func.name); - else if (!SystemKeyspace.NAME.equals(func.keyspace) && !functionName.keyspace.equals(func.keyspace)) - throw new InvalidRequestException(String.format("Statement on keyspace %s cannot refer to a user function in keyspace %s; " - + "user functions can only be used in the keyspace they are defined in", - functionName.keyspace, func.keyspace)); - return func; + stateFunc = new FunctionName(functionName.keyspace, stateFunc.name); + if (finalFunc != null) + finalFunc = new FunctionName(functionName.keyspace, finalFunc.name); } protected void grantPermissionsToCreator(QueryState state) http://git-wip-us.apache.org/repos/asf/cassandra/blob/fec40fd4/test/unit/org/apache/cassandra/cql3/validation/operations/AggregationTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/AggregationTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/AggregationTest.java index 120fc21..6a77368 100644 --- a/test/unit/org/apache/cassandra/cql3/validation/operations/AggregationTest.java +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/AggregationTest.java @@ -33,6 +33,7 @@ import org.apache.cassandra.cql3.UntypedResultSet; import org.apache.cassandra.cql3.UntypedResultSet.Row; import org.apache.cassandra.cql3.functions.Functions; import org.apache.cassandra.cql3.functions.UDAggregate; +import org.apache.cassandra.db.SystemKeyspace; import org.apache.cassandra.exceptions.FunctionExecutionException; import org.apache.cassandra.exceptions.InvalidRequestException; import org.apache.cassandra.service.ClientState; @@ -1233,21 +1234,26 @@ public class AggregationTest extends CQLTester "FINALFUNC " + shortFunctionName(fFinal) + ' ' + "INITCOND 1"); - assertInvalidMessage(String.format("Statement on keyspace %s cannot refer to a user type in keyspace %s; user types can only be used in the keyspace they are defined in", - KEYSPACE_PER_TEST, KEYSPACE), + assertInvalidMessage("mismatched input", // specifying a function using "keyspace.functionname" is a syntax error "CREATE AGGREGATE " + KEYSPACE_PER_TEST + ".test_wrong_ks(int) " + "SFUNC " + fStateWrong + ' ' + "STYPE " + type + " " + "FINALFUNC " + shortFunctionName(fFinal) + ' ' + "INITCOND 1"); - assertInvalidMessage(String.format("Statement on keyspace %s cannot refer to a user type in keyspace %s; user types can only be used in the keyspace they are defined in", - KEYSPACE_PER_TEST, KEYSPACE), + assertInvalidMessage("missing EOF", // specifying a function using "keyspace.functionname" is a syntax error "CREATE AGGREGATE " + KEYSPACE_PER_TEST + ".test_wrong_ks(int) " + "SFUNC " + shortFunctionName(fState) + ' ' + "STYPE " + type + " " + "FINALFUNC " + fFinalWrong + ' ' + "INITCOND 1"); + + assertInvalidMessage("missing EOF", // specifying a function using "keyspace.functionname" is a syntax error + "CREATE AGGREGATE " + KEYSPACE_PER_TEST + ".test_wrong_ks(int) " + + "SFUNC " + shortFunctionName(fState) + ' ' + + "STYPE " + type + ' ' + + "FINALFUNC " + SystemKeyspace.NAME + ".min " + + "INITCOND 1"); } @Test @@ -1279,17 +1285,17 @@ public class AggregationTest extends CQLTester assertInvalidMessage("The function state type should not be frozen", "CREATE AGGREGATE %s(set<int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE frozen<set<int>> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); String aggregation = createAggregate(KEYSPACE, "set<int>", "CREATE AGGREGATE %s(set<int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE set<int> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); assertRows(execute("SELECT " + aggregation + "(b) FROM %s"), @@ -1328,17 +1334,17 @@ public class AggregationTest extends CQLTester assertInvalidMessage("The function state type should not be frozen", "CREATE AGGREGATE %s(list<int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE frozen<list<int>> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + " " + "INITCOND null"); String aggregation = createAggregate(KEYSPACE, "list<int>", "CREATE AGGREGATE %s(list<int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE list<int> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); assertRows(execute("SELECT " + aggregation + "(b) FROM %s"), @@ -1377,17 +1383,17 @@ public class AggregationTest extends CQLTester assertInvalidMessage("The function state type should not be frozen", "CREATE AGGREGATE %s(map<int, int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE frozen<map<int, int>> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); String aggregation = createAggregate(KEYSPACE, "map<int, int>", "CREATE AGGREGATE %s(map<int, int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE map<int, int> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); assertRows(execute("SELECT " + aggregation + "(b) FROM %s"), @@ -1426,17 +1432,17 @@ public class AggregationTest extends CQLTester assertInvalidMessage("The function state type should not be frozen", "CREATE AGGREGATE %s(tuple<int, int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE frozen<tuple<int, int>> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); String aggregation = createAggregate(KEYSPACE, "tuple<int, int>", "CREATE AGGREGATE %s(tuple<int, int>) " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE tuple<int, int> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); assertRows(execute("SELECT " + aggregation + "(b) FROM %s"), @@ -1476,17 +1482,17 @@ public class AggregationTest extends CQLTester assertInvalidMessage("The function state type should not be frozen", "CREATE AGGREGATE %s(" + myType + ") " + - "SFUNC " + fState + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + "STYPE frozen<" + myType + "> " + - "FINALFUNC " + fFinal + " " + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); String aggregation = createAggregate(KEYSPACE, myType, "CREATE AGGREGATE %s(" + myType + ") " + - "SFUNC " + fState + " " + - "STYPE " + myType + " " + - "FINALFUNC " + fFinal + " " + + "SFUNC " + parseFunctionName(fState).name + ' ' + + "STYPE " + myType + ' ' + + "FINALFUNC " + parseFunctionName(fFinal).name + ' ' + "INITCOND null"); assertRows(execute("SELECT " + aggregation + "(b).f FROM %s"),
