Skip to content

Commit

Permalink
types: fix type merge about bit type (#23857) (#24027)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Apr 16, 2021
1 parent f5cac30 commit 25ab9ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
6 changes: 6 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,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
33 changes: 17 additions & 16 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.TypeUnspecified -> */
{
Expand Down Expand Up @@ -411,7 +412,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -444,7 +445,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -477,7 +478,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -510,7 +511,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeDouble,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -543,7 +544,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeDouble,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -642,7 +643,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeNewDate, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -675,7 +676,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeNewDate, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -807,7 +808,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeLonglong,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
Expand Down Expand Up @@ -890,27 +891,27 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
/* mysql.TypeBit -> */
{
// mysql.TypeUnspecified mysql.TypeTiny
mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeVarchar, mysql.TypeLonglong,
// mysql.TypeShort mysql.TypeLong
mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeLonglong, mysql.TypeLonglong,
// mysql.TypeFloat mysql.TypeDouble
mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeDouble, mysql.TypeDouble,
// mysql.TypeNull mysql.TypeTimestamp
mysql.TypeBit, mysql.TypeVarchar,
// mysql.TypeLonglong mysql.TypeInt24
mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeLonglong, mysql.TypeLonglong,
// mysql.TypeDate mysql.TypeTime
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeDatetime mysql.TypeYear
mysql.TypeVarchar, mysql.TypeVarchar,
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.TypeNewDecimal, mysql.TypeVarchar,
// mysql.TypeSet mysql.TypeTinyBlob
mysql.TypeVarchar, mysql.TypeTinyBlob,
// mysql.TypeMediumBlob mysql.TypeLongBlob
Expand Down Expand Up @@ -972,7 +973,7 @@ var fieldTypeMergeRules = [fieldTypeNum][fieldTypeNum]byte{
// mysql.TypeNewDate mysql.TypeVarchar
mysql.TypeVarchar, mysql.TypeVarchar,
// mysql.TypeBit <16>-<244>
mysql.TypeVarchar,
mysql.TypeNewDecimal,
// mysql.TypeJSON
mysql.TypeVarchar,
// mysql.TypeNewDecimal mysql.TypeEnum
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.TypeUnspecified, mysql.TypeNewDecimal:
Expand Down

0 comments on commit 25ab9ab

Please sign in to comment.