-
Notifications
You must be signed in to change notification settings - Fork 85
feat: implement distributed VECTOR_SEARCH with parallel fragment/index scanning #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
summaryzb
wants to merge
1
commit into
lance-format:main
Choose a base branch
from
summaryzb:dis_vec_search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...spark-3.4_2.12/src/test/java/org/lance/spark/search/SparkDistributedVectorSearchTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * Licensed 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.lance.spark.search; | ||
|
|
||
| public class SparkDistributedVectorSearchTest extends BaseSparkDistributedVectorSearchTest {} |
16 changes: 16 additions & 0 deletions
16
...spark-3.5_2.12/src/test/java/org/lance/spark/search/SparkDistributedVectorSearchTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * Licensed 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.lance.spark.search; | ||
|
|
||
| public class SparkDistributedVectorSearchTest extends BaseSparkDistributedVectorSearchTest {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
...e_2.12/src/main/java/org/lance/spark/search/LanceMergedSearchColumnarPartitionReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| /* | ||
| * Licensed 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.lance.spark.search; | ||
|
|
||
| import org.lance.Dataset; | ||
| import org.lance.index.DistanceType; | ||
| import org.lance.ipc.LanceScanner; | ||
| import org.lance.ipc.Query; | ||
| import org.lance.ipc.ScanOptions; | ||
| import org.lance.spark.LanceConstant; | ||
| import org.lance.spark.internal.LanceFragmentScanner; | ||
|
|
||
| import org.apache.arrow.vector.ipc.ArrowReader; | ||
| import org.apache.spark.sql.connector.read.PartitionReader; | ||
| import org.apache.spark.sql.types.StructField; | ||
| import org.apache.spark.sql.types.StructType; | ||
| import org.apache.spark.sql.vectorized.ColumnarBatch; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
|
|
||
| /** | ||
| * Worker-side partition reader for distributed VECTOR_SEARCH. Opens the Lance dataset locally and | ||
| * runs one of: | ||
| * | ||
| * <ul> | ||
| * <li>indexed unit ({@code indexSegments} non-empty) → {@code ScanOptions.indexSegments(...)} | ||
| * <li>fallback unit ({@code fragmentIds} non-empty) → {@code ScanOptions.fragmentIds(...)} | ||
| * </ul> | ||
| * | ||
| * and iterates Arrow batches into Spark {@link ColumnarBatch}. | ||
| */ | ||
| public class LanceMergedSearchColumnarPartitionReader implements PartitionReader<ColumnarBatch> { | ||
| private final LanceSearchInputPartition partition; | ||
| private Dataset dataset; | ||
| private LanceScanner scanner; | ||
| private ArrowReader reader; | ||
| private ColumnarBatch currentBatch; | ||
| private boolean finished; | ||
|
|
||
| public LanceMergedSearchColumnarPartitionReader(LanceSearchInputPartition partition) { | ||
| this.partition = partition; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean next() throws IOException { | ||
| if (finished) { | ||
| return false; | ||
| } | ||
| if (reader == null) { | ||
| openReader(); | ||
| } | ||
| if (reader.loadNextBatch()) { | ||
| currentBatch = | ||
| LanceSearchColumnarPartitionReader.toColumnarBatch( | ||
| reader.getVectorSchemaRoot(), partition.getSchema()); | ||
| return true; | ||
| } | ||
| finished = true; | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public ColumnarBatch get() { | ||
| return currentBatch; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| Throwable first = null; | ||
| if (currentBatch != null) { | ||
| try { | ||
| currentBatch.close(); | ||
| } catch (Throwable t) { | ||
| first = t; | ||
| } | ||
| } | ||
| first = closeQuietly(reader, first); | ||
| first = closeQuietly(scanner, first); | ||
| first = closeQuietly(dataset, first); | ||
| if (first != null) { | ||
| throw new IOException("Failed to close LanceMergedSearchColumnarPartitionReader", first); | ||
| } | ||
| } | ||
|
|
||
| private static Throwable closeQuietly(AutoCloseable closeable, Throwable carried) { | ||
| if (closeable == null) { | ||
| return carried; | ||
| } | ||
| try { | ||
| closeable.close(); | ||
| return carried; | ||
| } catch (Throwable t) { | ||
| return carried == null ? t : carried; | ||
| } | ||
| } | ||
|
|
||
| private void openReader() throws IOException { | ||
| dataset = | ||
| LanceFragmentScanner.openDatasetForWorker( | ||
| partition.getReadOptions(), | ||
| partition.getNamespaceImpl(), | ||
| partition.getNamespaceProperties(), | ||
| partition.getInitialStorageOptions()); | ||
| ScanOptions opts = buildScanOptions(partition); | ||
| try { | ||
| scanner = dataset.newScan(opts); | ||
| reader = scanner.scanBatches(); | ||
| } catch (Exception e) { | ||
| throw new IOException( | ||
| "Failed to open distributed search scan for partition: " + describe(partition), e); | ||
| } | ||
| } | ||
|
|
||
| private static ScanOptions buildScanOptions(LanceSearchInputPartition p) { | ||
| LanceSearchQuery base = p.getQuery(); | ||
| String column = base.getVectorColumn(); | ||
| if (column == null || column.isEmpty()) { | ||
| throw new IllegalStateException( | ||
| "vector column must be resolved on the driver before scheduling worker tasks"); | ||
| } | ||
|
|
||
| Query.Builder q = | ||
| new Query.Builder() | ||
| .setColumn(column) | ||
| .setKey(toFloatArray(base.getVector())) | ||
| .setK(base.getK()); | ||
| if (base.getDistanceType() != null && !base.getDistanceType().isEmpty()) { | ||
| q.setDistanceType(parseDistanceType(base.getDistanceType())); | ||
| } | ||
| if (base.getNprobes() != null) { | ||
| q.setMinimumNprobes(base.getNprobes()); | ||
| } | ||
| if (base.getEf() != null) { | ||
| q.setEf(base.getEf()); | ||
| } | ||
| if (base.getRefineFactor() != null) { | ||
| q.setRefineFactor(base.getRefineFactor()); | ||
| } | ||
|
|
||
| ScanOptions.Builder b = new ScanOptions.Builder().nearest(q.build()); | ||
| boolean fallbackUnit = p.getIndexSegments().isEmpty(); | ||
| boolean userRequestedPrefilter = Boolean.TRUE.equals(base.getPrefilter()); | ||
| // Lance's Scanner::nearest rejects fragment-restricted scans unless prefilter=true (a | ||
| // prefilter expression supplies the per-fragment limit). The lance-core JNI silently | ||
| // drops the nearest() error, leaving us with a non-vector scan and no `_distance` | ||
| // column. Force prefilter on fallback units so nearest+fragmentIds coexist. | ||
| // TODO: revisit once Lance JNI propagates Scanner::nearest errors and lifts this restriction. | ||
| if (userRequestedPrefilter || fallbackUnit) { | ||
| b.prefilter(true); | ||
| } | ||
| if (base.getFilter() != null) { | ||
| b.filter(base.getFilter()); | ||
| } | ||
| if (!base.getOutputColumns().isEmpty()) { | ||
| b.columns(base.getOutputColumns()); | ||
| } | ||
| if (Boolean.TRUE.equals(base.getWithRowId()) || schemaHasRowId(p.getSchema())) { | ||
| b.withRowId(true); | ||
| } | ||
|
|
||
| if (!p.getIndexSegments().isEmpty()) { | ||
| b.indexSegments(p.getIndexSegments()); | ||
| } else { | ||
| b.fragmentIds(p.getFragmentIds()); | ||
| } | ||
| return b.build(); | ||
| } | ||
|
|
||
| private static boolean schemaHasRowId(StructType schema) { | ||
| for (StructField field : schema.fields()) { | ||
| if (field.name().equals(LanceConstant.ROW_ID)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static DistanceType parseDistanceType(String name) { | ||
| switch (name.toLowerCase(Locale.ROOT)) { | ||
| case "l2": | ||
| case "euclidean": | ||
| return DistanceType.L2; | ||
| case "cosine": | ||
| return DistanceType.Cosine; | ||
| case "dot": | ||
| return DistanceType.Dot; | ||
| case "hamming": | ||
| return DistanceType.Hamming; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported distance_type: " + name); | ||
| } | ||
| } | ||
|
|
||
| private static float[] toFloatArray(List<Float> vec) { | ||
| float[] arr = new float[vec.size()]; | ||
| for (int i = 0; i < arr.length; i++) { | ||
| arr[i] = vec.get(i); | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| private static String describe(LanceSearchInputPartition p) { | ||
| if (!p.getIndexSegments().isEmpty()) { | ||
| return "indexed segments=" + p.getIndexSegments(); | ||
| } | ||
| return "fallback fragments=" + p.getFragmentIds(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can add distance ranger support after this pr merge