-
Notifications
You must be signed in to change notification settings - Fork 270
Add partial support for from_protobuf #14062
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
Draft
thirtiseven
wants to merge
9
commits into
NVIDIA:main
Choose a base branch
from
thirtiseven:from_protobuffer_v0
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.
+1,403
−9
Draft
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2ab5557
AI draft for protocol buffer support
thirtiseven 084e9c2
style
thirtiseven 7606925
address comments
thirtiseven c6cde2d
address comments
thirtiseven 6d4eb16
copyrights
thirtiseven 044ea96
column pruning
thirtiseven 6c1369e
Merge branch 'from_protobuffer_v0' of https://github.com/thirtiseven/…
thirtiseven a77d90e
fix
thirtiseven 2a66f9a
address comments
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Copyright (c) 2020-2025, NVIDIA CORPORATION. | ||
| # Copyright (c) 2020-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. | ||
|
|
@@ -857,6 +857,116 @@ def gen_bytes(): | |
| return bytes([ rand.randint(0, 255) for _ in range(length) ]) | ||
| self._start(rand, gen_bytes) | ||
|
|
||
|
|
||
| # ----------------------------------------------------------------------------- | ||
| # Protobuf (simple types) generators/utilities (for from_protobuf/to_protobuf tests) | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| _PROTOBUF_WIRE_VARINT = 0 | ||
| _PROTOBUF_WIRE_64BIT = 1 | ||
| _PROTOBUF_WIRE_LEN_DELIM = 2 | ||
| _PROTOBUF_WIRE_32BIT = 5 | ||
|
|
||
| def _encode_protobuf_uvarint(value): | ||
| """Encode a non-negative integer as protobuf varint.""" | ||
| if value is None: | ||
| raise ValueError("value must not be None") | ||
| if value < 0: | ||
| raise ValueError("uvarint only supports non-negative integers") | ||
| out = bytearray() | ||
| v = int(value) | ||
| while True: | ||
| b = v & 0x7F | ||
| v >>= 7 | ||
| if v: | ||
| out.append(b | 0x80) | ||
| else: | ||
| out.append(b) | ||
| break | ||
| return bytes(out) | ||
|
|
||
| def _encode_protobuf_key(field_number, wire_type): | ||
| return _encode_protobuf_uvarint((int(field_number) << 3) | int(wire_type)) | ||
|
|
||
| def _encode_protobuf_field(field_number, spark_type, value): | ||
| """ | ||
| Encode a single protobuf field for a subset of scalar types. | ||
| Notes on signed ints: | ||
| - Protobuf `int32`/`int64` use *varint* encoding of the two's-complement integer. | ||
| - Negative `int32` values are encoded as a 10-byte varint (because they are sign-extended to 64 bits). | ||
| """ | ||
| if value is None: | ||
| return b"" | ||
|
|
||
| if isinstance(spark_type, BooleanType): | ||
| return _encode_protobuf_key(field_number, _PROTOBUF_WIRE_VARINT) + _encode_protobuf_uvarint(1 if value else 0) | ||
| elif isinstance(spark_type, IntegerType): | ||
| # Match protobuf-java behavior for writeInt32NoTag: negative values are sign-extended and written as uint64. | ||
| u64 = int(value) & 0xFFFFFFFFFFFFFFFF | ||
| return _encode_protobuf_key(field_number, _PROTOBUF_WIRE_VARINT) + _encode_protobuf_uvarint(u64) | ||
| elif isinstance(spark_type, LongType): | ||
| u64 = int(value) & 0xFFFFFFFFFFFFFFFF | ||
| return _encode_protobuf_key(field_number, _PROTOBUF_WIRE_VARINT) + _encode_protobuf_uvarint(u64) | ||
| elif isinstance(spark_type, FloatType): | ||
| return _encode_protobuf_key(field_number, _PROTOBUF_WIRE_32BIT) + struct.pack("<f", float(value)) | ||
| elif isinstance(spark_type, DoubleType): | ||
| return _encode_protobuf_key(field_number, _PROTOBUF_WIRE_64BIT) + struct.pack("<d", float(value)) | ||
| elif isinstance(spark_type, StringType): | ||
| b = value.encode("utf-8") | ||
| return (_encode_protobuf_key(field_number, _PROTOBUF_WIRE_LEN_DELIM) + | ||
| _encode_protobuf_uvarint(len(b)) + b) | ||
| else: | ||
| raise ValueError("Unsupported type for protobuf simple generator: {}".format(spark_type)) | ||
|
|
||
|
|
||
| class ProtobufSimpleMessageRowGen(DataGen): | ||
|
||
| """ | ||
| Generates rows that include: | ||
| - one column per message field (Spark scalar types) | ||
| - a binary column containing a serialized protobuf message containing those fields | ||
|
|
||
| This is intentionally limited to the simple scalar types currently supported: | ||
| boolean/int32/int64/float/double/string. | ||
|
|
||
| Fields are omitted from the encoded message if the corresponding value is None. | ||
| """ | ||
| def __init__(self, fields, binary_col_name="bin", nullable=False): | ||
| """ | ||
| fields: list of (field_name, field_number, DataGen) | ||
| """ | ||
| self._fields = fields | ||
| self._binary_col_name = binary_col_name | ||
|
|
||
| struct_fields = [] | ||
| for (name, _num, gen) in fields: | ||
| struct_fields.append(StructField(name, gen.data_type, nullable=gen.nullable)) | ||
| struct_fields.append(StructField(binary_col_name, BinaryType(), nullable=True)) | ||
| super().__init__(StructType(struct_fields), nullable=nullable) | ||
|
|
||
| def __repr__(self): | ||
| return "ProtobufSimpleMessageRowGen({})".format( | ||
| ",".join(["{}#{}".format(n, num) for (n, num, _g) in self._fields])) | ||
|
|
||
| def _cache_repr(self): | ||
| kids = ",".join(["{}:{}#{}".format(n, str(g.data_type), num) for (n, num, g) in self._fields]) | ||
| return super()._cache_repr() + "(" + kids + "," + self._binary_col_name + ")" | ||
|
|
||
| def start(self, rand): | ||
| for (_name, _num, gen) in self._fields: | ||
| gen.start(rand) | ||
|
|
||
| def make_row(): | ||
| values = [] | ||
| encoded_parts = [] | ||
| for (name, num, gen) in self._fields: | ||
| v = gen.gen() | ||
| values.append(v) | ||
| encoded_parts.append(_encode_protobuf_field(num, gen.data_type, v)) | ||
| msg = b"".join(encoded_parts) | ||
| return tuple(values + [msg]) | ||
|
|
||
| self._start(rand, make_row) | ||
|
|
||
| # Note: Current(2023/06/06) maxmium IT data size is 7282688 bytes, so LRU cache with maxsize 128 | ||
| # will lead to 7282688 * 128 = 932 MB additional memory usage in edge case, which is acceptable. | ||
| @lru_cache(maxsize=128, typed=True) | ||
|
|
||
Oops, something went wrong.
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.
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.
Remove the trailing blank line at the end of the file. This follows standard code style guidelines and maintains consistency across the codebase.