github.com/matrixorigin/matrixone@v1.2.0/pkg/defines/type.go (about) 1 // Copyright 2021 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package defines 16 17 import ( 18 "context" 19 "math" 20 "sync" 21 22 "github.com/matrixorigin/matrixone/pkg/common/moerr" 23 ) 24 25 // information from: https://dev.mysql.com/doc/internals/en/com-query-response.html 26 // also in mysql 8.0.23 source code : include/field_types.h 27 28 type MysqlType uint8 29 30 const ( 31 MYSQL_TYPE_DECIMAL MysqlType = 0x00 //lenenc_str 32 MYSQL_TYPE_TINY MysqlType = 0x01 //int<1> int8 33 MYSQL_TYPE_SHORT MysqlType = 0x02 //int<2> int16 34 MYSQL_TYPE_LONG MysqlType = 0x03 //int<4> int32 35 MYSQL_TYPE_FLOAT MysqlType = 0x04 //(string.fix_len) -- (len=4) float 36 MYSQL_TYPE_DOUBLE MysqlType = 0x05 //(string.fix_len) -- (len=8) double 37 MYSQL_TYPE_NULL MysqlType = 0x06 //Text ResultSet: 0xFB; Binary ResultSet: The binary protocol sends NULL values as bits inside a bitmap instead of a full byte 38 MYSQL_TYPE_TIMESTAMP MysqlType = 0x07 // 39 MYSQL_TYPE_LONGLONG MysqlType = 0x08 //int<8> int64 40 MYSQL_TYPE_INT24 MysqlType = 0x09 //int<4> int32 41 MYSQL_TYPE_DATE MysqlType = 0x0a // 42 MYSQL_TYPE_TIME MysqlType = 0x0b 43 MYSQL_TYPE_DATETIME MysqlType = 0x0c 44 MYSQL_TYPE_YEAR MysqlType = 0x0d //int<2> int16 45 MYSQL_TYPE_NEWDATE MysqlType = 0x0e /**< Internal to MySQL. Not used in protocol */ 46 MYSQL_TYPE_VARCHAR MysqlType = 0x0f //lenenc_str 47 MYSQL_TYPE_BIT MysqlType = 0x10 //lenenc_str 48 MYSQL_TYPE_TIMESTAMP2 MysqlType = 0x11 // 49 MYSQL_TYPE_DATETIME2 MysqlType = 0x12 /**< Internal to MySQL. Not used in protocol */ 50 MYSQL_TYPE_TIME2 MysqlType = 0x13 /**< Internal to MySQL. Not used in protocol */ 51 MYSQL_TYPE_TYPED_ARRAY MysqlType = 0x14 /**< Used for replication only */ 52 53 MYSQL_TYPE_TEXT MysqlType = 241 // add text to distinct blob and blob 54 MYSQL_TYPE_INVALID MysqlType = 242 55 MYSQL_TYPE_UUID MysqlType = 243 56 MYSQL_TYPE_BOOL MysqlType = 244 /**< Currently just a placeholder */ 57 MYSQL_TYPE_JSON MysqlType = 0xf5 58 MYSQL_TYPE_NEWDECIMAL MysqlType = 0xf6 59 MYSQL_TYPE_ENUM MysqlType = 0xf7 60 MYSQL_TYPE_SET MysqlType = 0xf8 61 MYSQL_TYPE_TINY_BLOB MysqlType = 0xf9 62 MYSQL_TYPE_MEDIUM_BLOB MysqlType = 0xfa 63 MYSQL_TYPE_LONG_BLOB MysqlType = 0xfb 64 MYSQL_TYPE_BLOB MysqlType = 0xfc 65 MYSQL_TYPE_VAR_STRING MysqlType = 0xfd //lenenc_str 66 MYSQL_TYPE_STRING MysqlType = 0xfe //lenenc_str 67 MYSQL_TYPE_GEOMETRY MysqlType = 0xff 68 ) 69 70 func (typ *MysqlType) GetLength(width int32) uint32 { 71 switch *typ { 72 case MYSQL_TYPE_DECIMAL: 73 return uint32(width) + 2 74 case MYSQL_TYPE_BOOL: 75 return 1 76 case MYSQL_TYPE_BIT: 77 return uint32(width) 78 case MYSQL_TYPE_TINY: 79 return 8 80 case MYSQL_TYPE_SHORT: 81 return 16 82 case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24: 83 return 32 84 case MYSQL_TYPE_LONGLONG: 85 return 64 86 case MYSQL_TYPE_FLOAT: 87 if width != 32 && width != 0 { 88 return uint32(width) 89 } 90 return 23 91 case MYSQL_TYPE_DOUBLE: 92 if width != 64 && width != 0 { 93 return uint32(width) 94 } 95 return 53 96 case MYSQL_TYPE_VARCHAR, MYSQL_TYPE_STRING, MYSQL_TYPE_BLOB, MYSQL_TYPE_TEXT: 97 return uint32(width) * 3 98 case MYSQL_TYPE_DATE: 99 return 64 100 case MYSQL_TYPE_TIME: 101 return 64 102 case MYSQL_TYPE_DATETIME: 103 return 64 104 case MYSQL_TYPE_TIMESTAMP: 105 return 64 106 case MYSQL_TYPE_JSON: 107 return math.MaxUint32 108 default: 109 return math.MaxUint32 110 } 111 } 112 113 // flags 114 // in mysql 8.0.23 source code : include/mysql_com.h 115 const ( 116 NOT_NULL_FLAG uint32 = 1 << 0 /**< Field can't be NULL */ 117 PRI_KEY_FLAG uint32 = 1 << 1 /**< Field is part of a primary key */ 118 UNIQUE_KEY_FLAG uint32 = 1 << 2 /**< Field is part of a unique key */ 119 MULTIPLE_KEY_FLAG uint32 = 1 << 3 /**< Field is part of a key */ 120 BLOB_FLAG uint32 = 1 << 4 /**< Field is a blob */ 121 UNSIGNED_FLAG uint32 = 1 << 5 /**< Field is unsigned */ 122 ZEROFILL_FLAG uint32 = 1 << 6 /**< Field is zerofill */ 123 BINARY_FLAG uint32 = 1 << 7 /**< Field is binary */ 124 125 /* The following are only sent to new clients */ 126 ENUM_FLAG uint32 = 1 << 8 /**< field is an enum */ 127 AUTO_INCREMENT_FLAG uint32 = 1 << 9 /**< field is a autoincrement field */ 128 TIMESTAMP_FLAG uint32 = 1 << 10 /**< Field is a timestamp */ 129 SET_FLAG uint32 = 1 << 11 /**< field is a set */ 130 NO_DEFAULT_VALUE_FLAG uint32 = 1 << 12 /**< Field doesn't have default value */ 131 ON_UPDATE_NOW_FLAG uint32 = 1 << 13 /**< Field is set to NOW on UPDATE */ 132 NUM_FLAG uint32 = 1 << 15 /**< Field is num (for clients) */ 133 PART_KEY_FLAG uint32 = 1 << 14 /**< Intern; Part of some key */ 134 GROUP_FLAG uint32 = 1 << 15 /**< Intern: Group field */ 135 UNIQUE_FLAG uint32 = 1 << 16 /**< Intern: Used by sql_yacc */ 136 BINCMP_FLAG uint32 = 1 << 17 /**< Intern: Used by sql_yacc */ 137 GET_FIXED_FIELDS_FLAG uint32 = 1 << 18 /**< Used to get fields in item tree */ 138 FIELD_IN_PART_FUNC_FLAG uint32 = 1 << 19 /**< Field part of partition func */ 139 /** 140 Intern: Field in TABLE object for new version of altered table, 141 which participates in a newly added index. 142 */ 143 FIELD_IN_ADD_INDEX uint32 = (1 << 20) 144 FIELD_IS_RENAMED uint32 = (1 << 21) /**< Intern: Field is being renamed */ 145 FIELD_FLAGS_STORAGE_MEDIA uint32 = 22 /**< Field storage media, bit 22-23 */ 146 FIELD_FLAGS_STORAGE_MEDIA_MASK uint32 = (3 << FIELD_FLAGS_STORAGE_MEDIA) 147 FIELD_FLAGS_COLUMN_FORMAT uint32 = 24 /**< Field column format, bit 24-25 */ 148 FIELD_FLAGS_COLUMN_FORMAT_MASK uint32 = (3 << FIELD_FLAGS_COLUMN_FORMAT) 149 FIELD_IS_DROPPED uint32 = (1 << 26) /**< Intern: Field is being dropped */ 150 EXPLICIT_NULL_FLAG uint32 = (1 << 27) /**< Field is explicitly specified as NULL by the user */ 151 FIELD_IS_MARKED uint32 = (1 << 28) /**< Intern: field is marked, general purpose */ 152 153 /** Field will not be loaded in secondary engine. */ 154 NOT_SECONDARY_FLAG uint32 = (1 << 29) 155 /** Field is explicitly marked as invisible by the user. */ 156 FIELD_IS_INVISIBLE uint32 = (1 << 30) 157 ) 158 159 // TypeInt24 bounds. 160 const ( 161 MaxUint24 = 1<<24 - 1 162 MaxInt24 = 1<<23 - 1 163 MinInt24 = -1 << 23 164 ) 165 166 // use TenantIDKey{} UserIDKey{} RoleIDKey{} to get uint32 value from Context 167 168 type TenantIDKey struct{} 169 type UserIDKey struct{} 170 type RoleIDKey struct{} 171 type NodeIDKey struct{} 172 173 func GetAccountId(ctx context.Context) (uint32, error) { 174 if v := ctx.Value(TenantIDKey{}); v != nil { 175 return v.(uint32), nil 176 } else { 177 return 0, moerr.NewInternalError(ctx, "no account id in context") 178 } 179 } 180 181 func GetUserId(ctx context.Context) uint32 { 182 if v := ctx.Value(UserIDKey{}); v != nil { 183 return v.(uint32) 184 } 185 //zero means root user 186 return 0 187 } 188 189 func GetRoleId(ctx context.Context) uint32 { 190 if v := ctx.Value(RoleIDKey{}); v != nil { 191 return v.(uint32) 192 } 193 //zero means mo admin role 194 return 0 195 } 196 197 func AttachAccount(ctx context.Context, accId uint32, userId uint32, roleId uint32) context.Context { 198 return AttachRoleId(AttachUserId(AttachAccountId(ctx, accId), userId), roleId) 199 } 200 201 func AttachAccountId(ctx context.Context, accId uint32) context.Context { 202 return context.WithValue(ctx, TenantIDKey{}, accId) 203 } 204 205 func AttachUserId(ctx context.Context, userId uint32) context.Context { 206 return context.WithValue(ctx, UserIDKey{}, userId) 207 } 208 209 func AttachRoleId(ctx context.Context, roleId uint32) context.Context { 210 return context.WithValue(ctx, RoleIDKey{}, roleId) 211 } 212 213 // EngineKey use EngineKey{} to get engine from Context 214 type EngineKey struct{} 215 216 // SqlKey use SqlKey{} to get string value from Context 217 type SqlKey struct{} 218 type DatTypKey struct{} 219 220 // CarryOnCtxKeys defines keys needed to be serialized when pass context through net 221 var CarryOnCtxKeys = []any{TenantIDKey{}, UserIDKey{}, RoleIDKey{}} 222 223 // TemporaryTN use TemporaryTN to get temporary storage from Context 224 type TemporaryTN struct{} 225 226 type IsMoLogger struct{} 227 228 type SourceScanResKey struct{} 229 230 // Determine if now is a bg sql. 231 type BgKey struct{} 232 233 // Sp variable scope 234 type VarScopeKey struct{} 235 236 // Determine if it is a stored procedure 237 type InSp struct{} 238 239 // PkCheckByTN whether TN does primary key uniqueness check against transaction's workspace or not. 240 type PkCheckByTN struct{} 241 242 // StartTS is the start timestamp of a statement. 243 type StartTS struct{} 244 245 /* 246 The autoIncrCacheManager is initialized with a starting CN. 247 The autoIncrCacheManager instance of each CN is stored in type service in package cnservice. 248 The logic to manipulate the cache is in auto_incr.go 249 */ 250 type AutoIncrCacheManager struct { 251 Mu *sync.Mutex 252 AutoIncrCaches map[string]AutoIncrCache 253 MaxSize uint64 254 } 255 256 type AutoIncrCache struct { 257 CurNum uint64 258 MaxNum uint64 259 Step uint64 260 }