zhengruifeng opened a new pull request, #45431:
URL: https://github.com/apache/spark/pull/45431

   ### What changes were proposed in this pull request?
   Make `withColumnsRenamed` duplicated column name handling consistent with 
`withColumnRenamed`
   
   
   ### Why are the changes needed?
   `withColumnsRenamed` checks the column names duplication of output 
dataframe, this is not consistent with `withColumnRenamed`:
   1, `withColumnRenamed` doesn't do this check, and support output a dataframe 
with duplicated column names;
   2, when the input dataframe has duplicated column names, 
`withColumnsRenamed` always fail, even if the columns with the same name are 
not touched at all:
   
   
   ```
   In [8]: df1 = spark.createDataFrame([(1, "id2"),], ["id", "value"])
      ...: df2 = spark.createDataFrame([(1, 'x', 'id1'), ], ["id", 'a', 
"value"])
      ...: join = df2.join(df1, on=['id'], how='left')
      ...: join
   Out[8]: DataFrame[id: bigint, a: string, value: string, value: string]
   
   In [9]: join.withColumnRenamed('id', 'value')
   Out[9]: DataFrame[value: bigint, a: string, value: string, value: string]
   
   In [10]: join.withColumnsRenamed({'id' : 'value'})
   ...
   AnalysisException: [COLUMN_ALREADY_EXISTS] The column `value` already 
exists. Choose another name or rename the existing column. SQLSTATE: 42711
   
   In [11]: join.withColumnRenamed('a', 'b')
   Out[11]: DataFrame[id: bigint, b: string, value: string, value: string]
   
   In [12]: join.withColumnsRenamed({'a' : 'b'})
   ...
   AnalysisException: [COLUMN_ALREADY_EXISTS] The column `value` already 
exists. Choose another name or rename the existing column. SQLSTATE: 42711
   
   In [13]: join.withColumnRenamed('x', 'y')
   Out[13]: DataFrame[id: bigint, a: string, value: string, value: string]
   
   In [14]: join.withColumnsRenamed({'x' : 'y'})
   AnalysisException: [COLUMN_ALREADY_EXISTS] The column `value` already 
exists. Choose another name or rename the existing column. SQLSTATE: 42711
   
   In [15]: join.withColumnRenamed('value', 'new_value')
   Out[15]: DataFrame[id: bigint, a: string, new_value: string, new_value: 
string]
   
   In [16]: join.withColumnsRenamed({'value' : 'new_value'})
   AnalysisException: [COLUMN_ALREADY_EXISTS] The column `new_value` already 
exists. Choose another name or rename the existing column. SQLSTATE: 42711
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   yes
   
   
   
   
   ### How was this patch tested?
   updated tests
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   no


-- 
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