leaves12138 commented on code in PR #8549: URL: https://github.com/apache/paimon/pull/8549#discussion_r3563826806
########## paimon-core/src/test/java/org/apache/paimon/index/pkvector/PrimaryKeyVectorIndexOptionsTest.java: ########## @@ -0,0 +1,262 @@ +/* + * 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.pkvector; + +import org.apache.paimon.CoreOptions; +import org.apache.paimon.options.Options; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for {@link PrimaryKeyVectorIndexOptions}. */ +class PrimaryKeyVectorIndexOptionsTest { + + @Test + void testPluralFieldRegistryEnablesIndex() { + Map<String, String> options = new HashMap<>(); + options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding"); + + assertThat(new CoreOptions(options).primaryKeyVectorIndexEnabled()).isTrue(); + } + + @Test + void testFieldRegistryIsTheOnlyEnableSwitch() { + Map<String, String> options = new HashMap<>(); + options.put("pk-vector.index.column", "embedding"); + options.put("pk-vector.index.type", "ivf-pq"); + + assertThat(new CoreOptions(options).primaryKeyVectorIndexEnabled()).isFalse(); + } + + @Test + void testIndexTypeMustBeFieldScoped() { + Map<String, String> options = new HashMap<>(); + options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding"); + options.put("pk-vector.index.type", "ivf-pq"); + + assertThat(new CoreOptions(options).primaryKeyVectorIndexType("embedding")).isNull(); + } + + @Test + void testIndexOptionsMustBeFieldScoped() { + Map<String, String> options = new HashMap<>(); + options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding"); + options.put("pk-vector.index.options", "{\"nlist\":64}"); + + assertThat(new CoreOptions(options).primaryKeyVectorIndexOptions("embedding")).isNull(); + } + + @Test + void testDistanceMetricMustBeFieldScoped() { + Map<String, String> options = new HashMap<>(); + options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding"); + options.put("pk-vector.distance.metric", "l2"); + + assertThat(new CoreOptions(options).primaryKeyVectorDistanceMetric("embedding")) + .isEqualTo("inner_product"); + } + + @Test + void testFieldScopedDistanceMetricOverridesTableDefault() { + Map<String, String> options = new HashMap<>(); + options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding"); + options.put("pk-vector.distance.metric", "l2"); + options.put("fields.embedding.pk-vector.distance.metric", "cosine"); + + assertThat(new CoreOptions(options).primaryKeyVectorDistanceMetric("embedding")) + .isEqualTo("cosine"); + } + + @Test + void testFieldScopedAnnThresholdOverridesTableDefault() { + Map<String, String> options = new HashMap<>(); + options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding"); + options.put(CoreOptions.PK_VECTOR_ANN_MIN_ROWS.key(), "10000"); + options.put("fields.embedding.pk-vector.ann.min-rows", "20000"); + options.put("fields.embedding.pk-vector.l0.max-segments", "4"); + options.put("fields.embedding.pk-vector.l0.max-rows", "30000"); + options.put("fields.embedding.pk-vector.ann.max-rows", "90000"); + options.put("fields.embedding.pk-vector.ann.max-source-files", "16"); + options.put("fields.embedding.pk-vector.refine-factor", "6"); + + CoreOptions coreOptions = new CoreOptions(options); + assertThat(coreOptions.primaryKeyVectorAnnMinRows("embedding")).isEqualTo(20_000L); + assertThat(coreOptions.primaryKeyVectorL0MaxSegments("embedding")).isEqualTo(4); + assertThat(coreOptions.primaryKeyVectorL0MaxRows("embedding")).isEqualTo(30_000L); + assertThat(coreOptions.primaryKeyVectorAnnMaxRows("embedding")).isEqualTo(90_000L); + assertThat(coreOptions.primaryKeyVectorAnnMaxSourceFiles("embedding")).isEqualTo(16); + assertThat(coreOptions.primaryKeyVectorRefineFactor("embedding")).isEqualTo(6); + } + + @Test + void testFieldScopedJsonOptionsOverrideTableDefault() { + Map<String, String> options = new HashMap<>(); + options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding"); + options.put("fields.embedding.pk-vector.index.type", "ivf-pq"); + options.put("pk-vector.index.options", "{\"nlist\":64}"); + options.put("fields.embedding.pk-vector.index.options", "{\"nlist\":128}"); + + Options resolved = PrimaryKeyVectorIndexOptions.resolve(new CoreOptions(options)); + + assertThat(resolved.get("ivf-pq.nlist")).isEqualTo("128"); + } + + @Test + void testResolvesShortAndQualifiedAlgorithmOptions() { + CoreOptions coreOptions = + coreOptions( + "{\"nlist\":64,\"ivf-pq.pq.m\":\"8\"," + "\"fields.embedding.hnsw.m\":16}"); + + Options resolved = PrimaryKeyVectorIndexOptions.resolve(coreOptions); + + assertThat(resolved.get("ivf-pq.nlist")).isEqualTo("64"); + assertThat(resolved.get("ivf-pq.pq.m")).isEqualTo("8"); + assertThat(resolved.get("fields.embedding.hnsw.m")).isEqualTo("16"); + assertThat(resolved.get("ivf-pq.metric")).isEqualTo("l2"); + } + + @Test + void testHashIsCanonicalAcrossJsonPropertyOrder() { + assertThat(PrimaryKeyVectorIndexOptions.hash(coreOptions("{\"nlist\":64,\"pq.m\":8}"))) + .containsExactly( + PrimaryKeyVectorIndexOptions.hash( + coreOptions("{\"pq.m\":\"8\",\"nlist\":\"64\"}"))); + } + + @Test + void testHashIncludesEffectiveTopLevelAlgorithmOptions() { + assertThat(PrimaryKeyVectorIndexOptions.hash(coreOptions(null, "ivf-pq.nlist", "64"))) + .isNotEqualTo( + PrimaryKeyVectorIndexOptions.hash(coreOptions(null, "ivf-pq.nlist", "65"))); + } + + @Test + void testDefinitionIdIsStableAndDefinitionSensitive() { + CoreOptions first = coreOptions("{\"nlist\":64,\"pq.m\":8}"); + CoreOptions reordered = coreOptions("{\"pq.m\":8,\"nlist\":64}"); + String definitionId = + PrimaryKeyVectorIndexOptions.definitionId(7, "VECTOR<FLOAT, 8>", first); + + assertThat(PrimaryKeyVectorIndexOptions.definitionId(7, "VECTOR<FLOAT, 8>", reordered)) + .isEqualTo(definitionId); + assertThat(PrimaryKeyVectorIndexOptions.definitionId(8, "VECTOR<FLOAT, 8>", first)) + .isNotEqualTo(definitionId); + assertThat(PrimaryKeyVectorIndexOptions.definitionId(7, "VECTOR<FLOAT, 16>", first)) + .isNotEqualTo(definitionId); + assertThat( + PrimaryKeyVectorIndexOptions.definitionId( + 7, "VECTOR<FLOAT, 8>", coreOptions("{\"nlist\":65,\"pq.m\":8}"))) + .isNotEqualTo(definitionId); + } + + @Test + void testDefinitionIdExcludesOperationalThresholds() { + CoreOptions first = coreOptions("{\"nlist\":64}"); + first.toConfiguration().setString("fields.embedding.pk-vector.ann.min-rows", "10000"); + CoreOptions second = coreOptions("{\"nlist\":64}"); + second.toConfiguration().setString("fields.embedding.pk-vector.ann.min-rows", "20000"); + + assertThat(PrimaryKeyVectorIndexOptions.definitionId(7, "VECTOR<FLOAT, 8>", first)) + .isEqualTo( + PrimaryKeyVectorIndexOptions.definitionId(7, "VECTOR<FLOAT, 8>", second)); + } + + @Test + void testDefinitionIdIsStableAcrossFieldRename() { Review Comment: The definition ID helper is stable if callers manually rewrite all field-scoped options, but the real schema-evolution path does not do that. `SchemaManager.applyRenameColumnsToOptions` does not update `pk-vector.index.columns` or any `fields.<old>.pk-vector.*` / `fields.<old>.<algorithm>.*` keys. Renaming the configured vector column therefore fails schema validation because the registry still points to the old name (`Field embedding can not be found in table schema`). Please wire these options into column-rename handling and add a `SchemaManager` or catalog-level rename regression test that also verifies the definition ID remains unchanged. -- 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]
