-
Notifications
You must be signed in to change notification settings - Fork 638
feat(java): support row lineage and cdf apis #5362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92dc727
feat(java): support row lineage and cdf apis
yanghua 9ba5bbc
refactor
yanghua 02dc19c
refactor
yanghua be71dc9
address review comments
yanghua 79e1d59
address review comments
yanghua ed743fe
address review comments
yanghua b7c3773
address review comments
yanghua a2252b7
address review comments
yanghua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
|
||
| use crate::blocking_dataset::{BlockingDataset, NATIVE_DATASET}; | ||
| use crate::error::Result; | ||
| use crate::ffi::JNIEnvExt; | ||
| use crate::transaction::convert_to_java_transaction; | ||
| use crate::RT; | ||
| use arrow::ffi_stream::FFI_ArrowArrayStream; | ||
| use jni::objects::{JObject, JValue}; | ||
| use jni::sys::jlong; | ||
| use jni::JNIEnv; | ||
| use lance::dataset::delta::DatasetDelta as RustDatasetDelta; | ||
| use lance::dataset::scanner::DatasetRecordBatchStream; | ||
| use lance::dataset::transaction::Transaction; | ||
| use lance_io::ffi::to_ffi_arrow_array_stream; | ||
|
|
||
| pub const NATIVE_DELTA: &str = "nativeDeltaHandle"; | ||
|
|
||
| pub struct BlockingDatasetDelta { | ||
| pub(crate) inner: RustDatasetDelta, | ||
| } | ||
|
|
||
| fn attach_native_delta<'local>( | ||
| env: &mut JNIEnv<'local>, | ||
| delta: BlockingDatasetDelta, | ||
| java_dataset: &JObject<'local>, | ||
| ) -> Result<JObject<'local>> { | ||
| let j_delta = env.new_object("org/lance/delta/DatasetDelta", "()V", &[])?; | ||
|
|
||
| unsafe { env.set_rust_field(&j_delta, NATIVE_DELTA, delta) }?; | ||
|
|
||
| env.set_field( | ||
| &j_delta, | ||
| "dataset", | ||
| "Lorg/lance/Dataset;", | ||
| JValue::Object(java_dataset), | ||
| )?; | ||
| Ok(j_delta) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "system" fn Java_org_lance_delta_DatasetDeltaBuilder_nativeBuild<'local>( | ||
| mut env: JNIEnv<'local>, | ||
| _obj: JObject<'local>, | ||
| java_dataset: JObject<'local>, | ||
| compared_against_obj: JObject<'local>, | ||
| begin_version_obj: JObject<'local>, | ||
| end_version_obj: JObject<'local>, | ||
| ) -> JObject<'local> { | ||
| ok_or_throw!( | ||
| env, | ||
| inner_native_build( | ||
| &mut env, | ||
| java_dataset, | ||
| compared_against_obj, | ||
| begin_version_obj, | ||
| end_version_obj | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "system" fn Java_org_lance_Dataset_nativeBuildDelta<'local>( | ||
| mut env: JNIEnv<'local>, | ||
| java_dataset: JObject<'local>, | ||
| compared_against_obj: JObject<'local>, | ||
| begin_version_obj: JObject<'local>, | ||
| end_version_obj: JObject<'local>, | ||
| ) -> JObject<'local> { | ||
| ok_or_throw!( | ||
| env, | ||
| inner_native_build( | ||
| &mut env, | ||
| java_dataset, | ||
| compared_against_obj, | ||
| begin_version_obj, | ||
| end_version_obj | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| fn inner_native_build<'local>( | ||
| env: &mut JNIEnv<'local>, | ||
| java_dataset: JObject<'local>, | ||
| compared_against_obj: JObject<'local>, | ||
| begin_version_obj: JObject<'local>, | ||
| end_version_obj: JObject<'local>, | ||
| ) -> Result<JObject<'local>> { | ||
| let compared_against = env.get_u64_opt(&compared_against_obj)?; | ||
| let begin_version = env.get_u64_opt(&begin_version_obj)?; | ||
| let end_version = env.get_u64_opt(&end_version_obj)?; | ||
|
|
||
| let delta = { | ||
| let dataset_guard = | ||
| unsafe { env.get_rust_field::<_, _, BlockingDataset>(&java_dataset, NATIVE_DATASET)? }; | ||
|
|
||
| let mut builder = dataset_guard.inner.delta(); | ||
| if let Some(compared) = compared_against { | ||
| builder = builder.compared_against_version(compared); | ||
| } else if let (Some(begin), Some(end)) = (begin_version, end_version) { | ||
| builder = builder.with_begin_version(begin).with_end_version(end); | ||
| } | ||
| builder.build()? | ||
| }; | ||
|
|
||
| let blocking_delta = BlockingDatasetDelta { inner: delta }; | ||
| attach_native_delta(env, blocking_delta, &java_dataset) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "system" fn Java_org_lance_delta_DatasetDelta_listTransactions<'local>( | ||
| mut env: JNIEnv<'local>, | ||
| j_delta: JObject<'local>, | ||
| ) -> JObject<'local> { | ||
| ok_or_throw!(env, inner_list_transactions(&mut env, j_delta)) | ||
| } | ||
|
|
||
| fn inner_list_transactions<'local>( | ||
| env: &mut JNIEnv<'local>, | ||
| j_delta: JObject<'local>, | ||
| ) -> Result<JObject<'local>> { | ||
| let txs: Vec<Transaction> = { | ||
| let delta_guard = | ||
| unsafe { env.get_rust_field::<_, _, BlockingDatasetDelta>(&j_delta, NATIVE_DELTA) }?; | ||
| RT.block_on(delta_guard.inner.list_transactions())? | ||
| }; | ||
|
|
||
| let java_dataset = env | ||
| .get_field(&j_delta, "dataset", "Lorg/lance/Dataset;")? | ||
| .l()?; | ||
|
|
||
| let array_list = env.new_object("java/util/ArrayList", "()V", &[])?; | ||
| for tx in txs.into_iter() { | ||
| let jtx = convert_to_java_transaction(env, tx, &java_dataset)?; | ||
| env.call_method( | ||
| &array_list, | ||
| "add", | ||
| "(Ljava/lang/Object;)Z", | ||
| &[JValue::Object(&jtx)], | ||
| )?; | ||
| } | ||
| Ok(array_list) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "system" fn Java_org_lance_delta_DatasetDelta_getInsertedRows<'local>( | ||
| mut env: JNIEnv<'local>, | ||
| j_delta: JObject<'local>, | ||
| stream_addr: jlong, | ||
| ) { | ||
| ok_or_throw_without_return!(env, inner_get_inserted_rows(&mut env, j_delta, stream_addr)) | ||
| } | ||
|
|
||
| fn inner_get_inserted_rows<'local>( | ||
| env: &mut JNIEnv, | ||
| j_delta: JObject<'local>, | ||
| stream_addr: jlong, | ||
| ) -> Result<()> { | ||
| let delta_guard = | ||
| unsafe { env.get_rust_field::<_, _, BlockingDatasetDelta>(&j_delta, NATIVE_DELTA) }?; | ||
|
|
||
| let stream: DatasetRecordBatchStream = RT.block_on(delta_guard.inner.get_inserted_rows())?; | ||
| let ffi_stream = to_ffi_arrow_array_stream(stream, RT.handle().clone())?; | ||
|
|
||
| unsafe { std::ptr::write_unaligned(stream_addr as *mut FFI_ArrowArrayStream, ffi_stream) } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "system" fn Java_org_lance_delta_DatasetDelta_getUpdatedRows<'local>( | ||
| mut env: JNIEnv<'local>, | ||
| j_delta: JObject<'local>, | ||
| stream_addr: jlong, | ||
| ) { | ||
| ok_or_throw_without_return!(env, inner_get_updated_rows(&mut env, j_delta, stream_addr)) | ||
| } | ||
|
|
||
| fn inner_get_updated_rows<'local>( | ||
| env: &mut JNIEnv, | ||
| j_delta: JObject<'local>, | ||
| stream_addr: jlong, | ||
| ) -> Result<()> { | ||
| let delta_guard = | ||
| unsafe { env.get_rust_field::<_, _, BlockingDatasetDelta>(&j_delta, NATIVE_DELTA) }?; | ||
|
|
||
| let stream: DatasetRecordBatchStream = RT.block_on(delta_guard.inner.get_updated_rows())?; | ||
| let ffi_stream = to_ffi_arrow_array_stream(stream, RT.handle().clone())?; | ||
|
|
||
| unsafe { std::ptr::write_unaligned(stream_addr as *mut FFI_ArrowArrayStream, ffi_stream) } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "system" fn Java_org_lance_delta_DatasetDelta_releaseNativeDelta( | ||
| mut env: JNIEnv, | ||
| obj: JObject, | ||
| handle: jlong, | ||
| ) { | ||
| ok_or_throw_without_return!(env, inner_release_native_delta(&mut env, obj, handle)); | ||
| } | ||
|
|
||
| fn inner_release_native_delta(env: &mut JNIEnv, obj: JObject, _handle: jlong) -> Result<()> { | ||
| let _: BlockingDatasetDelta = unsafe { env.take_rust_field(obj, NATIVE_DELTA) }?; | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Licensed 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.lance.delta; | ||
|
|
||
| import org.lance.Dataset; | ||
| import org.lance.JniLoader; | ||
| import org.lance.LockManager; | ||
| import org.lance.Transaction; | ||
|
|
||
| import org.apache.arrow.c.ArrowArrayStream; | ||
| import org.apache.arrow.c.Data; | ||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.util.Preconditions; | ||
| import org.apache.arrow.vector.ipc.ArrowReader; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A view of differences between two versions of a dataset. | ||
| * | ||
| * <p>Created by {@link DatasetDeltaBuilder}. Provides methods to list transactions and stream | ||
| * inserted/updated rows between two versions. | ||
| */ | ||
| public class DatasetDelta implements Closeable { | ||
| static { | ||
| JniLoader.ensureLoaded(); | ||
| } | ||
|
|
||
| /** Native handle to the Rust DatasetDelta. */ | ||
| private long nativeDeltaHandle; | ||
|
|
||
| /** Base dataset used to compute the delta. Also used for Transaction conversion. */ | ||
| private Dataset dataset; | ||
|
|
||
| private final LockManager lockManager = new LockManager(); | ||
|
|
||
| private DatasetDelta() {} | ||
|
|
||
| /** | ||
| * List transactions between begin_version + 1 and end_version (inclusive). | ||
| * | ||
| * @return list of transactions | ||
| */ | ||
| public List<Transaction> listTransactions() { | ||
| try (LockManager.ReadLock readLock = lockManager.acquireReadLock()) { | ||
| Preconditions.checkArgument(nativeDeltaHandle != 0, "DatasetDelta is closed"); | ||
| return nativeListTransactions(); | ||
| } | ||
| } | ||
|
|
||
| private native List<Transaction> nativeListTransactions(); | ||
|
|
||
| /** Return a streaming ArrowReader for inserted rows. */ | ||
| public ArrowReader getInsertedRows() throws IOException { | ||
| try (LockManager.ReadLock readLock = lockManager.acquireReadLock()) { | ||
| Preconditions.checkArgument(nativeDeltaHandle != 0, "DatasetDelta is closed"); | ||
| BufferAllocator allocator = dataset.allocator(); | ||
| try (ArrowArrayStream s = ArrowArrayStream.allocateNew(allocator)) { | ||
| nativeGetInsertedRows(s.memoryAddress()); | ||
| return Data.importArrayStream(allocator, s); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private native void nativeGetInsertedRows(long streamAddress) throws IOException; | ||
|
|
||
| /** Return a streaming ArrowReader for updated rows. */ | ||
| public ArrowReader getUpdatedRows() throws IOException { | ||
|
yanghua marked this conversation as resolved.
|
||
| try (LockManager.ReadLock readLock = lockManager.acquireReadLock()) { | ||
| Preconditions.checkArgument(nativeDeltaHandle != 0, "DatasetDelta is closed"); | ||
| BufferAllocator allocator = dataset.allocator(); | ||
| try (ArrowArrayStream s = ArrowArrayStream.allocateNew(allocator)) { | ||
| nativeGetUpdatedRows(s.memoryAddress()); | ||
| return Data.importArrayStream(allocator, s); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private native void nativeGetUpdatedRows(long streamAddress) throws IOException; | ||
|
|
||
| @Override | ||
| public void close() { | ||
| try (LockManager.WriteLock writeLock = lockManager.acquireWriteLock()) { | ||
| if (nativeDeltaHandle != 0) { | ||
| releaseNativeDelta(nativeDeltaHandle); | ||
| nativeDeltaHandle = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private native void releaseNativeDelta(long handle); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.