Skip to content

Commit

Permalink
Merge pull request #295 from dolthub/fulghum/inline-check-position
Browse files Browse the repository at this point in the history
Allow inline column check constraint definitions to appear in any order
  • Loading branch information
fulghum authored Dec 20, 2023
2 parents 88fb354 + f14f938 commit 3729729
Show file tree
Hide file tree
Showing 5 changed files with 9,157 additions and 9,118 deletions.
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

0 comments on commit 3729729

Please sign in to comment.