You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "nikolamand-db (via GitHub)" <gi...@apache.org> on 2024/05/24 08:09:14 UTC
[PR] [SPARK-48413] ALTER COLUMN with collation [spark]
nikolamand-db opened a new pull request, #46734:
URL: https://github.com/apache/spark/pull/46734
<!--
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.
-->
Add support for changing collation of a column with `ALTER COLUMN` command. Use existing support for `ALTER COLUMN` with type to enable changing collations of column. Syntax example:
```
ALTER TABLE t1 ALTER COLUMN col TYPE STRING COLLATE UTF8_BINARY_LCASE
```
### 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.
-->
Enable changing collation on column.
### 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'.
-->
Yes, it adds support for changing collation of column.
### 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.
-->
Added tests to `DDLSuite` and `DataTypeSuite`.
### 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.
-->
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-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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1624895418
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala:
##########
@@ -2437,6 +2437,100 @@ abstract class DDLSuite extends QueryTest with DDLSuiteBase {
)
}
}
+
+ test("Change column collation") {
+ withTable("t1", "t2", "t3", "t4") {
+ // Plain `StringType`.
+ sql("CREATE TABLE t1(col STRING) USING parquet")
+ sql("INSERT INTO t1 VALUES ('a')")
+ checkAnswer(sql("SELECT COLLATION(col) FROM t1"), Row("UTF8_BINARY"))
+ sql("ALTER TABLE t1 ALTER COLUMN col TYPE STRING COLLATE UTF8_BINARY_LCASE")
+ checkAnswer(sql("SELECT COLLATION(col) FROM t1"), Row("UTF8_BINARY_LCASE"))
+
+ // Invalid "ALTER COLUMN" to Integer.
+ val alterInt = "ALTER TABLE t1 ALTER COLUMN col TYPE INTEGER"
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(alterInt)
+ },
+ errorClass = "NOT_SUPPORTED_CHANGE_COLUMN",
+ parameters = Map(
+ "originType" -> "\"STRING COLLATE UTF8_BINARY_LCASE\"",
+ "originName" -> "`col`",
+ "table" -> "`spark_catalog`.`default`.`t1`",
+ "newType" -> "\"INT\"",
+ "newName" -> "`col`"
+ ),
+ context = ExpectedContext(fragment = alterInt, start = 0, stop = alterInt.length - 1)
+ )
+
+ // `ArrayType` with collation.
+ sql("CREATE TABLE t2(col ARRAY<STRING>) USING parquet")
+ sql("INSERT INTO t2 VALUES (ARRAY('a'))")
+ checkAnswer(sql("SELECT COLLATION(col[0]) FROM t2"), Row("UTF8_BINARY"))
+ sql("ALTER TABLE t2 ALTER COLUMN col TYPE ARRAY<STRING COLLATE UTF8_BINARY_LCASE>")
+ checkAnswer(sql("SELECT COLLATION(col[0]) FROM t2"), Row("UTF8_BINARY_LCASE"))
+
+ // `MapType` with collation.
+ sql("CREATE TABLE t3(col MAP<STRING, STRING>) USING parquet")
+ sql("INSERT INTO t3 VALUES (MAP('k', 'v'))")
+ checkAnswer(sql("SELECT COLLATION(col['k']) FROM t3"), Row("UTF8_BINARY"))
+ sql(
+ """
+ |ALTER TABLE t3 ALTER COLUMN col TYPE
+ |MAP<STRING, STRING COLLATE UTF8_BINARY_LCASE>""".stripMargin)
+ checkAnswer(sql("SELECT COLLATION(col['k']) FROM t3"), Row("UTF8_BINARY_LCASE"))
+
+ // Invalid change of map key collation.
+ val alterMap =
+ "ALTER TABLE t3 ALTER COLUMN col TYPE " +
+ "MAP<STRING COLLATE UTF8_BINARY_LCASE, STRING>"
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(alterMap)
+ },
+ errorClass = "NOT_SUPPORTED_CHANGE_COLUMN",
+ parameters = Map(
+ "originType" -> "\"MAP<STRING, STRING COLLATE UTF8_BINARY_LCASE>\"",
+ "originName" -> "`col`",
+ "table" -> "`spark_catalog`.`default`.`t3`",
+ "newType" -> "\"MAP<STRING COLLATE UTF8_BINARY_LCASE, STRING>\"",
+ "newName" -> "`col`"
+ ),
+ context = ExpectedContext(fragment = alterMap, start = 0, stop = alterMap.length - 1)
+ )
+
+ // `StructType` with collation.
+ sql("CREATE TABLE t4(col STRUCT<a:STRING>) USING parquet")
+ sql("INSERT INTO t4 VALUES (NAMED_STRUCT('a', 'value'))")
+ checkAnswer(sql("SELECT COLLATION(col.a) FROM t4"), Row("UTF8_BINARY"))
+ sql("ALTER TABLE t4 ALTER COLUMN col TYPE STRUCT<a:STRING COLLATE UTF8_BINARY_LCASE>")
+ checkAnswer(sql("SELECT COLLATION(col.a) FROM t4"), Row("UTF8_BINARY_LCASE"))
+ }
+ }
+
+ test("Invalid collation change on partition and bucket columns") {
+ withTable("t1", "t2") {
+ sql("CREATE TABLE t1(col STRING, i INTEGER) USING parquet PARTITIONED BY (col)")
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql("ALTER TABLE t1 ALTER COLUMN col TYPE STRING COLLATE UTF8_BINARY_LCASE")
Review Comment:
why do we disallow it? I think we always store the string value as it is in the partition directory name, so collation doesn't matter.
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "olaky (via GitHub)" <gi...@apache.org>.
olaky commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1613167082
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
Review Comment:
let's already give this a name that reflects that is returns true of the it is allowed to evolve the type. This will be true for more cases than collations in the future
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -445,7 +446,8 @@ case class AlterTableChangeColumnCommand(
// name(by resolver) and dataType.
private def columnEqual(
field: StructField, other: StructField, resolver: Resolver): Boolean = {
- resolver(field.name, other.name) && field.dataType == other.dataType
+ resolver(field.name, other.name) &&
+ DataType.equalsIgnoreCompatibleCollationAndNullability(field.dataType, other.dataType)
Review Comment:
The comment of the command in line 359 is not outdated
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
+ (tn || !fn) && equalsIgnoreCompatibleCollationAndNullability(fromElement, toElement)
+
+ case (MapType(fromKey, fromValue, fn), MapType(toKey, toValue, tn)) =>
+ (tn || !fn) &&
+ // Map keys cannot change collation.
+ equalsIgnoreCompatibleNullability(fromKey, toKey) &&
+ equalsIgnoreCompatibleCollationAndNullability(fromValue, toValue)
+
+ case (StructType(fromFields), StructType(toFields)) =>
+ fromFields.length == toFields.length &&
+ fromFields.zip(toFields).forall { case (fromField, toField) =>
Review Comment:
I would rather do a forAll on fromFields and look up each field by name in toFields. That way we do not depend on the order
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
+ (tn || !fn) && equalsIgnoreCompatibleCollationAndNullability(fromElement, toElement)
Review Comment:
Why do we want to allow setting the type nullable?
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
+ (tn || !fn) && equalsIgnoreCompatibleCollationAndNullability(fromElement, toElement)
+
+ case (MapType(fromKey, fromValue, fn), MapType(toKey, toValue, tn)) =>
Review Comment:
let's also not abbreviate here
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
+ (tn || !fn) && equalsIgnoreCompatibleCollationAndNullability(fromElement, toElement)
+
+ case (MapType(fromKey, fromValue, fn), MapType(toKey, toValue, tn)) =>
+ (tn || !fn) &&
+ // Map keys cannot change collation.
+ equalsIgnoreCompatibleNullability(fromKey, toKey) &&
+ equalsIgnoreCompatibleCollationAndNullability(fromValue, toValue)
+
+ case (StructType(fromFields), StructType(toFields)) =>
+ fromFields.length == toFields.length &&
Review Comment:
or do we use a map to not depend on the order of fields?
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
Review Comment:
```suggestion
case (ArrayType(fromElementType, fromContainsNull), ArrayType(toElementType, toContainsNull)) =>
```
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "olaky (via GitHub)" <gi...@apache.org>.
olaky commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1615729683
##########
sql/api/src/main/scala/org/apache/spark/sql/types/StructField.scala:
##########
@@ -124,6 +124,13 @@ case class StructField(
throw SparkException.internalError(s"Unexpected data type $dt")
}
+ /**
+ * Updates the StructField with a new dataType.
+ */
+ def withNewType(dataType: DataType): StructField = {
Review Comment:
```suggestion
private [sql] def withNewType(dataType: DataType): StructField = {
```
Or even something more restrictive please because this must not be called if equalsIgnoreCompatibleCollation does not hold. Do we maybe not put it here but in ddl.scala? I believe copy is public
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "olaky (via GitHub)" <gi...@apache.org>.
olaky commented on PR #46734:
URL: https://github.com/apache/spark/pull/46734#issuecomment-2134484640
> We can address this in a followup but we also shouldn't allow alter from string -> collated string if table is partitioned or bucketed by a that column
I actually think that this is possible: Collations do not affect how we store files (we keep partitioning by UTF8_BINARY), and hence it would be possible to allow this. Am I missing something?
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1615600823
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -445,7 +446,8 @@ case class AlterTableChangeColumnCommand(
// name(by resolver) and dataType.
private def columnEqual(
field: StructField, other: StructField, resolver: Resolver): Boolean = {
- resolver(field.name, other.name) && field.dataType == other.dataType
+ resolver(field.name, other.name) &&
+ DataType.equalsIgnoreCompatibleCollationAndNullability(field.dataType, other.dataType)
Review Comment:
Fixed, please 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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1624892730
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -432,6 +445,10 @@ case class AlterTableChangeColumnCommand(
}.getOrElse(throw QueryCompilationErrors.cannotFindColumnError(name, schema.fieldNames))
}
+ // Change the dataType of the column.
+ private def withNewType(column: StructField, dataType: DataType): StructField =
+ column.copy(dataType = dataType)
Review Comment:
nit: This looks more concise than calling `withNewType`, do we really need to create this 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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on PR #46734:
URL: https://github.com/apache/spark/pull/46734#issuecomment-2133416340
We can address this in a followup but we also shouldn't allow alter from string -> collated string if table is partitioned or bucketed by a source column
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1615594377
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -396,8 +396,9 @@ case class AlterTableChangeColumnCommand(
val newDataSchema = table.dataSchema.fields.map { field =>
if (field.name == originColumn.name) {
// Create a new column from the origin column with the new comment.
+ val newField = field.copy(dataType = newColumn.dataType)
Review Comment:
Updated the comment, please 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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on PR #46734:
URL: https://github.com/apache/spark/pull/46734#issuecomment-2133569759
This PR is currently blocked by https://github.com/apache/spark/pull/46758, need to fix to pass the 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-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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1624968863
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -432,6 +445,10 @@ case class AlterTableChangeColumnCommand(
}.getOrElse(throw QueryCompilationErrors.cannotFindColumnError(name, schema.fieldNames))
}
+ // Change the dataType of the column.
+ private def withNewType(column: StructField, dataType: DataType): StructField =
+ column.copy(dataType = dataType)
Review Comment:
This was suggested by @johanl-db, please check https://github.com/apache/spark/pull/46734#discussion_r1613401422.
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #46734:
URL: https://github.com/apache/spark/pull/46734#issuecomment-2146014017
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "johanl-db (via GitHub)" <gi...@apache.org>.
johanl-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1613401422
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -396,8 +396,9 @@ case class AlterTableChangeColumnCommand(
val newDataSchema = table.dataSchema.fields.map { field =>
if (field.name == originColumn.name) {
// Create a new column from the origin column with the new comment.
+ val newField = field.copy(dataType = newColumn.dataType)
Review Comment:
Can you make it more explicit that we effectively allow the data type to change now but we only allow type changes that differ by collation?
E.g. by splitting `columnEqual` into checking that names match on one side and that the type change is supported on the other + add a ` withNewType` method.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -396,8 +396,9 @@ case class AlterTableChangeColumnCommand(
val newDataSchema = table.dataSchema.fields.map { field =>
if (field.name == originColumn.name) {
// Create a new column from the origin column with the new comment.
+ val newField = field.copy(dataType = newColumn.dataType)
Review Comment:
Lines 359-367: the comment needs to be updated to be clear we now allow changing collation.
Side note: it gives the hive-style syntax for ALTER COLUMN but that's only one of the two syntaxes, see:
https://github.com/apache/spark/blob/master/sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4#L130
The other one is the one (partially) documented and the more capable/preferred one: https://spark.apache.org/docs/3.5.1/sql-ref-syntax-ddl-alter-table.html#parameters-4
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1615599084
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
+ (tn || !fn) && equalsIgnoreCompatibleCollationAndNullability(fromElement, toElement)
+
+ case (MapType(fromKey, fromValue, fn), MapType(toKey, toValue, tn)) =>
+ (tn || !fn) &&
+ // Map keys cannot change collation.
+ equalsIgnoreCompatibleNullability(fromKey, toKey) &&
+ equalsIgnoreCompatibleCollationAndNullability(fromValue, toValue)
+
+ case (StructType(fromFields), StructType(toFields)) =>
+ fromFields.length == toFields.length &&
+ fromFields.zip(toFields).forall { case (fromField, toField) =>
Review Comment:
Please check https://github.com/apache/spark/pull/46734#discussion_r1615598800.
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
+ (tn || !fn) && equalsIgnoreCompatibleCollationAndNullability(fromElement, toElement)
+
+ case (MapType(fromKey, fromValue, fn), MapType(toKey, toValue, tn)) =>
+ (tn || !fn) &&
+ // Map keys cannot change collation.
+ equalsIgnoreCompatibleNullability(fromKey, toKey) &&
+ equalsIgnoreCompatibleCollationAndNullability(fromValue, toValue)
+
+ case (StructType(fromFields), StructType(toFields)) =>
+ fromFields.length == toFields.length &&
Review Comment:
Please check https://github.com/apache/spark/pull/46734#discussion_r1615598800.
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1615595612
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -396,8 +396,9 @@ case class AlterTableChangeColumnCommand(
val newDataSchema = table.dataSchema.fields.map { field =>
if (field.name == originColumn.name) {
// Create a new column from the origin column with the new comment.
+ val newField = field.copy(dataType = newColumn.dataType)
Review Comment:
Clarified in comment, split the `columnEqual` to two functions and added `withNewType` in `StructField`, please 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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1624895418
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala:
##########
@@ -2437,6 +2437,100 @@ abstract class DDLSuite extends QueryTest with DDLSuiteBase {
)
}
}
+
+ test("Change column collation") {
+ withTable("t1", "t2", "t3", "t4") {
+ // Plain `StringType`.
+ sql("CREATE TABLE t1(col STRING) USING parquet")
+ sql("INSERT INTO t1 VALUES ('a')")
+ checkAnswer(sql("SELECT COLLATION(col) FROM t1"), Row("UTF8_BINARY"))
+ sql("ALTER TABLE t1 ALTER COLUMN col TYPE STRING COLLATE UTF8_BINARY_LCASE")
+ checkAnswer(sql("SELECT COLLATION(col) FROM t1"), Row("UTF8_BINARY_LCASE"))
+
+ // Invalid "ALTER COLUMN" to Integer.
+ val alterInt = "ALTER TABLE t1 ALTER COLUMN col TYPE INTEGER"
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(alterInt)
+ },
+ errorClass = "NOT_SUPPORTED_CHANGE_COLUMN",
+ parameters = Map(
+ "originType" -> "\"STRING COLLATE UTF8_BINARY_LCASE\"",
+ "originName" -> "`col`",
+ "table" -> "`spark_catalog`.`default`.`t1`",
+ "newType" -> "\"INT\"",
+ "newName" -> "`col`"
+ ),
+ context = ExpectedContext(fragment = alterInt, start = 0, stop = alterInt.length - 1)
+ )
+
+ // `ArrayType` with collation.
+ sql("CREATE TABLE t2(col ARRAY<STRING>) USING parquet")
+ sql("INSERT INTO t2 VALUES (ARRAY('a'))")
+ checkAnswer(sql("SELECT COLLATION(col[0]) FROM t2"), Row("UTF8_BINARY"))
+ sql("ALTER TABLE t2 ALTER COLUMN col TYPE ARRAY<STRING COLLATE UTF8_BINARY_LCASE>")
+ checkAnswer(sql("SELECT COLLATION(col[0]) FROM t2"), Row("UTF8_BINARY_LCASE"))
+
+ // `MapType` with collation.
+ sql("CREATE TABLE t3(col MAP<STRING, STRING>) USING parquet")
+ sql("INSERT INTO t3 VALUES (MAP('k', 'v'))")
+ checkAnswer(sql("SELECT COLLATION(col['k']) FROM t3"), Row("UTF8_BINARY"))
+ sql(
+ """
+ |ALTER TABLE t3 ALTER COLUMN col TYPE
+ |MAP<STRING, STRING COLLATE UTF8_BINARY_LCASE>""".stripMargin)
+ checkAnswer(sql("SELECT COLLATION(col['k']) FROM t3"), Row("UTF8_BINARY_LCASE"))
+
+ // Invalid change of map key collation.
+ val alterMap =
+ "ALTER TABLE t3 ALTER COLUMN col TYPE " +
+ "MAP<STRING COLLATE UTF8_BINARY_LCASE, STRING>"
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(alterMap)
+ },
+ errorClass = "NOT_SUPPORTED_CHANGE_COLUMN",
+ parameters = Map(
+ "originType" -> "\"MAP<STRING, STRING COLLATE UTF8_BINARY_LCASE>\"",
+ "originName" -> "`col`",
+ "table" -> "`spark_catalog`.`default`.`t3`",
+ "newType" -> "\"MAP<STRING COLLATE UTF8_BINARY_LCASE, STRING>\"",
+ "newName" -> "`col`"
+ ),
+ context = ExpectedContext(fragment = alterMap, start = 0, stop = alterMap.length - 1)
+ )
+
+ // `StructType` with collation.
+ sql("CREATE TABLE t4(col STRUCT<a:STRING>) USING parquet")
+ sql("INSERT INTO t4 VALUES (NAMED_STRUCT('a', 'value'))")
+ checkAnswer(sql("SELECT COLLATION(col.a) FROM t4"), Row("UTF8_BINARY"))
+ sql("ALTER TABLE t4 ALTER COLUMN col TYPE STRUCT<a:STRING COLLATE UTF8_BINARY_LCASE>")
+ checkAnswer(sql("SELECT COLLATION(col.a) FROM t4"), Row("UTF8_BINARY_LCASE"))
+ }
+ }
+
+ test("Invalid collation change on partition and bucket columns") {
+ withTable("t1", "t2") {
+ sql("CREATE TABLE t1(col STRING, i INTEGER) USING parquet PARTITIONED BY (col)")
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql("ALTER TABLE t1 ALTER COLUMN col TYPE STRING COLLATE UTF8_BINARY_LCASE")
Review Comment:
why do we disallow it? I think we always store the string value as it is in the partition directory name, so collation doesn't matter.
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #46734: [SPARK-48413][SQL] ALTER COLUMN with collation
URL: https://github.com/apache/spark/pull/46734
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1615598800
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
+ from: DataType,
+ to: DataType): Boolean = {
+ (from, to) match {
+ case (_: StringType, _: StringType) => true
+
+ case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
+ (tn || !fn) && equalsIgnoreCompatibleCollationAndNullability(fromElement, toElement)
Review Comment:
Changed the approach, we now only check for possible collation difference; nullability must remain the same. In this way by checking only for possible collation difference we scope down the changes whereby previously we were checking for complete data type equality (only comment changes were allowed in alter column command).
--
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-48413][SQL] ALTER COLUMN with collation [spark]
Posted by "nikolamand-db (via GitHub)" <gi...@apache.org>.
nikolamand-db commented on code in PR #46734:
URL: https://github.com/apache/spark/pull/46734#discussion_r1615600457
##########
sql/api/src/main/scala/org/apache/spark/sql/types/DataType.scala:
##########
@@ -408,6 +408,37 @@ object DataType {
}
}
+ /**
+ * Check if `from` is equal to `to` type except for collations and nullability, which are
+ * both checked to be compatible so that data of type `from` can be interpreted as of type `to`.
+ */
+ private[sql] def equalsIgnoreCompatibleCollationAndNullability(
Review Comment:
Please check https://github.com/apache/spark/pull/46734#discussion_r1615598800. Part of the effort was to change the name of this method to `equalsIgnoreCompatibleCollation`.
--
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