tidb icon indicating copy to clipboard operation
tidb copied to clipboard

cannot use 1 << iota (untyped int constant 2147483648) as SQLMode value in constant declaration (overflows)

Open xyproto opened this issue 1 year ago • 3 comments

Hi,

tidb does not compile for ARM 6 or 7, for instance when building with GOARM=7 GOOS=linux GOARCH=arm go build:

# github.com/pingcap/tidb/pkg/parser/mysql
vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go:462:2: cannot use 1 << iota (untyped int constant 2147483648) as SQLMode value in constant declaration (overflows)
vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go:463:2: cannot use 1 << iota (untyped int constant 4294967296) as SQLMode value in constant declaration (overflows)
vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go:44:19: cannot use 4294967295 (untyped int constant) as int value in struct literal (overflows)
vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go:45:19: cannot use 4294967295 (untyped int constant) as int value in struct literal (overflows)

After making these changes, tidb compiles again, but I am not sure that it keeps the correct behavior:

diff --git a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go
index c9862fb5..768355f8 100644
--- a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go
+++ b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go
@@ -16,6 +16,7 @@ package mysql
 import (
        "fmt"
        "strings"
+       "math"

        "github.com/pingcap/errors"
        "github.com/pingcap/tidb/pkg/parser/format"
@@ -428,7 +429,7 @@ func SetSQLMode(ori SQLMode, add SQLMode) SQLMode {
 // consts for sql modes.
 // see https://dev.mysql.com/doc/internals/en/query-event.html#q-sql-mode-code
 const (
-       ModeRealAsFloat SQLMode = 1 << iota
+       ModeRealAsFloat SQLMode = math.MaxUint16
        ModePipesAsConcat
        ModeANSIQuotes
        ModeIgnoreSpace
diff --git a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go
index c69e2903..a2c0ac70 100644
--- a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go
+++ b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go
@@ -13,6 +13,8 @@

 package mysql

+import "math"
+
 type lengthAndDecimal struct {
        length  int
        decimal int
@@ -41,8 +43,8 @@ var defaultLengthAndDecimal = map[byte]lengthAndDecimal{
        TypeTinyBlob:   {255, 0},
        TypeBlob:       {65535, 0},
        TypeMediumBlob: {16777215, 0},
-       TypeLongBlob:   {4294967295, 0},
-       TypeJSON:       {4294967295, 0},
+       TypeLongBlob:   {math.MaxUint16, 0},
+       TypeJSON:       {math.MaxUint16, 0},
        TypeNull:       {0, 0},
        TypeSet:        {-1, 0},
        TypeEnum:       {-1, 0},

8.0.11-TiDB-None

xyproto avatar Jun 26 '24 15:06 xyproto

cannot use 1 << iota (untyped int constant 2147483648) as SQLMode value in constant declaration (overflows)

Maybe it's better to change the underlying type SQLMode from int to int64.

-       ModeRealAsFloat SQLMode = 1 << iota
+       ModeRealAsFloat SQLMode = math.MaxUint16

iota has special meaning, it should not be changed

https://go.dev/ref/spec#Iota

lance6716 avatar Jun 27 '24 02:06 lance6716

Sure, I have no opinion on what the best fix is.

xyproto avatar Jun 27 '24 08:06 xyproto

Perhaps something like this, then:

diff --git a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go
index c9862fb5..5ef91133 100644
--- a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go
+++ b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/const.go
@@ -338,7 +338,7 @@ var DefaultLengthOfTimeFraction = map[int]int{

// SQLMode is the type for MySQL sql_mode.
// See https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
-type SQLMode int
+type SQLMode int64

// HasNoZeroDateMode detects if 'NO_ZERO_DATE' mode is set in SQLMode
func (m SQLMode) HasNoZeroDateMode() bool {
diff --git a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go
index c69e2903..742b17ff 100644
--- a/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go
+++ b/vendor/github.com/pingcap/tidb/pkg/parser/mysql/util.go
@@ -14,7 +14,7 @@
package mysql

type lengthAndDecimal struct {
-       length  int
+       length  int64
decimal int
}

@@ -62,7 +62,7 @@ func IsIntegerType(tp byte) bool {
// or column value is calculated from an expression.
// For example: "select count(*) from t;", the column type is int64 and flen in ResultField will be 21.
// See https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html
-func GetDefaultFieldLengthAndDecimal(tp byte) (flen int, decimal int) {
+func GetDefaultFieldLengthAndDecimal(tp byte) (flen int64, decimal int) {
val, ok := defaultLengthAndDecimal[tp]
if ok {
return val.length, val.decimal
@@ -86,7 +86,7 @@ var defaultLengthAndDecimalForCast = map[byte]lengthAndDecimal{

// GetDefaultFieldLengthAndDecimalForCast returns the default display length (flen) and decimal length for casted column
// when flen or decimal is not specified.
-func GetDefaultFieldLengthAndDecimalForCast(tp byte) (flen int, decimal int) {
+func GetDefaultFieldLengthAndDecimalForCast(tp byte) (flen int64, decimal int) {
val, ok := defaultLengthAndDecimalForCast[tp]
if ok {
return val.length, val.decimal

xyproto avatar Jun 27 '24 09:06 xyproto

Created a small PR.

xyproto avatar Aug 06 '24 14:08 xyproto