diff --git a/server/ast/column_table_def.go b/server/ast/column_table_def.go index dd3dc9de2c..c9dca6cfb1 100644 --- a/server/ast/column_table_def.go +++ b/server/ast/column_table_def.go @@ -65,6 +65,10 @@ func nodeColumnTableDef(node *tree.ColumnTableDef) (_ *vitess.ColumnDefinition, if err != nil { return nil, err } + // Wrap any default expression using a function call in parens to match MySQL's column default requirements + if _, ok := defaultExpr.(*vitess.FuncExpr); ok { + defaultExpr = &vitess.ParenExpr{Expr: defaultExpr} + } if len(node.CheckExprs) > 0 { return nil, fmt.Errorf("column-declared CHECK expressions are not yet supported") } diff --git a/testing/go/create_table_test.go b/testing/go/create_table_test.go index cea87f60d4..a58d6a648e 100755 --- a/testing/go/create_table_test.go +++ b/testing/go/create_table_test.go @@ -45,5 +45,23 @@ func TestCreateTable(t *testing.T) { }, }, }, + { + Name: "Create table with column default expression using function", + Assertions: []ScriptTestAssertion{ + { + // Test with a function in the column default expression + Query: "create table t1 (pk int primary key, c1 TEXT default length('Hello World!'));", + Expected: []sql.Row{}, + }, + { + Query: "insert into t1(pk) values (1);", + Expected: []sql.Row{}, + }, + { + Query: "select * from t1;", + Expected: []sql.Row{{1, "12"}}, + }, + }, + }, }) }