github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/etc.go (about) 1 // Copyright 2014 The ql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSES/QL-LICENSE file. 4 5 // Copyright 2015 PingCAP, Inc. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package types 19 20 import ( 21 "io" 22 23 "github.com/pingcap/errors" 24 25 "github.com/XiaoMi/Gaea/mysql" 26 "github.com/XiaoMi/Gaea/parser/opcode" 27 "github.com/XiaoMi/Gaea/parser/terror" 28 ast "github.com/XiaoMi/Gaea/parser/types" 29 ) 30 31 // IsTypeBlob returns a boolean indicating whether the tp is a blob type. 32 var IsTypeBlob = ast.IsTypeBlob 33 34 // IsTypeChar returns a boolean indicating 35 // whether the tp is the char type like a string type or a varchar type. 36 var IsTypeChar = ast.IsTypeChar 37 38 // IsTypeVarchar returns a boolean indicating 39 // whether the tp is the varchar type like a varstring type or a varchar type. 40 func IsTypeVarchar(tp byte) bool { 41 return tp == mysql.TypeVarString || tp == mysql.TypeVarchar 42 } 43 44 // IsTypeUnspecified returns a boolean indicating whether the tp is the Unspecified type. 45 func IsTypeUnspecified(tp byte) bool { 46 return tp == mysql.TypeUnspecified 47 } 48 49 // IsTypePrefixable returns a boolean indicating 50 // whether an index on a column with the tp can be defined with a prefix. 51 func IsTypePrefixable(tp byte) bool { 52 return IsTypeBlob(tp) || IsTypeChar(tp) 53 } 54 55 // IsTypeFractionable returns a boolean indicating 56 // whether the tp can has time fraction. 57 func IsTypeFractionable(tp byte) bool { 58 return tp == mysql.TypeDatetime || tp == mysql.TypeDuration || tp == mysql.TypeTimestamp 59 } 60 61 // IsTypeTime returns a boolean indicating 62 // whether the tp is time type like datetime, date or timestamp. 63 func IsTypeTime(tp byte) bool { 64 return tp == mysql.TypeDatetime || tp == mysql.TypeDate || tp == mysql.TypeTimestamp 65 } 66 67 // IsTypeNumeric returns a boolean indicating whether the tp is numeric type. 68 func IsTypeNumeric(tp byte) bool { 69 switch tp { 70 case mysql.TypeBit, mysql.TypeTiny, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeNewDecimal, 71 mysql.TypeDecimal, mysql.TypeFloat, mysql.TypeDouble, mysql.TypeShort: 72 return true 73 } 74 return false 75 } 76 77 // IsTemporalWithDate returns a boolean indicating 78 // whether the tp is time type with date. 79 func IsTemporalWithDate(tp byte) bool { 80 return IsTypeTime(tp) 81 } 82 83 // IsBinaryStr returns a boolean indicating 84 // whether the field type is a binary string type. 85 func IsBinaryStr(ft *FieldType) bool { 86 if ft.Collate == mysql.CollationBin && IsString(ft.Tp) { 87 return true 88 } 89 return false 90 } 91 92 // IsNonBinaryStr returns a boolean indicating 93 // whether the field type is a non-binary string type. 94 func IsNonBinaryStr(ft *FieldType) bool { 95 if ft.Collate != mysql.CollationBin && IsString(ft.Tp) { 96 return true 97 } 98 return false 99 } 100 101 // IsString returns a boolean indicating 102 // whether the field type is a string type. 103 func IsString(tp byte) bool { 104 return IsTypeChar(tp) || IsTypeBlob(tp) || IsTypeVarchar(tp) || IsTypeUnspecified(tp) 105 } 106 107 var kind2Str = map[byte]string{ 108 KindNull: "null", 109 KindInt64: "bigint", 110 KindUint64: "unsigned bigint", 111 KindFloat32: "float", 112 KindFloat64: "double", 113 KindString: "char", 114 KindBytes: "bytes", 115 KindBinaryLiteral: "bit/hex literal", 116 KindMysqlDecimal: "decimal", 117 KindMysqlDuration: "time", 118 KindMysqlEnum: "enum", 119 KindMysqlBit: "bit", 120 KindMysqlSet: "set", 121 KindMysqlTime: "datetime", 122 KindInterface: "interface", 123 KindMinNotNull: "min_not_null", 124 KindMaxValue: "max_value", 125 KindRaw: "raw", 126 KindMysqlJSON: "json", 127 } 128 129 // TypeStr converts tp to a string. 130 var TypeStr = ast.TypeStr 131 132 // KindStr converts kind to a string. 133 func KindStr(kind byte) (r string) { 134 return kind2Str[kind] 135 } 136 137 // TypeToStr converts a field to a string. 138 // It is used for converting Text to Blob, 139 // or converting Char to Binary. 140 // Args: 141 // tp: type enum 142 // cs: charset 143 var TypeToStr = ast.TypeToStr 144 145 // EOFAsNil filtrates errors, 146 // If err is equal to io.EOF returns nil. 147 func EOFAsNil(err error) error { 148 if terror.ErrorEqual(err, io.EOF) { 149 return nil 150 } 151 return errors.Trace(err) 152 } 153 154 // InvOp2 returns an invalid operation error. 155 func InvOp2(x, y interface{}, o opcode.Op) (interface{}, error) { 156 return nil, errors.Errorf("Invalid operation: %v %v %v (mismatched types %T and %T)", x, o, y, x, y) 157 } 158 159 // overflow returns an overflowed error. 160 func overflow(v interface{}, tp byte) error { 161 return ErrOverflow.GenWithStack("constant %v overflows %s", v, TypeStr(tp)) 162 } 163 164 // IsTypeTemporal checks if a type is a temporal type. 165 func IsTypeTemporal(tp byte) bool { 166 switch tp { 167 case mysql.TypeDuration, mysql.TypeDatetime, mysql.TypeTimestamp, 168 mysql.TypeDate, mysql.TypeNewDate: 169 return true 170 } 171 return false 172 }