Skip to content

Commit

Permalink
[parser] types: Fix description for year type (pingcap#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu authored and xhebox committed Oct 8, 2021
1 parent a56cf86 commit 663f548
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
2 changes: 2 additions & 0 deletions parser/types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func (ft *FieldType) CompactStr() string {
case mysql.TypeBit, mysql.TypeShort, mysql.TypeTiny, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeString, mysql.TypeVarString:
// Flen is always shown.
suffix = fmt.Sprintf("(%d)", displayFlen)
case mysql.TypeYear:
suffix = fmt.Sprintf("(%d)", ft.Flen)
}
return ts + suffix
}
Expand Down
166 changes: 166 additions & 0 deletions parser/types/field_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package types

import (
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/util/testleak"
)

func TestT(t *testing.T) {
CustomVerboseFlag = true
TestingT(t)
}

var _ = Suite(&testFieldTypeSuite{})

type testFieldTypeSuite struct {
}

func (s *testFieldTypeSuite) TestFieldType(c *C) {
defer testleak.AfterTest(c)()
ft := NewFieldType(mysql.TypeDuration)
c.Assert(ft.Flen, Equals, UnspecifiedLength)
c.Assert(ft.Decimal, Equals, UnspecifiedLength)
ft.Decimal = 5
c.Assert(ft.String(), Equals, "time(5)")

ft = NewFieldType(mysql.TypeLong)
ft.Flen = 5
ft.Flag = mysql.UnsignedFlag | mysql.ZerofillFlag
c.Assert(ft.String(), Equals, "int(5) UNSIGNED ZEROFILL")
c.Assert(ft.InfoSchemaStr(), Equals, "int(5) unsigned")

ft = NewFieldType(mysql.TypeFloat)
ft.Flen = 12 // Default
ft.Decimal = 3 // Not Default
c.Assert(ft.String(), Equals, "float(12,3)")
ft = NewFieldType(mysql.TypeFloat)
ft.Flen = 12 // Default
ft.Decimal = -1 // Default
c.Assert(ft.String(), Equals, "float")
ft = NewFieldType(mysql.TypeFloat)
ft.Flen = 5 // Not Default
ft.Decimal = -1 // Default
c.Assert(ft.String(), Equals, "float")
ft = NewFieldType(mysql.TypeFloat)
ft.Flen = 7 // Not Default
ft.Decimal = 3 // Not Default
c.Assert(ft.String(), Equals, "float(7,3)")

ft = NewFieldType(mysql.TypeDouble)
ft.Flen = 22 // Default
ft.Decimal = 3 // Not Default
c.Assert(ft.String(), Equals, "double(22,3)")
ft = NewFieldType(mysql.TypeDouble)
ft.Flen = 22 // Default
ft.Decimal = -1 // Default
c.Assert(ft.String(), Equals, "double")
ft = NewFieldType(mysql.TypeDouble)
ft.Flen = 5 // Not Default
ft.Decimal = -1 // Default
c.Assert(ft.String(), Equals, "double")
ft = NewFieldType(mysql.TypeDouble)
ft.Flen = 7 // Not Default
ft.Decimal = 3 // Not Default
c.Assert(ft.String(), Equals, "double(7,3)")

ft = NewFieldType(mysql.TypeBlob)
ft.Flen = 10
ft.Charset = "UTF8"
ft.Collate = "UTF8_UNICODE_GI"
c.Assert(ft.String(), Equals, "text CHARACTER SET UTF8 COLLATE UTF8_UNICODE_GI")

ft = NewFieldType(mysql.TypeVarchar)
ft.Flen = 10
ft.Flag |= mysql.BinaryFlag
c.Assert(ft.String(), Equals, "varchar(10) BINARY")

ft = NewFieldType(mysql.TypeString)
ft.Charset = charset.CollationBin
ft.Flag |= mysql.BinaryFlag
c.Assert(ft.String(), Equals, "binary(1)")

ft = NewFieldType(mysql.TypeEnum)
ft.Elems = []string{"a", "b"}
c.Assert(ft.String(), Equals, "enum('a','b')")

ft = NewFieldType(mysql.TypeEnum)
ft.Elems = []string{"'a'", "'b'"}
c.Assert(ft.String(), Equals, "enum('''a''','''b''')")

ft = NewFieldType(mysql.TypeEnum)
ft.Elems = []string{"a\nb", "a\tb", "a\rb"}
c.Assert(ft.String(), Equals, "enum('a\\nb','a\tb','a\\rb')")

ft = NewFieldType(mysql.TypeEnum)
ft.Elems = []string{"a\nb", "a'\t\r\nb", "a\rb"}
c.Assert(ft.String(), Equals, "enum('a\\nb','a'' \\r\\nb','a\\rb')")

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"a", "b"}
c.Assert(ft.String(), Equals, "set('a','b')")

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"'a'", "'b'"}
c.Assert(ft.String(), Equals, "set('''a''','''b''')")

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"a\nb", "a'\t\r\nb", "a\rb"}
c.Assert(ft.String(), Equals, "set('a\\nb','a'' \\r\\nb','a\\rb')")

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"a'\nb", "a'b\tc"}
c.Assert(ft.String(), Equals, "set('a''\\nb','a''b c')")

ft = NewFieldType(mysql.TypeTimestamp)
ft.Flen = 8
ft.Decimal = 2
c.Assert(ft.String(), Equals, "timestamp(2)")
ft = NewFieldType(mysql.TypeTimestamp)
ft.Flen = 8
ft.Decimal = 0
c.Assert(ft.String(), Equals, "timestamp")

ft = NewFieldType(mysql.TypeDatetime)
ft.Flen = 8
ft.Decimal = 2
c.Assert(ft.String(), Equals, "datetime(2)")
ft = NewFieldType(mysql.TypeDatetime)
ft.Flen = 8
ft.Decimal = 0
c.Assert(ft.String(), Equals, "datetime")

ft = NewFieldType(mysql.TypeDate)
ft.Flen = 8
ft.Decimal = 2
c.Assert(ft.String(), Equals, "date")
ft = NewFieldType(mysql.TypeDate)
ft.Flen = 8
ft.Decimal = 0
c.Assert(ft.String(), Equals, "date")

ft = NewFieldType(mysql.TypeYear)
ft.Flen = 4
ft.Decimal = 0
c.Assert(ft.String(), Equals, "year(4)")
ft = NewFieldType(mysql.TypeYear)
ft.Flen = 2
ft.Decimal = 2
c.Assert(ft.String(), Equals, "year(2)") // Note: Invalid year.
}

0 comments on commit 663f548

Please sign in to comment.