-
Notifications
You must be signed in to change notification settings - Fork 284
Add protobuf integration-test dependency infrastructure (plugin-0) #14885
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
thirtiseven
wants to merge
7
commits into
NVIDIA:main
Choose a base branch
from
thirtiseven:from_protobuf_plugin_0
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
7 commits
Select commit
Hold shift + click to select a range
d8e2530
Add protobuf integration-test dependency infrastructure (plugin-0)
thirtiseven 9e23bdd
Merge remote-tracking branch 'origin/main' into from_protobuf_plugin_0
thirtiseven ae0e557
signoff
thirtiseven 8a7b4e2
Warn when INCLUDE_SPARK_PROTOBUF_JAR=true but jars are missing
thirtiseven 3dc8dbb
Write descFilePath with plain Python open(), not Hadoop FS
thirtiseven f1f780f
Trim comments to WHY-only
thirtiseven 899625a
Drop stale review-context comment
thirtiseven 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
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,130 @@ | ||
| # 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. | ||
|
|
||
| import inspect | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from asserts import assert_gpu_fallback_collect | ||
| from marks import allow_non_gpu | ||
| from spark_session import is_before_spark_340, with_cpu_session | ||
| import pyspark.sql.functions as f | ||
|
|
||
| if os.environ.get('INCLUDE_SPARK_PROTOBUF_JAR', 'true').lower() == 'false': | ||
| pytestmark = pytest.mark.skip(reason="INCLUDE_SPARK_PROTOBUF_JAR is disabled") | ||
| else: | ||
| pytestmark = pytest.mark.skipif( | ||
| is_before_spark_340(), reason="from_protobuf is Spark 3.4.0+") | ||
|
|
||
|
|
||
| def _try_import_from_protobuf(): | ||
| try: | ||
| from pyspark.sql.protobuf.functions import from_protobuf | ||
| return from_protobuf | ||
| except Exception: | ||
| return None | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def from_protobuf_fn(): | ||
| fn = _try_import_from_protobuf() | ||
| if fn is None: | ||
| pytest.skip("from_protobuf not available") | ||
| return fn | ||
|
|
||
|
|
||
| def _encode_varint(value): | ||
| out = bytearray() | ||
| value &= 0xFFFFFFFFFFFFFFFF | ||
| while True: | ||
| bits = value & 0x7F | ||
| value >>= 7 | ||
| if value: | ||
| out.append(bits | 0x80) | ||
| else: | ||
| out.append(bits) | ||
| return bytes(out) | ||
|
|
||
|
|
||
| def _encode_simple_message(i32_value, s_value): | ||
| buf = bytearray() | ||
| buf += _encode_varint((1 << 3) | 0) # field 1, VARINT | ||
| buf += _encode_varint(i32_value) | ||
| s_bytes = s_value.encode("utf-8") | ||
| buf += _encode_varint((2 << 3) | 2) # field 2, LENGTH-DELIMITED | ||
| buf += _encode_varint(len(s_bytes)) | ||
| buf += s_bytes | ||
| return bytes(buf) | ||
|
|
||
|
|
||
| def _build_simple_descriptor_bytes(spark): | ||
| D = spark.sparkContext._jvm.com.google.protobuf.DescriptorProtos | ||
| i32_field = D.FieldDescriptorProto.newBuilder() \ | ||
| .setName("i32").setNumber(1) \ | ||
| .setLabel(D.FieldDescriptorProto.Label.LABEL_OPTIONAL) \ | ||
| .setType(D.FieldDescriptorProto.Type.TYPE_INT32).build() | ||
| s_field = D.FieldDescriptorProto.newBuilder() \ | ||
| .setName("s").setNumber(2) \ | ||
| .setLabel(D.FieldDescriptorProto.Label.LABEL_OPTIONAL) \ | ||
| .setType(D.FieldDescriptorProto.Type.TYPE_STRING).build() | ||
| msg = D.DescriptorProto.newBuilder() \ | ||
| .setName("Simple").addField(i32_field).addField(s_field).build() | ||
| file_builder = D.FileDescriptorProto.newBuilder() \ | ||
| .setName("simple.proto").setPackage("test").addMessageType(msg) \ | ||
| .setSyntax("proto2") | ||
| fds = D.FileDescriptorSet.newBuilder().addFile(file_builder.build()).build() | ||
| return bytes(fds.toByteArray()) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def simple_desc(spark_tmp_path): | ||
| desc_path = spark_tmp_path + "/simple.desc" | ||
| desc_bytes = with_cpu_session(_build_simple_descriptor_bytes) | ||
| with open(desc_path, "wb") as fp: | ||
| fp.write(desc_bytes) | ||
| return desc_path, desc_bytes | ||
|
|
||
|
|
||
| _smoke_rows = [(1, "a"), (-2, "bb"), (0, ""), (12345, "hello")] | ||
|
|
||
|
|
||
| def _make_smoke_df(spark): | ||
| encoded = [(_encode_simple_message(i, s),) for (i, s) in _smoke_rows] | ||
| return spark.createDataFrame(encoded, ["bin"]) | ||
|
|
||
|
|
||
| @allow_non_gpu("ProjectExec", "ProtobufDataToCatalyst") | ||
| def test_from_protobuf_smoke_path_api(simple_desc, from_protobuf_fn): | ||
| desc_path, _ = simple_desc | ||
|
|
||
| def run(spark): | ||
| return _make_smoke_df(spark).select( | ||
| from_protobuf_fn(f.col("bin"), "test.Simple", desc_path).alias("d")) | ||
|
|
||
| assert_gpu_fallback_collect(run, "ProtobufDataToCatalyst") | ||
|
|
||
|
|
||
| @allow_non_gpu("ProjectExec", "ProtobufDataToCatalyst") | ||
| def test_from_protobuf_smoke_binary_descriptor_api(simple_desc, from_protobuf_fn): | ||
| if "binaryDescriptorSet" not in inspect.signature(from_protobuf_fn).parameters: | ||
| pytest.skip("binaryDescriptorSet kwarg is Spark 3.5+ only") | ||
| _, desc_bytes = simple_desc | ||
|
|
||
| def run(spark): | ||
| return _make_smoke_df(spark).select( | ||
| from_protobuf_fn(f.col("bin"), "test.Simple", | ||
| binaryDescriptorSet=bytearray(desc_bytes)).alias("d")) | ||
|
|
||
| assert_gpu_fallback_collect(run, "ProtobufDataToCatalyst") | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please validate that these tests work on a distributed setup like with HDFS? According to AI (which could totally be wrong) desc_path is read as a local file by pyspark. Because desc_path is written to with spark_tmp_path, which is a distributed setup for things like Dataproc, it could fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, it did not work on HDFS. Updated to
spark_tmp_pathnow.