Skip to content

Commit

Permalink
expression: return upper bound for enum (#41021)
Browse files Browse the repository at this point in the history
close #40855
  • Loading branch information
YangKeao authored Feb 11, 2023
1 parent 48bc046 commit be48b2d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 14 additions & 0 deletions expression/builtin_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,17 @@ func TestGreatestLeastFunc(t *testing.T) {
_, err = funcs[ast.Least].getFunction(ctx, []Expression{NewZero(), NewOne()})
require.NoError(t, err)
}

func TestRefineArgsWithCastEnum(t *testing.T) {
ctx := createContext(t)
zeroUintConst := primitiveValsToConstants(ctx, []interface{}{uint64(0)})[0]
enumType := types.NewFieldTypeBuilder().SetType(mysql.TypeEnum).SetElems([]string{"1", "2", "3"}).AddFlag(mysql.EnumSetAsIntFlag).Build()
enumCol := &Column{RetType: &enumType}

f := funcs[ast.EQ].(*compareFunctionClass)
require.NotNil(t, f)

args := f.refineArgsByUnsignedFlag(ctx, []Expression{zeroUintConst, enumCol})
require.Equal(t, zeroUintConst, args[0])
require.Equal(t, enumCol, args[1])
}
12 changes: 10 additions & 2 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func IntergerUnsignedUpperBound(intType byte) uint64 {
case mysql.TypeBit:
return math.MaxUint64
case mysql.TypeEnum:
return math.MaxUint64
// enum can have at most 65535 distinct elements
// it would be better to use len(FieldType.GetElems()), but we only have a byte type here
return 65535
case mysql.TypeSet:
return math.MaxUint64
default:
Expand All @@ -73,8 +75,12 @@ func IntergerSignedUpperBound(intType byte) int64 {
return math.MaxInt32
case mysql.TypeLonglong:
return math.MaxInt64
case mysql.TypeEnum:
// enum can have at most 65535 distinct elements
// it would be better to use len(FieldType.GetElems()), but we only have a byte type here
return 65535
default:
panic("Input byte is not a mysql type")
panic("Input byte is not a mysql int type")
}
}

Expand All @@ -91,6 +97,8 @@ func IntergerSignedLowerBound(intType byte) int64 {
return math.MinInt32
case mysql.TypeLonglong:
return math.MinInt64
case mysql.TypeEnum:
return 0
default:
panic("Input byte is not a mysql type")
}
Expand Down

0 comments on commit be48b2d

Please sign in to comment.