|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.lumina.index; |
| 20 | + |
| 21 | +import org.apache.paimon.data.BinaryRow; |
| 22 | +import org.apache.paimon.data.GenericArray; |
| 23 | +import org.apache.paimon.data.GenericRow; |
| 24 | +import org.apache.paimon.fs.FileIOFinder; |
| 25 | +import org.apache.paimon.fs.Path; |
| 26 | +import org.apache.paimon.fs.local.LocalFileIO; |
| 27 | +import org.apache.paimon.globalindex.GlobalIndexBuilderUtils; |
| 28 | +import org.apache.paimon.globalindex.GlobalIndexSingletonWriter; |
| 29 | +import org.apache.paimon.globalindex.ResultEntry; |
| 30 | +import org.apache.paimon.index.IndexFileMeta; |
| 31 | +import org.apache.paimon.io.CompactIncrement; |
| 32 | +import org.apache.paimon.io.DataIncrement; |
| 33 | +import org.apache.paimon.options.Options; |
| 34 | +import org.apache.paimon.schema.Schema; |
| 35 | +import org.apache.paimon.schema.SchemaManager; |
| 36 | +import org.apache.paimon.schema.SchemaUtils; |
| 37 | +import org.apache.paimon.schema.TableSchema; |
| 38 | +import org.apache.paimon.table.AppendOnlyFileStoreTable; |
| 39 | +import org.apache.paimon.table.CatalogEnvironment; |
| 40 | +import org.apache.paimon.table.sink.BatchTableCommit; |
| 41 | +import org.apache.paimon.table.sink.BatchTableWrite; |
| 42 | +import org.apache.paimon.table.sink.BatchWriteBuilder; |
| 43 | +import org.apache.paimon.table.sink.CommitMessage; |
| 44 | +import org.apache.paimon.table.sink.CommitMessageImpl; |
| 45 | +import org.apache.paimon.types.ArrayType; |
| 46 | +import org.apache.paimon.types.DataField; |
| 47 | +import org.apache.paimon.types.DataType; |
| 48 | +import org.apache.paimon.types.DataTypes; |
| 49 | +import org.apache.paimon.types.FloatType; |
| 50 | +import org.apache.paimon.types.RowType; |
| 51 | +import org.apache.paimon.utils.Range; |
| 52 | + |
| 53 | +import org.aliyun.lumina.Lumina; |
| 54 | +import org.aliyun.lumina.LuminaException; |
| 55 | +import org.junit.jupiter.api.BeforeAll; |
| 56 | +import org.junit.jupiter.api.BeforeEach; |
| 57 | +import org.junit.jupiter.api.Test; |
| 58 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 59 | + |
| 60 | +import java.nio.file.Files; |
| 61 | +import java.nio.file.Paths; |
| 62 | +import java.util.Collections; |
| 63 | +import java.util.List; |
| 64 | + |
| 65 | +import static org.apache.paimon.CoreOptions.DATA_EVOLUTION_ENABLED; |
| 66 | +import static org.apache.paimon.CoreOptions.GLOBAL_INDEX_ENABLED; |
| 67 | +import static org.apache.paimon.CoreOptions.PATH; |
| 68 | +import static org.apache.paimon.CoreOptions.ROW_TRACKING_ENABLED; |
| 69 | +import static org.assertj.core.api.Assertions.assertThat; |
| 70 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 71 | + |
| 72 | +/** |
| 73 | + * Mixed language E2E test for Java Lumina vector index building and Python reading. |
| 74 | + * |
| 75 | + * <p>Java writes data and builds a Lumina vector index, then Python reads and searches it. |
| 76 | + */ |
| 77 | +public class JavaPyLuminaE2ETest { |
| 78 | + |
| 79 | + @BeforeAll |
| 80 | + public static void checkNativeLibrary() { |
| 81 | + if (!Lumina.isLibraryLoaded()) { |
| 82 | + try { |
| 83 | + Lumina.loadLibrary(); |
| 84 | + } catch (LuminaException e) { |
| 85 | + assumeTrue(false, "Lumina native library not available: " + e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + java.nio.file.Path tempDir = Paths.get("../paimon-python/pypaimon/tests/e2e").toAbsolutePath(); |
| 91 | + |
| 92 | + protected Path warehouse; |
| 93 | + |
| 94 | + @BeforeEach |
| 95 | + public void before() throws Exception { |
| 96 | + if (!Files.exists(tempDir.resolve("warehouse"))) { |
| 97 | + Files.createDirectories(tempDir.resolve("warehouse")); |
| 98 | + } |
| 99 | + warehouse = new Path("file://" + tempDir.resolve("warehouse")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @EnabledIfSystemProperty(named = "run.e2e.tests", matches = "true") |
| 104 | + public void testLuminaVectorIndexWrite() throws Exception { |
| 105 | + String tableName = "test_lumina_vector"; |
| 106 | + Path tablePath = new Path(warehouse.toString() + "/default.db/" + tableName); |
| 107 | + |
| 108 | + int dimension = 4; |
| 109 | + |
| 110 | + RowType rowType = |
| 111 | + RowType.of( |
| 112 | + new DataType[] {DataTypes.INT(), new ArrayType(new FloatType())}, |
| 113 | + new String[] {"id", "embedding"}); |
| 114 | + |
| 115 | + Options options = new Options(); |
| 116 | + options.set(PATH, tablePath.toString()); |
| 117 | + options.set(ROW_TRACKING_ENABLED, true); |
| 118 | + options.set(DATA_EVOLUTION_ENABLED, true); |
| 119 | + options.set(GLOBAL_INDEX_ENABLED, true); |
| 120 | + options.setString(LuminaVectorIndexOptions.DIMENSION.key(), String.valueOf(dimension)); |
| 121 | + options.setString(LuminaVectorIndexOptions.DISTANCE_METRIC.key(), "l2"); |
| 122 | + options.setString(LuminaVectorIndexOptions.ENCODING_TYPE.key(), "rawf32"); |
| 123 | + |
| 124 | + TableSchema tableSchema = |
| 125 | + SchemaUtils.forceCommit( |
| 126 | + new SchemaManager(LocalFileIO.create(), tablePath), |
| 127 | + new Schema( |
| 128 | + rowType.getFields(), |
| 129 | + Collections.emptyList(), |
| 130 | + Collections.emptyList(), |
| 131 | + options.toMap(), |
| 132 | + "")); |
| 133 | + |
| 134 | + AppendOnlyFileStoreTable table = |
| 135 | + new AppendOnlyFileStoreTable( |
| 136 | + FileIOFinder.find(tablePath), |
| 137 | + tablePath, |
| 138 | + tableSchema, |
| 139 | + CatalogEnvironment.empty()); |
| 140 | + |
| 141 | + // Test vectors: 6 vectors of dimension 4 |
| 142 | + float[][] vectors = |
| 143 | + new float[][] { |
| 144 | + new float[] {1.0f, 0.0f, 0.0f, 0.0f}, |
| 145 | + new float[] {0.9f, 0.1f, 0.0f, 0.0f}, |
| 146 | + new float[] {0.0f, 1.0f, 0.0f, 0.0f}, |
| 147 | + new float[] {0.0f, 0.0f, 1.0f, 0.0f}, |
| 148 | + new float[] {0.0f, 0.0f, 0.0f, 1.0f}, |
| 149 | + new float[] {0.95f, 0.05f, 0.0f, 0.0f} |
| 150 | + }; |
| 151 | + |
| 152 | + // Write data rows |
| 153 | + BatchWriteBuilder writeBuilder = table.newBatchWriteBuilder(); |
| 154 | + try (BatchTableWrite write = writeBuilder.newWrite(); |
| 155 | + BatchTableCommit commit = writeBuilder.newCommit()) { |
| 156 | + for (int i = 0; i < vectors.length; i++) { |
| 157 | + write.write(GenericRow.of(i, new GenericArray(vectors[i]))); |
| 158 | + } |
| 159 | + commit.commit(write.prepareCommit()); |
| 160 | + } |
| 161 | + |
| 162 | + // Build Lumina vector index on "embedding" column |
| 163 | + DataField embeddingField = table.rowType().getField("embedding"); |
| 164 | + Options indexOptions = table.coreOptions().toConfiguration(); |
| 165 | + LuminaVectorIndexOptions luminaOptions = new LuminaVectorIndexOptions(indexOptions); |
| 166 | + |
| 167 | + GlobalIndexSingletonWriter writer = |
| 168 | + (GlobalIndexSingletonWriter) |
| 169 | + GlobalIndexBuilderUtils.createIndexWriter( |
| 170 | + table, |
| 171 | + LuminaVectorGlobalIndexerFactory.IDENTIFIER, |
| 172 | + embeddingField, |
| 173 | + indexOptions); |
| 174 | + |
| 175 | + // Write vectors to index |
| 176 | + for (float[] vec : vectors) { |
| 177 | + writer.write(vec); |
| 178 | + } |
| 179 | + |
| 180 | + List<ResultEntry> entries = writer.finish(); |
| 181 | + assertThat(entries).hasSize(1); |
| 182 | + assertThat(entries.get(0).rowCount()).isEqualTo(vectors.length); |
| 183 | + |
| 184 | + Range rowRange = new Range(0, vectors.length - 1); |
| 185 | + List<IndexFileMeta> indexFiles = |
| 186 | + GlobalIndexBuilderUtils.toIndexFileMetas( |
| 187 | + table.fileIO(), |
| 188 | + table.store().pathFactory().globalIndexFileFactory(), |
| 189 | + table.coreOptions(), |
| 190 | + rowRange, |
| 191 | + embeddingField.id(), |
| 192 | + LuminaVectorGlobalIndexerFactory.IDENTIFIER, |
| 193 | + entries); |
| 194 | + |
| 195 | + // Commit the index |
| 196 | + DataIncrement dataIncrement = DataIncrement.indexIncrement(indexFiles); |
| 197 | + CommitMessage message = |
| 198 | + new CommitMessageImpl( |
| 199 | + BinaryRow.EMPTY_ROW, |
| 200 | + 0, |
| 201 | + null, |
| 202 | + dataIncrement, |
| 203 | + CompactIncrement.emptyIncrement()); |
| 204 | + try (BatchTableCommit commit = writeBuilder.newCommit()) { |
| 205 | + commit.commit(Collections.singletonList(message)); |
| 206 | + } |
| 207 | + |
| 208 | + // Verify index was committed |
| 209 | + List<org.apache.paimon.manifest.IndexManifestEntry> indexEntries = |
| 210 | + table.indexManifestFileReader().read(table.latestSnapshot().get().indexManifest()); |
| 211 | + assertThat(indexEntries).hasSize(1); |
| 212 | + assertThat(indexEntries.get(0).indexFile().indexType()) |
| 213 | + .isEqualTo(LuminaVectorGlobalIndexerFactory.IDENTIFIER); |
| 214 | + } |
| 215 | +} |
0 commit comments