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

expression: return upper bound for enum #41021

Merged
merged 4 commits into from
Feb 11, 2023
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is MaxUint64 in IntergerUnsignedUpperBound. Besides, do we need to cover IntergerSignedLowerBound?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I also modify it to 65536. I think 65536 is a more reasonable value.

Besides, do we need to cover IntergerSignedLowerBound?

It would be better to set it to zero 🤔, though I don't know whether it's reachable.

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