You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "gengliangwang (via GitHub)" <gi...@apache.org> on 2025/07/16 22:50:38 UTC
[PR] [SPARK-52641][SQL] Primary key columns should not be nullable [spark]
gengliangwang opened a new pull request, #51525:
URL: https://github.com/apache/spark/pull/51525
<!--
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.
-->
When creating or replacing a table with primary key, all the columns of the primary key should not be nullable
### 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.
-->
Correct the expected table schema when create/replace table with primary key
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No, the feature is not released yet
### 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.
-->
New UT
### 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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #51525:
URL: https://github.com/apache/spark/pull/51525#discussion_r2212292613
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/ConstraintParseSuiteBase.scala:
##########
@@ -78,13 +78,15 @@ abstract class ConstraintParseSuiteBase extends AnalysisTest with SharedSparkSes
}
protected def verifyConstraints(
- sql: String,
- constraints: Seq[TableConstraint],
- isCreateTable: Boolean = true): Unit = {
+ sql: String,
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "shkhrgpt (via GitHub)" <gi...@apache.org>.
shkhrgpt commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3083140216
Should you be adding the following unit tests?
```
test("Create table with mixed case primary key columns") {
val sql = "CREATE TABLE t (FirstName STRING, LastName STRING, PRIMARY KEY (firstName, LASTNAME)) USING parquet"
// Verify both columns are non-nullable regardless of case
}
test("Primary key with nullable column explicitly specified should fail") {
val sql = "CREATE TABLE t (id INT NULL PRIMARY KEY) USING parquet"
// Should throw appropriate error
}
```
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "shkhrgpt (via GitHub)" <gi...@apache.org>.
shkhrgpt commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3084938346
> ```
> test("Create table with mixed case primary key columns") {
> val sql = "CREATE TABLE t (FirstName STRING, LastName STRING, PRIMARY KEY (firstName, LASTNAME)) USING parquet"
> // Verify both columns are non-nullable regardless of case
> }
> ```
>
> There is a similar test case in the test.
@gengliangwang Thanks for the reply. Can you please point out to me which test covers this scenario?
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3084389799
cc @peter-toth
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang closed pull request #51525: [SPARK-52641][SQL] Primary key columns should not be nullable
URL: https://github.com/apache/spark/pull/51525
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on code in PR #51525:
URL: https://github.com/apache/spark/pull/51525#discussion_r2211824357
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -4894,8 +4894,23 @@ class AstBuilder extends DataTypeAstBuilder
.mkString(", ")
throw QueryParsingErrors.multiplePrimaryKeysError(ctx, primaryKeyColumns)
}
+ // If there is a primary key constraint, all the columns in the primary key are not null.
+ val updatedColumns = if (primaryKeys.nonEmpty) {
+ val lowerCasePkColumns = primaryKeys.head.asInstanceOf[PrimaryKeyConstraint].columns
Review Comment:
SQL statements are mostly case-insensitive.
For example
```
create table test10(a INT, A INT)
```
will fail:
```
org.apache.spark.sql.AnalysisException: [COLUMN_ALREADY_EXISTS] The column `a` already exists. Choose another name or rename the existing column. SQLSTATE: 42711
```
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #51525:
URL: https://github.com/apache/spark/pull/51525#discussion_r2213581344
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/PrimaryKeyConstraintParseSuite.scala:
##########
@@ -122,7 +132,19 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
tableName = "t",
userProvidedName = null)
val constraints = Seq(constraint)
- verifyConstraints(sql, constraints, isCreateTable = false)
+ verifyConstraints(sql, constraints, isCreateTable = false, columnANullable = false,
+ columnBNullable = false)
+ }
+
+ test("Replace table with composite primary key - case insensitive") {
Review Comment:
Do we support `case sensitive` case? According to the code of this PR, it seems not, can we revise this test case a little like the following because we will not have another test case with the postfix, `- case sensitive`.
```
- Replace table with composite primary key - case insensitive
+ Replace table with composite primary key case-insensitively
```
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on code in PR #51525:
URL: https://github.com/apache/spark/pull/51525#discussion_r2213920204
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/PrimaryKeyConstraintParseSuite.scala:
##########
@@ -122,7 +132,19 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
tableName = "t",
userProvidedName = null)
val constraints = Seq(constraint)
- verifyConstraints(sql, constraints, isCreateTable = false)
+ verifyConstraints(sql, constraints, isCreateTable = false, columnANullable = false,
+ columnBNullable = false)
+ }
+
+ test("Replace table with composite primary key - case insensitive") {
Review Comment:
Thanks, I changed to `Replace table with composite primary key - case insensitivity`.
The naming is consistent with other cases in the test suite, such as
* Replace table with composite primary key - table level
* Replace table with primary key - column level
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "shkhrgpt (via GitHub)" <gi...@apache.org>.
shkhrgpt commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3085024910
> @shkhrgpt https://github.com/apache/spark/pull/51525/files#diff-460369f5ce4d4fcfba8ee4d7d6a2a75156052d2eb42b6a75734c2fdf18500761R61
>
> If you think we need more complicated case like yours, I can add one
As I understand it, the test you’ve added doesn’t fully cover the scenario I suggested. It mainly verifies case-insensitive parsing, but it doesn’t check whether the primary key columns are correctly marked as non-nullable. That behavior is the key focus of this PR. Adding the proposed test would help ensure that this functionality works as expected, especially when column names use mixed 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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3085461973
@anchovYu @cloud-fan @dongjoon-hyun @shkhrgpt @peter-toth thanks for the review.
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3084960478
@shkhrgpt https://github.com/apache/spark/pull/51525/files#diff-460369f5ce4d4fcfba8ee4d7d6a2a75156052d2eb42b6a75734c2fdf18500761R61
If you think we need more complicated case like yours, I can add one
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3085081300
@shkhrgpt sure, I have added your test 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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on PR #51525:
URL: https://github.com/apache/spark/pull/51525#issuecomment-3084897246
@shkhrgpt
```
test("Create table with mixed case primary key columns") {
val sql = "CREATE TABLE t (FirstName STRING, LastName STRING, PRIMARY KEY (firstName, LASTNAME)) USING parquet"
// Verify both columns are non-nullable regardless of case
}
```
There is a similar test case in the test.
```
test("Primary key with nullable column explicitly specified should fail") {
val sql = "CREATE TABLE t (id INT NULL PRIMARY KEY) USING parquet"
// Should throw appropriate error
}
```
Spark doesn't support explicitly setting `NULLABLE` in create table statement.
--
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-52641][SQL] Primary key columns should not be nullable [spark]
Posted by "anchovYu (via GitHub)" <gi...@apache.org>.
anchovYu commented on code in PR #51525:
URL: https://github.com/apache/spark/pull/51525#discussion_r2211817633
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -4894,8 +4894,23 @@ class AstBuilder extends DataTypeAstBuilder
.mkString(", ")
throw QueryParsingErrors.multiplePrimaryKeysError(ctx, primaryKeyColumns)
}
+ // If there is a primary key constraint, all the columns in the primary key are not null.
+ val updatedColumns = if (primaryKeys.nonEmpty) {
+ val lowerCasePkColumns = primaryKeys.head.asInstanceOf[PrimaryKeyConstraint].columns
Review Comment:
Does it not support case-sensitive columns? e.g. column (a, A, b, B, primary key(A, B))
--
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