[SPARK-56166][PYTHON] Use ArrowBatchTransformer.enforce_schema to replace column-wise type coercion logic#54967
Open
Yicong-Huang wants to merge 1 commit intoapache:masterfrom
Open
Conversation
…lace manual type coercion logic ### What changes were proposed in this pull request? Replace manual column-by-column type coercion with `ArrowBatchTransformer.enforce_schema` in three places: 1. `ArrowStreamArrowUDTFSerializer.apply_type_coercion` in serializers.py 2. `ArrowStreamArrowUDFSerializer.create_batch` in serializers.py 3. `process_results` in worker.py (scalar Arrow iter UDF path) Also: - Add `arrow_cast` parameter to `enforce_schema` for strict type matching mode - Add `KeyError` handling in `enforce_schema` for missing columns with user-friendly error - Remove now-unused `coerce_arrow_array` imports from serializers.py and worker.py ### Why are the changes needed? These three places duplicated the same coerce-and-reassemble logic that `enforce_schema` already provides. Consolidating reduces code duplication and ensures consistent error handling. ### Does this PR introduce _any_ user-facing change? Error messages for type/schema mismatches in Arrow UDTFs are slightly changed to be consistent with other Arrow UDF error messages. ### How was this patch tested? Existing tests in `test_arrow_udtf.py` and `test_arrow_udf_scalar.py`. ### Was this patch authored or co-authored using generative AI tooling? Yes.
c537147 to
e6a55c9
Compare
| # If so, use index-based access (faster than name lookup). | ||
| batch_names = [batch.schema.field(i).name for i in range(batch.num_columns)] | ||
| target_names = [field.name for field in arrow_schema] | ||
| use_index = batch_names == target_names |
Contributor
There was a problem hiding this comment.
on use_index, do we need to take care of nested cases?
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changes were proposed in this pull request?
Replace manual column-by-column type coercion with
ArrowBatchTransformer.enforce_schemain three places:ArrowStreamArrowUDTFSerializer.apply_type_coercionin serializers.pyArrowStreamArrowUDFSerializer.create_batchin serializers.pyprocess_resultsin worker.pyAlso:
arrow_castparameter toenforce_schemafor strict type matching modeKeyErrorhandling inenforce_schemafor missing columns with user-friendly errorWhy are the changes needed?
These three places duplicated the same coerce-and-reassemble logic that
enforce_schemaalready provides.Does this PR introduce any user-facing change?
Error messages for type/schema mismatches in Arrow UDTFs are slightly changed to be consistent with other Arrow UDF error messages.
How was this patch tested?
Existing tests in
test_arrow_udtf.pyandtest_arrow_udf_scalar.py.Was this patch authored or co-authored using generative AI tooling?
No.