[SPARK-55674][PYTHON] Optimize 0-column table conversion in Spark Connect#54468
Closed
Yicong-Huang wants to merge 1 commit intoapache:masterfrom
Closed
Conversation
Member
|
Can't we apply this to spark/python/pyspark/sql/conversion.py Line 289 in 01bfd80 ? |
Contributor
Author
Not for this case. This case is "data" is empty but schema is non empty, so we could not use convert and preserve the information from data: the columns will mismatch. |
Member
|
Merged to master. |
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
pa.Table.from_struct_array(pa.array([{}] * len(data), type=pa.struct([])))withpa.Table.from_batches([pa.RecordBatch.from_pandas(data)])inconnect/session.pywhen handling 0-column pandas DataFrames. This is O(1) operation, regardless how many rows are there.Why are the changes needed?
The original approach constructs
len(data)Python dict objects ([{}] * len(data)), which is O(n).pa.RecordBatch.from_pandasis an O(1) operation regardless of the number of rows, as it reads rowcount directly from pandas index metadata without allocating per-row Python objects.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
Existing tests.
Was this patch authored or co-authored using generative AI tooling?
No.