hudi-agent commented on code in PR #19310: URL: https://github.com/apache/hudi/pull/19310#discussion_r3633182831
########## hudi-common/src/main/java/org/apache/hudi/common/index/vector/VectorDistanceMetric.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.hudi.common.index.vector; + +import java.io.Serializable; + +/** + * Distance metrics for vector similarity search. + * + * <p>All metrics are returned as distances (smaller = more similar), + * so they can be compared uniformly with a min-heap. + */ +public enum VectorDistanceMetric implements Serializable { + + /** + * Cosine distance: 1 - cosine_similarity. + * Range: [0, 2]. 0 = identical direction, 2 = opposite. + */ + COSINE { + @Override + public float compute(float[] a, float[] b) { + checkDimensions(a, b); + double dot = 0; + double normA = 0; + double normB = 0; + for (int i = 0; i < a.length; i++) { + dot += (double) a[i] * b[i]; + normA += (double) a[i] * a[i]; + normB += (double) b[i] * b[i]; + } + double denom = Math.sqrt(normA) * Math.sqrt(normB); + return denom == 0.0 ? 1.0f : (float) (1.0 - dot / denom); + } + }, + + /** + * Euclidean (L2) distance. + * Range: [0, ∞). 0 = identical. + */ + L2 { + @Override + public float compute(float[] a, float[] b) { + checkDimensions(a, b); + double sum = 0; + for (int i = 0; i < a.length; i++) { + double d = (double) a[i] - b[i]; + sum += d * d; + } + return (float) Math.sqrt(sum); + } + }, + + /** + * Maximum inner product distance: negated dot product. + * Negated so smaller = higher similarity, consistent with the min-heap contract. + */ + DOT_PRODUCT { + @Override + public float compute(float[] a, float[] b) { + checkDimensions(a, b); + double dot = 0; + for (int i = 0; i < a.length; i++) { + dot += (double) a[i] * b[i]; + } + return (float) -dot; + } + }; + + /** + * Compute the distance between two float vectors. + * + * @param a first vector + * @param b second vector + * @return non-negative distance (smaller = more similar) + */ + public abstract float compute(float[] a, float[] b); + + /** + * Parse a metric name (case-insensitive). + * + * @param name e.g. "cosine", "l2", "dot_product" + * @return matching enum constant + */ + public static VectorDistanceMetric fromString(String name) { + return valueOf(name.toUpperCase().replace(" ", "_").replace("-", "_")); Review Comment: 🤖 `name.toUpperCase()` uses the default locale here. In a Turkish-locale JVM `"cosine"`/`"ivfflat"` uppercase to `COSİNE`/`İVFFLAT` (dotted İ), so `valueOf(...)` throws on otherwise-valid input. Elsewhere in hudi-common this is pinned to `Locale.ROOT` (e.g. `WriteConcurrencyMode`, `WriteOperationType`). Same pattern applies to `VectorIndexType.fromString` (line 57) and the `toUpperCase()`/`toLowerCase()` calls in `VectorIndexOptions` — `getQuantizer` (293), `getRaBitQStorage` (346), `getSearchMode` (376). Could you pin these to `Locale.ROOT`? <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag quality.</i></sub> -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
