You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by sn...@apache.org on 2015/07/09 18:30:58 UTC
[1/3] cassandra git commit: hashCode in UDFunction broken
Repository: cassandra
Updated Branches:
refs/heads/cassandra-2.2 bf0328f48 -> af1c65843
refs/heads/trunk 8c22b4a81 -> 0c59c1f5b
hashCode in UDFunction broken
patch by Robert Stupp; reviewed by Aleksey Yeschenko for CASSANDRA-9750
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/af1c6584
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/af1c6584
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/af1c6584
Branch: refs/heads/cassandra-2.2
Commit: af1c65843efbcbd14ab8e1fefda7ec8650e71074
Parents: bf0328f
Author: Robert Stupp <sn...@snazy.de>
Authored: Thu Jul 9 23:27:31 2015 +0700
Committer: Robert Stupp <sn...@snazy.de>
Committed: Thu Jul 9 23:27:31 2015 +0700
----------------------------------------------------------------------
.../org/apache/cassandra/cql3/functions/Functions.java | 13 +++++++++++++
.../apache/cassandra/cql3/functions/UDAggregate.java | 2 +-
.../apache/cassandra/cql3/functions/UDFunction.java | 2 +-
3 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/af1c6584/src/java/org/apache/cassandra/cql3/functions/Functions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/functions/Functions.java b/src/java/org/apache/cassandra/cql3/functions/Functions.java
index 018c35c..85f2817 100644
--- a/src/java/org/apache/cassandra/cql3/functions/Functions.java
+++ b/src/java/org/apache/cassandra/cql3/functions/Functions.java
@@ -375,6 +375,19 @@ public abstract class Functions
return true;
}
+ public static int typeHashCode(AbstractType<?> t)
+ {
+ return t.asCQL3Type().toString().hashCode();
+ }
+
+ public static int typeHashCode(List<AbstractType<?>> types)
+ {
+ int h = 0;
+ for (AbstractType<?> type : types)
+ h = h * 31 + typeHashCode(type);
+ return h;
+ }
+
private static class FunctionsMigrationListener extends MigrationListener
{
public void onUpdateUserType(String ksName, String typeName) {
http://git-wip-us.apache.org/repos/asf/cassandra/blob/af1c6584/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 f153aed..c9fbbaa 100644
--- a/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
+++ b/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
@@ -210,6 +210,6 @@ public class UDAggregate extends AbstractFunction implements AggregateFunction
@Override
public int hashCode()
{
- return Objects.hashCode(name, argTypes, returnType, stateFunction, finalFunction, stateType, initcond);
+ return Objects.hashCode(name, Functions.typeHashCode(argTypes), Functions.typeHashCode(returnType), stateFunction, finalFunction, stateType, initcond);
}
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/af1c6584/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/functions/UDFunction.java b/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
index aa6d555..ab92232 100644
--- a/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
+++ b/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
@@ -276,7 +276,7 @@ public abstract class UDFunction extends AbstractFunction implements ScalarFunct
@Override
public int hashCode()
{
- return Objects.hashCode(name, argNames, argTypes, returnType, language, body);
+ return Objects.hashCode(name, Functions.typeHashCode(argTypes), Functions.typeHashCode(returnType), returnType, language, body);
}
public void userTypeUpdated(String ksName, String typeName)
[3/3] cassandra git commit: Merge branch 'cassandra-2.2' into trunk
Posted by sn...@apache.org.
Merge branch 'cassandra-2.2' into trunk
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/0c59c1f5
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/0c59c1f5
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/0c59c1f5
Branch: refs/heads/trunk
Commit: 0c59c1f5bc5a852c8023ec0b63a6d992dc0e1955
Parents: 8c22b4a af1c658
Author: Robert Stupp <sn...@snazy.de>
Authored: Thu Jul 9 23:30:05 2015 +0700
Committer: Robert Stupp <sn...@snazy.de>
Committed: Thu Jul 9 23:30:05 2015 +0700
----------------------------------------------------------------------
.../apache/cassandra/cql3/functions/UDAggregate.java | 2 +-
.../apache/cassandra/cql3/functions/UDFunction.java | 2 +-
src/java/org/apache/cassandra/schema/Functions.java | 13 +++++++++++++
3 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0c59c1f5/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0c59c1f5/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0c59c1f5/src/java/org/apache/cassandra/schema/Functions.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/schema/Functions.java
index 8d73e48,0000000..ccef1ed
mode 100644,000000..100644
--- a/src/java/org/apache/cassandra/schema/Functions.java
+++ b/src/java/org/apache/cassandra/schema/Functions.java
@@@ -1,216 -1,0 +1,229 @@@
+/*
+ * 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.schema;
+
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.google.common.collect.ImmutableMultimap;
+
+import org.apache.cassandra.cql3.functions.*;
+import org.apache.cassandra.db.marshal.AbstractType;
+
+import static com.google.common.collect.Iterables.filter;
+
+/**
+ * An immutable container for a keyspace's UDAs and UDFs (and, in case of {@link org.apache.cassandra.db.SystemKeyspace},
+ * native functions and aggregates).
+ */
+public final class Functions implements Iterable<Function>
+{
+ private final ImmutableMultimap<FunctionName, Function> functions;
+
+ private Functions(Builder builder)
+ {
+ functions = builder.functions.build();
+ }
+
+ public static Builder builder()
+ {
+ return new Builder();
+ }
+
+ public static Functions none()
+ {
+ return builder().build();
+ }
+
+ public Iterator<Function> iterator()
+ {
+ return functions.values().iterator();
+ }
+
+ public Stream<Function> stream()
+ {
+ return functions.values().stream();
+ }
+
+ /**
+ * @return a stream of keyspace's UDFs
+ */
+ public Stream<UDFunction> udfs()
+ {
+ return stream().filter(f -> f instanceof UDFunction).map(f -> (UDFunction) f);
+ }
+
+ /**
+ * @return a stream of keyspace's UDAs
+ */
+ public Stream<UDAggregate> udas()
+ {
+ return stream().filter(f -> f instanceof UDAggregate).map(f -> (UDAggregate) f);
+ }
+
+ /**
+ * @return a collection of aggregates that use the provided function as either a state or a final function
+ * @param function the referree function
+ */
+ public Collection<UDAggregate> aggregatesUsingFunction(Function function)
+ {
+ return udas().filter(uda -> uda.hasReferenceTo(function)).collect(Collectors.toList());
+ }
+
+ /**
+ * Get all function overloads with the specified name
+ *
+ * @param name fully qualified function name
+ * @return an empty list if the function name is not found; a non-empty collection of {@link Function} otherwise
+ */
+ public Collection<Function> get(FunctionName name)
+ {
+ return functions.get(name);
+ }
+
+ /**
+ * Find the function with the specified name
+ *
+ * @param name fully qualified function name
+ * @param argTypes function argument types
+ * @return an empty {@link Optional} if the function name is not found; a non-empty optional of {@link Function} otherwise
+ */
+ public Optional<Function> find(FunctionName name, List<AbstractType<?>> argTypes)
+ {
+ return get(name).stream()
+ .filter(fun -> typesMatch(fun.argTypes(), argTypes))
+ .findAny();
+ }
+
+ /*
+ * We need to compare the CQL3 representation of the type because comparing
+ * the AbstractType will fail for example if a UDT has been changed.
+ * Reason is that UserType.equals() takes the field names and types into account.
+ * Example CQL sequence that would fail when comparing AbstractType:
+ * CREATE TYPE foo ...
+ * CREATE FUNCTION bar ( par foo ) RETURNS foo ...
+ * ALTER TYPE foo ADD ...
+ * or
+ * ALTER TYPE foo ALTER ...
+ * or
+ * ALTER TYPE foo RENAME ...
+ */
+ public static boolean typesMatch(AbstractType<?> t1, AbstractType<?> t2)
+ {
+ return t1.asCQL3Type().toString().equals(t2.asCQL3Type().toString());
+ }
+
+ public static boolean typesMatch(List<AbstractType<?>> t1, List<AbstractType<?>> t2)
+ {
+ if (t1.size() != t2.size())
+ return false;
+
+ for (int i = 0; i < t1.size(); i++)
+ if (!typesMatch(t1.get(i), t2.get(i)))
+ return false;
+
+ return true;
+ }
+
++ public static int typeHashCode(AbstractType<?> t)
++ {
++ return t.asCQL3Type().toString().hashCode();
++ }
++
++ public static int typeHashCode(List<AbstractType<?>> types)
++ {
++ int h = 0;
++ for (AbstractType<?> type : types)
++ h = h * 31 + typeHashCode(type);
++ return h;
++ }
++
+ /**
+ * Create a Functions instance with the provided function added
+ */
+ public Functions with(Function fun)
+ {
+ if (find(fun.name(), fun.argTypes()).isPresent())
+ throw new IllegalStateException(String.format("Function %s already exists", fun.name()));
+
+ return builder().add(this).add(fun).build();
+ }
+
+ /**
+ * Creates a Functions instance with the function with the provided name and argument types removed
+ */
+ public Functions without(FunctionName name, List<AbstractType<?>> argTypes)
+ {
+ Function fun =
+ find(name, argTypes).orElseThrow(() -> new IllegalStateException(String.format("Function %s doesn't exists", name)));
+
+ return builder().add(filter(this, f -> f != fun)).build();
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ return this == o || (o instanceof Functions && functions.equals(((Functions) o).functions));
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return functions.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ return functions.values().toString();
+ }
+
+ public static final class Builder
+ {
+ final ImmutableMultimap.Builder<FunctionName, Function> functions = new ImmutableMultimap.Builder<>();
+
+ private Builder()
+ {
+ }
+
+ public Functions build()
+ {
+ return new Functions(this);
+ }
+
+ public Builder add(Function fun)
+ {
+ functions.put(fun.name(), fun);
+ return this;
+ }
+
+ public Builder add(Function... funs)
+ {
+ for (Function fun : funs)
+ add(fun);
+ return this;
+ }
+
+ public Builder add(Iterable<? extends Function> funs)
+ {
+ funs.forEach(this::add);
+ return this;
+ }
+ }
+}
[2/3] cassandra git commit: hashCode in UDFunction broken
Posted by sn...@apache.org.
hashCode in UDFunction broken
patch by Robert Stupp; reviewed by Aleksey Yeschenko for CASSANDRA-9750
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/af1c6584
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/af1c6584
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/af1c6584
Branch: refs/heads/trunk
Commit: af1c65843efbcbd14ab8e1fefda7ec8650e71074
Parents: bf0328f
Author: Robert Stupp <sn...@snazy.de>
Authored: Thu Jul 9 23:27:31 2015 +0700
Committer: Robert Stupp <sn...@snazy.de>
Committed: Thu Jul 9 23:27:31 2015 +0700
----------------------------------------------------------------------
.../org/apache/cassandra/cql3/functions/Functions.java | 13 +++++++++++++
.../apache/cassandra/cql3/functions/UDAggregate.java | 2 +-
.../apache/cassandra/cql3/functions/UDFunction.java | 2 +-
3 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/af1c6584/src/java/org/apache/cassandra/cql3/functions/Functions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/functions/Functions.java b/src/java/org/apache/cassandra/cql3/functions/Functions.java
index 018c35c..85f2817 100644
--- a/src/java/org/apache/cassandra/cql3/functions/Functions.java
+++ b/src/java/org/apache/cassandra/cql3/functions/Functions.java
@@ -375,6 +375,19 @@ public abstract class Functions
return true;
}
+ public static int typeHashCode(AbstractType<?> t)
+ {
+ return t.asCQL3Type().toString().hashCode();
+ }
+
+ public static int typeHashCode(List<AbstractType<?>> types)
+ {
+ int h = 0;
+ for (AbstractType<?> type : types)
+ h = h * 31 + typeHashCode(type);
+ return h;
+ }
+
private static class FunctionsMigrationListener extends MigrationListener
{
public void onUpdateUserType(String ksName, String typeName) {
http://git-wip-us.apache.org/repos/asf/cassandra/blob/af1c6584/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 f153aed..c9fbbaa 100644
--- a/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
+++ b/src/java/org/apache/cassandra/cql3/functions/UDAggregate.java
@@ -210,6 +210,6 @@ public class UDAggregate extends AbstractFunction implements AggregateFunction
@Override
public int hashCode()
{
- return Objects.hashCode(name, argTypes, returnType, stateFunction, finalFunction, stateType, initcond);
+ return Objects.hashCode(name, Functions.typeHashCode(argTypes), Functions.typeHashCode(returnType), stateFunction, finalFunction, stateType, initcond);
}
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/af1c6584/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/functions/UDFunction.java b/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
index aa6d555..ab92232 100644
--- a/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
+++ b/src/java/org/apache/cassandra/cql3/functions/UDFunction.java
@@ -276,7 +276,7 @@ public abstract class UDFunction extends AbstractFunction implements ScalarFunct
@Override
public int hashCode()
{
- return Objects.hashCode(name, argNames, argTypes, returnType, language, body);
+ return Objects.hashCode(name, Functions.typeHashCode(argTypes), Functions.typeHashCode(returnType), returnType, language, body);
}
public void userTypeUpdated(String ksName, String typeName)