github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/tidbparser/dependency/mysql/util.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package mysql 15 16 type lengthAndDecimal struct { 17 length int 18 decimal int 19 } 20 21 // defaultLengthAndDecimal provides default Flen and Decimal for fields 22 // from CREATE TABLE when they are unspecified. 23 var defaultLengthAndDecimal = map[byte]lengthAndDecimal{ 24 TypeBit: {1, 0}, 25 TypeTiny: {4, 0}, 26 TypeShort: {6, 0}, 27 TypeInt24: {9, 0}, 28 TypeLong: {11, 0}, 29 TypeLonglong: {20, 0}, 30 TypeDouble: {22, -1}, 31 TypeFloat: {12, -1}, 32 TypeNewDecimal: {11, 0}, 33 TypeDuration: {10, 0}, 34 TypeDate: {10, 0}, 35 TypeTimestamp: {19, 0}, 36 TypeDatetime: {19, 0}, 37 TypeYear: {4, 0}, 38 TypeString: {1, 0}, 39 TypeVarchar: {5, 0}, 40 TypeVarString: {5, 0}, 41 TypeTinyBlob: {255, 0}, 42 TypeBlob: {65535, 0}, 43 TypeMediumBlob: {16777215, 0}, 44 TypeLongBlob: {4294967295, 0}, 45 TypeJSON: {4294967295, 0}, 46 TypeNull: {0, 0}, 47 TypeSet: {-1, 0}, 48 TypeEnum: {-1, 0}, 49 } 50 51 // IsIntegerType indicate whether tp is an integer type. 52 func IsIntegerType(tp byte) bool { 53 switch tp { 54 case TypeTiny, TypeShort, TypeInt24, TypeLong, TypeLonglong: 55 return true 56 } 57 return false 58 } 59 60 // GetDefaultFieldLengthAndDecimal returns the default display length (flen) and decimal length for column. 61 // Call this when no Flen assigned in ddl. 62 // or column value is calculated from an expression. 63 // For example: "select count(*) from t;", the column type is int64 and Flen in ResultField will be 21. 64 // See https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html 65 func GetDefaultFieldLengthAndDecimal(tp byte) (flen int, decimal int) { 66 val, ok := defaultLengthAndDecimal[tp] 67 if ok { 68 return val.length, val.decimal 69 } 70 return -1, -1 71 } 72 73 // defaultLengthAndDecimal provides default Flen and Decimal for fields 74 // from CAST when they are unspecified. 75 var defaultLengthAndDecimalForCast = map[byte]lengthAndDecimal{ 76 TypeString: {0, -1}, // Flen & Decimal differs. 77 TypeDate: {10, 0}, 78 TypeDatetime: {19, 0}, 79 TypeNewDecimal: {11, 0}, 80 TypeDuration: {10, 0}, 81 TypeLonglong: {22, 0}, 82 TypeJSON: {4194304, 0}, // Flen differs. 83 } 84 85 // GetDefaultFieldLengthAndDecimalForCast returns the default display length (flen) and decimal length for casted column 86 // when flen or decimal is not specified. 87 func GetDefaultFieldLengthAndDecimalForCast(tp byte) (flen int, decimal int) { 88 val, ok := defaultLengthAndDecimalForCast[tp] 89 if ok { 90 return val.length, val.decimal 91 } 92 return -1, -1 93 }