Skip to content
Merged
14 changes: 13 additions & 1 deletion pkg/frontend/mysql_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2141,7 +2141,19 @@ func setColLength(column *MysqlColumn, width int32) {
column.length = column.columnType.GetLength(width)
}

func setColFlag(column *MysqlColumn) {
func setColFlag(column *MysqlColumn, col *planPb.ColDef) {
if col == nil {
return
}
if col.NotNull || col.Typ.NotNullable {
column.flag |= uint16(defines.NOT_NULL_FLAG)
}
if col.Primary {
column.flag |= uint16(defines.PRI_KEY_FLAG)
}
if col.Unique {
column.flag |= uint16(defines.UNIQUE_KEY_FLAG)
}
if column.auto_incr {
column.flag |= uint16(defines.AUTO_INCREMENT_FLAG)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/frontend/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ func colDef2MysqlColumn(ctx context.Context, col *plan.ColDef) (*MysqlColumn, er
if err = setMysqlColumnTypeInfo(ctx, typ, c); err != nil {
return nil, err
}
setColFlag(c)
setColFlag(c, col)

// For TIMESTAMPADD function compatibility with MySQL:
// GetResultColumnsFromPlan sets the return type based on input type and unit:
Expand Down
54 changes: 54 additions & 0 deletions pkg/frontend/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,60 @@ func TestColDef2MysqlColumnStringMetadata(t *testing.T) {
}
}

func TestColDef2MysqlColumnConstraintFlags(t *testing.T) {
for _, tc := range []struct {
name string
col *plan2.ColDef
want uint16
}{
{
name: "primary and auto increment",
col: &plan2.ColDef{
Name: "id",
Typ: plan2.Type{
Id: int32(types.T_int32),
NotNullable: true,
AutoIncr: true,
},
NotNull: true,
Primary: true,
},
want: uint16(defines.NOT_NULL_FLAG | defines.PRI_KEY_FLAG | defines.AUTO_INCREMENT_FLAG),
},
{
name: "unique",
col: &plan2.ColDef{
Name: "uk",
Typ: plan2.Type{Id: int32(types.T_int32), NotNullable: true},
NotNull: true,
Unique: true,
},
want: uint16(defines.NOT_NULL_FLAG | defines.UNIQUE_KEY_FLAG),
},
} {
t.Run(tc.name, func(t *testing.T) {
col, err := colDef2MysqlColumn(context.Background(), tc.col)
require.NoError(t, err)
require.Equal(t, tc.want, col.Flag())

proto := &MysqlProtocolImpl{io: NewIOPackage(true)}
packet := proto.makeColumnDefinition41Payload(col, int(COM_QUERY))
pos := HeaderOffset
for range 6 {
_, next, ok := proto.readStringLenEnc(packet, pos)
require.True(t, ok)
pos = next
}
_, pos, ok := proto.io.ReadUint8(packet, pos)
require.True(t, ok)
flagsPos := pos + 2 + 4 + 1
flags, _, ok := proto.io.ReadUint16(packet, flagsPos)
require.True(t, ok)
require.Equal(t, tc.want, flags)
})
}
}

func Test_setMysqlColumnTypeMetadataFloatingPointDecimals(t *testing.T) {
cases := []struct {
name string
Expand Down
1,914 changes: 980 additions & 934 deletions pkg/pb/plan/plan.pb.go

Large diffs are not rendered by default.

Loading
Loading