github.com/matrixorigin/matrixone@v0.7.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  	"math"
    19  	"sync"
    20  )
    21  
    22  // information from: https://dev.mysql.com/doc/internals/en/com-query-response.html
    23  // also in mysql 8.0.23 source code : include/field_types.h
    24  
    25  type MysqlType uint8
    26  
    27  const (
    28  	MYSQL_TYPE_DECIMAL     MysqlType = 0x00 //lenenc_str
    29  	MYSQL_TYPE_TINY        MysqlType = 0x01 //int<1> int8
    30  	MYSQL_TYPE_SHORT       MysqlType = 0x02 //int<2> int16
    31  	MYSQL_TYPE_LONG        MysqlType = 0x03 //int<4> int32
    32  	MYSQL_TYPE_FLOAT       MysqlType = 0x04 //(string.fix_len) -- (len=4) float
    33  	MYSQL_TYPE_DOUBLE      MysqlType = 0x05 //(string.fix_len) -- (len=8) double
    34  	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
    35  	MYSQL_TYPE_TIMESTAMP   MysqlType = 0x07 //
    36  	MYSQL_TYPE_LONGLONG    MysqlType = 0x08 //int<8> int64
    37  	MYSQL_TYPE_INT24       MysqlType = 0x09 //int<4> int32
    38  	MYSQL_TYPE_DATE        MysqlType = 0x0a //
    39  	MYSQL_TYPE_TIME        MysqlType = 0x0b
    40  	MYSQL_TYPE_DATETIME    MysqlType = 0x0c
    41  	MYSQL_TYPE_YEAR        MysqlType = 0x0d //int<2> int16
    42  	MYSQL_TYPE_NEWDATE     MysqlType = 0x0e /**< Internal to MySQL. Not used in protocol */
    43  	MYSQL_TYPE_VARCHAR     MysqlType = 0x0f //lenenc_str
    44  	MYSQL_TYPE_BIT         MysqlType = 0x10 //lenenc_str
    45  	MYSQL_TYPE_TIMESTAMP2  MysqlType = 0x11 //
    46  	MYSQL_TYPE_DATETIME2   MysqlType = 0x12 /**< Internal to MySQL. Not used in protocol */
    47  	MYSQL_TYPE_TIME2       MysqlType = 0x13 /**< Internal to MySQL. Not used in protocol */
    48  	MYSQL_TYPE_TYPED_ARRAY MysqlType = 0x14 /**< Used for replication only */
    49  
    50  	MYSQL_TYPE_TEXT        MysqlType = 241 // add text to distinct blob and blob
    51  	MYSQL_TYPE_INVALID     MysqlType = 242
    52  	MYSQL_TYPE_UUID        MysqlType = 243
    53  	MYSQL_TYPE_BOOL        MysqlType = 244 /**< Currently just a placeholder */
    54  	MYSQL_TYPE_JSON        MysqlType = 0xf5
    55  	MYSQL_TYPE_NEWDECIMAL  MysqlType = 0xf6
    56  	MYSQL_TYPE_ENUM        MysqlType = 0xf7
    57  	MYSQL_TYPE_SET         MysqlType = 0xf8
    58  	MYSQL_TYPE_TINY_BLOB   MysqlType = 0xf9
    59  	MYSQL_TYPE_MEDIUM_BLOB MysqlType = 0xfa
    60  	MYSQL_TYPE_LONG_BLOB   MysqlType = 0xfb
    61  	MYSQL_TYPE_BLOB        MysqlType = 0xfc
    62  	MYSQL_TYPE_VAR_STRING  MysqlType = 0xfd //lenenc_str
    63  	MYSQL_TYPE_STRING      MysqlType = 0xfe //lenenc_str
    64  	MYSQL_TYPE_GEOMETRY    MysqlType = 0xff
    65  )
    66  
    67  func (typ *MysqlType) GetLength(width int32) uint32 {
    68  	switch *typ {
    69  	case MYSQL_TYPE_DECIMAL:
    70  		return uint32(width)
    71  	case MYSQL_TYPE_BOOL:
    72  		return 1
    73  	case MYSQL_TYPE_TINY:
    74  		return 8
    75  	case MYSQL_TYPE_SHORT:
    76  		return 16
    77  	case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
    78  		return 32
    79  	case MYSQL_TYPE_LONGLONG:
    80  		return 64
    81  	case MYSQL_TYPE_FLOAT:
    82  		return 32
    83  	case MYSQL_TYPE_DOUBLE:
    84  		return 64
    85  	case MYSQL_TYPE_VARCHAR, MYSQL_TYPE_STRING, MYSQL_TYPE_BLOB, MYSQL_TYPE_TEXT:
    86  		return uint32(width) * 3
    87  	case MYSQL_TYPE_DATE:
    88  		return 64
    89  	case MYSQL_TYPE_TIME:
    90  		return 64
    91  	case MYSQL_TYPE_DATETIME:
    92  		return 64
    93  	case MYSQL_TYPE_TIMESTAMP:
    94  		return 64
    95  	case MYSQL_TYPE_JSON:
    96  		return math.MaxUint32
    97  	default:
    98  		return math.MaxUint32
    99  	}
   100  }
   101  
   102  // flags
   103  // in mysql 8.0.23 source code : include/mysql_com.h
   104  const (
   105  	NOT_NULL_FLAG     uint32 = 1 << 0 /**< Field can't be NULL */
   106  	PRI_KEY_FLAG      uint32 = 1 << 1 /**< Field is part of a primary key */
   107  	UNIQUE_KEY_FLAG   uint32 = 1 << 2 /**< Field is part of a unique key */
   108  	MULTIPLE_KEY_FLAG uint32 = 1 << 3 /**< Field is part of a key */
   109  	BLOB_FLAG         uint32 = 1 << 4 /**< Field is a blob */
   110  	UNSIGNED_FLAG     uint32 = 1 << 5 /**< Field is unsigned */
   111  	ZEROFILL_FLAG     uint32 = 1 << 6 /**< Field is zerofill */
   112  	BINARY_FLAG       uint32 = 1 << 7 /**< Field is binary   */
   113  
   114  	/* The following are only sent to new clients */
   115  	ENUM_FLAG               uint32 = 1 << 8  /**< field is an enum */
   116  	AUTO_INCREMENT_FLAG     uint32 = 1 << 9  /**< field is a autoincrement field */
   117  	TIMESTAMP_FLAG          uint32 = 1 << 10 /**< Field is a timestamp */
   118  	SET_FLAG                uint32 = 1 << 11 /**< field is a set */
   119  	NO_DEFAULT_VALUE_FLAG   uint32 = 1 << 12 /**< Field doesn't have default value */
   120  	ON_UPDATE_NOW_FLAG      uint32 = 1 << 13 /**< Field is set to NOW on UPDATE */
   121  	NUM_FLAG                uint32 = 1 << 15 /**< Field is num (for clients) */
   122  	PART_KEY_FLAG           uint32 = 1 << 14 /**< Intern; Part of some key */
   123  	GROUP_FLAG              uint32 = 1 << 15 /**< Intern: Group field */
   124  	UNIQUE_FLAG             uint32 = 1 << 16 /**< Intern: Used by sql_yacc */
   125  	BINCMP_FLAG             uint32 = 1 << 17 /**< Intern: Used by sql_yacc */
   126  	GET_FIXED_FIELDS_FLAG   uint32 = 1 << 18 /**< Used to get fields in item tree */
   127  	FIELD_IN_PART_FUNC_FLAG uint32 = 1 << 19 /**< Field part of partition func */
   128  	/**
   129  	Intern: Field in TABLE object for new version of altered table,
   130  		  which participates in a newly added index.
   131  	*/
   132  	FIELD_IN_ADD_INDEX             uint32 = (1 << 20)
   133  	FIELD_IS_RENAMED               uint32 = (1 << 21) /**< Intern: Field is being renamed */
   134  	FIELD_FLAGS_STORAGE_MEDIA      uint32 = 22        /**< Field storage media, bit 22-23 */
   135  	FIELD_FLAGS_STORAGE_MEDIA_MASK uint32 = (3 << FIELD_FLAGS_STORAGE_MEDIA)
   136  	FIELD_FLAGS_COLUMN_FORMAT      uint32 = 24 /**< Field column format, bit 24-25 */
   137  	FIELD_FLAGS_COLUMN_FORMAT_MASK uint32 = (3 << FIELD_FLAGS_COLUMN_FORMAT)
   138  	FIELD_IS_DROPPED               uint32 = (1 << 26) /**< Intern: Field is being dropped */
   139  	EXPLICIT_NULL_FLAG             uint32 = (1 << 27) /**< Field is explicitly specified as NULL by the user */
   140  	FIELD_IS_MARKED                uint32 = (1 << 28) /**< Intern: field is marked, general purpose */
   141  
   142  	/** Field will not be loaded in secondary engine. */
   143  	NOT_SECONDARY_FLAG uint32 = (1 << 29)
   144  	/** Field is explicitly marked as invisible by the user. */
   145  	FIELD_IS_INVISIBLE uint32 = (1 << 30)
   146  )
   147  
   148  // TypeInt24 bounds.
   149  const (
   150  	MaxUint24 = 1<<24 - 1
   151  	MaxInt24  = 1<<23 - 1
   152  	MinInt24  = -1 << 23
   153  )
   154  
   155  // use TenantIDKey{} UserIDKey{} RoleIDKey{} to get uint32 value from Context
   156  
   157  type TenantIDKey struct{}
   158  type UserIDKey struct{}
   159  type RoleIDKey struct{}
   160  
   161  // EngineKey use EngineKey{} to get engine from Context
   162  type EngineKey struct{}
   163  
   164  // SqlKey use SqlKey{} to get string value from Context
   165  type SqlKey struct{}
   166  
   167  // CarryOnCtxKeys defines keys needed to be serialized when pass context through net
   168  var CarryOnCtxKeys = []any{TenantIDKey{}, UserIDKey{}, RoleIDKey{}}
   169  
   170  // TemporaryDN use TemporaryDN to get temporary storage from Context
   171  type TemporaryDN struct{}
   172  
   173  type AutoIncrCaches struct {
   174  	Mu             *sync.Mutex
   175  	AutoIncrCaches map[string]AutoIncrCache
   176  }
   177  
   178  type AutoIncrCache struct {
   179  	CurNum uint64
   180  	MaxNum uint64
   181  	Step   uint64
   182  }