diff --git a/go/vt/vtgate/vindexes/vschema.go b/go/vt/vtgate/vindexes/vschema.go index 1762b42fa8e..16b528322b6 100644 --- a/go/vt/vtgate/vindexes/vschema.go +++ b/go/vt/vtgate/vindexes/vschema.go @@ -203,16 +203,22 @@ type Column struct { // MarshalJSON returns a JSON representation of Column. func (col *Column) MarshalJSON() ([]byte, error) { cj := struct { - Name string `json:"name"` - Type string `json:"type,omitempty"` - Invisible bool `json:"invisible,omitempty"` - Default string `json:"default,omitempty"` + Name string `json:"name"` + Type string `json:"type,omitempty"` + Invisible bool `json:"invisible,omitempty"` + Default string `json:"default,omitempty"` + Size int32 `json:"size,omitempty"` + Scale int32 `json:"scale,omitempty"` + Nullable bool `json:"nullable,omitempty"` + Values []string `json:"values,omitempty"` }{ - Name: col.Name.String(), - Type: querypb.Type_name[int32(col.Type)], - } - if col.Invisible { - cj.Invisible = true + Name: col.Name.String(), + Type: querypb.Type_name[int32(col.Type)], + Invisible: col.Invisible, + Size: col.Size, + Scale: col.Scale, + Nullable: col.Nullable, + Values: col.Values, } if col.Default != nil { cj.Default = sqlparser.String(col.Default) diff --git a/go/vt/vtgate/vindexes/vschema_test.go b/go/vt/vtgate/vindexes/vschema_test.go index 83cf8f52b05..40cba720a0c 100644 --- a/go/vt/vtgate/vindexes/vschema_test.go +++ b/go/vt/vtgate/vindexes/vschema_test.go @@ -424,10 +424,12 @@ func TestVSchemaViews(t *testing.T) { "columns": [ { "name": "c1", + "nullable": true, "type": "NULL_TYPE" }, { "name": "c2", + "nullable": true, "type": "VARCHAR" } ] @@ -440,6 +442,51 @@ func TestVSchemaViews(t *testing.T) { require.JSONEq(t, want, got) } +func TestColumnMarshal(t *testing.T) { + tests := []struct { + name string + col Column + wanted string + }{ + { + name: "Decimal column", + col: Column{ + Name: sqlparser.NewIdentifierCI("col1"), + Type: sqltypes.Decimal, + Size: 15, + Scale: 2, + }, + wanted: `{"name":"col1", "scale":2, "size":15, "type":"DECIMAL"}`, + }, + { + name: "Decimal column with no scale", + col: Column{ + Name: sqlparser.NewIdentifierCI("col1"), + Type: sqltypes.Decimal, + Size: 15, + Scale: 0, + }, + wanted: `{"name":"col1", "size":15, "type":"DECIMAL"}`, + }, + { + name: "enum with values column", + col: Column{ + Name: sqlparser.NewIdentifierCI("col1"), + Type: sqltypes.Enum, + Values: []string{"{A", "B\"", "C"}, + }, + wanted: `{"name":"col1","type":"ENUM","values":["{A","B\"","C"]}`, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + res, err := test.col.MarshalJSON() + require.NoError(t, err) + require.JSONEq(t, test.wanted, string(res), string(res)) + }) + } +} + func TestVSchemaForeignKeys(t *testing.T) { good := vschemapb.SrvVSchema{ Keyspaces: map[string]*vschemapb.Keyspace{ @@ -482,10 +529,12 @@ func TestVSchemaForeignKeys(t *testing.T) { "columns": [ { "name": "c1", + "nullable": true, "type": "NULL_TYPE" }, { "name": "c2", + "nullable": true, "type": "VARCHAR" } ],