You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "xiangfu0 (via GitHub)" <gi...@apache.org> on 2023/07/31 19:39:38 UTC

[GitHub] [pinot] xiangfu0 commented on a diff in pull request #11222: [vector] Adding vector functions

xiangfu0 commented on code in PR #11222:
URL: https://github.com/apache/pinot/pull/11222#discussion_r1279793652


##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/VectorFunctions.java:
##########
@@ -0,0 +1,134 @@
+/**
+ * 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.pinot.common.function.scalar;
+
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+/**
+ * Inbuilt Vector Transformation Functions
+ * The functions can be used as UDFs in Query when added in the FunctionRegistry.
+ * @ScalarFunction annotation is used with each method for the registration
+ *
+ * Example usage:
+ */
+public class VectorFunctions {
+  private VectorFunctions() {
+  }
+
+  /**
+   * Returns the cosine distance between two vectors
+   * @param vector1 vector1
+   * @param vector2 vector2
+   * @return cosine distance
+   */
+  @ScalarFunction(names = {"cosinedistance", "cosine_distance"})
+  public static double cosineDistance(float[] vector1, float[] vector2) {
+    if (vector1.length != vector2.length) {
+      throw new IllegalArgumentException("Vector lengths do not match");
+    }
+    double dotProduct = 0.0;
+    double norm1 = 0.0;
+    double norm2 = 0.0;
+    for (int i = 0; i < vector1.length; i++) {
+      dotProduct += vector1[i] * vector2[i];
+      norm1 += Math.pow(vector1[i], 2);
+      norm2 += Math.pow(vector2[i], 2);
+    }
+    return 1 - (dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2)));

Review Comment:
   Make `cosineDistance` can take third optional argument default value, which is used when either vector has a norm of 0



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org