Skip to content

[SPARK-47692][SQL] Fix default StringType meaning in implicit casting#45819

Closed
mihailomilosevic2001 wants to merge 112 commits into
apache:masterfrom
mihailomilosevic2001:SPARK-47692
Closed

[SPARK-47692][SQL] Fix default StringType meaning in implicit casting#45819
mihailomilosevic2001 wants to merge 112 commits into
apache:masterfrom
mihailomilosevic2001:SPARK-47692

Conversation

@mihailomilosevic2001
Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Addition of priority flag to StringType.

Why are the changes needed?

In order to follow casting rules for collations, we need to know whether StringType is considered default, implicit or explicit.

Does this PR introduce any user-facing change?

Yes.

How was this patch tested?

Implicit tests in CollationSuite.

Was this patch authored or co-authored using generative AI tooling?

No.

@mihailomilosevic2001
Copy link
Copy Markdown
Contributor Author

@dbatomic @stefankandic @uros-db @stevomitric @nikolamand-db Could you take a look at this PR? It introduces a new flag for collations and makes sure most of the functions that return StringType without StringType in inputs return default collation.

@nikolamand-db
Copy link
Copy Markdown
Contributor

How do we want to handle cases like string split (work in progress, used as illustration) in general? The regex parameter accepts only StringType and if I understand the plan would fail when we explicitly set the default collation type to be something other than UTF8_BINARY and run query like select split('abc', 'b'). However, in this case the regex parameter's collation doesn't have any semantic meaning and I don't think the queries alike should fail.

@mihailomilosevic2001
Copy link
Copy Markdown
Contributor Author

If the collation is not important to the function execution we can create a separate pattern for it where we will exclude parameters that do not need casting. For example, substring is done in that way. We only cast if it is DefaultST or ExplicitST to ImplicitST, as we need the return type to have implicit collation.

@mihailomilosevic2001 mihailomilosevic2001 changed the title [SPARK-47692][SQL] Addition of priority flag to StringType [WIP][SPARK-47692][SQL] Addition of priority flag to StringType Apr 15, 2024
# Conflicts:
#	sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala
#	sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
@mihailomilosevic2001 mihailomilosevic2001 changed the title [WIP][SPARK-47692][SQL] Addition of priority flag to StringType [WIP][SPARK-47692][SQL] Fix default StringType meaning in implicit casting Apr 21, 2024
@mihailomilosevic2001 mihailomilosevic2001 changed the title [WIP][SPARK-47692][SQL] Fix default StringType meaning in implicit casting [SPARK-47692][SQL] Fix default StringType meaning in implicit casting Apr 22, 2024
Copy link
Copy Markdown
Contributor

@uros-db uros-db left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

val implicitTypes = expr.map(_.dataType)
val implicitTypes = expr.filter {
case Literal(_, _: StringType) => false
case Cast(child, _: StringType, _, _) => child.dataType.isInstanceOf[StringType]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also check cast.getTagValue(Cast.USER_SPECIFIED_CAST).isEmpty to make sure it's not a user-specified cast.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait, if users use scala/python DataFrame to cast a column to a StringType with specific collation, should it be explicit collation?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for SQL: CAST(col AS STRING WITH ...)

# Conflicts:
#	sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCasts.scala
@cloud-fan
Copy link
Copy Markdown
Contributor

thanks, merging to master!

@cloud-fan cloud-fan closed this in 461ffa1 Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants