HyukjinKwon opened a new pull request, #805:
URL: https://github.com/apache/spark-connect-rust/pull/805

   ### What changes were proposed in this pull request?
   
   Two follow-ups of #785 in `python/pyspark/sql/connect/proto/_rust.py`.
   
   **A protobuf whose builder lacks the hooked function.** 
`_hook_protobuf_builder` read
   `builder.BuildTopDescriptorsAndMessages` after its `try`. With a protobuf 
whose builder lacks or
   renames that internal function, `import pyspark.sql.connect.proto` raised 
`AttributeError`:
   
   ```
   from google.protobuf.internal import builder
   del builder.BuildTopDescriptorsAndMessages
   import pyspark.sql.connect.proto
   # AttributeError: module 'google.protobuf.internal.builder' has no attribute 
'BuildTopDescriptorsAndMessages'
   ```
   
   The attribute is now read inside the `try`. So such a protobuf is passed 
over without the hook,
   as a protobuf of no builder (before 3.20) already was. No released protobuf 
lacks the function;
   this guards the import of the package against one that does.
   
   **The module of the runtime's classes of a file built before the hook.** The 
runtime makes
   classes for the messages of the files that a plugin's file imports, such as 
Delta Lake's
   `delta/connect/base.proto`. A class takes the name of the generated module 
that protobuf's builder
   built of the file since the hook was installed. Without one, it took 
protoc's name of the file's
   path. Delta Lake's `delta/connect/proto/__init__.py` imports `base_pb2` 
before the modules that
   import the protos of Spark Connect, which install the hook. So in that 
import order, which
   `from delta.tables import DeltaTable` then `DeltaTable.forPath(...)` takes, 
the class was named
   after `delta.connect.base_pb2`, a module that does not exist. In the other 
order it was named after
   `delta.connect.proto.base_pb2`, which upb's class of the type is:
   
   ```
   type(DeltaScan(dt._to_proto()).to_delta_relation(client).scan.table)
   # before: delta.connect.base_pb2.DeltaTable   (only when base_pb2 was built 
before the hook)
   # after:  delta.connect.proto.base_pb2.DeltaTable, in every import order
   ```
   
   `_module_of_file` now looks for an imported module of the file, one whose 
`DESCRIPTOR` is the
   file, and takes the module of protobuf's classes in it. It reads only the 
modules' globals
   (`vars`), so no module's `__getattr__` runs. If there is no such module, it 
falls back to
   protoc's name, as before.
   
   ### Why are the changes needed?
   
   So that importing `pyspark.sql.connect.proto` does not depend on an internal 
function of
   protobuf, and so that the classes of a plugin's types are named after the 
plugin's module, as upb's
   are, whatever the import order.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. In the import order above, the runtime classes of the messages of a 
file like Delta's
   `base.proto` now have the `__module__` of their generated module, which 
shows in their `repr` and
   in error messages. And `import pyspark.sql.connect.proto` no longer fails on 
a protobuf whose
   builder lacks the function.
   
   ### How was this patch tested?
   
   - **New tests in `test_proto_rust_offline.py`. Both fail without the change 
and pass with it.**
     - 
`test_classes_of_a_file_built_before_the_hook_are_of_its_generated_module`: a 
file built by
       protobuf's own builder, under a module name other than protoc's, before 
a plugin's file that
       imports it.
     - 
`test_a_protobuf_whose_builder_lacks_the_hooked_function_imports_the_package`: 
a subprocess
       that deletes the function before the import.
   - **Python suites.**
     - `test_proto_rust_offline.py` and `test_ml_offline.py`: 462 passed on upb.
     - `test_proto_rust_offline.py` with pure-Python protobuf and without 
protobuf: 2 passed and 51
       skipped each, as on master.
     - `test_dropin_offline.py`: 2365 passed and 8 skipped.
   - **Probes with Delta Lake 4.0.0's own modules, upb and pure-Python, not 
committed as they are
     third-party code.** The import orders were the proto package first, 
`base_pb2` first,
     `relations_pb2` first, and 16 threads importing at once. In each, the 
class of `scan.table` is
     `delta.connect.proto.base_pb2.DeltaTable`. 32 threads building plans at 
once got the same bytes,
     and 20 reloads of the modules kept their classes.
   - 86 unrelated modules behave as without the drop-in, before and after a 
reload: googleapis, grpc
     health and reflection 1.85.1, protobuf's own well-known types, and proto2, 
proto3 and editions
     modules written for the probe.
   - `ruff check` and `ruff format --check` (0.14.0), and 
`scripts/audit_no_stubs.sh`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to