Skip to content

Commit

Permalink
Add version and latestVersion mismatch test case
Browse files Browse the repository at this point in the history
  • Loading branch information
LuQQiu committed Apr 23, 2024
1 parent 4b5cdbb commit 6d2b28f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
11 changes: 8 additions & 3 deletions java/core/lance-jni/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl JavaException {
pub enum Error {
#[snafu(display("JNI error: {}", message))]
Jni { message: String },
#[snafu(display("Invalid argument: {}", message))]
InvalidArgument { message: String },
#[snafu(display("Invalid argument: {}, location: {}", message, location))]
InvalidArgument { message: String, location: Location },
#[snafu(display("IO error: {}, location: {}", message, location))]
IO { message: String, location: Location },
#[snafu(display("Arrow error: {}", message))]
Expand All @@ -55,7 +55,7 @@ pub enum Error {
DatasetNotFound { path: String, location: Location },
#[snafu(display("Dataset already exists error: {}, location {}", uri, location))]
DatasetAlreadyExists { uri: String, location: Location },
#[snafu(display("Unknown error"))]
#[snafu(display("Unknown error: {}", message))]
Other { message: String },
}

Expand Down Expand Up @@ -97,6 +97,7 @@ impl From<Utf8Error> for Error {
fn from(source: Utf8Error) -> Self {
Self::InvalidArgument {
message: source.to_string(),
location: location!(),
}
}
}
Expand Down Expand Up @@ -133,6 +134,10 @@ impl From<lance::Error> for Error {
lance::Error::IO { message, location } => Self::IO { message, location },
lance::Error::Arrow { message, location } => Self::Arrow { message, location },
lance::Error::Index { message, location } => Self::Index { message, location },
lance::Error::InvalidInput { source, location } => Self::InvalidArgument {
message: source.to_string(),
location,
},
_ => Self::Other {
message: source.to_string(),
},
Expand Down
2 changes: 2 additions & 0 deletions java/core/lance-jni/src/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn fragment_count_rows(dataset: &BlockingDataset, fragment_id: jlong) -> Result<
let Some(fragment) = dataset.inner.get_fragment(fragment_id as usize) else {
return Err(Error::InvalidArgument {
message: format!("Fragment not found: {}", fragment_id),
location: location!(),
});
};
Ok(RT.block_on(fragment.count_rows())? as jint)
Expand Down Expand Up @@ -69,6 +70,7 @@ impl FragmentScanner {
.project(&columns)
.map_err(|e| Error::InvalidArgument {
message: format!("Failed to select columns: {}", e),
location: location!(),
})?
} else {
schema.clone()
Expand Down
6 changes: 5 additions & 1 deletion java/core/src/main/java/com/lancedb/lance/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ public static Dataset open(String path, BufferAllocator allocator) throws IOExce
public static native Dataset openNative(String path);

/**
* Opens a dataset from the specified path using the native library.
* Create a new version of dataset.
*
* @param allocator the buffer allocator
* @param path The file path of the dataset to open.
* @param operation The operation to apply to the dataset.
* @param readVersion The version of the dataset that was used as the base for the changes.
* This is not needed for overwrite or restore operations.
* @return A new instance of {@link Dataset} linked to the opened dataset.
*/
public static Dataset commit(BufferAllocator allocator, String path,
Expand Down
32 changes: 31 additions & 1 deletion java/core/src/test/java/com/lancedb/lance/DatasetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
package com.lancedb.lance;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Optional;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -56,7 +59,7 @@ void testCreateEmptyDataset() {
String datasetPath = tempDir.resolve("new_empty_dataset").toString();
try (RootAllocator allocator = new RootAllocator(Long.MAX_VALUE)) {
TestUtils.SimpleTestDataset testDataset = new TestUtils.SimpleTestDataset(allocator, datasetPath);
testDataset.createEmptyDataset();
testDataset.createEmptyDataset().close();
}
}

Expand All @@ -69,4 +72,31 @@ void testOpenInvalidPath() {
dataset = Dataset.open(validPath, new RootAllocator());
});
}


@Test
void testDatasetVersion() {
String datasetPath = tempDir.resolve("dataset_version").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());
assertEquals(1, dataset.latestVersion());
try (Dataset dataset2 = testDataset.write(1, 5)) {
assertEquals(1, dataset.version());
assertEquals(2, dataset.latestVersion());
assertEquals(2, dataset2.version());
assertEquals(2, dataset2.latestVersion());
try (Dataset dataset3 = testDataset.write(2, 3)) {
assertEquals(1, dataset.version());
assertEquals(3, dataset.latestVersion());
assertEquals(2, dataset2.version());
assertEquals(3, dataset2.latestVersion());
assertEquals(3, dataset3.version());
assertEquals(3, dataset3.latestVersion());
}
}
}
}
}
}
27 changes: 17 additions & 10 deletions java/core/src/test/java/com/lancedb/lance/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -59,16 +60,16 @@ public Schema getSchema() {
return schema;
}

public void createEmptyDataset() {
try (Dataset dataset = Dataset.create(allocator, datasetPath,
schema, new WriteParams.Builder().build())) {
assertEquals(0, dataset.countRows());
assertEquals(schema, dataset.getSchema());
var fragments = dataset.getFragments();
assertEquals(0, fragments.size());
assertEquals(1, dataset.version());
assertEquals(1, dataset.latestVersion());
}
public Dataset createEmptyDataset() {
Dataset dataset = Dataset.create(allocator, datasetPath,
schema, new WriteParams.Builder().build());
assertEquals(0, dataset.countRows());
assertEquals(schema, dataset.getSchema());
var fragments = dataset.getFragments();
assertEquals(0, fragments.size());
assertEquals(1, dataset.version());
assertEquals(1, dataset.latestVersion());
return dataset;
}

public FragmentMetadata createNewFragment(int fragmentId, int rowCount) {
Expand All @@ -93,6 +94,12 @@ public FragmentMetadata createNewFragment(int fragmentId, int rowCount) {
}
return fragmentMeta;
}

public Dataset write(int version, int rowCount) {
FragmentMetadata metadata = createNewFragment(rowCount, rowCount);
FragmentOperation.Append appendOp = new FragmentOperation.Append(List.of(metadata));
return Dataset.commit(allocator, datasetPath, appendOp, Optional.of(version));
}
}

public static class RandomAccessDataset {
Expand Down

0 comments on commit 6d2b28f

Please sign in to comment.