Skip to content
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

Allow inline column check constraint definitions to appear in any order #295

Merged
merged 2 commits into from
Dec 20, 2023
Merged
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
15 changes: 15 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,11 @@ func (ts *TableSpec) Format(buf *TrackedBuffer) {
// AddColumn appends the given column to the list in the spec
func (ts *TableSpec) AddColumn(cd *ColumnDefinition) {
ts.Columns = append(ts.Columns, cd)

// Move any inline check constraints up from the column definition to the table spec
if cd.Type.Constraint != nil {
ts.Constraints = append(ts.Constraints, cd.Type.Constraint)
}
}

// AddIndex appends the given index to the list in the spec
Expand Down Expand Up @@ -2621,6 +2626,9 @@ type ColumnType struct {
// Foreign key specification
ForeignKeyDef *ForeignKeyDefinition

// Check constraint specification
Constraint *ConstraintDefinition

// Generated columns
GeneratedExpr Expr // The expression used to generate this column
Stored BoolVal // Default is Virtual (not stored)
Expand Down Expand Up @@ -2672,6 +2680,13 @@ func (ct *ColumnType) merge(other ColumnType) error {
ct.ForeignKeyDef = other.ForeignKeyDef
}

if other.Constraint != nil {
if ct.Constraint != nil {
return errors.New("cannot include more than one check constraint in a column definition")
}
ct.Constraint = other.Constraint
}

if other.Comment != nil {
if ct.Comment != nil {
return errors.New("cannot include more than one comment for a column definition")
Expand Down
4 changes: 4 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type parseTest struct {

var (
validSQL = []parseTest{
{
input: "create table t123 (c1 varchar(5) check (c1 in ('v1', 'v2')) NOT NULL);",
output: "create table t123 (\n\tc1 varchar(5) not null,\n\tcheck (c1 in ('v1', 'v2'))\n)",
},
{
input: "INSERT INTO hourly_logins (applications_id, count, hour) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE count = count + VALUES(count)",
output: "insert into hourly_logins(applications_id, `count`, `hour`) values (:v1, :v2, :v3) on duplicate key update count = `count` + values(`count`)",
Expand Down
Loading