-
Notifications
You must be signed in to change notification settings - Fork 85
Add JNI Variant extraction support #4756
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
Open
nartal1
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
nartal1:variant-support-jni
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,89 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| #include "cudf_jni_apis.hpp" | ||
| #include "jni_utils.hpp" | ||
|
|
||
| #include <cudf/io/experimental/variant.hpp> | ||
| #include <cudf/types.hpp> | ||
| #include <cudf/utilities/default_stream.hpp> | ||
| #include <cudf/utilities/memory_resource.hpp> | ||
|
|
||
| extern "C" { | ||
|
|
||
| JNIEXPORT jlong JNICALL Java_com_nvidia_spark_rapids_jni_VariantUtils_getVariantFieldValue( | ||
| JNIEnv* env, jclass, jlong variant_struct_handle, jstring j_path) | ||
| { | ||
| JNI_NULL_CHECK(env, variant_struct_handle, "variant struct column is null", 0); | ||
| JNI_NULL_CHECK(env, j_path, "path is null", 0); | ||
| JNI_TRY | ||
| { | ||
| cudf::jni::auto_set_device(env); | ||
| auto const& variant_struct = *reinterpret_cast<cudf::column_view const*>(variant_struct_handle); | ||
| cudf::jni::native_jstring path(env, j_path); | ||
| return cudf::jni::release_as_jlong( | ||
| cudf::io::parquet::experimental::get_variant_field(variant_struct, | ||
| path.get(), | ||
| cudf::get_default_stream(), | ||
| cudf::get_current_device_resource_ref())); | ||
| } | ||
| JNI_CATCH(env, 0); | ||
| } | ||
|
|
||
| JNIEXPORT jlong JNICALL Java_com_nvidia_spark_rapids_jni_VariantUtils_castVariantValue( | ||
| JNIEnv* env, jclass, jlong value_bytes_handle, jint cudf_type_id) | ||
| { | ||
| JNI_NULL_CHECK(env, value_bytes_handle, "value bytes column is null", 0); | ||
| JNI_TRY | ||
| { | ||
| cudf::jni::auto_set_device(env); | ||
| auto const& value_bytes = *reinterpret_cast<cudf::column_view const*>(value_bytes_handle); | ||
| return cudf::jni::release_as_jlong(cudf::io::parquet::experimental::cast_variant( | ||
| value_bytes, | ||
| cudf::data_type{static_cast<cudf::type_id>(cudf_type_id)}, | ||
| cudf::get_default_stream(), | ||
| cudf::get_current_device_resource_ref())); | ||
| } | ||
| JNI_CATCH(env, 0); | ||
| } | ||
|
|
||
| JNIEXPORT jlong JNICALL Java_com_nvidia_spark_rapids_jni_VariantUtils_extractVariantField( | ||
| JNIEnv* env, jclass, jlong variant_struct_handle, jstring j_path, jint cudf_type_id) | ||
| { | ||
| JNI_NULL_CHECK(env, variant_struct_handle, "variant struct column is null", 0); | ||
| JNI_NULL_CHECK(env, j_path, "path is null", 0); | ||
| JNI_TRY | ||
| { | ||
| cudf::jni::auto_set_device(env); | ||
| auto const& variant_struct = *reinterpret_cast<cudf::column_view const*>(variant_struct_handle); | ||
| cudf::jni::native_jstring path(env, j_path); | ||
| return cudf::jni::release_as_jlong(cudf::io::parquet::experimental::extract_variant_field( | ||
| variant_struct, | ||
| path.get(), | ||
| cudf::data_type{static_cast<cudf::type_id>(cudf_type_id)}, | ||
| cudf::get_default_stream(), | ||
| cudf::get_current_device_resource_ref())); | ||
| } | ||
| JNI_CATCH(env, 0); | ||
| } | ||
|
|
||
| JNIEXPORT jboolean JNICALL Java_com_nvidia_spark_rapids_jni_VariantUtils_isAvailableNative(JNIEnv*, | ||
| jclass) | ||
| { | ||
| return JNI_TRUE; | ||
| } | ||
|
|
||
| } // extern "C" |
109 changes: 109 additions & 0 deletions
109
src/main/java/com/nvidia/spark/rapids/jni/VariantUtils.java
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,109 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. | ||
| * | ||
| * 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 com.nvidia.spark.rapids.jni; | ||
|
|
||
| import ai.rapids.cudf.ColumnVector; | ||
| import ai.rapids.cudf.ColumnView; | ||
| import ai.rapids.cudf.DType; | ||
| import ai.rapids.cudf.NativeDepsLoader; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * JNI bridge to cuDF's experimental Parquet Variant field extraction APIs. | ||
| */ | ||
| public class VariantUtils { | ||
| static { | ||
| NativeDepsLoader.loadNativeDeps(); | ||
| } | ||
|
|
||
| private static final List<DType> SUPPORTED_TYPES = Arrays.asList( | ||
| DType.STRING, DType.INT8, DType.INT16, DType.INT32, DType.INT64); | ||
|
|
||
| private VariantUtils() {} | ||
|
|
||
| private static void validateTargetType(DType targetType) { | ||
| Objects.requireNonNull(targetType, "targetType"); | ||
| if (!SUPPORTED_TYPES.contains(targetType)) { | ||
| throw new IllegalArgumentException("unsupported Variant target type: " + targetType + | ||
| "; supported types are " + SUPPORTED_TYPES); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extract raw Variant-encoded value bytes at {@code path} from a Variant struct column. | ||
| * | ||
| * @param variantStruct Variant materialization: STRUCT(metadata LIST<UINT8>, | ||
| * value LIST<UINT8>, optional shredded children...) | ||
| * @param path JSONPath-like path accepted by cuDF's Variant extractor. Paths are expected to | ||
| * be ASCII object-field paths like {@code x}, {@code $.x}, or {@code $.x.y}. | ||
| * @return LIST<UINT8> column of raw encoded Variant values | ||
| */ | ||
| public static ColumnVector getVariantFieldValue(ColumnView variantStruct, String path) { | ||
| Objects.requireNonNull(variantStruct, "variantStruct"); | ||
| Objects.requireNonNull(path, "path"); | ||
| return new ColumnVector(getVariantFieldValue(variantStruct.getNativeView(), path)); | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Decode raw Variant-encoded value bytes into {@code targetType}. Supported target types are | ||
| * {@link DType#STRING}, {@link DType#INT8}, {@link DType#INT16}, {@link DType#INT32}, and | ||
| * {@link DType#INT64}. | ||
| */ | ||
| public static ColumnVector castVariantValue(ColumnView valueBytes, DType targetType) { | ||
| Objects.requireNonNull(valueBytes, "valueBytes"); | ||
| validateTargetType(targetType); | ||
| return new ColumnVector(castVariantValue( | ||
| valueBytes.getNativeView(), targetType.getTypeId().getNativeId())); | ||
| } | ||
|
|
||
| /** | ||
| * Extract a Variant field and decode it into {@code targetType} in one native call. | ||
| * Supported target types are {@link DType#STRING}, {@link DType#INT8}, {@link DType#INT16}, | ||
| * {@link DType#INT32}, and {@link DType#INT64}. | ||
| */ | ||
| public static ColumnVector extractVariantField( | ||
| ColumnView variantStruct, String path, DType targetType) { | ||
| Objects.requireNonNull(variantStruct, "variantStruct"); | ||
| Objects.requireNonNull(path, "path"); | ||
| validateTargetType(targetType); | ||
| return new ColumnVector(extractVariantField( | ||
| variantStruct.getNativeView(), path, targetType.getTypeId().getNativeId())); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true when the loaded JNI library exposes the Variant extraction entry points. | ||
| */ | ||
| public static boolean isAvailable() { | ||
| try { | ||
| return isAvailableNative(); | ||
| } catch (UnsatisfiedLinkError e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static native long getVariantFieldValue(long variantStructHandle, String path); | ||
|
|
||
| private static native long castVariantValue(long valueBytesHandle, int cudfTypeId); | ||
|
|
||
| private static native long extractVariantField( | ||
| long variantStructHandle, String path, int cudfTypeId); | ||
|
|
||
| private static native boolean isAvailableNative(); | ||
| } | ||
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.