github.com/dolthub/go-mysql-server@v0.18.0/sql/types/typecheck.go (about) 1 // Copyright 2022 Dolthub, 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 // 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 types 16 17 import ( 18 "github.com/dolthub/vitess/go/sqltypes" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 ) 22 23 // IsBlobType checks if t is BLOB 24 func IsBlobType(t sql.Type) bool { 25 if t == nil { 26 return false 27 } 28 switch t.Type() { 29 case sqltypes.Blob: 30 return true 31 default: 32 return false 33 } 34 } 35 36 // IsBinaryType checks if t is BINARY, VARBINARY, or BLOB 37 func IsBinaryType(t sql.Type) bool { 38 if t == nil { 39 return false 40 } 41 switch t.Type() { 42 case sqltypes.Binary, sqltypes.VarBinary, sqltypes.Blob, sqltypes.TypeJSON, sqltypes.Geometry: 43 return true 44 default: 45 return false 46 } 47 } 48 49 // IsDecimal checks if t is a DECIMAL type. 50 func IsDecimal(t sql.Type) bool { 51 _, ok := t.(DecimalType_) 52 return ok 53 } 54 55 // IsBit checks if t is a BIT type. 56 func IsBit(t sql.Type) bool { 57 _, ok := t.(BitType_) 58 return ok 59 } 60 61 // IsFloat checks if t is float type. 62 func IsFloat(t sql.Type) bool { 63 return t == Float32 || t == Float64 64 } 65 66 // IsInteger checks if t is an integer type. 67 func IsInteger(t sql.Type) bool { 68 return IsSigned(t) || IsUnsigned(t) 69 } 70 71 // IsJSON returns true if the specified type is a JSON type. 72 func IsJSON(t sql.Type) bool { 73 _, ok := t.(JsonType) 74 return ok 75 } 76 77 // IsGeometry returns true if the specified type is a Geometry type. 78 func IsGeometry(t sql.Type) bool { 79 switch t.(type) { 80 case GeometryType, PointType, LineStringType, PolygonType: 81 return true 82 default: 83 return false 84 } 85 } 86 87 // IsNull returns true if expression is nil or is Null Type, otherwise false. 88 func IsNull(ex sql.Expression) bool { 89 return ex == nil || ex.Type() == Null 90 } 91 92 // IsNumber checks if t is a number type 93 func IsNumber(t sql.Type) bool { 94 switch t.(type) { 95 case NumberTypeImpl_, DecimalType_, BitType_, YearType_, SystemBoolType: 96 return true 97 default: 98 return false 99 } 100 } 101 102 // IsSigned checks if t is a signed type. 103 func IsSigned(t sql.Type) bool { 104 // systemBoolType is Int8 105 if _, ok := t.(SystemBoolType); ok { 106 return true 107 } 108 return t == Int8 || t == Int16 || t == Int24 || t == Int32 || t == Int64 || t == Boolean 109 } 110 111 // IsText checks if t is a CHAR, VARCHAR, TEXT, BINARY, VARBINARY, or BLOB (including TEXT and BLOB variants). 112 func IsText(t sql.Type) bool { 113 _, ok := t.(StringType) 114 return ok 115 } 116 117 // IsTextBlob checks if t is one of the TEXTs or BLOBs. 118 func IsTextBlob(t sql.Type) bool { 119 if t == nil { 120 return false 121 } 122 switch t.Type() { 123 case sqltypes.Text, sqltypes.Blob: 124 return true 125 default: 126 return false 127 } 128 } 129 130 // IsTextOnly checks if t is CHAR, VARCHAR, or one of the TEXTs. 131 func IsTextOnly(t sql.Type) bool { 132 if t == nil { 133 return false 134 } 135 switch t.Type() { 136 case sqltypes.Char, sqltypes.VarChar, sqltypes.Text: 137 return true 138 default: 139 return false 140 } 141 } 142 143 // IsTimespan checks if t is a time (timespan) 144 func IsTimespan(t sql.Type) bool { 145 _, ok := t.(TimespanType_) 146 return ok 147 } 148 149 // IsTime checks if t is a timestamp, date or datetime 150 func IsTime(t sql.Type) bool { 151 _, ok := t.(datetimeType) 152 return ok 153 } 154 155 // IsDateType checks if t is a date 156 func IsDateType(t sql.Type) bool { 157 dt, ok := t.(datetimeType) 158 return ok && dt.baseType == sqltypes.Date 159 } 160 161 // IsDatetimeType checks if t is a datetime 162 func IsDatetimeType(t sql.Type) bool { 163 dt, ok := t.(datetimeType) 164 return ok && dt.baseType == sqltypes.Datetime 165 } 166 167 // IsTimestampType checks if t is a timestamp 168 func IsTimestampType(t sql.Type) bool { 169 dt, ok := t.(datetimeType) 170 return ok && dt.baseType == sqltypes.Timestamp 171 } 172 173 // IsEnum checks if t is a enum 174 func IsEnum(t sql.Type) bool { 175 _, ok := t.(EnumType) 176 return ok 177 } 178 179 // IsSet checks if t is a set 180 func IsSet(t sql.Type) bool { 181 _, ok := t.(SetType) 182 return ok 183 } 184 185 // IsTuple checks if t is a tuple type. 186 // Note that TupleType instances with just 1 value are not considered 187 // as a tuple, but a parenthesized value. 188 func IsTuple(t sql.Type) bool { 189 v, ok := t.(TupleType) 190 return ok && len(v) > 1 191 } 192 193 // IsUnsigned checks if t is an unsigned type. 194 func IsUnsigned(t sql.Type) bool { 195 if svt, ok := t.(sql.SystemVariableType); ok { 196 t = svt.UnderlyingType() 197 } 198 199 return t == Uint8 || t == Uint16 || t == Uint24 || t == Uint32 || t == Uint64 200 } 201 202 // IsYear checks if t is a year type. 203 func IsYear(t sql.Type) bool { 204 _, ok := t.(YearType_) 205 return ok 206 }