You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "adelapena (via GitHub)" <gi...@apache.org> on 2023/06/29 17:53:01 UTC

[GitHub] [cassandra] adelapena opened a new pull request, #2459: CASSANDRA-18640 trunk: Add vector similarity functions

adelapena opened a new pull request, #2459:
URL: https://github.com/apache/cassandra/pull/2459

   Add new CQL functions for vector similarity:
   * similarity_cosine
   * similarity_euclidean
   * similarity_dot_product


-- 
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


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

Posted by "adelapena (via GitHub)" <gi...@apache.org>.
adelapena commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1247799836


##########
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:
   We do, added



-- 
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


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

Posted by "adelapena (via GitHub)" <gi...@apache.org>.
adelapena commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1247799702


##########
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:
   We do, added



##########
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:
   We do, added



-- 
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


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

Posted by "ekaterinadimitrova2 (via GitHub)" <gi...@apache.org>.
ekaterinadimitrova2 commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1258846311


##########
doc/modules/cassandra/partials/vector_functions.adoc:
##########
@@ -0,0 +1,41 @@
+[cols=",",options="header",]
+|===
+|Function | Description
+
+| `similarity_cosine(vector, vector)` | Calculates the cosine similarity score between two float vectors of the same dimension.
+
+Examples:
+
+`similarity_cosine([0.1, 0.2], null)` -> `null`
+
+`similarity_cosine([0.1, 0.2], [0.1, 0.2])` -> `1`
+
+`similarity_cosine([0.1, 0.2], [-0.1, -0.2])` -> `0`
+
+`similarity_cosine([0.1, 0.2], [0.9, 0.8])` -> `0.964238`

Review Comment:
   is this one correct?



##########
src/java/org/apache/cassandra/cql3/CQL3Type.java:
##########
@@ -867,6 +867,12 @@ public CQL3Type prepare(String keyspace, Types udts) throws InvalidRequestExcept
                 CQL3Type type = element.prepare(keyspace, udts);
                 return new Vector(type.getType(), dimention);
             }
+
+            @Override
+            public String toString()
+            {
+                return "vector<" + element.toString() + ", " + dimention + '>';

Review Comment:
   ```suggestion
                   return "vector<" + element.toString() + ", " + dimension + '>';
   ```



##########
test/unit/org/apache/cassandra/cql3/functions/VectorFctsTest.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.util.Arrays;
+import java.util.Collection;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.lucene.index.VectorSimilarityFunction;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+@RunWith(Parameterized.class)
+public class VectorFctsTest extends CQLTester
+{
+    @Parameterized.Parameter
+    public String function;
+
+    @Parameterized.Parameter(1)
+    public VectorSimilarityFunction luceneFunction;
+
+    @Parameterized.Parameters(name = "{index}: function={0}")
+    public static Collection<Object[]> data()
+    {
+        return Arrays.asList(new Object[][]{
+        { "system.similarity_cosine", VectorSimilarityFunction.COSINE },
+        { "system.similarity_euclidean", VectorSimilarityFunction.EUCLIDEAN },
+        { "system.similarity_dot_product", VectorSimilarityFunction.DOT_PRODUCT }
+        });
+    }
+
+    @Test
+    public void testVectorSimilarityFunction()
+    {
+        createTable(KEYSPACE, "CREATE TABLE %s (pk int primary key,  value vector<float, 2>, " +

Review Comment:
   ```suggestion
           createTable(KEYSPACE, "CREATE TABLE %s (pk int PRIMARY KEY,  value vector<float, 2>, " +
   ```



-- 
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


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

Posted by "adelapena (via GitHub)" <gi...@apache.org>.
adelapena commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1277385746


##########
src/java/org/apache/cassandra/cql3/CQL3Type.java:
##########
@@ -867,6 +867,12 @@ public CQL3Type prepare(String keyspace, Types udts) throws InvalidRequestExcept
                 CQL3Type type = element.prepare(keyspace, udts);
                 return new Vector(type.getType(), dimention);
             }
+
+            @Override
+            public String toString()
+            {
+                return "vector<" + element.toString() + ", " + dimention + '>';

Review Comment:
   Good catch. This misspelling was pointed out during the review of CASSANDRA-18504, but we still missed it in some places. I think I have fixed it everywhere.



-- 
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


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

Posted by "Maxwell-Guo (via GitHub)" <gi...@apache.org>.
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


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

Posted by "adelapena (via GitHub)" <gi...@apache.org>.
adelapena commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1247800362


##########
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:
   I have renamed it to `createSimilarityFunctionFactory`.



-- 
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


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

Posted by "adelapena (via GitHub)" <gi...@apache.org>.
adelapena commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1247799040


##########
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:
   Indeed we can drop the reverse iteration and have more consistent error messages with some fixes on `FunctionParameter.sameAs`.
   



-- 
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


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

Posted by "adelapena (via GitHub)" <gi...@apache.org>.
adelapena commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1247799315


##########
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:
   We do, added



-- 
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


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

Posted by "ekaterinadimitrova2 (via GitHub)" <gi...@apache.org>.
ekaterinadimitrova2 commented on code in PR #2459:
URL: https://github.com/apache/cassandra/pull/2459#discussion_r1258846311


##########
doc/modules/cassandra/partials/vector_functions.adoc:
##########
@@ -0,0 +1,41 @@
+[cols=",",options="header",]
+|===
+|Function | Description
+
+| `similarity_cosine(vector, vector)` | Calculates the cosine similarity score between two float vectors of the same dimension.
+
+Examples:
+
+`similarity_cosine([0.1, 0.2], null)` -> `null`
+
+`similarity_cosine([0.1, 0.2], [0.1, 0.2])` -> `1`
+
+`similarity_cosine([0.1, 0.2], [-0.1, -0.2])` -> `0`
+
+`similarity_cosine([0.1, 0.2], [0.9, 0.8])` -> `0.964238`

Review Comment:
   is this one correct?



-- 
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