Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiny clean up model and column package. #313

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 3 additions & 37 deletions column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/types"
)

Expand All @@ -44,33 +43,6 @@ type IndexedCol struct {
X kv.Index
}

func (c *Col) getTypeStr() string {
ans := []string{types.FieldTypeToStr(c.Tp, c.Charset)}
if c.Flen != -1 {
if c.Decimal == -1 {
ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
} else {
ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
}
}
if mysql.HasUnsignedFlag(c.Flag) {
ans = append(ans, "UNSIGNED")
}
if mysql.HasZerofillFlag(c.Flag) {
ans = append(ans, "ZEROFILL")
}
if mysql.HasBinaryFlag(c.Flag) {
ans = append(ans, "BINARY")
}
if c.Charset != "" && c.Charset != charset.CharsetBin {
ans = append(ans, fmt.Sprintf("CHARACTER SET %s", c.Charset))
}
if c.Collate != "" {
ans = append(ans, fmt.Sprintf("COLLATE %s", c.Collate))
}
return strings.Join(ans, " ")
}

// String implements fmt.Stringer interface.
func (c *Col) String() string {
ans := []string{c.Name.O, types.FieldTypeToStr(c.Tp, c.Charset)}
Expand Down Expand Up @@ -108,7 +80,7 @@ func FindCols(cols []*Col, names []string) ([]*Col, error) {
return rcols, nil
}

// FindOnUpdateCols finds columns have OnUpdateNow flag.
// FindOnUpdateCols finds columns which have OnUpdateNow flag.
func FindOnUpdateCols(cols []*Col) []*Col {
var rcols []*Col
for _, c := range cols {
Expand All @@ -120,16 +92,10 @@ func FindOnUpdateCols(cols []*Col) []*Col {
return rcols
}

// TypeError returns error for invalid value type.
func (c *Col) TypeError(v interface{}) error {
return errors.Errorf("cannot use %v (type %T) in assignment to, or comparison with, column %s (type %s)",
v, v, c.Name, types.FieldTypeToStr(c.Tp, c.Charset))
}

// CastValues casts values based on columns type.
func CastValues(ctx context.Context, rec []interface{}, cols []*Col) (err error) {
for _, c := range cols {
rec[c.Offset], err = types.Convert(rec[c.Offset], &c.FieldType)
rec[c.Offset], err = types.Convert(rec[c.Offset], c.FieldType)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -254,7 +220,7 @@ func (c *Col) CheckNotNull(data interface{}) error {
func CheckNotNull(cols []*Col, row []interface{}) error {
for _, c := range cols {
if err := c.CheckNotNull(row[c.Offset]); err != nil {
return err
return errors.Trace(err)
}
}
return nil
Expand Down
2 changes: 0 additions & 2 deletions column/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ func (s *testColumnSuite) TestString(c *C) {
},
}
col.Flen = 2
col.getTypeStr()
col.Decimal = 1
col.Charset = mysql.DefaultCharset
col.Collate = mysql.DefaultCollationName
col.Flag |= mysql.ZerofillFlag | mysql.UnsignedFlag | mysql.BinaryFlag | mysql.AutoIncrementFlag | mysql.NotNullFlag
col.getTypeStr()

cs := col.String()
c.Assert(len(cs), Greater, 0)
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func convertToTime(arg interface{}, tp byte) (interface{}, error) {
f := types.NewFieldType(tp)
f.Decimal = mysql.MaxFsp

v, err := types.Convert(arg, f)
v, err := types.Convert(arg, *f)
if err != nil {
return nil, err
}
Expand All @@ -50,7 +50,7 @@ func convertToDuration(arg interface{}) (interface{}, error) {
f := types.NewFieldType(mysql.TypeDuration)
f.Decimal = mysql.MaxFsp

v, err := types.Convert(arg, f)
v, err := types.Convert(arg, *f)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion expression/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (f *FunctionCast) Eval(ctx context.Context, args map[interface{}]interface{
if value == nil {
return nil, nil
}
return types.Cast(value, f.Tp), nil
return types.Cast(value, *f.Tp), nil
}

// Accept implements Expression Accept interface.
Expand Down
2 changes: 1 addition & 1 deletion expression/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func getTimeValue(ctx context.Context, v interface{}, tp byte, fsp int) (interfa
m := map[interface{}]interface{}{}
v := Eval(x, nil, m)
ft := types.NewFieldType(mysql.TypeLonglong)
xval, err := types.Convert(v, ft)
xval, err := types.Convert(v, *ft)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
5 changes: 2 additions & 3 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import (
// ColumnInfo provides meta data describing of a table column.
type ColumnInfo struct {
ID int64 `json:"id"`
Name CIStr `json:"name"` // Column Name.
Name CIStr `json:"name"`
Offset int `json:"offset"`
DefaultValue interface{} `json:"default"` // Default Value.
DefaultValue interface{} `json:"default"`
types.FieldType `json:"type"`
}

// TableInfo provides meta data describing a DB table.
type TableInfo struct {
// Table name.
ID int64 `json:"id"`
Name CIStr `json:"name"`
Charset string `json:"charset"`
Expand Down
2 changes: 1 addition & 1 deletion plan/plans/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (r *TableDefaultPlan) filterBinOp(ctx context.Context, x *expression.Binary
return r, false, nil
}

if rval, err = types.Convert(rval, &c.FieldType); err != nil {
if rval, err = types.Convert(rval, c.FieldType); err != nil {
return nil, false, err
}

Expand Down
2 changes: 1 addition & 1 deletion plan/plans/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (r *indexPlan) Filter(ctx context.Context, expr expression.Expression) (pla
break
}

if val, err = types.Convert(val, &col.FieldType); err != nil {
if val, err = types.Convert(val, col.FieldType); err != nil {
return nil, false, err
}
r.spans = filterSpans(r.spans, toSpans(x.Op, val))
Expand Down
2 changes: 1 addition & 1 deletion plan/plans/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (p *UnionPlan) fetchSrc(ctx context.Context, i int, t memkv.Temp) error {
if srcRf.Flen > rf.Col.Flen {
rf.Col.Flen = srcRf.Col.Flen
}
row.Data[i], err = types.Convert(row.Data[i], &rf.Col.FieldType)
row.Data[i], err = types.Convert(row.Data[i], rf.Col.FieldType)
if err != nil {
return errors.Trace(err)
}
Expand Down
10 changes: 5 additions & 5 deletions util/types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func convertIntToInt(val, lowerBound, upperBound int64, tp byte) (converted int6
return
}

func convertToInt(val interface{}, target *FieldType) (converted int64, err error) {
func convertToInt(val interface{}, target FieldType) (converted int64, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use pointor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see FieldType variable is only used for readable variable, also in ColumnInfo struct with no pointer FieldType, so i think it is better to use a none-pointer variable here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned about performence issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, u are right.

tp := target.Tp
lowerBound := signedLowerBound[tp]
upperBound := signedUpperBound[tp]
Expand Down Expand Up @@ -170,7 +170,7 @@ func convertFloatToUint(val float64, upperBound uint64, tp byte) (converted uint
return
}

func convertToUint(val interface{}, target *FieldType) (converted uint64, err error) {
func convertToUint(val interface{}, target FieldType) (converted uint64, err error) {
tp := target.Tp
upperBound := unsignedUpperBound[tp]
switch v := val.(type) {
Expand Down Expand Up @@ -212,13 +212,13 @@ func convertToUint(val interface{}, target *FieldType) (converted uint64, err er
}

// typeError returns error for invalid value type.
func typeError(v interface{}, target *FieldType) error {
func typeError(v interface{}, target FieldType) error {
return errors.Errorf("cannot use %v (type %T) in assignment to, or comparison with, column type %s)",
v, v, target.String())
}

// Cast casts val to certain types and does not return error.
func Cast(val interface{}, target *FieldType) (v interface{}) {
func Cast(val interface{}, target FieldType) (v interface{}) {
tp := target.Tp
switch tp {
case mysql.TypeString:
Expand Down Expand Up @@ -285,7 +285,7 @@ func Cast(val interface{}, target *FieldType) (v interface{}) {
}

// Convert converts the val with type tp.
func Convert(val interface{}, target *FieldType) (v interface{}, err error) {
func Convert(val interface{}, target FieldType) (v interface{}, err error) {
tp := target.Tp
if val == nil {
return nil, nil
Expand Down
Loading