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

types: port 'HasCharset' function from MySQL #270

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,15 @@ func (ft *FieldType) StorageLength() int {
return VarStorageLen
}
}

// HasCharset indicates if a COLUMN has an associated charset. Returning false here prevents some information
// statements(like `SHOW CREATE TABLE`) from attaching a CHARACTER SET clause to the column.
func HasCharset(ft *FieldType) bool {
switch ft.Tp {
case mysql.TypeVarchar, mysql.TypeString, mysql.TypeVarString, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is too long, maybe we could spilit it into two.

Copy link
Member Author

Choose a reason for hiding this comment

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

How about this?

    case                                                                               
        mysql.TypeVarchar,    
        mysql.TypeString,    
        mysql.TypeVarString,    
        mysql.TypeBlob,    
        mysql.TypeTinyBlob,     
        mysql.TypeMediumBlob,     
        mysql.TypeLongBlob:    
        return !mysql.HasBinaryFlag(ft.Flag)    
    case                     
        mysql.TypeEnum,    
        mysql.TypeSet:    
        return true 

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

After this PR, I think there is something need attention. @xiekeyi98

create table t (
a bit(11),
b int,
c int(3),
d float,
e double,
f decimal(10,2),
g numeric(10,2),
h date,
i datetime,
j timestamp,
k time,
m year,
c0 char(10),
c1 varchar(10),
c2 binary(10),
c3 varbinary(10),
c4 blob(50),
c5 text(50),
c6 ENUM('value1','value2'),
c7 SET('value1','value2'),
c8 JSON
) charset="utf8mb4"

In MySQL:

test> show full columns from t;
+-------+-------------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+---------+
| Field | Type                    | Collation          | Null | Key | Default           | Extra                       | Privileges                      | Comment |
+-------+-------------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+---------+
| a     | bit(11)                 | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| b     | int(11)                 | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| c     | int(3)                  | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| d     | float                   | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| e     | double                  | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| f     | decimal(10,2)           | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| g     | decimal(10,2)           | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| h     | date                    | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| i     | datetime                | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| j     | timestamp               | <null>             | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | select,insert,update,references |         |
| k     | time                    | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| m     | year(4)                 | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| c0    | char(10)                | utf8mb4_general_ci | YES  |     | <null>            |                             | select,insert,update,references |         |
| c1    | varchar(10)             | utf8mb4_general_ci | YES  |     | <null>            |                             | select,insert,update,references |         |
| c2    | binary(10)              | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| c3    | varbinary(10)           | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| c4    | tinyblob                | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
| c5    | tinytext                | utf8mb4_general_ci | YES  |     | <null>            |                             | select,insert,update,references |         |
| c6    | enum('value1','value2') | utf8mb4_general_ci | YES  |     | <null>            |                             | select,insert,update,references |         |
| c7    | set('value1','value2')  | utf8mb4_general_ci | YES  |     | <null>            |                             | select,insert,update,references |         |
| c8    | json                    | <null>             | YES  |     | <null>            |                             | select,insert,update,references |         |
+-------+-------------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+---------+

In TiDB

+-------+-------------------------+-------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type                    | Collation   | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+-------------------------+-------------+------+-----+---------+-------+---------------------------------+---------+
| a     | bit(11)                 | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| b     | int(11)                 | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| c     | int(3)                  | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| d     | float                   | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| e     | double                  | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| f     | decimal(10,2)           | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| g     | decimal(10,2)           | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| h     | date                    | NULL        | YES  |     | <null>  |       | select,insert,update,references |         |
| i     | datetime                | NULL        | YES  |     | <null>  |       | select,insert,update,references |         |
| j     | timestamp               | NULL        | YES  |     | <null>  |       | select,insert,update,references |         |
| k     | time                    | NULL        | YES  |     | <null>  |       | select,insert,update,references |         |
| m     | year(4)                 | NULL        | YES  |     | <null>  |       | select,insert,update,references |         |
| c0    | char(10)                | utf8mb4_bin | YES  |     | <null>  |       | select,insert,update,references |         |
| c1    | varchar(10)             | utf8mb4_bin | YES  |     | <null>  |       | select,insert,update,references |         |
| c2    | binary(10)              | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| c3    | varbinary(10)           | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| c4    | blob                    | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
| c5    | text                    | utf8mb4_bin | YES  |     | <null>  |       | select,insert,update,references |         |
| c6    | enum('value1','value2') | utf8mb4_bin | YES  |     | <null>  |       | select,insert,update,references |         |
| c7    | set('value1','value2')  | utf8mb4_bin | YES  |     | <null>  |       | select,insert,update,references |         |
| c8    | json                    | binary      | YES  |     | <null>  |       | select,insert,update,references |         |
+-------+-------------------------+-------------+------+-----+---------+-------+---------------------------------+---------+

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep.
Maybe something should be refactored.
Thanks for your guidance.
❤️

return !mysql.HasBinaryFlag(ft.Flag)
case mysql.TypeEnum, mysql.TypeSet:
return true
}
return false
}
78 changes: 77 additions & 1 deletion types/field_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package types
package types_test

