Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.paimon.types.DataType;
import org.apache.paimon.utils.Range;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;

Expand All @@ -37,6 +38,10 @@ public class LuminaVectorGlobalIndexer implements VectorGlobalIndexer {
private final DataType fieldType;
private final LuminaVectorIndexOptions options;

// Metric persisted in the index files read by this indexer, guarded by this.
private volatile String persistedMetric;
private boolean persistedMetricConflict;

public LuminaVectorGlobalIndexer(DataType fieldType, Options options) {
this.fieldType = fieldType;
this.options = new LuminaVectorIndexOptions(options);
Expand All @@ -54,11 +59,41 @@ public GlobalIndexReader createReader(
long totalRowCount,
List<Range> rowRanges,
ExecutorService executor) {
return new LuminaVectorGlobalIndexReader(fileReader, files, fieldType, options, executor);
LuminaVectorGlobalIndexReader reader =
new LuminaVectorGlobalIndexReader(fileReader, files, fieldType, options, executor);
recordPersistedMetric(files.get(0).metadata());
return reader;
}

/**
* Returns the metric persisted in the index files read so far, so that raw search and
* refinement score rows in the same metric as the index. Falls back to the configured metric
* before any reader is created.
*/
@Override
public String metric() {
return options.metric().getLuminaName();
String metric = persistedMetric;
return metric == null ? options.metric().getLuminaName() : metric;
}

private synchronized void recordPersistedMetric(byte[] metadata) {
if (persistedMetricConflict) {
return;
}
String metric;
try {
metric = LuminaIndexMeta.deserialize(metadata).metric().getLuminaName();
} catch (IOException | IllegalArgumentException e) {
// The reader reports invalid metadata when it loads the index.
return;
}
if (persistedMetric == null) {
persistedMetric = metric;
} else if (!persistedMetric.equals(metric)) {
// Shards built with different metrics have no common metric. Keep using the
// configured metric, as before, instead of failing the search.
persistedMetricConflict = true;
persistedMetric = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@

package org.apache.paimon.lumina.index;

import org.apache.paimon.fs.Path;
import org.apache.paimon.globalindex.GlobalIndexIOMeta;
import org.apache.paimon.globalindex.GlobalIndexReader;
import org.apache.paimon.globalindex.GlobalIndexerFactoryUtils;
import org.apache.paimon.globalindex.io.GlobalIndexFileReader;
import org.apache.paimon.options.Options;
import org.apache.paimon.types.DataTypes;

import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/** Tests for Lumina global indexer factory identifiers. */
/** Tests for Lumina global indexer factory identifiers and the indexer metric. */
public class LuminaVectorGlobalIndexerFactoryTest {

@Test
Expand All @@ -41,4 +52,69 @@ public void testLoadNewAndLegacyIdentifiers() {
assertThat(GlobalIndexerFactoryUtils.load("lumina-vector-ann"))
.isExactlyInstanceOf(LegacyLuminaVectorGlobalIndexerFactory.class);
}

@Test
public void testMetricBeforeReaderUsesConfiguration() {
LuminaVectorGlobalIndexer indexer = newIndexer();
assertThat(indexer.metric()).isEqualTo(configuredMetric());
}

@Test
public void testMetricFollowsPersistedIndexMeta() throws Exception {
LuminaVectorGlobalIndexer indexer = newIndexer();
readShard(indexer, metaBytes(LuminaVectorMetric.L2));
assertThat(indexer.metric()).isEqualTo(LuminaVectorMetric.L2.getLuminaName());
}

@Test
public void testConflictingShardMetricsKeepConfiguredMetric() throws Exception {
LuminaVectorGlobalIndexer indexer = newIndexer();
readShard(indexer, metaBytes(LuminaVectorMetric.L2));
readShard(indexer, metaBytes(LuminaVectorMetric.COSINE));
assertThat(indexer.metric()).isEqualTo(configuredMetric());

readShard(indexer, metaBytes(LuminaVectorMetric.L2));
assertThat(indexer.metric()).isEqualTo(configuredMetric());
}

@Test
public void testCorruptMetaKeepsConfiguredMetric() throws Exception {
LuminaVectorGlobalIndexer indexer = newIndexer();
readShard(indexer, "not json".getBytes(StandardCharsets.UTF_8));
assertThat(indexer.metric()).isEqualTo(configuredMetric());

Map<String, String> unknownMetric = new HashMap<>();
unknownMetric.put("index.dimension", "2");
unknownMetric.put("distance.metric", "unknown");
readShard(indexer, new LuminaIndexMeta(unknownMetric).serialize());
assertThat(indexer.metric()).isEqualTo(configuredMetric());
}

private static LuminaVectorGlobalIndexer newIndexer() {
return new LuminaVectorGlobalIndexer(DataTypes.ARRAY(DataTypes.FLOAT()), new Options());
}

private static String configuredMetric() {
return LuminaVectorMetric.INNER_PRODUCT.getLuminaName();
}

private static byte[] metaBytes(LuminaVectorMetric metric) throws Exception {
Map<String, String> meta = new HashMap<>();
meta.put("index.dimension", "2");
meta.put("distance.metric", metric.getLuminaName());
return new LuminaIndexMeta(meta).serialize();
}

private static void readShard(LuminaVectorGlobalIndexer indexer, byte[] metadata)
throws Exception {
GlobalIndexFileReader fileReader =
meta -> {
throw new AssertionError("Index file should not be opened");
};
GlobalIndexIOMeta file = new GlobalIndexIOMeta(new Path("/shard.index"), 1L, metadata);
try (GlobalIndexReader reader =
indexer.createReader(fileReader, Collections.singletonList(file), 1L, null, null)) {
assertThat(reader).isInstanceOf(LuminaVectorGlobalIndexReader.class);
}
}
}
Loading