dongjoon-hyun opened a new pull request, #59598:
URL: https://github.com/apache/spark/pull/59598
### What changes were proposed in this pull request?
This PR aims to quote a constraint name in `Constraint.toDDL()` if needed.
`BaseConstraint.toDDL()`, which `PrimaryKey`, `Unique`, `ForeignKey`, and
`Check` share, already quotes the constraint columns
(`NamedReference.toString`) and the referenced table of a foreign key
(`Identifier.toString`) with `QuotingUtils.quoteIfNeeded`, but it writes the
constraint name as it is. This PR applies `QuotingUtils.quoteIfNeeded` to the
constraint name, too.
```diff
- return "CONSTRAINT " + name + " " + toDescription();
+ return "CONSTRAINT " + QuotingUtils.quoteIfNeeded(name) + " " +
toDescription();
```
Like the existing quoting of the columns and the referenced table, this does
not quote reserved keywords under `spark.sql.ansi.enforceReservedKeywords=true`
because `Constraint.toDDL()` is a public API which does not depend on the
session configuration.
### Why are the changes needed?
`Constraint.toDDL()` returns the definition of a constraint in the DDL
format, and `SHOW CREATE TABLE` uses it for DSv2 tables. Since the constraint
name is not quoted, the output of `SHOW CREATE TABLE` cannot be run again when
a constraint name is not a regular identifier. This happens not only with
user-provided names but also with the names which Spark generates from the
table name (`<table>_pk`, `<table>_uniq_<suffix>`, `<table>_chk_<suffix>`, and
`<table>_<referenced table>_fk_<suffix>`).
For example, consider a DSv2 table with an unnamed primary key
(`InMemoryTableCatalog` of the test).
```sql
CREATE TABLE non_part_test_catalog.ns.`tbl-1` (a INT, PRIMARY KEY (a)) USING
_;
SHOW CREATE TABLE non_part_test_catalog.ns.`tbl-1`;
```
**BEFORE**
```
CREATE TABLE non_part_test_catalog.ns.`tbl-1` (
a INT NOT NULL,
CONSTRAINT tbl-1_pk PRIMARY KEY (a) NOT ENFORCED NORELY)
USING _
```
Running the output again fails.
```
[INVALID_IDENTIFIER] The unquoted identifier tbl-1_pk is invalid and must be
back quoted as: `tbl-1_pk`.
```
User-provided names fail in the same way.
```
CONSTRAINT my pk PRIMARY KEY (a) NOT ENFORCED NORELY --
PARSE_SYNTAX_ERROR
CONSTRAINT uk-b UNIQUE (b) NOT ENFORCED NORELY --
INVALID_IDENTIFIER
CONSTRAINT fk.c FOREIGN KEY (c) REFERENCES ... (id) ... --
PARSE_SYNTAX_ERROR
CONSTRAINT c`1 CHECK (c IS NOT NULL) ENFORCED NORELY --
PARSE_SYNTAX_ERROR
```
**AFTER**
```
CREATE TABLE non_part_test_catalog.ns.`tbl-1` (
a INT NOT NULL,
CONSTRAINT `tbl-1_pk` PRIMARY KEY (a) NOT ENFORCED NORELY)
USING _
```
### Does this PR introduce _any_ user-facing change?
Yes, only for the constraint names which are not regular identifiers. `SHOW
CREATE TABLE` of DSv2 tables, `Constraint.toDDL()` (and `toString()`), and the
`CONSTRAINT_ALREADY_EXISTS` error message show such names with back-quotes so
that the output can be run again. For example, the `CONSTRAINT_ALREADY_EXISTS`
error shows the existing constraint as ``CONSTRAINT `c 1` CHECK (a > 0)
ENFORCED NORELY`` instead of `CONSTRAINT c 1 CHECK (a > 0) ENFORCED NORELY`.
The output for the other names is unchanged. `DESCRIBE TABLE EXTENDED` is also
unchanged because it shows the constraint name in its own column like the
column names.
This bug exists since Apache Spark 4.1.0
([v4.1.0](https://github.com/apache/spark/releases/tag/v4.1.0), 2025-12-11)
where SPARK-52142 started to show table constraints in `SHOW CREATE TABLE`.
### How was this patch tested?
Pass the CIs with the newly added test cases.
- `ConstraintSuite` checks that `toDDL()` quotes the name of every
constraint type (`CHECK`, `UNIQUE`, `PRIMARY KEY`, and `FOREIGN KEY`) if needed.
- `v2.ShowCreateTableSuite` checks that the `SHOW CREATE TABLE` output of a
table, whose constraint names need quoting including a generated one, can be
run again and gives the same output.
Without this fix, the new `v2.ShowCreateTableSuite` test case fails like the
following.
```
- SHOW CREATE TABLE using V2 catalog V2 command: SPARK-XXXXX: show table
constraints whose names need quoting *** FAILED ***
Array(..., "CONSTRAINT tbl-1_pk PRIMARY KEY (a) NOT ENFORCED NORELY,",
"CONSTRAINT uk b UNIQUE (b) NOT ENFORCED NORELY,", ...) did not equal
Array(..., "CONSTRAINT `tbl-1_pk` PRIMARY KEY (a) NOT ENFORCED NORELY,",
"CONSTRAINT `uk b` UNIQUE (b) NOT ENFORCED NORELY,", ...)
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]