From be48b2d6497b6674e639d1377f88940a2e40eda8 Mon Sep 17 00:00:00 2001 From: YangKeao Date: Sat, 11 Feb 2023 13:40:00 +0800 Subject: [PATCH] expression: return upper bound for enum (#41021) close pingcap/tidb#40855 --- expression/builtin_compare_test.go | 14 ++++++++++++++ types/convert.go | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/expression/builtin_compare_test.go b/expression/builtin_compare_test.go index 78229b77b1f60..849142308ec93 100644 --- a/expression/builtin_compare_test.go +++ b/expression/builtin_compare_test.go @@ -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]) +} diff --git a/types/convert.go b/types/convert.go index f98e4fe6202d4..715f49b140c0c 100644 --- a/types/convert.go +++ b/types/convert.go @@ -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: @@ -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") } } @@ -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") }