jerry-024 commented on code in PR #110: URL: https://github.com/apache/paimon-vector-index/pull/110#discussion_r4068057688
########## java/src/main/java/org/apache/paimon/index/vector/VectorRangeSearchResult.java: ########## @@ -0,0 +1,242 @@ +// 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.paimon.index.vector; + +import java.util.Arrays; +import java.util.Objects; + +/** + * CSR range-search output in core scan order, with raw distances and no sorting or top-K cap. + * IVF-Flat distances are exact; SQ, PQ and RQ return their core distance estimates. Each query + * occupies [lims[query], lims[query + 1]). Public construction and array-returning accessors make + * defensive copies; native construction takes exclusive ownership of its arrays. + * + * <p>For allocation-free consumption, use {@link #hitCount()}, {@link #labelAt(int)}, + * {@link #distanceAt(int)}, and the half-open bounds {@link #queryStart(int)} and + * {@link #queryEnd(int)}. Counter accessors taking a query index also avoid copying arrays. + * Hit indices and query bounds fit in {@code int}, while labels and counters retain their full + * {@code long} range. Indexed accessors reject out-of-range indices with + * {@link IndexOutOfBoundsException}. + */ +public final class VectorRangeSearchResult { + + private final long[] labels; + private final float[] distances; + private final long[] lims; + private final long[] listsProbed; + private final long[] rowsScanned; + private final long[] rowsCommitted; + private final long[] earlyAbandoned; + private final long listReads; + + static VectorRangeSearchResult fromNative( + long[] labels, + float[] distances, + long[] lims, + long[] listsProbed, + long[] rowsScanned, + long[] rowsCommitted, + long[] earlyAbandoned, + long listReads) { + return new VectorRangeSearchResult( + labels, + distances, + lims, + listsProbed, + rowsScanned, + rowsCommitted, + earlyAbandoned, + listReads, + false); + } + + public VectorRangeSearchResult( + long[] labels, + float[] distances, + long[] lims, + long[] listsProbed, + long[] rowsScanned, + long[] rowsCommitted, + long[] earlyAbandoned, + long listReads) { + this( + labels, + distances, + lims, + listsProbed, + rowsScanned, + rowsCommitted, + earlyAbandoned, + listReads, + true); + } + + private VectorRangeSearchResult( + long[] labels, + float[] distances, + long[] lims, + long[] listsProbed, + long[] rowsScanned, + long[] rowsCommitted, + long[] earlyAbandoned, + long listReads, + boolean copyArrays) { + Objects.requireNonNull(labels, "labels"); + Objects.requireNonNull(distances, "distances"); + Objects.requireNonNull(lims, "lims"); + this.labels = copyArrays ? labels.clone() : labels; + this.distances = copyArrays ? distances.clone() : distances; + this.lims = copyArrays ? lims.clone() : lims; + if (this.labels.length != this.distances.length + || this.lims.length == 0 + || this.lims[0] != 0 + || this.lims[this.lims.length - 1] != this.labels.length) { + throw new IllegalArgumentException("invalid CSR result shape"); + } + for (int offset = 1; offset < this.lims.length; offset++) { + if (this.lims[offset] < this.lims[offset - 1] + || this.lims[offset] > this.labels.length) { + throw new IllegalArgumentException("invalid CSR limits"); + } + } + this.listsProbed = validatedCounters(listsProbed, "listsProbed", copyArrays); + this.rowsScanned = validatedCounters(rowsScanned, "rowsScanned", copyArrays); + this.rowsCommitted = validatedCounters(rowsCommitted, "rowsCommitted", copyArrays); + this.earlyAbandoned = validatedCounters(earlyAbandoned, "earlyAbandoned", copyArrays); + if (listReads < 0) { + throw new IllegalArgumentException("listReads must be non-negative"); + } + this.listReads = listReads; + } + + public int queryCount() { + return lims.length - 1; + } + + public int hitCount() { + return labels.length; + } + + public int queryStart(int queryIndex) { + checkQueryIndex(queryIndex); + return Math.toIntExact(lims[queryIndex]); + } + + public int queryEnd(int queryIndex) { + checkQueryIndex(queryIndex); + return Math.toIntExact(lims[queryIndex + 1]); + } + + public long[] labels() { + return labels.clone(); + } + + public long labelAt(int hitIndex) { + return labels[hitIndex]; + } + + public float[] distances() { Review Comment: <!-- dlf-review --> **[Major] Make raw distance semantics explicit across bindings** `fromEndpoints` accepts public-space values, but range results expose core values as plain `distances` (`L2` is squared and inner product is negated). A caller that compares a returned value with the original endpoint therefore gets the wrong answer. Please apply one explicit contract across Rust, C, C++, Java, and Python: name result values `rawDistances`/`raw_distances` (including indexed accessors), and make raw-band constructors internal or explicitly raw-named so the public construction path is `fromEndpoints`. -- 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]
