You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "Maxwell-Guo (via GitHub)" <gi...@apache.org> on 2023/06/30 06:23:59 UTC

[GitHub] [cassandra] Maxwell-Guo commented on a diff in pull request #2459: CASSANDRA-18640 trunk: Add vector similarity functions

Maxwell-Guo commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1247471388


##########
src/java/org/apache/cassandra/cql3/functions/FunctionFactory.java:
##########
@@ -101,9 +112,14 @@ public NativeFunction getOrCreateFunction(List<? extends AssignmentTestable> arg
                 throw new InvalidRequestException(String.format("Cannot infer type of argument %s in call to " +
                                                                 "function %s: use type casts to disambiguate",
                                                                 arg, this));
-            parameter.validateType(name, arg, type);
             type = type.udfType();
-            types.add(type);
+            types.set(i, type);
+        }
+
+        // Validate the inferred types of the arguments, again favouring a left-to-right reading.
+        for (int i = 0; i < args.size(); i++)
+        {
+            parameters.get(i).validateType(name, args.get(i), types.get(i));

Review Comment:
   why we validateType them independently here? Can't we move back to line 115? Can you help give me an example about support "left to right", you know we have change the loop about the args from one to three . If there are many parameters(dimention), there may be three times the overhead
   



##########
src/java/org/apache/cassandra/cql3/functions/FunctionParameter.java:
##########
@@ -336,4 +341,55 @@ public String toString()
             }
         };
     }
+
+    /**
+     * @param type the type of the vector elements
+     * @return a function parameter definition that accepts values of type {@link VectorType} with elements of the
+     * specified {@code type} and any dimensions.
+     */
+    static FunctionParameter vector(CQL3Type type)
+    {
+        return new FunctionParameter()
+        {
+            @Override
+            public AbstractType<?> inferType(String keyspace,
+                                             AssignmentTestable arg,
+                                             @Nullable AbstractType<?> receiverType,
+                                             List<AbstractType<?>> inferredTypes)

Review Comment:
   do we need a  annotation  of nullable for infferredType as line 60 ?



##########
src/java/org/apache/cassandra/cql3/functions/VectorFcts.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.cql3.functions;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+import org.apache.cassandra.cql3.CQL3Type;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.FloatType;
+import org.apache.cassandra.db.marshal.VectorType;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.transport.ProtocolVersion;
+import org.apache.lucene.index.VectorSimilarityFunction;
+
+public class VectorFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(similarity_function("similarity_cosine", VectorSimilarityFunction.COSINE, false));
+        functions.add(similarity_function("similarity_euclidean", VectorSimilarityFunction.EUCLIDEAN, true));
+        functions.add(similarity_function("similarity_dot_product", VectorSimilarityFunction.DOT_PRODUCT, true));
+    }
+
+    private static FunctionFactory similarity_function(String name,

Review Comment:
   can we rename the function name to similarityFunction?



##########
src/java/org/apache/cassandra/cql3/functions/FunctionParameter.java:
##########
@@ -83,9 +86,9 @@ static FunctionParameter optional(FunctionParameter wrapped)
             public AbstractType<?> inferType(String keyspace,
                                              AssignmentTestable arg,
                                              @Nullable AbstractType<?> receiverType,
-                                             List<AbstractType<?>> previousTypes)
+                                             List<AbstractType<?>> inferredTypes)

Review Comment:
   do we need a  annotation  of nullable for infferredType as line 60 ?



##########
src/java/org/apache/cassandra/cql3/functions/FunctionParameter.java:
##########
@@ -130,7 +133,7 @@ static FunctionParameter fixed(CQL3Type... types)
             public AbstractType<?> inferType(String keyspace,
                                              AssignmentTestable arg,
                                              @Nullable AbstractType<?> receiverType,
-                                             List<AbstractType<?>> previousTypes)
+                                             List<AbstractType<?>> inferredTypes)

Review Comment:
   do we need a  annotation  of nullable for infferredType as line 60 ?



##########
src/java/org/apache/cassandra/cql3/functions/FunctionParameter.java:
##########
@@ -168,7 +171,7 @@ static FunctionParameter anyType(boolean inferFromReceiver)
             public AbstractType<?> inferType(String keyspace,
                                              AssignmentTestable arg,
                                              @Nullable AbstractType<?> receiverType,
-                                             List<AbstractType<?>> previousTypes)
+                                             List<AbstractType<?>> inferredTypes)

Review Comment:
   do we need a  annotation  of nullable for infferredType as line 60 ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org