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: fix type merge about bit type (#23857) #24026

Merged
merged 6 commits into from
May 10, 2021
Merged
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
6 changes: 6 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,12 @@ func (s *testSuiteP2) TestUnion(c *C) {
tk.MustExec("create table t(a int, b decimal(6, 3))")
tk.MustExec("insert into t values(1, 1.000)")
tk.MustQuery("select count(distinct a), sum(distinct a), avg(distinct a) from (select a from t union all select b from t) tmp;").Check(testkit.Rows("1 1.000 1.0000000"))

// #issue 23832
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a bit(20), b float, c double, d int)")
tk.MustExec("insert into t values(10, 10, 10, 10), (1, -1, 2, -2), (2, -2, 1, 1), (2, 1.1, 2.1, 10.1)")
tk.MustQuery("select a from t union select 10 order by a").Check(testkit.Rows("1", "2", "10"))
}

func (s *testSuite2) TestUnionLimit(c *C) {
Expand Down
89 changes: 45 additions & 44 deletions types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func DefaultCharsetForType(tp byte) (string, string) {
// This is used in hybrid field type expression.
// For example "select case c when 1 then 2 when 2 then 'tidb' from t;"
// The result field type of the case expression is the merged type of the two when clause.
// See https://github.com/mysql/mysql-server/blob/5.7/sql/field.cc#L1042
// See https://github.com/mysql/mysql-server/blob/8.0/sql/field.cc#L1042
func MergeFieldType(a byte, b byte) byte {
ia := getFieldTypeIndex(a)
ib := getFieldTypeIndex(b)
Expand Down Expand Up @@ -358,6 +358,7 @@ const (
fieldTypeNum = fieldTypeTearFrom + (255 - fieldTypeTearTo)
)

// https://github.com/mysql/mysql-server/blob/8.0/sql/field.cc#L248
var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
/* mysql.TypeDecimal -> */
{
Expand Down Expand Up @@ -410,9 +411,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeTiny,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
Expand Down Expand Up @@ -443,9 +444,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeShort,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
Expand Down Expand Up @@ -476,9 +477,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeLong,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
Expand Down Expand Up @@ -509,9 +510,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeFloat,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeDouble,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeDouble, mysql.TypeVarchar,
Expand Down Expand Up @@ -542,9 +543,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeDouble,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeDouble,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeDouble, mysql.TypeVarchar,
Expand Down Expand Up @@ -641,9 +642,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeLonglong,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeNewDate, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
Expand Down Expand Up @@ -674,9 +675,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeInt24,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeNewDate, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
Expand Down Expand Up @@ -806,9 +807,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeYear,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
Expand Down Expand Up @@ -889,29 +890,29 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
},
/* mysql.TypeBit -> */
{
//mysql.TypeDecimal mysql.TypeTiny
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeShort mysql.TypeLong
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeFloat mysql.TypeDouble
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeNull mysql.TypeTimestamp
// mysql.TypeUnspecified mysql.TypeTiny
mysql.TypeVarchar, mysql.TypeLonglong,
// mysql.TypeShort mysql.TypeLong
mysql.TypeLonglong, mysql.TypeLonglong,
// mysql.TypeFloat mysql.TypeDouble
mysql.TypeDouble, mysql.TypeDouble,
// mysql.TypeNull mysql.TypeTimestamp
mysql.TypeBit, mysql.TypeVarchar,
//mysql.TypeLonglong mysql.TypeInt24
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeDate mysql.TypeTime
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeDatetime mysql.TypeYear
// mysql.TypeLonglong mysql.TypeInt24
mysql.TypeLonglong, mysql.TypeLonglong,
// mysql.TypeDate mysql.TypeTime
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeNewDate mysql.TypeVarchar
// mysql.TypeDatetime mysql.TypeYear
mysql.TypeVarchar, mysql.TypeLonglong,
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeBit,
//mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeSet mysql.TypeTinyBlob
// mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
// mysql.TypeSet mysql.TypeTinyBlob
mysql.TypeVarchar, mysql.TypeTinyBlob,
//mysql.TypeMediumBlob mysql.TypeLongBlob
mysql.TypeMediumBlob, mysql.TypeLongBlob,
Expand Down Expand Up @@ -971,9 +972,9 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
mysql.TypeVarchar, mysql.TypeNewDecimal,
//mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
//mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
//mysql.TypeJSON
// mysql.TypeBit <16>-<244>
mysql.TypeNewDecimal,
// mysql.TypeJSON
mysql.TypeVarchar,
//mysql.TypeNewDecimal mysql.TypeEnum
mysql.TypeNewDecimal, mysql.TypeVarchar,
Expand Down
4 changes: 3 additions & 1 deletion types/field_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,11 @@ func (s *testFieldTypeSuite) TestAggFieldType(c *C) {
c.Assert(aggTp.Tp, Equals, mysql.TypeDouble)
case mysql.TypeTimestamp, mysql.TypeDate, mysql.TypeDuration,
mysql.TypeDatetime, mysql.TypeNewDate, mysql.TypeVarchar,
mysql.TypeBit, mysql.TypeJSON, mysql.TypeEnum, mysql.TypeSet,
mysql.TypeJSON, mysql.TypeEnum, mysql.TypeSet,
mysql.TypeVarString, mysql.TypeGeometry:
c.Assert(aggTp.Tp, Equals, mysql.TypeVarchar)
case mysql.TypeBit:
c.Assert(aggTp.Tp, Equals, mysql.TypeLonglong)
case mysql.TypeString:
c.Assert(aggTp.Tp, Equals, mysql.TypeString)
case mysql.TypeDecimal, mysql.TypeNewDecimal:
Expand Down