import (
"fmt"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/mysql"
. "github.com/pingcap/parser/types"
_ "github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tidb/util/testleak"
)

Expand All @@ -39,12 +44,14 @@ func (s *testFieldTypeSuite) TestFieldType(c *C) {
c.Assert(ft.Decimal, Equals, UnspecifiedLength)
ft.Decimal = 5
c.Assert(ft.String(), Equals, "time(5)")
c.Assert(HasCharset(ft), IsFalse)

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")
c.Assert(HasCharset(ft), IsFalse)

ft = NewFieldType(mysql.TypeFloat)
ft.Flen = 12 // Default
Expand All @@ -62,6 +69,7 @@ func (s *testFieldTypeSuite) TestFieldType(c *C) {
ft.Flen = 7 // Not Default
ft.Decimal = 3 // Not Default
c.Assert(ft.String(), Equals, "float(7,3)")
c.Assert(HasCharset(ft), IsFalse)

ft = NewFieldType(mysql.TypeDouble)
ft.Flen = 22 // Default
Expand All @@ -79,88 +87,156 @@ func (s *testFieldTypeSuite) TestFieldType(c *C) {
ft.Flen = 7 // Not Default
ft.Decimal = 3 // Not Default
c.Assert(ft.String(), Equals, "double(7,3)")
c.Assert(HasCharset(ft), IsFalse)

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")
c.Assert(HasCharset(ft), IsTrue)

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

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

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

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

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')")
c.Assert(HasCharset(ft), IsTrue)

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')")
c.Assert(HasCharset(ft), IsTrue)

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

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

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')")
c.Assert(HasCharset(ft), IsTrue)

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

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

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

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

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

func (s *testFieldTypeSuite) TestHasCharsetFromStmt(c *C) {
template := "CREATE TABLE t(a %s)"

types := []struct {
strType string
hasCharset bool
}{
{"int", false},
{"real", false},
{"float", false},
{"bit", false},
{"bool", false},
{"char(1)", true},
{"national char(1)", true},
{"binary", false},
{"varchar(1)", true},
{"national varchar(1)", true},
{"varbinary(1)", false},
{"year", false},
{"date", false},
{"time", false},
{"datetime", false},
{"timestamp", false},
{"blob", false},
{"tinyblob", false},
{"mediumblob", false},
{"longblob", false},
{"bit", false},
{"text", true},
{"tinytext", true},
{"mediumtext", true},
{"longtext", true},
{"json", false},
{"enum('1')", true},
{"set('1')", true},
}

p := parser.New()
for _, t := range types {
sql := fmt.Sprintf(template, t.strType)
stmt, err := p.ParseOneStmt(sql, "", "")
c.Assert(err, IsNil)

col := stmt.(*ast.CreateTableStmt).Cols[0]
c.Assert(HasCharset(col.Tp), Equals, t.hasCharset)
}
}