Skip to content
Closed
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 @@ -65,24 +65,17 @@ public boolean rely() {
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 "CONSTRAINT " + name + " " + toDescription();
}

public String toDescription() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this PR establishes the invariant toDDL == "CONSTRAINT " + name + " " + toDescription, consider implementing it structurally so the two methods can't drift apart, e.g. simplify toDDL above to:

@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 "CONSTRAINT " + name + " " + toDescription();
}

This also removes the duplicated String.format template.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good point, updated, thanks!

return String.format(
"CONSTRAINT %s %s %s %s",
name,
"%s %s %s",
definition(),
enforced ? "ENFORCED" : "NOT ENFORCED",
rely ? "RELY" : "NORELY");
}

public String toDescription() {
StringJoiner joiner = new StringJoiner(" ");
joiner.add(definition());
joiner.add(enforced ? "ENFORCED" : "NOT ENFORCED");
if (rely) {
joiner.add("RELY");
}
return joiner.toString();
}

@Override
public String toString() {
return toDDL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,20 @@ class DescribeTableSuite extends command.DescribeTableSuiteBase
|$defaultUsing
""".stripMargin)

// Skipped showing NORELY since it is the default value.
// ENFORCED/NOT ENFORCED and RELY/NORELY are always emitted to match SHOW CREATE TABLE.
var expectedConstraintsDdl = Array(
"# Constraints,,",
"pk_table_pk,PRIMARY KEY (id) NOT ENFORCED,",
"pk_table_pk,PRIMARY KEY (id) NOT ENFORCED NORELY,",
s"fk_a,FOREIGN KEY (a) REFERENCES $fkTable (id) NOT ENFORCED RELY,",
"uk_b,UNIQUE (b) NOT ENFORCED,",
"uk_a_c,UNIQUE (a, c) NOT ENFORCED,",
"c1,CHECK (c IS NOT NULL) ENFORCED,",
"c2,CHECK (id > 0) ENFORCED,"
"uk_b,UNIQUE (b) NOT ENFORCED NORELY,",
"uk_a_c,UNIQUE (a, c) NOT ENFORCED NORELY,",
"c1,CHECK (c IS NOT NULL) ENFORCED NORELY,",
"c2,CHECK (id > 0) ENFORCED NORELY,"
)
var descDdL = sql(s"DESCRIBE EXTENDED $tbl").collect().map(_.mkString(","))
.dropWhile(_ != "# Constraints,,")
assert(descDdL === expectedConstraintsDdl)

// Show non-default value for RELY.
sql(s"ALTER TABLE $tbl ADD CONSTRAINT c3 CHECK (b IS NOT NULL) RELY")
descDdL = sql(s"DESCRIBE EXTENDED $tbl").collect().map(_.mkString(","))
.dropWhile(_ != "# Constraints,,")
Expand All @@ -386,7 +385,7 @@ class DescribeTableSuite extends command.DescribeTableSuiteBase
descDdL = sql(s"DESCRIBE EXTENDED $tbl").collect().map(_.mkString(","))
.dropWhile(_ != "# Constraints,,")
assert(descDdL === expectedConstraintsDdl
.filter(_ != "c1,CHECK (c IS NOT NULL) ENFORCED,"))
.filter(_ != "c1,CHECK (c IS NOT NULL) ENFORCED NORELY,"))
}
}
}
Expand Down