JoshRosen opened a new pull request #25745: [SPARK-29033][SQL][WIP
URL: https://github.com/apache/spark/pull/25745
 
 
   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: 
https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: 
https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a 
faster review.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section 
is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster 
reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class 
hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other 
DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   Spark 2.x has two separate implementations of the "create named struct" 
expression: regular `CreateNamedStruct` and `CreateNamedStructUnsafe`. The 
former produces `GenericInternalRows`, while the latter produces `UnsafeRow`s. 
Both two expressions both extend the `CreateNameStructLike` trait.
   
   The "unsafe" version was added in SPARK-9373 / #7689 to support structs in 
`GenerateUnsafeProjection` (this was fairly early in the Tungsten effort, circa 
mid-2015 / Spark 1.5.x). 
   
   This PR changes Spark so the UnsafeRow-based codepath is always used and 
removes the GenericRow-based path. For ease-of-review, I've broken this into 
two commits:
   
   - The first commit changes `CreateNamedStruct` to use the 
`CreateNamedStructUnsafe` implementation (producing `UnsafeRow`) and deletes 
`CreateNamedStructUnsafe`.
   - The second commit removes the  `CreateNameStructLike` trait (since at that 
point it only has a single implementation).
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   The old `CreateNamedStruct` code path allocated a fresh `GenericInternalRow` 
on every invocation, incurring object allocation and primitive-boxing 
performance overheads. I suspect that this can be a significant performance 
problem in code which uses `Dataset`s: Spark's `ExpressionEncoder` uses 
`CreateNamedStruct` for serializing / deserializing case classes, so the 
`GenericInternalRow` performance problems can impact typed Dataset operations 
(e.g. `.map()`).
   
   ### Does this PR introduce any user-facing change?
   <!--
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If no, write 'No'.
   -->
   
   There is no expected user-facing behavioral change.
   
   However, there **is** an  binary-compatibility-breaking change due to the 
deletion of `CreateNamedStructUnsafe` and `CreateNamedStructLike`. I suspect 
that these were originally intended to be internal / private classes and 
therefore propose that this is an acceptable breaking change in Spark 3.x 
(since it seems pretty unlikely that Spark users would be directly using those 
internal interfaces).
   
   I think there's also code-simplification benefits: if we only have a single 
implementation of `CreateNamedStruct` then we're removing the possibility for 
bugs in case code authors pattern-match on concrete classes instead of the 
`CreateNamedStructLike` trait.
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   -->
   
   I believe this is covered by Spark's existing tests. 
`CreateNamedStructUnsafe` is already used in `GenerateUnsafeProjection` and 
thus already had some pre-existing test coverage. 
   
   ⚠️ I've marked this PR as `WIP` pending some benchmarking and time to think 
about possible corner-cases. 
   
   #### Performance benchmark
   
   Here's a (somewhat hacky) end-to-end performance benchmark exercising this 
codepath in `spark-shell`:
   
   ```scala
   val start = System.currentTimeMillis()
   spark.sql("select struct(id, id, id, id, id, id, id, id, id) from range(1000 
* 1000 * 50)").rdd.count
   val end = System.currentTimeMillis()
   println(end - start)
   println
   ```
   
   On my laptop, this takes ~21 seconds after.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to