diff --git a/java/lance-jni/src/transaction.rs b/java/lance-jni/src/transaction.rs index df80ba94f3f..ea5996aaeed 100644 --- a/java/lance-jni/src/transaction.rs +++ b/java/lance-jni/src/transaction.rs @@ -394,6 +394,22 @@ fn convert_to_java_operation_inner<'local>( ], )?) } + Operation::CreateIndex { + new_indices, + removed_indices, + } => { + let java_new_indices = export_vec(env, &new_indices)?; + let java_removed_indices = export_vec(env, &removed_indices)?; + + Ok(env.new_object( + "org/lance/operation/CreateIndex", + "(Ljava/util/List;Ljava/util/List;)V", + &[ + JValue::Object(&java_new_indices), + JValue::Object(&java_removed_indices), + ], + )?) + } Operation::Update { removed_fragment_ids, updated_fragments, diff --git a/java/src/test/java/org/lance/TransactionTest.java b/java/src/test/java/org/lance/TransactionTest.java index 3d7172f29dd..c9d0e937263 100644 --- a/java/src/test/java/org/lance/TransactionTest.java +++ b/java/src/test/java/org/lance/TransactionTest.java @@ -13,7 +13,12 @@ */ package org.lance; +import org.lance.index.IndexOptions; +import org.lance.index.IndexParams; +import org.lance.index.IndexType; +import org.lance.index.scalar.ScalarIndexParams; import org.lance.operation.Append; +import org.lance.operation.CreateIndex; import org.apache.arrow.memory.RootAllocator; import org.junit.jupiter.api.Test; @@ -25,7 +30,10 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TransactionTest { @@ -63,4 +71,42 @@ public void testTransaction(@TempDir Path tempDir) { } } } + + @Test + public void testReadTransactionCreateIndex(@TempDir Path tempDir) { + String datasetPath = tempDir.resolve("read_transaction_create_index").toString(); + try (RootAllocator allocator = new RootAllocator(Long.MAX_VALUE)) { + TestUtils.SimpleTestDataset testDataset = + new TestUtils.SimpleTestDataset(allocator, datasetPath); + + try (Dataset dataset = testDataset.createEmptyDataset()) { + assertEquals(1, dataset.version()); + } + + try (Dataset dataset = testDataset.write(1, 10)) { + ScalarIndexParams scalarParams = ScalarIndexParams.create("btree", "{\"zone_size\": 2048}"); + IndexParams indexParams = IndexParams.builder().setScalarIndexParams(scalarParams).build(); + + dataset.createIndex( + IndexOptions.builder(Collections.singletonList("id"), IndexType.BTREE, indexParams) + .withIndexName("btree_id_index") + .build()); + + assertTrue( + dataset.listIndexes().contains("btree_id_index"), + "Expected 'btree_id_index' to be created"); + + Transaction readTx = dataset.readTransaction().orElse(null); + assertNotNull(readTx, "readTransaction() should return a transaction for CreateIndex"); + assertEquals("CreateIndex", readTx.operation().name()); + + assertInstanceOf(CreateIndex.class, readTx.operation()); + CreateIndex op = (CreateIndex) readTx.operation(); + assertFalse(op.getNewIndices().isEmpty(), "newIndices should not be empty for CreateIndex"); + assertTrue( + op.getRemovedIndices().isEmpty(), "removedIndices should be empty for CreateIndex"); + assertEquals("btree_id_index", (op.getNewIndices().get(0).name())); + } + } + } }