Skip to content

Commit 9211330

Browse files
authored
feat(java): support delete rows from the dataset (#3498)
1 parent 356d132 commit 9211330

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

java/core/lance-jni/src/blocking_dataset.rs

+17
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,23 @@ fn inner_take(
861861
Ok(**byte_array)
862862
}
863863

864+
#[no_mangle]
865+
pub extern "system" fn Java_com_lancedb_lance_Dataset_nativeDelete(
866+
mut env: JNIEnv,
867+
java_dataset: JObject,
868+
predicate: JString,
869+
) {
870+
ok_or_throw_without_return!(env, inner_delete(&mut env, java_dataset, predicate))
871+
}
872+
873+
fn inner_delete(env: &mut JNIEnv, java_dataset: JObject, predicate: JString) -> Result<()> {
874+
let predicate_str = predicate.extract(env)?;
875+
let mut dataset_guard =
876+
unsafe { env.get_rust_field::<_, _, BlockingDataset>(java_dataset, NATIVE_DATASET) }?;
877+
RT.block_on(dataset_guard.inner.delete(&predicate_str))?;
878+
Ok(())
879+
}
880+
864881
//////////////////////////////
865882
// Schema evolution Methods //
866883
//////////////////////////////

java/core/src/main/java/com/lancedb/lance/Dataset.java

+14
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,20 @@ public void close() throws IOException {
391391

392392
private native byte[] nativeTake(List<Long> indices, List<String> columns);
393393

394+
/**
395+
* Delete rows of data by predicate.
396+
*
397+
* @param predicate the predicate to delete
398+
*/
399+
public void delete(String predicate) {
400+
try (LockManager.WriteLock writeLock = lockManager.acquireWriteLock()) {
401+
Preconditions.checkArgument(nativeDatasetHandle != 0, "Dataset is closed");
402+
nativeDelete(predicate);
403+
}
404+
}
405+
406+
private native void nativeDelete(String predicate);
407+
394408
/**
395409
* Gets the URI of the dataset.
396410
*

java/core/src/test/java/com/lancedb/lance/DatasetTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,39 @@ void testCalculateDataSize() {
531531
}
532532
}
533533
}
534+
535+
@Test
536+
void testDeleteRows() {
537+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
538+
String datasetPath = tempDir.resolve(testMethodName).toString();
539+
try (RootAllocator allocator = new RootAllocator(Long.MAX_VALUE)) {
540+
TestUtils.SimpleTestDataset testDataset =
541+
new TestUtils.SimpleTestDataset(allocator, datasetPath);
542+
dataset = testDataset.createEmptyDataset();
543+
544+
try (Dataset dataset2 = testDataset.write(1, 5)) {
545+
// Initially there are 5 rows
546+
assertEquals(5, dataset2.countRows());
547+
548+
// Delete rows where id > 2 (should delete id=3, id=4)
549+
dataset2.delete("id > 2");
550+
551+
// Now verify we have 3 rows left (id=0, id=1, id=2)
552+
assertEquals(3, dataset2.countRows());
553+
554+
// Verify the rows that remain
555+
assertEquals(0, dataset2.countRows("id > 2"));
556+
assertEquals(3, dataset2.countRows("id <= 2"));
557+
558+
// Delete another row
559+
dataset2.delete("id = 1");
560+
561+
// Now verify we have 2 rows left (id=0, id=2)
562+
assertEquals(2, dataset2.countRows());
563+
assertEquals(1, dataset2.countRows("id = 0"));
564+
assertEquals(1, dataset2.countRows("id = 2"));
565+
assertEquals(0, dataset2.countRows("id = 1"));
566+
}
567+
}
568+
}
534569
}

0 commit comments

Comments
 (0)