Skip to content

[SPARK-52142][SQL] Display table constraints in SHOW CREATE TABLE COMMAND #51541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ public boolean rely() {

@Override
public String toDDL() {
// The validation status is not included in the DDL output as it's not part of
// the Spark SQL syntax for constraints.
return String.format(
"CONSTRAINT %s %s %s %s %s",
"CONSTRAINT %s %s %s %s",
name,
definition(),
enforced ? "ENFORCED" : "NOT ENFORCED",
validationStatus,
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

For my understanding, where can we see the status after this, @gengliangwang ?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. We have to remove it since the validation status is not supported in the parser.
We can still display it in the "DESC TABLE" command

rely ? "RELY" : "NORELY");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ case class ShowCreateTableExec(
private def showTableDataColumns(table: Table, builder: StringBuilder): Unit = {
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
val columns = CharVarcharUtils.getRawSchema(table.columns.asSchema, conf).fields.map(_.toDDL)
builder ++= concatByMultiLines(columns)
val constraints = table.constraints().map(_.toDDL)
builder ++= concatByMultiLines(columns ++ constraints)
}

private def showTableUsing(table: Table, builder: StringBuilder): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,60 @@ class ShowCreateTableSuite extends command.ShowCreateTableSuiteBase with Command
)
}
}

test("show table constraints") {
withNamespaceAndTable("ns", "tbl", nonPartitionCatalog) { t =>
withTable("other_table") {
sql(
s"""
|CREATE TABLE other_table (
| id STRING PRIMARY KEY
|)
|USING parquet
""".stripMargin)
sql(
s"""
|CREATE TABLE $t (
| a INT,
| b STRING,
| c STRING,
| PRIMARY KEY (a),
| CONSTRAINT uk_b UNIQUE (b),
| CONSTRAINT fk_c FOREIGN KEY (c) REFERENCES other_table(id) RELY,
| CONSTRAINT c1 CHECK (c IS NOT NULL),
| CONSTRAINT c2 CHECK (a > 0)
|)
|$defaultUsing
""".stripMargin)
var showDDL = getShowCreateDDL(t)
val expectedDDLPrefix = Array(
s"CREATE TABLE $nonPartitionCatalog.ns.tbl (",
"a INT,",
"b STRING,",
"c STRING,",
"CONSTRAINT tbl_pk PRIMARY KEY (a) NOT ENFORCED NORELY,",
"CONSTRAINT uk_b UNIQUE (b) NOT ENFORCED NORELY,",
"CONSTRAINT fk_c FOREIGN KEY (c) REFERENCES other_table (id) NOT ENFORCED RELY,",
"CONSTRAINT c1 CHECK (c IS NOT NULL) ENFORCED NORELY,"
)
assert(showDDL === expectedDDLPrefix ++ Array(
"CONSTRAINT c2 CHECK (a > 0) ENFORCED NORELY)",
defaultUsing))

sql(s"ALTER TABLE $t ADD CONSTRAINT c3 CHECK (b IS NOT NULL) ENFORCED RELY")
showDDL = getShowCreateDDL(t)
val expectedDDLArrayWithNewConstraint = expectedDDLPrefix ++ Array(
"CONSTRAINT c2 CHECK (a > 0) ENFORCED NORELY,",
"CONSTRAINT c3 CHECK (b IS NOT NULL) ENFORCED RELY)",
defaultUsing
)
assert(showDDL === expectedDDLArrayWithNewConstraint)
sql(s"ALTER TABLE $t DROP CONSTRAINT c1")
showDDL = getShowCreateDDL(t)
val expectedDDLArrayAfterDrop = expectedDDLArrayWithNewConstraint.filterNot(
_.contains("c1 CHECK (c IS NOT NULL) ENFORCED NORELY"))
assert(showDDL === expectedDDLArrayAfterDrop)
}
}
}
}