Skip to content

Commit 2ca953c

Browse files
YangKeaoblacktear23
authored andcommitted
expression: return upper bound for enum (pingcap#41021)
close pingcap#40855
1 parent 6915256 commit 2ca953c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

expression/builtin_compare_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,17 @@ func TestGreatestLeastFunc(t *testing.T) {
406406
_, err = funcs[ast.Least].getFunction(ctx, []Expression{NewZero(), NewOne()})
407407
require.NoError(t, err)
408408
}
409+
410+
func TestRefineArgsWithCastEnum(t *testing.T) {
411+
ctx := createContext(t)
412+
zeroUintConst := primitiveValsToConstants(ctx, []interface{}{uint64(0)})[0]
413+
enumType := types.NewFieldTypeBuilder().SetType(mysql.TypeEnum).SetElems([]string{"1", "2", "3"}).AddFlag(mysql.EnumSetAsIntFlag).Build()
414+
enumCol := &Column{RetType: &enumType}
415+
416+
f := funcs[ast.EQ].(*compareFunctionClass)
417+
require.NotNil(t, f)
418+
419+
args := f.refineArgsByUnsignedFlag(ctx, []Expression{zeroUintConst, enumCol})
420+
require.Equal(t, zeroUintConst, args[0])
421+
require.Equal(t, enumCol, args[1])
422+
}

types/convert.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ func IntergerUnsignedUpperBound(intType byte) uint64 {
5252
case mysql.TypeBit:
5353
return math.MaxUint64
5454
case mysql.TypeEnum:
55-
return math.MaxUint64
55+
// enum can have at most 65535 distinct elements
56+
// it would be better to use len(FieldType.GetElems()), but we only have a byte type here
57+
return 65535
5658
case mysql.TypeSet:
5759
return math.MaxUint64
5860
default:
@@ -73,8 +75,12 @@ func IntergerSignedUpperBound(intType byte) int64 {
7375
return math.MaxInt32
7476
case mysql.TypeLonglong:
7577
return math.MaxInt64
78+
case mysql.TypeEnum:
79+
// enum can have at most 65535 distinct elements
80+
// it would be better to use len(FieldType.GetElems()), but we only have a byte type here
81+
return 65535
7682
default:
77-
panic("Input byte is not a mysql type")
83+
panic("Input byte is not a mysql int type")
7884
}
7985
}
8086

@@ -91,6 +97,8 @@ func IntergerSignedLowerBound(intType byte) int64 {
9197
return math.MinInt32
9298
case mysql.TypeLonglong:
9399
return math.MinInt64
100+
case mysql.TypeEnum:
101+
return 0
94102
default:
95103
panic("Input byte is not a mysql type")
96104
}

0 commit comments

Comments
 (0)