You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "vladanvasi-db (via GitHub)" <gi...@apache.org> on 2024/10/21 16:02:26 UTC
[PR] [SPARK-50060][Collations] Fixed behavior of set operators when dealing with conflicting collations [spark]
vladanvasi-db opened a new pull request, #48585:
URL: https://github.com/apache/spark/pull/48585
<!--
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.
-->
In this PR, I propose disabling casting between different collations in set operators(`union [all]`, `except [all]` and `intersect`).
### 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.
-->
These changes are needed to ensure the correct behavior of set operators with collated strings. This way, the user will get appropriate error in case of collation collision.
### 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.
### 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.
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.
-->
### 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.
-->
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810873051
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala:
##########
@@ -148,6 +149,10 @@ object AnsiTypeCoercion extends TypeCoercionBase {
// interval type the string should be promoted as. There are many possible interval
// types, such as year interval, month interval, day interval, hour interval, etc.
case (_: StringType, _: AnsiIntervalType) => None
+ // [SPARK-50060] If a binary operation contains at least one collated string types, we can't
+ // decide which collation the result should have.
+ case (d1: StringType, d2: StringType) if SchemaUtils.hasNonUTF8BinaryCollation(d1) ||
Review Comment:
After adding the case when we have collated strings as column types in the output of set operators, we do not have to modify the `AnsiTypeCoercion` and `TypeCoercion` and add this case-matchings.
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1815055481
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationSQLExpressionsSuite.scala:
##########
@@ -3260,6 +3260,39 @@ class CollationSQLExpressionsSuite
}
}
+ test("SPARK-50060: set operators with conflicting collations") {
Review Comment:
hmm, does this provide more test coverage than the golden files?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810881746
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala:
##########
@@ -200,4 +199,15 @@ object CollationTypeCasts extends TypeCoercionRule {
}
}
}
+
+ /**
+ * Returns whether the expression has explicit collated output type.
+ */
+ def hasExplicitCollation(expr: Expression): Boolean = {
+ expr match {
+ case _: Collate => true
+ case alias: Alias => hasExplicitCollation(alias.child)
Review Comment:
I will add a test case for this with an expression that gets the subquery as its input in these set operators
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "mihailom-db (via GitHub)" <gi...@apache.org>.
mihailom-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1815655287
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -1014,7 +1016,9 @@ object TypeCoercion extends TypeCoercionBase {
case (_: StringType, datetime: DatetimeType) => datetime
case (_: StringType, AnyTimestampType) => AnyTimestampType.defaultConcreteType
case (_: StringType, BinaryType) => BinaryType
- // Cast any atomic type to string.
+ // Cast any atomic type to string except if there are strings with different collations. In
+ // that case we skip the cast.
+ case (st1: StringType, st2: StringType) if st1.collationId != st2.collationId => null
Review Comment:
This seems like we are adding something unnecessary. The comment said we cast any atomic type and that rule is checking for whether any is instance of StringType, so we cannot hit this by any rule below actually, so we should be fine with getting the default which is null? I am good with adding additional comment as to why we do not cast StringType in the existing rule, but this is just moving the default branch earlier.
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1815254766
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -1014,7 +1016,9 @@ object TypeCoercion extends TypeCoercionBase {
case (_: StringType, datetime: DatetimeType) => datetime
case (_: StringType, AnyTimestampType) => AnyTimestampType.defaultConcreteType
case (_: StringType, BinaryType) => BinaryType
- // Cast any atomic type to string.
+ // Cast any atomic type to string except if there are strings with different collations. In
+ // that case we skip the cast.
+ case (st1: StringType, st2: StringType) if st1.collationId != st2.collationId => null
Review Comment:
I updated a comment, agree that we should not hit this branch in any execution
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810671076
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala:
##########
@@ -162,7 +162,7 @@ object CollationTypeCasts extends TypeCoercionRule {
*/
def getOutputCollation(expr: Seq[Expression]): StringType = {
val explicitTypes = expr.filter {
- case _: Collate => true
+ case _: Collate | Alias(_: Collate, _) => true
Review Comment:
Someone else will probably want to see if an expression has explicit collation as well; can you maybe add this as a separate function?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "mihailom-db (via GitHub)" <gi...@apache.org>.
mihailom-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812152739
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
- castedTypes.enqueue(widenTypeOpt)
+ val outputType = findOutputType(children, attrIndex)
+ castedTypes.enqueue(outputType)
getWidestTypes(children, attrIndex + 1, castedTypes)
}
+ /** Given children of the operator, determines the output type of the
+ * `attrIndex`-th attribute
+ */
+ private def findOutputType(children: Seq[LogicalPlan], attrIndex: Int): Option[DataType] = {
Review Comment:
By same type I mean strings but with different collations.
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1815257743
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationSQLExpressionsSuite.scala:
##########
@@ -3260,6 +3260,39 @@ class CollationSQLExpressionsSuite
}
}
+ test("SPARK-50060: set operators with conflicting collations") {
Review Comment:
Yes, I added also the test for collisions between default collation and explicit collation. In case we modify the behavior later, this test should also be changed.
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1814477281
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -356,6 +367,71 @@ abstract class TypeCoercionBase {
plan
}
}
+
+ /** Builds new children for set operators while resolving collation conflicts in respective
+ * children columns. This is being done before any other type widening because STRING can also
+ * be casted to a different dataType, but conflicts between collations have to be reported. */
+ private def buildNewChildrenWithResolvedCollation(
+ children: Seq[LogicalPlan]): Seq[LogicalPlan] = {
+ require(children.forall(_.output.length == children.head.output.length))
+ // Since this function is called for set operators, we can be sure that their children
+ // are all `Project` nodes. An `assert` is added for double checking the assumption.
Review Comment:
I'm confused, how do we guarantee it? The children of `Union` can be anything, right?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810836800
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -310,6 +310,14 @@ abstract class TypeCoercionBase {
}
}
+ /** Checks whether the output of a logical plan has a collated string output type at
+ * the corresponding index */
+ private def outputHasNonUTF8BinaryCollation(
+ children: Seq[LogicalPlan],
Review Comment:
nit: 4 spaces indentation
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810870040
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala:
##########
@@ -148,6 +149,10 @@ object AnsiTypeCoercion extends TypeCoercionBase {
// interval type the string should be promoted as. There are many possible interval
// types, such as year interval, month interval, day interval, hour interval, etc.
case (_: StringType, _: AnsiIntervalType) => None
+ // [SPARK-50060] If a binary operation contains at least one collated string types, we can't
+ // decide which collation the result should have.
+ case (d1: StringType, d2: StringType) if SchemaUtils.hasNonUTF8BinaryCollation(d1) ||
Review Comment:
`CollationTypeCast` will work for expressions, but it will not work for set operators such as `union`. However, this can be removed since we do not use it when we have collated string column in the output
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812157798
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
- castedTypes.enqueue(widenTypeOpt)
+ val outputType = findOutputType(children, attrIndex)
+ castedTypes.enqueue(outputType)
getWidestTypes(children, attrIndex + 1, castedTypes)
}
+ /** Given children of the operator, determines the output type of the
+ * `attrIndex`-th attribute
+ */
+ private def findOutputType(children: Seq[LogicalPlan], attrIndex: Int): Option[DataType] = {
Review Comment:
You cannot catch this in `CollationTypeCasts` or any type coercion rule for that matter, since they only operate on individual expressions, and here the projects are spread across different subqueries
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #48585: [SPARK-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion
URL: https://github.com/apache/spark/pull/48585
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "mihailom-db (via GitHub)" <gi...@apache.org>.
mihailom-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810840747
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala:
##########
@@ -148,6 +149,10 @@ object AnsiTypeCoercion extends TypeCoercionBase {
// interval type the string should be promoted as. There are many possible interval
// types, such as year interval, month interval, day interval, hour interval, etc.
case (_: StringType, _: AnsiIntervalType) => None
+ // [SPARK-50060] If a binary operation contains at least one collated string types, we can't
+ // decide which collation the result should have.
+ case (d1: StringType, d2: StringType) if SchemaUtils.hasNonUTF8BinaryCollation(d1) ||
Review Comment:
By design we should not hit this rule. The CollationTypeCast is done first in the TypeCoercion and AnsiTypeCoercion rules. @cloud-fan am I missing something here?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala:
##########
@@ -200,4 +199,15 @@ object CollationTypeCasts extends TypeCoercionRule {
}
}
}
+
+ /**
+ * Returns whether the expression has explicit collated output type.
+ */
+ def hasExplicitCollation(expr: Expression): Boolean = {
+ expr match {
+ case _: Collate => true
+ case alias: Alias => hasExplicitCollation(alias.child)
Review Comment:
Are we sure this is safe to do for all other expressions not related to your PR? It might be that some expressions gets a subquery as input, could you please make sure we do not break something with propagating explicit collation like this?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on PR #48585:
URL: https://github.com/apache/spark/pull/48585#issuecomment-2429107480
@stefankandic @stevomitric can you plese review this PR?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810661923
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala:
##########
@@ -148,6 +149,10 @@ object AnsiTypeCoercion extends TypeCoercionBase {
// interval type the string should be promoted as. There are many possible interval
// types, such as year interval, month interval, day interval, hour interval, etc.
case (_: StringType, _: AnsiIntervalType) => None
+ // [SPARK-50060] If a binary operation contains at least one collated string types, we can't
+ // decide which collation the result should have.
+ case (d1: StringType, d2: StringType) if !UnsafeRowUtils.isBinaryStable(d1) ||
+ !UnsafeRowUtils.isBinaryStable(d2) => None
Review Comment:
we should probably not use `UnsafeRowUtils` here, `SchemaUtils.hasNonUTF8BinaryCollation` would be a better choice
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala:
##########
@@ -162,7 +162,7 @@ object CollationTypeCasts extends TypeCoercionRule {
*/
def getOutputCollation(expr: Seq[Expression]): StringType = {
val explicitTypes = expr.filter {
- case _: Collate => true
+ case _: Collate | Alias(_: Collate, _) => true
Review Comment:
Someone else will probably want to see if an expression is explicit as well; can you maybe add this as a separate function?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -310,6 +310,14 @@ abstract class TypeCoercionBase {
}
}
+ /** Checks whether the output of a logical plan has a collated string output type at
+ * the corresponding index */
+ private def hasOutputCollatedStringTypes(
+ children: Seq[LogicalPlan],
+ attrIndex: Int): Boolean = {
+ children.exists(p => !UnsafeRowUtils.isBinaryStable(p.output(attrIndex).dataType))
Review Comment:
ditto
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationSQLExpressionsSuite.scala:
##########
@@ -3260,6 +3260,46 @@ class CollationSQLExpressionsSuite
}
}
+ test("SPARK-50060: set operators with conflicting and non-conflicting collations") {
+ val setOperators = Seq[(String, Seq[Row])](
+ ("UNION", Seq(Row("a"))),
+ ("INTERSECT", Seq(Row("a"))),
+ ("EXCEPT", Seq()),
+ ("UNION ALL", Seq(Row("A"), Row("a"))),
+ ("INTERSECT ALL", Seq(Row("a"))),
+ ("EXCEPT ALL", Seq()),
+ ("UNION DISTINCT", Seq(Row("a"))),
+ ("INTERSECT DISTINCT", Seq(Row("a"))),
+ ("EXCEPT DISTINCT", Seq()))
+
+ Seq[Boolean](true, false).foreach{ ansi_enabled =>
Review Comment:
we can merge this like:
```code
Seq(true, false).foreach { ansiEnabled =>
withSQLConf(
SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString,
SqlApiConf.DEFAULT_COLLATION -> "UNICODE"
) {
// Your code here
}
}
```
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationSQLExpressionsSuite.scala:
##########
@@ -3260,6 +3260,46 @@ class CollationSQLExpressionsSuite
}
}
+ test("SPARK-50060: set operators with conflicting and non-conflicting collations") {
+ val setOperators = Seq[(String, Seq[Row])](
+ ("UNION", Seq(Row("a"))),
+ ("INTERSECT", Seq(Row("a"))),
+ ("EXCEPT", Seq()),
+ ("UNION ALL", Seq(Row("A"), Row("a"))),
+ ("INTERSECT ALL", Seq(Row("a"))),
+ ("EXCEPT ALL", Seq()),
+ ("UNION DISTINCT", Seq(Row("a"))),
+ ("INTERSECT DISTINCT", Seq(Row("a"))),
+ ("EXCEPT DISTINCT", Seq()))
+
+ Seq[Boolean](true, false).foreach{ ansi_enabled =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi_enabled.toString) {
+ withSQLConf(SqlApiConf.DEFAULT_COLLATION -> "UNICODE") {
+ setOperators.foreach { case (operator, result) =>
+ // Check whether conflict between explicit collations is detected
+ val conflictingQuery =
+ s"SELECT 'a' COLLATE UTF8_LCASE $operator SELECT 'A' COLLATE UNICODE_CI"
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(conflictingQuery)
+ },
+ condition = "COLLATION_MISMATCH.EXPLICIT",
+ parameters = Map(
+ "explicitTypes" -> "\"STRING COLLATE UTF8_LCASE\", \"STRING COLLATE UNICODE_CI\"")
+ )
+
+ // Check whether conflict between default and explicit collation is resolved in
+ // favor of the explicit collation
+ val nonConflictingQuery = s"SELECT 'a' COLLATE UTF8_LCASE AS val $operator " +
+ s"SELECT 'A' AS val ORDER BY val COLLATE UTF8_BINARY"
+ checkAnswer(sql(nonConflictingQuery), result)
Review Comment:
can we add some cases with implicit mismatch? We have a table and select its column, or we use some string function on the literal?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -905,6 +924,10 @@ object TypeCoercion extends TypeCoercionBase {
/** Promotes all the way to StringType. */
private def stringPromotion(dt1: DataType, dt2: DataType): Option[DataType] = (dt1, dt2) match {
+ // [SPARK-50060] If a binary operation contains at least one collated string type,
+ // we can't decide which collation the result should have.
+ case (d1 : StringType, d2: StringType) if !UnsafeRowUtils.isBinaryStable(d1) ||
+ !UnsafeRowUtils.isBinaryStable(d2) => None
Review Comment:
ditto
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -310,6 +310,14 @@ abstract class TypeCoercionBase {
}
}
+ /** Checks whether the output of a logical plan has a collated string output type at
+ * the corresponding index */
+ private def hasOutputCollatedStringTypes(
Review Comment:
```suggestion
private def outputHasNonUTF8BinaryCollation(
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,9 +343,20 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
- castedTypes.enqueue(widenTypeOpt)
+ // If the `children` output has at least one collated string type as the output type at the
+ // `attrIndex`-th attribute, `CollationTypeCasts` is called to determine the output collation.
+ if (hasOutputCollatedStringTypes(children, attrIndex)) {
+ // Since this function is called for set operators, we can be sure that their children
+ // are all `Project` nodes.
+ val collatedOutputType = CollationTypeCasts.getOutputCollation(
Review Comment:
Can we maybe move this into a separate function which returns which type should be added to the queue? Also we could add an assert that all of those are actually `Project` nodes
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810842204
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala:
##########
@@ -148,6 +149,10 @@ object AnsiTypeCoercion extends TypeCoercionBase {
// interval type the string should be promoted as. There are many possible interval
// types, such as year interval, month interval, day interval, hour interval, etc.
case (_: StringType, _: AnsiIntervalType) => None
+ // [SPARK-50060] If a binary operation contains at least one collated string types, we can't
+ // decide which collation the result should have.
+ case (d1: StringType, d2: StringType) if SchemaUtils.hasNonUTF8BinaryCollation(d1) ||
Review Comment:
This makes me think that maybe we should not put string collation type coercion in an individual rule, as some type coercion rules must be aware of string collation as well. Shall we just make each type coercion rule collation aware?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1814473089
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -310,6 +313,14 @@ abstract class TypeCoercionBase {
}
}
+ /** Checks whether the output of a logical plan has a collated string output type at
+ * the corresponding index */
+ private def outputHasNonUTF8BinaryCollation(
+ children: Seq[LogicalPlan],
Review Comment:
```suggestion
children: Seq[LogicalPlan],
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -310,6 +313,14 @@ abstract class TypeCoercionBase {
}
}
+ /** Checks whether the output of a logical plan has a collated string output type at
+ * the corresponding index */
+ private def outputHasNonUTF8BinaryCollation(
+ children: Seq[LogicalPlan],
+ attrIndex: Int): Boolean = {
Review Comment:
```suggestion
attrIndex: Int): Boolean = {
```
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1814473686
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -356,6 +367,71 @@ abstract class TypeCoercionBase {
plan
}
}
+
+ /** Builds new children for set operators while resolving collation conflicts in respective
+ * children columns. This is being done before any other type widening because STRING can also
+ * be casted to a different dataType, but conflicts between collations have to be reported. */
+ private def buildNewChildrenWithResolvedCollation(
+ children: Seq[LogicalPlan]): Seq[LogicalPlan] = {
Review Comment:
```suggestion
children: Seq[LogicalPlan]): Seq[LogicalPlan] = {
```
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1815057614
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -1014,7 +1016,9 @@ object TypeCoercion extends TypeCoercionBase {
case (_: StringType, datetime: DatetimeType) => datetime
case (_: StringType, AnyTimestampType) => AnyTimestampType.defaultConcreteType
case (_: StringType, BinaryType) => BinaryType
- // Cast any atomic type to string.
+ // Cast any atomic type to string except if there are strings with different collations. In
+ // that case we skip the cast.
+ case (st1: StringType, st2: StringType) if st1.collationId != st2.collationId => null
Review Comment:
I think this makes sense. Just for my curiosity, can we really hit this branch today? I don't think there is any function that only accept a certion string collation and we can't cast the input string with a different collation.
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #48585:
URL: https://github.com/apache/spark/pull/48585#issuecomment-2437816535
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812115111
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationSQLExpressionsSuite.scala:
##########
@@ -3260,6 +3260,72 @@ class CollationSQLExpressionsSuite
}
}
+ test("SPARK-50060: set operators with conflicting and non-conflicting collations") {
+ val setOperators = Seq[(String, Seq[Row])](
+ ("UNION", Seq(Row("a"))),
+ ("INTERSECT", Seq(Row("a"))),
+ ("EXCEPT", Seq()),
+ ("UNION ALL", Seq(Row("A"), Row("a"))),
+ ("INTERSECT ALL", Seq(Row("a"))),
+ ("EXCEPT ALL", Seq()),
+ ("UNION DISTINCT", Seq(Row("a"))),
+ ("INTERSECT DISTINCT", Seq(Row("a"))),
+ ("EXCEPT DISTINCT", Seq()))
+
+ Seq[Boolean](true, false).foreach{ ansi_enabled =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi_enabled.toString,
+ SqlApiConf.DEFAULT_COLLATION -> "UNICODE") {
+ setOperators.foreach { case (operator, result) =>
Review Comment:
sorry for not catching this earlier but in cases where we have multiple nested for each calls we can just wrap all of them in a for loop like:
```code
for {
ansiEnabled <- Seq(true, false)
(operator, result) <- setOperators
} {
withSQLConf(
SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString,
SqlApiConf.DEFAULT_COLLATION -> "UNICODE"
) {
// some code
}
}
```
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "mihailom-db (via GitHub)" <gi...@apache.org>.
mihailom-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812149237
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
- castedTypes.enqueue(widenTypeOpt)
+ val outputType = findOutputType(children, attrIndex)
+ castedTypes.enqueue(outputType)
getWidestTypes(children, attrIndex + 1, castedTypes)
}
+ /** Given children of the operator, determines the output type of the
+ * `attrIndex`-th attribute
+ */
+ private def findOutputType(children: Seq[LogicalPlan], attrIndex: Int): Option[DataType] = {
Review Comment:
I find this weird. So far we always special cased expressions in CollationTypeCast. As @cloud-fan we have understanding that collations are only touched in that rule. This moves us to making double work. I would say it is better to move the widening of collations to CollationTypeCast by special casing the setOperations there (except, union, intersect) it is only three expressions. This is because we keep code in a cleaner state in that way. What do you guys think? @stefankandic @vladanvasi-db
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "mihailom-db (via GitHub)" <gi...@apache.org>.
mihailom-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812151599
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
- castedTypes.enqueue(widenTypeOpt)
+ val outputType = findOutputType(children, attrIndex)
+ castedTypes.enqueue(outputType)
getWidestTypes(children, attrIndex + 1, castedTypes)
}
+ /** Given children of the operator, determines the output type of the
+ * `attrIndex`-th attribute
+ */
+ private def findOutputType(children: Seq[LogicalPlan], attrIndex: Int): Option[DataType] = {
Review Comment:
Also, who is to guarantee that we only have strings here, your change assumes that all attrIndexed at the same place will have same type, why do we then even call widening if that is the case?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812378336
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
Review Comment:
These changes would require us to override `CollationTypeCasts.apply` method and there add a special case for these set operators with collated input types. If that is what you would also recommend, I will proceed with that approach and refactor the `CollationTypeCast`'s apply method
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on PR #48585:
URL: https://github.com/apache/spark/pull/48585#issuecomment-2429239159
also cc: @mihailom-db
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810706779
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala:
##########
@@ -162,7 +162,7 @@ object CollationTypeCasts extends TypeCoercionRule {
*/
def getOutputCollation(expr: Seq[Expression]): StringType = {
val explicitTypes = expr.filter {
- case _: Collate => true
+ case _: Collate | Alias(_: Collate, _) => true
Review Comment:
also can aliases be nested?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala:
##########
@@ -162,7 +162,7 @@ object CollationTypeCasts extends TypeCoercionRule {
*/
def getOutputCollation(expr: Seq[Expression]): StringType = {
val explicitTypes = expr.filter {
- case _: Collate => true
+ case _: Collate | Alias(_: Collate, _) => true
Review Comment:
also can aliases be nested (probably not but check)?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1810842204
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala:
##########
@@ -148,6 +149,10 @@ object AnsiTypeCoercion extends TypeCoercionBase {
// interval type the string should be promoted as. There are many possible interval
// types, such as year interval, month interval, day interval, hour interval, etc.
case (_: StringType, _: AnsiIntervalType) => None
+ // [SPARK-50060] If a binary operation contains at least one collated string types, we can't
+ // decide which collation the result should have.
+ case (d1: StringType, d2: StringType) if SchemaUtils.hasNonUTF8BinaryCollation(d1) ||
Review Comment:
This makes me think that maybe we should not put string collation type coercion in an individual rule, as other type coercion rules must be aware of string collation as well. Shall we just make each type coercion rule collation aware?
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "mihailom-db (via GitHub)" <gi...@apache.org>.
mihailom-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812237283
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
- castedTypes.enqueue(widenTypeOpt)
+ val outputType = findOutputType(children, attrIndex)
+ castedTypes.enqueue(outputType)
getWidestTypes(children, attrIndex + 1, castedTypes)
}
+ /** Given children of the operator, determines the output type of the
+ * `attrIndex`-th attribute
+ */
+ private def findOutputType(children: Seq[LogicalPlan], attrIndex: Int): Option[DataType] = {
Review Comment:
Could we then just check Unpivot as well? There are three rules that are run before CollationTypeCasts and those are
```
UnpivotCoercion ::
WidenSetOperationTypes ::
ProcedureArgumentCoercion
```
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "mihailom-db (via GitHub)" <gi...@apache.org>.
mihailom-db commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812241254
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
- castedTypes.enqueue(widenTypeOpt)
+ val outputType = findOutputType(children, attrIndex)
+ castedTypes.enqueue(outputType)
getWidestTypes(children, attrIndex + 1, castedTypes)
}
+ /** Given children of the operator, determines the output type of the
+ * `attrIndex`-th attribute
+ */
+ private def findOutputType(children: Seq[LogicalPlan], attrIndex: Int): Option[DataType] = {
Review Comment:
And maybe adding a comment in the typeCoercionRules should be added to give attention to string collations when adding new rules.
--
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-50060][SQL] Fixed behavior of set operators when dealing with conflicting collations [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1812339882
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -335,12 +343,30 @@ abstract class TypeCoercionBase {
// Return the result after the widen data types have been found for all the children
if (attrIndex >= children.head.output.length) return castedTypes.toSeq
- // For the attrIndex-th attribute, find the widest type
- val widenTypeOpt = findWiderCommonType(children.map(_.output(attrIndex).dataType))
Review Comment:
I think it's clearer if `CollationTypeCasts` is the only rule doing string collation type coercion. Should `findWiderCommonType` return None if there are string collations, so that we can handle it later in `CollationTypeCasts`?
--
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-50060][SQL] Disabled conversion between different collated types in TypeCoercion and AnsiTypeCoercion [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48585:
URL: https://github.com/apache/spark/pull/48585#discussion_r1815057614
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -1014,7 +1016,9 @@ object TypeCoercion extends TypeCoercionBase {
case (_: StringType, datetime: DatetimeType) => datetime
case (_: StringType, AnyTimestampType) => AnyTimestampType.defaultConcreteType
case (_: StringType, BinaryType) => BinaryType
- // Cast any atomic type to string.
+ // Cast any atomic type to string except if there are strings with different collations. In
+ // that case we skip the cast.
+ case (st1: StringType, st2: StringType) if st1.collationId != st2.collationId => null
Review Comment:
I think this makes sense. Just for my curiosity, can we really hit this branch today? I don't think there is any function that only accept a certion string collation.
--
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