You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "aknayar (via GitHub)" <gi...@apache.org> on 2026/03/31 04:26:22 UTC

[PR] Collation-aware PIVOT [spark]

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

   <!--
   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.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### 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 PivotFirst to respect pivot value collation. Below is a minimalistic repro for the bug:
   
   ```
   from pyspark.sql import SparkSession
   
   spark = SparkSession.builder.appName("PivotCollationTest").getOrCreate()
   
   data = [(1, "SALES", 100), (1, "sales", 50)]
   df = spark.createDataFrame(data, ["emp_id", "dept", "amount"])
   df.createOrReplaceTempView("df")
   
   COLLATED = "(SELECT emp_id, COLLATE(dept, 'UTF8_LCASE') AS dept, amount FROM df)"
   
   print("Pivot with 'SALES' - expects 150, gets 150")
   spark.sql(f"""
       SELECT * FROM {COLLATED}
       PIVOT (SUM(amount) FOR dept IN ('SALES'))
   """).show()
   
   print("Pivot with 'sales' - expects 150, gets NULL")
   spark.sql(f"""
       SELECT * FROM {COLLATED}
       PIVOT (SUM(amount) FOR dept IN ('sales'))
   """).show()
   
   spark.stop()
   ```
   
   Given the `UTF8_LCASE` collation, both PIVOT queries should return a `SUM(amount)` of 150. However, since Scala's ImmutableHashMap does a byte-level comparison of the keys in `pivotIndex` with incoming `pivotColumn` values, `SUM(amount)` for `'SALES'` returns `150` and `NULL` for `sales`. This is because the HashMap does a byte-by-byte comparison between `sales` and the resolved group-key (`SALES`).
   
   The fix is to fall back to `TreeMap` (already used on nested types) for non-binary-stable string types (e.g., UTF8_LCASE, UNICODE_CI).
   
   
   ### 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.
   -->
   
   There is currently a correctness bug. `PIVOT` compares atomic strings at the byte level, completely ignoring specified collation and silently returning `NULL`.
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   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'.
   -->
   Yes, collation-aware pivot values will now adhere to the specified collation. This will result in fewer false negative pivot column value matches and more meaningful results.
   
   ### How was this patch tested?
   <!--
   If tests were added, s ay 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.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   Unit tests testing `UTF8_LCASE` and `UNICODE_CI` collation with nulls.
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   Generated-by: claude-4.6-opus-high
   


-- 
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-unsubscribe@spark.apache.org

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


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


Re: [PR] [SPARK-56306][SQL] Fix collation-aware PIVOT [spark]

Posted by "aknayar (via GitHub)" <gi...@apache.org>.
aknayar commented on code in PR #55109:
URL: https://github.com/apache/spark/pull/55109#discussion_r3019777212


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/PivotFirst.scala:
##########
@@ -86,20 +86,23 @@ case class PivotFirst(
 
   override val dataType: DataType = ArrayType(valueDataType)
 
-  val pivotIndex: Map[Any, Int] = if (pivotColumn.dataType.isInstanceOf[AtomicType]) {
-    HashMap(pivotColumnValues.zipWithIndex: _*)
-  } else {
+  private val usesTreeMap: Boolean =
+    !TypeUtils.typeWithProperEquals(pivotColumn.dataType) ||
+      !pivotColumn.dataType.isInstanceOf[AtomicType]

Review Comment:
   Done!



-- 
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-unsubscribe@spark.apache.org

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


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


Re: [PR] [SPARK-56306][SQL] Fix collation-aware PIVOT [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #55109:
URL: https://github.com/apache/spark/pull/55109#discussion_r3019635728


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/PivotFirst.scala:
##########
@@ -86,20 +86,23 @@ case class PivotFirst(
 
   override val dataType: DataType = ArrayType(valueDataType)
 
-  val pivotIndex: Map[Any, Int] = if (pivotColumn.dataType.isInstanceOf[AtomicType]) {
-    HashMap(pivotColumnValues.zipWithIndex: _*)
-  } else {
+  private val usesTreeMap: Boolean =
+    !TypeUtils.typeWithProperEquals(pivotColumn.dataType) ||
+      !pivotColumn.dataType.isInstanceOf[AtomicType]

Review Comment:
   nit: The `|| !pivotColumn.dataType.isInstanceOf[AtomicType]` clause is redundant. `typeWithProperEquals` already returns `false` for all non-`AtomicType` types (see the `case _ => false` catch-all in `TypeUtils.typeWithProperEquals`), so `!typeWithProperEquals(...)` alone produces identical results.
   
   ```suggestion
     private val usesTreeMap: Boolean =
       !TypeUtils.typeWithProperEquals(pivotColumn.dataType)
   ```



-- 
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-unsubscribe@spark.apache.org

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


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


Re: [PR] [SPARK-56306][SQL] Fix collation-aware PIVOT [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #55109:
URL: https://github.com/apache/spark/pull/55109#issuecomment-4174021066

   thanks, merging to master!


-- 
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-unsubscribe@spark.apache.org

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


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


Re: [PR] [SPARK-56306][SQL] Fix collation-aware PIVOT [spark]

Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #55109:
URL: https://github.com/apache/spark/pull/55109#discussion_r3017292458


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/PivotFirst.scala:
##########
@@ -86,20 +87,23 @@ case class PivotFirst(
 
   override val dataType: DataType = ArrayType(valueDataType)
 
-  val pivotIndex: Map[Any, Int] = if (pivotColumn.dataType.isInstanceOf[AtomicType]) {
-    HashMap(pivotColumnValues.zipWithIndex: _*)
-  } else {
+  private val usesTreeMap: Boolean =
+    !UnsafeRowUtils.isBinaryStable(pivotColumn.dataType) ||

Review Comment:
   I think that `typeWithProperEquals` is more suitable here instead



-- 
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-unsubscribe@spark.apache.org

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


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


Re: [PR] [SPARK-56306][SQL] Fix collation-aware PIVOT [spark]

Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on PR #55109:
URL: https://github.com/apache/spark/pull/55109#issuecomment-4165853573

   @cloud-fan Please take a look when you get a chance. Thanks!


-- 
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-unsubscribe@spark.apache.org

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


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


Re: [PR] [SPARK-56306][SQL] Fix collation-aware PIVOT [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #55109: [SPARK-56306][SQL] Fix collation-aware PIVOT
URL: https://github.com/apache/spark/pull/55109


-- 
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-unsubscribe@spark.apache.org

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


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