Skip to content

Commit

Permalink
serialize on update expressions (#7115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycor authored Dec 16, 2023
1 parent b7aa988 commit 5012d33
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 17 deletions.
15 changes: 14 additions & 1 deletion go/cmd/dolt/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ func schemaFromCreateTableStmt(createTableStmt string) (schema.Schema, error) {
}
}

cols := []schema.Column{}
var cols []schema.Column
for _, col := range create.TableSpec.Columns {
internalTyp, err := types.ColumnTypeToType(&col.Type)
typeInfo, err := typeinfo.FromSqlType(internalTyp)
Expand All @@ -898,17 +898,30 @@ func schemaFromCreateTableStmt(createTableStmt string) (schema.Schema, error) {
if col.Type.Default != nil {
col.Type.Default.Format(defBuf)
}

genBuf := ast.NewTrackedBuffer(nil)
if col.Type.GeneratedExpr != nil {
col.Type.GeneratedExpr.Format(genBuf)
}

onUpBuf := ast.NewTrackedBuffer(nil)
if col.Type.OnUpdate != nil {
col.Type.OnUpdate.Format(onUpBuf)
}

var comment string
if col.Type.Comment != nil {
comment = col.Type.Comment.String()
}

sCol := schema.Column{
Name: col.Name.String(),
Kind: typeInfo.NomsKind(),
IsPartOfPK: primaryCols[col.Name.Lowered()],
TypeInfo: typeInfo,
Default: defBuf.String(),
Generated: "", // TODO
OnUpdate: "", // TODO
Virtual: false, // TODO
AutoIncrement: col.Type.Autoincrement == true,
Comment: comment,
Expand Down
30 changes: 28 additions & 2 deletions go/gen/fb/serial/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/cespare/xxhash v1.1.0
github.com/creasty/defaults v1.6.0
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-mysql-server v0.17.1-0.20231214194603-39acc5e6f988
github.com/dolthub/go-mysql-server v0.17.1-0.20231215225524-76f133444ea4
github.com/dolthub/swiss v0.1.0
github.com/goccy/go-json v0.10.2
github.com/google/go-github/v57 v57.0.0
Expand Down
4 changes: 2 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e h1:kPsT4a47cw1+y/N5SSCkma7FhAPw7KeGmD6c9PBZW9Y=
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.17.1-0.20231214194603-39acc5e6f988 h1:oYhHtAZFJujmu6NeHfjliwNaVrbaq5pYbl9Pu84Bq2I=
github.com/dolthub/go-mysql-server v0.17.1-0.20231214194603-39acc5e6f988/go.mod h1:zJCyPiYe9VZ9xIQTv7S1OFKwyoVQoeGxZXNtkFxTcOI=
github.com/dolthub/go-mysql-server v0.17.1-0.20231215225524-76f133444ea4 h1:s1YRhf3iwEO89iO7pfq+eIV6kQHl9h7XS6MlYKhPptw=
github.com/dolthub/go-mysql-server v0.17.1-0.20231215225524-76f133444ea4/go.mod h1:zJCyPiYe9VZ9xIQTv7S1OFKwyoVQoeGxZXNtkFxTcOI=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488 h1:0HHu0GWJH0N6a6keStrHhUAK5/o9LVfkh44pvsV4514=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488/go.mod h1:ehexgi1mPxRTk0Mok/pADALuHbvATulTh6gzr7NzZto=
github.com/dolthub/jsonpath v0.0.2-0.20230525180605-8dc13778fd72 h1:NfWmngMi1CYUWU4Ix8wM+USEhjc+mhPlT9JUR/anvbQ=
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/merge/merge_prolly_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ func resolveDefaults(ctx *sql.Context, tableName string, mergedSchema schema.Sch
return false, nil
}

if col.Default != "" || col.Generated != "" {
if col.Default != "" || col.Generated != "" || col.OnUpdate != "" {
expr, err := index.ResolveDefaultExpression(ctx, tableName, mergedSchema, col)
if err != nil {
return true, err
Expand Down Expand Up @@ -1443,7 +1443,7 @@ func remapTupleWithColumnDefaults(
// If the column is a new column, then look up any default or generated value in a second pass, after the
// non-default and non-generated fields have been established. Virtual columns have been excluded, so any
// generated column is stored.
if col.Default != "" || col.Generated != "" {
if col.Default != "" || col.Generated != "" || col.OnUpdate != "" {
secondPass = append(secondPass, to)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/mvdata/engine_table_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (s *SqlEngineTableWriter) createTable() error {
// upstream to make the dolt schema
sqlCols := make([]string, len(s.tableSchema.Schema))
for i, c := range s.tableSchema.Schema {
sqlCols[i] = sql.GenerateCreateTableColumnDefinition(c, c.Default.String(), sql.Collation_Default)
sqlCols[i] = sql.GenerateCreateTableColumnDefinition(c, c.Default.String(), c.OnUpdate.String(), sql.Collation_Default)
}
var pks string
var sep string
Expand Down
3 changes: 3 additions & 0 deletions go/libraries/doltcore/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type Column struct {
// Generated is the generated value of this column. This is the string representation of a sql.Expression.
Generated string

// OnUpdate is the on update value of this column. This is the string representation of a sql.Expression.
OnUpdate string

// Virtual is true if this is a virtual column.
Virtual bool

Expand Down
30 changes: 27 additions & 3 deletions go/libraries/doltcore/schema/encoding/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,23 @@ func serializeSchemaAsFlatbuffer(sch schema.Schema) ([]byte, error) {
indexes := serializeSecondaryIndexes(b, sch, sch.Indexes().AllIndexes())
checks := serializeChecks(b, sch.Checks().AllChecks())

var hasFeaturesAfterTryAccessors bool
for _, col := range sch.GetAllCols().GetColumns() {
if col.OnUpdate != "" {
hasFeaturesAfterTryAccessors = true
break
}
}

serial.TableSchemaStart(b)
serial.TableSchemaAddClusteredIndex(b, rows)
serial.TableSchemaAddColumns(b, columns)
serial.TableSchemaAddSecondaryIndexes(b, indexes)
serial.TableSchemaAddChecks(b, checks)
serial.TableSchemaAddCollation(b, serial.Collation(sch.GetCollation()))
if hasFeaturesAfterTryAccessors {
serial.TableSchemaAddHasFeaturesAfterTryAccessors(b, hasFeaturesAfterTryAccessors)
}
root := serial.TableSchemaEnd(b)
bs := serial.FinishMessage(b, root, []byte(serial.TableSchemaFileID))
return bs, nil
Expand Down Expand Up @@ -215,15 +226,21 @@ func serializeSchemaColumns(b *fb.Builder, sch schema.Schema) fb.UOffsetT {
// serialize columns in |cols|
for i := len(cols) - 1; i >= 0; i-- {
col := cols[i]
defVal := ""
var defVal, onUpdateVal string
if col.Default != "" {
defVal = col.Default
} else {
defVal = col.Generated
}

if col.OnUpdate != "" {
onUpdateVal = col.OnUpdate
}

co := b.CreateString(col.Comment)
do := b.CreateString(defVal)
ou := b.CreateString(onUpdateVal)

typeString := sqlTypeString(col.TypeInfo)
to := b.CreateString(typeString)
no := b.CreateString(col.Name)
Expand All @@ -242,6 +259,9 @@ func serializeSchemaColumns(b *fb.Builder, sch schema.Schema) fb.UOffsetT {
serial.ColumnAddNullable(b, col.IsNullable())
serial.ColumnAddGenerated(b, col.Generated != "")
serial.ColumnAddVirtual(b, col.Virtual)
if onUpdateVal != "" {
serial.ColumnAddOnUpdateValue(b, ou)
}
serial.ColumnAddHidden(b, false)
offs[i] = serial.ColumnEnd(b)
}
Expand Down Expand Up @@ -319,8 +339,7 @@ func deserializeColumns(ctx context.Context, s *serial.TableSchema) ([]schema.Co
return nil, err
}

defVal := ""
generatedVal := ""
var defVal, generatedVal, onUpdateVal string
if c.DefaultValue() != nil {
if c.Generated() {
generatedVal = string(c.DefaultValue())
Expand All @@ -329,6 +348,10 @@ func deserializeColumns(ctx context.Context, s *serial.TableSchema) ([]schema.Co
}
}

if c.OnUpdateValue() != nil {
onUpdateVal = string(c.OnUpdateValue())
}

cols[i] = schema.Column{
Name: string(c.Name()),
Tag: c.Tag(),
Expand All @@ -337,6 +360,7 @@ func deserializeColumns(ctx context.Context, s *serial.TableSchema) ([]schema.Co
TypeInfo: sqlType,
Default: defVal,
Generated: generatedVal,
OnUpdate: onUpdateVal,
Virtual: c.Virtual(),
AutoIncrement: c.AutoIncrement(),
Comment: string(c.Comment()),
Expand Down
6 changes: 6 additions & 0 deletions go/libraries/doltcore/sqle/enginetest/dolt_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,12 @@ func TestColumnDefaults(t *testing.T) {
enginetest.TestColumnDefaults(t, h)
}

func TestOnUpdateExprScripts(t *testing.T) {
h := newDoltHarness(t)
defer h.Close()
enginetest.TestOnUpdateExprScripts(t, h)
}

func TestAlterTable(t *testing.T) {
// This is a newly added test in GMS that dolt doesn't support yet
h := newDoltHarness(t).WithSkippedQueries([]string{"ALTER TABLE t42 ADD COLUMN s varchar(20), drop check check1"})
Expand Down
8 changes: 6 additions & 2 deletions go/libraries/doltcore/sqle/sqlfmt/schema_fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ func GenerateCreateTableColumnDefinition(col schema.Column, tableCollation sql.C

// GenerateCreateTableIndentedColumnDefinition returns column definition for CREATE TABLE statement with no indentation
func GenerateCreateTableIndentedColumnDefinition(col schema.Column, tableCollation sql.CollationID) string {
var defaultVal, genVal *sql.ColumnDefaultValue
var defaultVal, genVal, onUpdateVal *sql.ColumnDefaultValue
if col.Default != "" {
defaultVal = sql.NewUnresolvedColumnDefaultValue(col.Default)
}
if col.Generated != "" {
genVal = sql.NewUnresolvedColumnDefaultValue(col.Generated)
}
if col.OnUpdate != "" {
onUpdateVal = sql.NewUnresolvedColumnDefaultValue(col.OnUpdate)
}

return sql.GenerateCreateTableColumnDefinition(
&sql.Column{
Expand All @@ -50,7 +53,8 @@ func GenerateCreateTableIndentedColumnDefinition(col schema.Column, tableCollati
Comment: col.Comment,
Generated: genVal,
Virtual: col.Virtual,
}, col.Default, tableCollation)
OnUpdate: onUpdateVal,
}, col.Default, col.OnUpdate, tableCollation)
}

// GenerateCreateTableIndexDefinition returns index definition for CREATE TABLE statement with indentation of 2 spaces
Expand Down
14 changes: 11 additions & 3 deletions go/libraries/doltcore/sqle/sqlutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@ func FromDoltSchema(dbName, tableName string, sch schema.Schema) (sql.PrimaryKey
extra = "auto_increment"
}

var deflt, generated *sql.ColumnDefaultValue
var deflt, generated, onUpdate *sql.ColumnDefaultValue
if col.Default != "" {
deflt = sql.NewUnresolvedColumnDefaultValue(col.Default)
}
if col.Generated != "" {
generated = sql.NewUnresolvedColumnDefaultValue(col.Generated)
}
if col.OnUpdate != "" {
onUpdate = sql.NewUnresolvedColumnDefaultValue(col.OnUpdate)
}

cols[i] = &sql.Column{
Name: col.Name,
Type: sqlType,
Default: deflt,
Generated: generated,
OnUpdate: onUpdate,
Nullable: col.IsNullable(),
DatabaseSource: dbName,
Source: tableName,
Expand Down Expand Up @@ -141,14 +145,17 @@ func ToDoltCol(tag uint64, col *sql.Column) (schema.Column, error) {
return schema.Column{}, err
}

defaultVal := ""
generatedVal := ""
var defaultVal, generatedVal, onUpdateVal string
if col.Default != nil {
defaultVal = col.Default.String()
} else {
generatedVal = col.Generated.String()
}

if col.OnUpdate != nil {
onUpdateVal = col.OnUpdate.String()
}

c := schema.Column{
Name: col.Name,
Tag: tag,
Expand All @@ -157,6 +164,7 @@ func ToDoltCol(tag uint64, col *sql.Column) (schema.Column, error) {
TypeInfo: typeInfo,
Default: defaultVal,
Generated: generatedVal,
OnUpdate: onUpdateVal,
Virtual: col.Virtual,
AutoIncrement: col.AutoIncrement,
Comment: col.Comment,
Expand Down
6 changes: 6 additions & 0 deletions go/serial/schema.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ table TableSchema {
secondary_indexes:[Index];
checks:[CheckConstraint];
collation:Collation;

// this field is necessary because older dolt clients weren't using TryAccessor for Columns, but are in TableSchemas
has_features_after_try_accessors:bool;
}

table Column {
Expand Down Expand Up @@ -55,6 +58,9 @@ table Column {
hidden:bool;
generated:bool;
virtual:bool;

// sql on update value
on_update_value:string;
}

table Index {
Expand Down

0 comments on commit 5012d33

Please sign in to comment.