sarutak opened a new pull request #33106:
URL: https://github.com/apache/spark/pull/33106


   ### 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.
   -->
   This PR fixes an issue that field names of structs generated by `arrays_zip` 
function could be unexpectedly re-written by analyzer/optimizer.
   Here is an example.
   ```
   val df = sc.parallelize(Seq((Array(1, 2), Array(3, 4)))).toDF("a1", 
"b1").selectExpr("arrays_zip(a1, b1) as zipped")
   df.printSchema
   root
    |-- zipped: array (nullable = true)
    |    |-- element: struct (containsNull = false)
    |    |    |-- a1: integer (nullable = true)                                 
     // OK. a1 is expected name
    |    |    |-- b1: integer (nullable = true)                                 
     // OK. b1 is expected name
   
   df.explain
   == Physical Plan ==
   *(1) Project [arrays_zip(_1#3, _2#4) AS zipped#12]               // Not OK. 
field names are re-written as _1 and _2 respectively
   
   df.write.parquet("/tmp/test.parquet")
   val df2 = spark.read.parquet("/tmp/test.parquet")
   
   df2.printSchema
   root
    |-- zipped: array (nullable = true)
    |    |-- element: struct (containsNull = true)
    |    |    |-- _1: integer (nullable = true)                                 
     // Not OK. a1 is expected but got _1
    |    |    |-- _2: integer (nullable = true)                                 
     // Not OK. b1 is expected but got _2
   ```
   
   This issue happens when `AliasHelper.replaceAliasButKeepName` or 
`AliasHelper.trimNonTopLevelAliases` called via analyzer/optimizer is applied.
   
https://github.com/apache/spark/blob/b89cd8d75a0e78c6953cdd21c6e9c41495ed018f/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala#L883
   
https://github.com/apache/spark/blob/b89cd8d75a0e78c6953cdd21c6e9c41495ed018f/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala#L3759
   I investigated functions which can be affected this issue but I found only 
`arrays_zip` so far.
   
   To fix this issue, this PR changes the definition of `ArraysZip` to retain 
field names to avoid being re-written by analyzer/optimizer.
   
   ### 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.
   -->
   This is apparently a bug.
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as 
the documentation fix.
   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 possible, please also clarify if this is a user-facing change compared to 
the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   No. After this change, the field names are no longer re-written but it 
should be expected behavior for users.
   
   ### 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.
   -->
   New tests.


-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to