sqlglot expressions datatypes.
1"""sqlglot expressions datatypes.""" 2 3from __future__ import annotations 4 5import typing as t 6from enum import auto 7 8from sqlglot.helper import AutoName 9from sqlglot.errors import ErrorLevel, ParseError 10from sqlglot.expressions.core import ( 11 Expr, 12 Expression, 13 _TimeUnit, 14 Identifier, 15 Dot, 16 maybe_copy, 17) 18from builtins import type as Type 19 20if t.TYPE_CHECKING: 21 from sqlglot._typing import DataTypeArgs 22 from sqlglot.dialects.dialect import DialectType 23 from typing_extensions import Self, Unpack 24 25 26class DataTypeParam(Expression): 27 arg_types = {"this": True, "expression": False} 28 29 @property 30 def this(self) -> Expr: 31 return self.args["this"] 32 33 @property 34 def name(self) -> str: 35 return self.this.name 36 37 38class DType(AutoName): 39 ARRAY = auto() 40 AGGREGATEFUNCTION = auto() 41 SIMPLEAGGREGATEFUNCTION = auto() 42 BIGDECIMAL = auto() 43 BIGINT = auto() 44 BIGNUM = auto() 45 BIGSERIAL = auto() 46 BINARY = auto() 47 BIT = auto() 48 BLOB = auto() 49 BOOLEAN = auto() 50 BPCHAR = auto() 51 CHAR = auto() 52 CHARACTER_SET = auto() 53 DATE = auto() 54 DATE32 = auto() 55 DATEMULTIRANGE = auto() 56 DATERANGE = auto() 57 DATETIME = auto() 58 DATETIME2 = auto() 59 DATETIME64 = auto() 60 DECIMAL = auto() 61 DECIMAL32 = auto() 62 DECIMAL64 = auto() 63 DECIMAL128 = auto() 64 DECIMAL256 = auto() 65 DECFLOAT = auto() 66 DOUBLE = auto() 67 DYNAMIC = auto() 68 ENUM = auto() 69 ENUM8 = auto() 70 ENUM16 = auto() 71 FILE = auto() 72 FIXEDSTRING = auto() 73 FLOAT = auto() 74 GEOGRAPHY = auto() 75 GEOGRAPHYPOINT = auto() 76 GEOMETRY = auto() 77 POINT = auto() 78 RING = auto() 79 LINESTRING = auto() 80 MULTILINESTRING = auto() 81 POLYGON = auto() 82 MULTIPOLYGON = auto() 83 HLLSKETCH = auto() 84 HSTORE = auto() 85 IMAGE = auto() 86 INET = auto() 87 INT = auto() 88 INT128 = auto() 89 INT256 = auto() 90 INT4MULTIRANGE = auto() 91 INT4RANGE = auto() 92 INT8MULTIRANGE = auto() 93 INT8RANGE = auto() 94 INTERVAL = auto() 95 IPADDRESS = auto() 96 IPPREFIX = auto() 97 IPV4 = auto() 98 IPV6 = auto() 99 JSON = auto() 100 JSONB = auto() 101 LIST = auto() 102 LONGBLOB = auto() 103 LONGTEXT = auto() 104 LOWCARDINALITY = auto() 105 MAP = auto() 106 MEDIUMBLOB = auto() 107 MEDIUMINT = auto() 108 MEDIUMTEXT = auto() 109 MONEY = auto() 110 NAME = auto() 111 NCHAR = auto() 112 NESTED = auto() 113 NOTHING = auto() 114 NULL = auto() 115 NUMMULTIRANGE = auto() 116 NUMRANGE = auto() 117 NVARCHAR = auto() 118 OBJECT = auto() 119 RANGE = auto() 120 ROWVERSION = auto() 121 SERIAL = auto() 122 SET = auto() 123 SMALLDATETIME = auto() 124 SMALLINT = auto() 125 SMALLMONEY = auto() 126 SMALLSERIAL = auto() 127 STRUCT = auto() 128 SUPER = auto() 129 TEXT = auto() 130 TINYBLOB = auto() 131 TINYTEXT = auto() 132 TIME = auto() 133 TIMETZ = auto() 134 TIME_NS = auto() 135 TIMESTAMP = auto() 136 TIMESTAMPNTZ = auto() 137 TIMESTAMPLTZ = auto() 138 TIMESTAMPTZ = auto() 139 TIMESTAMP_S = auto() 140 TIMESTAMP_MS = auto() 141 TIMESTAMP_NS = auto() 142 TINYINT = auto() 143 TSMULTIRANGE = auto() 144 TSRANGE = auto() 145 TSTZMULTIRANGE = auto() 146 TSTZRANGE = auto() 147 UBIGINT = auto() 148 UINT = auto() 149 UINT128 = auto() 150 UINT256 = auto() 151 UMEDIUMINT = auto() 152 UDECIMAL = auto() 153 UDOUBLE = auto() 154 UNION = auto() 155 UNKNOWN = auto() # Sentinel value, useful for type annotation 156 USERDEFINED = "USER-DEFINED" 157 USMALLINT = auto() 158 UTINYINT = auto() 159 UUID = auto() 160 VARBINARY = auto() 161 VARCHAR = auto() 162 VARIANT = auto() 163 VECTOR = auto() 164 XML = auto() 165 YEAR = auto() 166 TDIGEST = auto() 167 168 def into_expr(self, **kwargs: object) -> DataType: 169 """Converts this `DType` into a `DataType` instance. 170 171 Args: 172 **kwargs (object): additional arguments to pass in the constructor of DataType. 173 Returns: 174 DataType: the resulting `DataType` instance. 175 """ 176 return DataType(this=self).set_kwargs(kwargs) 177 178 179class DataType(Expression): 180 arg_types = { 181 "this": True, 182 "expressions": False, 183 "nested": False, 184 "values": False, 185 "kind": False, 186 "nullable": False, 187 "collate": False, 188 } 189 190 is_data_type: t.ClassVar[bool] = True 191 192 Type: t.ClassVar[Type[DType]] = DType 193 194 STRUCT_TYPES: t.ClassVar[set[DType]] = { 195 DType.FILE, 196 DType.NESTED, 197 DType.OBJECT, 198 DType.STRUCT, 199 DType.UNION, 200 } 201 202 ARRAY_TYPES: t.ClassVar[set[DType]] = { 203 DType.ARRAY, 204 DType.LIST, 205 } 206 207 NESTED_TYPES: t.ClassVar[set[DType]] = { 208 DType.FILE, 209 DType.NESTED, 210 DType.OBJECT, 211 DType.STRUCT, 212 DType.UNION, 213 DType.ARRAY, 214 DType.LIST, 215 DType.MAP, 216 } 217 218 TEXT_TYPES: t.ClassVar[set[DType]] = { 219 DType.CHAR, 220 DType.NCHAR, 221 DType.NVARCHAR, 222 DType.TEXT, 223 DType.TINYTEXT, 224 DType.MEDIUMTEXT, 225 DType.LONGTEXT, 226 DType.VARCHAR, 227 DType.NAME, 228 } 229 230 BINARY_TYPES: t.ClassVar[set[DType]] = { 231 DType.BINARY, 232 DType.VARBINARY, 233 DType.TINYBLOB, 234 DType.BLOB, 235 DType.MEDIUMBLOB, 236 DType.LONGBLOB, 237 } 238 239 SIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = { 240 DType.BIGINT, 241 DType.INT, 242 DType.INT128, 243 DType.INT256, 244 DType.MEDIUMINT, 245 DType.SMALLINT, 246 DType.TINYINT, 247 } 248 249 UNSIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = { 250 DType.UBIGINT, 251 DType.UINT, 252 DType.UINT128, 253 DType.UINT256, 254 DType.UMEDIUMINT, 255 DType.USMALLINT, 256 DType.UTINYINT, 257 } 258 259 INTEGER_TYPES: t.ClassVar[set[DType]] = { 260 DType.BIGINT, 261 DType.INT, 262 DType.INT128, 263 DType.INT256, 264 DType.MEDIUMINT, 265 DType.SMALLINT, 266 DType.TINYINT, 267 DType.UBIGINT, 268 DType.UINT, 269 DType.UINT128, 270 DType.UINT256, 271 DType.UMEDIUMINT, 272 DType.USMALLINT, 273 DType.UTINYINT, 274 DType.BIT, 275 } 276 277 FLOAT_TYPES: t.ClassVar[set[DType]] = { 278 DType.DOUBLE, 279 DType.FLOAT, 280 } 281 282 REAL_TYPES: t.ClassVar[set[DType]] = { 283 DType.DOUBLE, 284 DType.FLOAT, 285 DType.BIGDECIMAL, 286 DType.DECIMAL, 287 DType.DECIMAL32, 288 DType.DECIMAL64, 289 DType.DECIMAL128, 290 DType.DECIMAL256, 291 DType.DECFLOAT, 292 DType.MONEY, 293 DType.SMALLMONEY, 294 DType.UDECIMAL, 295 DType.UDOUBLE, 296 } 297 298 NUMERIC_TYPES: t.ClassVar[set[DType]] = { 299 DType.BIGINT, 300 DType.INT, 301 DType.INT128, 302 DType.INT256, 303 DType.MEDIUMINT, 304 DType.SMALLINT, 305 DType.TINYINT, 306 DType.UBIGINT, 307 DType.UINT, 308 DType.UINT128, 309 DType.UINT256, 310 DType.UMEDIUMINT, 311 DType.USMALLINT, 312 DType.UTINYINT, 313 DType.BIT, 314 DType.DOUBLE, 315 DType.FLOAT, 316 DType.BIGDECIMAL, 317 DType.DECIMAL, 318 DType.DECIMAL32, 319 DType.DECIMAL64, 320 DType.DECIMAL128, 321 DType.DECIMAL256, 322 DType.DECFLOAT, 323 DType.MONEY, 324 DType.SMALLMONEY, 325 DType.UDECIMAL, 326 DType.UDOUBLE, 327 } 328 329 TEMPORAL_TYPES: t.ClassVar[set[DType]] = { 330 DType.DATE, 331 DType.DATE32, 332 DType.DATETIME, 333 DType.DATETIME2, 334 DType.DATETIME64, 335 DType.SMALLDATETIME, 336 DType.TIME, 337 DType.TIMESTAMP, 338 DType.TIMESTAMPNTZ, 339 DType.TIMESTAMPLTZ, 340 DType.TIMESTAMPTZ, 341 DType.TIMESTAMP_MS, 342 DType.TIMESTAMP_NS, 343 DType.TIMESTAMP_S, 344 DType.TIMETZ, 345 } 346 347 @classmethod 348 def build( 349 cls, 350 dtype: DATA_TYPE, 351 dialect: DialectType = None, 352 udt: bool = False, 353 copy: bool = True, 354 **kwargs: Unpack[DataTypeArgs], 355 ) -> Self: 356 """ 357 Constructs a DataType object. 358 359 Args: 360 dtype: the data type of interest. 361 dialect: the dialect to use for parsing `dtype`, in case it's a string. 362 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 363 DataType, thus creating a user-defined type. 364 copy: whether to copy the data type. 365 kwargs: additional arguments to pass in the constructor of DataType. 366 367 Returns: 368 The constructed DataType object. 369 """ 370 if isinstance(dtype, str): 371 return cls.from_str(dtype, dialect, udt, **kwargs) 372 elif isinstance(dtype, DType): 373 data_type_exp = cls(this=dtype) 374 if kwargs: 375 for k, v in kwargs.items(): 376 data_type_exp.set(k, v) 377 return data_type_exp 378 elif isinstance(dtype, (Identifier, Dot)) and udt: 379 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 380 elif isinstance(dtype, cls): 381 return maybe_copy(dtype, copy) 382 else: 383 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DType") 384 385 @classmethod 386 def from_str( 387 cls, 388 dtype: str, 389 dialect: DialectType = None, 390 udt: bool = False, 391 **kwargs: Unpack[DataTypeArgs], 392 ) -> Self: 393 """ 394 Constructs a `DataType` object from a `str` representation. 395 396 Args: 397 dtype: the data type of interest. 398 dialect: the dialect to use for parsing `dtype`. 399 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 400 `DataType`, thus creating a user-defined type. 401 kwargs: additional arguments to pass in the constructor of `DataType`. 402 403 Returns: 404 The constructed `DataType` object. 405 """ 406 from sqlglot import parse_one 407 408 if dtype.upper() == "UNKNOWN": 409 return cls(this=DType.UNKNOWN, **kwargs) 410 try: 411 return parse_one( 412 dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE 413 ).set_kwargs(kwargs) 414 except ParseError: 415 if udt: 416 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 417 raise 418 419 def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool: 420 """ 421 Checks whether this DataType matches one of the provided data types. Nested types or precision 422 will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>. 423 424 Args: 425 dtypes: the data types to compare this DataType to. 426 check_nullable: whether to take the NULLABLE type constructor into account for the comparison. 427 If false, it means that NULLABLE<INT> is equivalent to INT. 428 429 Returns: 430 True, if and only if there is a type in `dtypes` which is equal to this DataType. 431 """ 432 self_is_nullable: bool | None = self.args.get("nullable") 433 for dtype in dtypes: 434 other_type = DataType.build(dtype, copy=False, udt=True) 435 other_is_nullable: bool | None = other_type.args.get("nullable") 436 if ( 437 other_type.expressions 438 or (check_nullable and (self_is_nullable or other_is_nullable)) 439 or self.this == DType.USERDEFINED 440 or other_type.this == DType.USERDEFINED 441 ): 442 matches = self == other_type 443 else: 444 matches = self.this == other_type.this 445 446 if matches: 447 return True 448 return False 449 450 451class PseudoType(DataType): 452 arg_types = {"this": True} 453 454 455class ObjectIdentifier(DataType): 456 arg_types = {"this": True} 457 458 459class IntervalSpan(DataType): 460 arg_types = {"this": True, "expression": True} 461 462 463class Interval(_TimeUnit): 464 arg_types = {"this": False, "unit": False} 465 466 467DATA_TYPE = t.Union[str, Identifier, Dot, DataType, DType]
27class DataTypeParam(Expression): 28 arg_types = {"this": True, "expression": False} 29 30 @property 31 def this(self) -> Expr: 32 return self.args["this"] 33 34 @property 35 def name(self) -> str: 36 return self.this.name
Inherited Members
- sqlglot.expressions.core.Expr
- Expr
- is_var_len_args
- is_subquery
- is_cast
- is_data_type
- is_primitive
- dump
- load
- pipe
- apply
- sqlglot.expressions.core.Expression
- expression
- expressions
- text
- is_string
- is_number
- to_py
- is_int
- is_star
- alias
- alias_column_names
- alias_or_name
- output_name
- type
- is_type
- is_leaf
- meta
- meta_get
- copy
- add_comments
- pop_comments
- append
- set
- set_kwargs
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- to_s
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- and_
- or_
- not_
- update_positions
- as_
- isin
- between
- is_
- like
- ilike
- eq
- neq
- rlike
- div
- asc
- desc
- args
- parent
- arg_key
- index
- comments
39class DType(AutoName): 40 ARRAY = auto() 41 AGGREGATEFUNCTION = auto() 42 SIMPLEAGGREGATEFUNCTION = auto() 43 BIGDECIMAL = auto() 44 BIGINT = auto() 45 BIGNUM = auto() 46 BIGSERIAL = auto() 47 BINARY = auto() 48 BIT = auto() 49 BLOB = auto() 50 BOOLEAN = auto() 51 BPCHAR = auto() 52 CHAR = auto() 53 CHARACTER_SET = auto() 54 DATE = auto() 55 DATE32 = auto() 56 DATEMULTIRANGE = auto() 57 DATERANGE = auto() 58 DATETIME = auto() 59 DATETIME2 = auto() 60 DATETIME64 = auto() 61 DECIMAL = auto() 62 DECIMAL32 = auto() 63 DECIMAL64 = auto() 64 DECIMAL128 = auto() 65 DECIMAL256 = auto() 66 DECFLOAT = auto() 67 DOUBLE = auto() 68 DYNAMIC = auto() 69 ENUM = auto() 70 ENUM8 = auto() 71 ENUM16 = auto() 72 FILE = auto() 73 FIXEDSTRING = auto() 74 FLOAT = auto() 75 GEOGRAPHY = auto() 76 GEOGRAPHYPOINT = auto() 77 GEOMETRY = auto() 78 POINT = auto() 79 RING = auto() 80 LINESTRING = auto() 81 MULTILINESTRING = auto() 82 POLYGON = auto() 83 MULTIPOLYGON = auto() 84 HLLSKETCH = auto() 85 HSTORE = auto() 86 IMAGE = auto() 87 INET = auto() 88 INT = auto() 89 INT128 = auto() 90 INT256 = auto() 91 INT4MULTIRANGE = auto() 92 INT4RANGE = auto() 93 INT8MULTIRANGE = auto() 94 INT8RANGE = auto() 95 INTERVAL = auto() 96 IPADDRESS = auto() 97 IPPREFIX = auto() 98 IPV4 = auto() 99 IPV6 = auto() 100 JSON = auto() 101 JSONB = auto() 102 LIST = auto() 103 LONGBLOB = auto() 104 LONGTEXT = auto() 105 LOWCARDINALITY = auto() 106 MAP = auto() 107 MEDIUMBLOB = auto() 108 MEDIUMINT = auto() 109 MEDIUMTEXT = auto() 110 MONEY = auto() 111 NAME = auto() 112 NCHAR = auto() 113 NESTED = auto() 114 NOTHING = auto() 115 NULL = auto() 116 NUMMULTIRANGE = auto() 117 NUMRANGE = auto() 118 NVARCHAR = auto() 119 OBJECT = auto() 120 RANGE = auto() 121 ROWVERSION = auto() 122 SERIAL = auto() 123 SET = auto() 124 SMALLDATETIME = auto() 125 SMALLINT = auto() 126 SMALLMONEY = auto() 127 SMALLSERIAL = auto() 128 STRUCT = auto() 129 SUPER = auto() 130 TEXT = auto() 131 TINYBLOB = auto() 132 TINYTEXT = auto() 133 TIME = auto() 134 TIMETZ = auto() 135 TIME_NS = auto() 136 TIMESTAMP = auto() 137 TIMESTAMPNTZ = auto() 138 TIMESTAMPLTZ = auto() 139 TIMESTAMPTZ = auto() 140 TIMESTAMP_S = auto() 141 TIMESTAMP_MS = auto() 142 TIMESTAMP_NS = auto() 143 TINYINT = auto() 144 TSMULTIRANGE = auto() 145 TSRANGE = auto() 146 TSTZMULTIRANGE = auto() 147 TSTZRANGE = auto() 148 UBIGINT = auto() 149 UINT = auto() 150 UINT128 = auto() 151 UINT256 = auto() 152 UMEDIUMINT = auto() 153 UDECIMAL = auto() 154 UDOUBLE = auto() 155 UNION = auto() 156 UNKNOWN = auto() # Sentinel value, useful for type annotation 157 USERDEFINED = "USER-DEFINED" 158 USMALLINT = auto() 159 UTINYINT = auto() 160 UUID = auto() 161 VARBINARY = auto() 162 VARCHAR = auto() 163 VARIANT = auto() 164 VECTOR = auto() 165 XML = auto() 166 YEAR = auto() 167 TDIGEST = auto() 168 169 def into_expr(self, **kwargs: object) -> DataType: 170 """Converts this `DType` into a `DataType` instance. 171 172 Args: 173 **kwargs (object): additional arguments to pass in the constructor of DataType. 174 Returns: 175 DataType: the resulting `DataType` instance. 176 """ 177 return DataType(this=self).set_kwargs(kwargs)
An enumeration.
ARRAY =
<DType.ARRAY: 'ARRAY'>
AGGREGATEFUNCTION =
<DType.AGGREGATEFUNCTION: 'AGGREGATEFUNCTION'>
SIMPLEAGGREGATEFUNCTION =
<DType.SIMPLEAGGREGATEFUNCTION: 'SIMPLEAGGREGATEFUNCTION'>
BIGDECIMAL =
<DType.BIGDECIMAL: 'BIGDECIMAL'>
BIGINT =
<DType.BIGINT: 'BIGINT'>
BIGNUM =
<DType.BIGNUM: 'BIGNUM'>
BIGSERIAL =
<DType.BIGSERIAL: 'BIGSERIAL'>
BINARY =
<DType.BINARY: 'BINARY'>
BIT =
<DType.BIT: 'BIT'>
BLOB =
<DType.BLOB: 'BLOB'>
BOOLEAN =
<DType.BOOLEAN: 'BOOLEAN'>
BPCHAR =
<DType.BPCHAR: 'BPCHAR'>
CHAR =
<DType.CHAR: 'CHAR'>
CHARACTER_SET =
<DType.CHARACTER_SET: 'CHARACTER_SET'>
DATE =
<DType.DATE: 'DATE'>
DATE32 =
<DType.DATE32: 'DATE32'>
DATEMULTIRANGE =
<DType.DATEMULTIRANGE: 'DATEMULTIRANGE'>
DATERANGE =
<DType.DATERANGE: 'DATERANGE'>
DATETIME =
<DType.DATETIME: 'DATETIME'>
DATETIME2 =
<DType.DATETIME2: 'DATETIME2'>
DATETIME64 =
<DType.DATETIME64: 'DATETIME64'>
DECIMAL =
<DType.DECIMAL: 'DECIMAL'>
DECIMAL32 =
<DType.DECIMAL32: 'DECIMAL32'>
DECIMAL64 =
<DType.DECIMAL64: 'DECIMAL64'>
DECIMAL128 =
<DType.DECIMAL128: 'DECIMAL128'>
DECIMAL256 =
<DType.DECIMAL256: 'DECIMAL256'>
DECFLOAT =
<DType.DECFLOAT: 'DECFLOAT'>
DOUBLE =
<DType.DOUBLE: 'DOUBLE'>
DYNAMIC =
<DType.DYNAMIC: 'DYNAMIC'>
ENUM =
<DType.ENUM: 'ENUM'>
ENUM8 =
<DType.ENUM8: 'ENUM8'>
ENUM16 =
<DType.ENUM16: 'ENUM16'>
FILE =
<DType.FILE: 'FILE'>
FIXEDSTRING =
<DType.FIXEDSTRING: 'FIXEDSTRING'>
FLOAT =
<DType.FLOAT: 'FLOAT'>
GEOGRAPHY =
<DType.GEOGRAPHY: 'GEOGRAPHY'>
GEOGRAPHYPOINT =
<DType.GEOGRAPHYPOINT: 'GEOGRAPHYPOINT'>
GEOMETRY =
<DType.GEOMETRY: 'GEOMETRY'>
POINT =
<DType.POINT: 'POINT'>
RING =
<DType.RING: 'RING'>
LINESTRING =
<DType.LINESTRING: 'LINESTRING'>
MULTILINESTRING =
<DType.MULTILINESTRING: 'MULTILINESTRING'>
POLYGON =
<DType.POLYGON: 'POLYGON'>
MULTIPOLYGON =
<DType.MULTIPOLYGON: 'MULTIPOLYGON'>
HLLSKETCH =
<DType.HLLSKETCH: 'HLLSKETCH'>
HSTORE =
<DType.HSTORE: 'HSTORE'>
IMAGE =
<DType.IMAGE: 'IMAGE'>
INET =
<DType.INET: 'INET'>
INT =
<DType.INT: 'INT'>
INT128 =
<DType.INT128: 'INT128'>
INT256 =
<DType.INT256: 'INT256'>
INT4MULTIRANGE =
<DType.INT4MULTIRANGE: 'INT4MULTIRANGE'>
INT4RANGE =
<DType.INT4RANGE: 'INT4RANGE'>
INT8MULTIRANGE =
<DType.INT8MULTIRANGE: 'INT8MULTIRANGE'>
INT8RANGE =
<DType.INT8RANGE: 'INT8RANGE'>
INTERVAL =
<DType.INTERVAL: 'INTERVAL'>
IPADDRESS =
<DType.IPADDRESS: 'IPADDRESS'>
IPPREFIX =
<DType.IPPREFIX: 'IPPREFIX'>
IPV4 =
<DType.IPV4: 'IPV4'>
IPV6 =
<DType.IPV6: 'IPV6'>
JSON =
<DType.JSON: 'JSON'>
JSONB =
<DType.JSONB: 'JSONB'>
LIST =
<DType.LIST: 'LIST'>
LONGBLOB =
<DType.LONGBLOB: 'LONGBLOB'>
LONGTEXT =
<DType.LONGTEXT: 'LONGTEXT'>
LOWCARDINALITY =
<DType.LOWCARDINALITY: 'LOWCARDINALITY'>
MAP =
<DType.MAP: 'MAP'>
MEDIUMBLOB =
<DType.MEDIUMBLOB: 'MEDIUMBLOB'>
MEDIUMINT =
<DType.MEDIUMINT: 'MEDIUMINT'>
MEDIUMTEXT =
<DType.MEDIUMTEXT: 'MEDIUMTEXT'>
MONEY =
<DType.MONEY: 'MONEY'>
NAME =
<DType.NAME: 'NAME'>
NCHAR =
<DType.NCHAR: 'NCHAR'>
NESTED =
<DType.NESTED: 'NESTED'>
NOTHING =
<DType.NOTHING: 'NOTHING'>
NULL =
<DType.NULL: 'NULL'>
NUMMULTIRANGE =
<DType.NUMMULTIRANGE: 'NUMMULTIRANGE'>
NUMRANGE =
<DType.NUMRANGE: 'NUMRANGE'>
NVARCHAR =
<DType.NVARCHAR: 'NVARCHAR'>
OBJECT =
<DType.OBJECT: 'OBJECT'>
RANGE =
<DType.RANGE: 'RANGE'>
ROWVERSION =
<DType.ROWVERSION: 'ROWVERSION'>
SERIAL =
<DType.SERIAL: 'SERIAL'>
SET =
<DType.SET: 'SET'>
SMALLDATETIME =
<DType.SMALLDATETIME: 'SMALLDATETIME'>
SMALLINT =
<DType.SMALLINT: 'SMALLINT'>
SMALLMONEY =
<DType.SMALLMONEY: 'SMALLMONEY'>
SMALLSERIAL =
<DType.SMALLSERIAL: 'SMALLSERIAL'>
STRUCT =
<DType.STRUCT: 'STRUCT'>
SUPER =
<DType.SUPER: 'SUPER'>
TEXT =
<DType.TEXT: 'TEXT'>
TINYBLOB =
<DType.TINYBLOB: 'TINYBLOB'>
TINYTEXT =
<DType.TINYTEXT: 'TINYTEXT'>
TIME =
<DType.TIME: 'TIME'>
TIMETZ =
<DType.TIMETZ: 'TIMETZ'>
TIME_NS =
<DType.TIME_NS: 'TIME_NS'>
TIMESTAMP =
<DType.TIMESTAMP: 'TIMESTAMP'>
TIMESTAMPNTZ =
<DType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>
TIMESTAMPLTZ =
<DType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>
TIMESTAMPTZ =
<DType.TIMESTAMPTZ: 'TIMESTAMPTZ'>
TIMESTAMP_S =
<DType.TIMESTAMP_S: 'TIMESTAMP_S'>
TIMESTAMP_MS =
<DType.TIMESTAMP_MS: 'TIMESTAMP_MS'>
TIMESTAMP_NS =
<DType.TIMESTAMP_NS: 'TIMESTAMP_NS'>
TINYINT =
<DType.TINYINT: 'TINYINT'>
TSMULTIRANGE =
<DType.TSMULTIRANGE: 'TSMULTIRANGE'>
TSRANGE =
<DType.TSRANGE: 'TSRANGE'>
TSTZMULTIRANGE =
<DType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>
TSTZRANGE =
<DType.TSTZRANGE: 'TSTZRANGE'>
UBIGINT =
<DType.UBIGINT: 'UBIGINT'>
UINT =
<DType.UINT: 'UINT'>
UINT128 =
<DType.UINT128: 'UINT128'>
UINT256 =
<DType.UINT256: 'UINT256'>
UMEDIUMINT =
<DType.UMEDIUMINT: 'UMEDIUMINT'>
UDECIMAL =
<DType.UDECIMAL: 'UDECIMAL'>
UDOUBLE =
<DType.UDOUBLE: 'UDOUBLE'>
UNION =
<DType.UNION: 'UNION'>
UNKNOWN =
<DType.UNKNOWN: 'UNKNOWN'>
USERDEFINED =
<DType.USERDEFINED: 'USER-DEFINED'>
USMALLINT =
<DType.USMALLINT: 'USMALLINT'>
UTINYINT =
<DType.UTINYINT: 'UTINYINT'>
UUID =
<DType.UUID: 'UUID'>
VARBINARY =
<DType.VARBINARY: 'VARBINARY'>
VARCHAR =
<DType.VARCHAR: 'VARCHAR'>
VARIANT =
<DType.VARIANT: 'VARIANT'>
VECTOR =
<DType.VECTOR: 'VECTOR'>
XML =
<DType.XML: 'XML'>
YEAR =
<DType.YEAR: 'YEAR'>
TDIGEST =
<DType.TDIGEST: 'TDIGEST'>
169 def into_expr(self, **kwargs: object) -> DataType: 170 """Converts this `DType` into a `DataType` instance. 171 172 Args: 173 **kwargs (object): additional arguments to pass in the constructor of DataType. 174 Returns: 175 DataType: the resulting `DataType` instance. 176 """ 177 return DataType(this=self).set_kwargs(kwargs)
180class DataType(Expression): 181 arg_types = { 182 "this": True, 183 "expressions": False, 184 "nested": False, 185 "values": False, 186 "kind": False, 187 "nullable": False, 188 "collate": False, 189 } 190 191 is_data_type: t.ClassVar[bool] = True 192 193 Type: t.ClassVar[Type[DType]] = DType 194 195 STRUCT_TYPES: t.ClassVar[set[DType]] = { 196 DType.FILE, 197 DType.NESTED, 198 DType.OBJECT, 199 DType.STRUCT, 200 DType.UNION, 201 } 202 203 ARRAY_TYPES: t.ClassVar[set[DType]] = { 204 DType.ARRAY, 205 DType.LIST, 206 } 207 208 NESTED_TYPES: t.ClassVar[set[DType]] = { 209 DType.FILE, 210 DType.NESTED, 211 DType.OBJECT, 212 DType.STRUCT, 213 DType.UNION, 214 DType.ARRAY, 215 DType.LIST, 216 DType.MAP, 217 } 218 219 TEXT_TYPES: t.ClassVar[set[DType]] = { 220 DType.CHAR, 221 DType.NCHAR, 222 DType.NVARCHAR, 223 DType.TEXT, 224 DType.TINYTEXT, 225 DType.MEDIUMTEXT, 226 DType.LONGTEXT, 227 DType.VARCHAR, 228 DType.NAME, 229 } 230 231 BINARY_TYPES: t.ClassVar[set[DType]] = { 232 DType.BINARY, 233 DType.VARBINARY, 234 DType.TINYBLOB, 235 DType.BLOB, 236 DType.MEDIUMBLOB, 237 DType.LONGBLOB, 238 } 239 240 SIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = { 241 DType.BIGINT, 242 DType.INT, 243 DType.INT128, 244 DType.INT256, 245 DType.MEDIUMINT, 246 DType.SMALLINT, 247 DType.TINYINT, 248 } 249 250 UNSIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = { 251 DType.UBIGINT, 252 DType.UINT, 253 DType.UINT128, 254 DType.UINT256, 255 DType.UMEDIUMINT, 256 DType.USMALLINT, 257 DType.UTINYINT, 258 } 259 260 INTEGER_TYPES: t.ClassVar[set[DType]] = { 261 DType.BIGINT, 262 DType.INT, 263 DType.INT128, 264 DType.INT256, 265 DType.MEDIUMINT, 266 DType.SMALLINT, 267 DType.TINYINT, 268 DType.UBIGINT, 269 DType.UINT, 270 DType.UINT128, 271 DType.UINT256, 272 DType.UMEDIUMINT, 273 DType.USMALLINT, 274 DType.UTINYINT, 275 DType.BIT, 276 } 277 278 FLOAT_TYPES: t.ClassVar[set[DType]] = { 279 DType.DOUBLE, 280 DType.FLOAT, 281 } 282 283 REAL_TYPES: t.ClassVar[set[DType]] = { 284 DType.DOUBLE, 285 DType.FLOAT, 286 DType.BIGDECIMAL, 287 DType.DECIMAL, 288 DType.DECIMAL32, 289 DType.DECIMAL64, 290 DType.DECIMAL128, 291 DType.DECIMAL256, 292 DType.DECFLOAT, 293 DType.MONEY, 294 DType.SMALLMONEY, 295 DType.UDECIMAL, 296 DType.UDOUBLE, 297 } 298 299 NUMERIC_TYPES: t.ClassVar[set[DType]] = { 300 DType.BIGINT, 301 DType.INT, 302 DType.INT128, 303 DType.INT256, 304 DType.MEDIUMINT, 305 DType.SMALLINT, 306 DType.TINYINT, 307 DType.UBIGINT, 308 DType.UINT, 309 DType.UINT128, 310 DType.UINT256, 311 DType.UMEDIUMINT, 312 DType.USMALLINT, 313 DType.UTINYINT, 314 DType.BIT, 315 DType.DOUBLE, 316 DType.FLOAT, 317 DType.BIGDECIMAL, 318 DType.DECIMAL, 319 DType.DECIMAL32, 320 DType.DECIMAL64, 321 DType.DECIMAL128, 322 DType.DECIMAL256, 323 DType.DECFLOAT, 324 DType.MONEY, 325 DType.SMALLMONEY, 326 DType.UDECIMAL, 327 DType.UDOUBLE, 328 } 329 330 TEMPORAL_TYPES: t.ClassVar[set[DType]] = { 331 DType.DATE, 332 DType.DATE32, 333 DType.DATETIME, 334 DType.DATETIME2, 335 DType.DATETIME64, 336 DType.SMALLDATETIME, 337 DType.TIME, 338 DType.TIMESTAMP, 339 DType.TIMESTAMPNTZ, 340 DType.TIMESTAMPLTZ, 341 DType.TIMESTAMPTZ, 342 DType.TIMESTAMP_MS, 343 DType.TIMESTAMP_NS, 344 DType.TIMESTAMP_S, 345 DType.TIMETZ, 346 } 347 348 @classmethod 349 def build( 350 cls, 351 dtype: DATA_TYPE, 352 dialect: DialectType = None, 353 udt: bool = False, 354 copy: bool = True, 355 **kwargs: Unpack[DataTypeArgs], 356 ) -> Self: 357 """ 358 Constructs a DataType object. 359 360 Args: 361 dtype: the data type of interest. 362 dialect: the dialect to use for parsing `dtype`, in case it's a string. 363 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 364 DataType, thus creating a user-defined type. 365 copy: whether to copy the data type. 366 kwargs: additional arguments to pass in the constructor of DataType. 367 368 Returns: 369 The constructed DataType object. 370 """ 371 if isinstance(dtype, str): 372 return cls.from_str(dtype, dialect, udt, **kwargs) 373 elif isinstance(dtype, DType): 374 data_type_exp = cls(this=dtype) 375 if kwargs: 376 for k, v in kwargs.items(): 377 data_type_exp.set(k, v) 378 return data_type_exp 379 elif isinstance(dtype, (Identifier, Dot)) and udt: 380 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 381 elif isinstance(dtype, cls): 382 return maybe_copy(dtype, copy) 383 else: 384 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DType") 385 386 @classmethod 387 def from_str( 388 cls, 389 dtype: str, 390 dialect: DialectType = None, 391 udt: bool = False, 392 **kwargs: Unpack[DataTypeArgs], 393 ) -> Self: 394 """ 395 Constructs a `DataType` object from a `str` representation. 396 397 Args: 398 dtype: the data type of interest. 399 dialect: the dialect to use for parsing `dtype`. 400 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 401 `DataType`, thus creating a user-defined type. 402 kwargs: additional arguments to pass in the constructor of `DataType`. 403 404 Returns: 405 The constructed `DataType` object. 406 """ 407 from sqlglot import parse_one 408 409 if dtype.upper() == "UNKNOWN": 410 return cls(this=DType.UNKNOWN, **kwargs) 411 try: 412 return parse_one( 413 dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE 414 ).set_kwargs(kwargs) 415 except ParseError: 416 if udt: 417 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 418 raise 419 420 def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool: 421 """ 422 Checks whether this DataType matches one of the provided data types. Nested types or precision 423 will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>. 424 425 Args: 426 dtypes: the data types to compare this DataType to. 427 check_nullable: whether to take the NULLABLE type constructor into account for the comparison. 428 If false, it means that NULLABLE<INT> is equivalent to INT. 429 430 Returns: 431 True, if and only if there is a type in `dtypes` which is equal to this DataType. 432 """ 433 self_is_nullable: bool | None = self.args.get("nullable") 434 for dtype in dtypes: 435 other_type = DataType.build(dtype, copy=False, udt=True) 436 other_is_nullable: bool | None = other_type.args.get("nullable") 437 if ( 438 other_type.expressions 439 or (check_nullable and (self_is_nullable or other_is_nullable)) 440 or self.this == DType.USERDEFINED 441 or other_type.this == DType.USERDEFINED 442 ): 443 matches = self == other_type 444 else: 445 matches = self.this == other_type.this 446 447 if matches: 448 return True 449 return False
arg_types =
{'this': True, 'expressions': False, 'nested': False, 'values': False, 'kind': False, 'nullable': False, 'collate': False}
STRUCT_TYPES: ClassVar[set[DType]] =
{<DType.FILE: 'FILE'>, <DType.STRUCT: 'STRUCT'>, <DType.NESTED: 'NESTED'>, <DType.OBJECT: 'OBJECT'>, <DType.UNION: 'UNION'>}
NESTED_TYPES: ClassVar[set[DType]] =
{<DType.FILE: 'FILE'>, <DType.STRUCT: 'STRUCT'>, <DType.MAP: 'MAP'>, <DType.NESTED: 'NESTED'>, <DType.OBJECT: 'OBJECT'>, <DType.UNION: 'UNION'>, <DType.ARRAY: 'ARRAY'>, <DType.LIST: 'LIST'>}
TEXT_TYPES: ClassVar[set[DType]] =
{<DType.VARCHAR: 'VARCHAR'>, <DType.LONGTEXT: 'LONGTEXT'>, <DType.TINYTEXT: 'TINYTEXT'>, <DType.NVARCHAR: 'NVARCHAR'>, <DType.TEXT: 'TEXT'>, <DType.MEDIUMTEXT: 'MEDIUMTEXT'>, <DType.CHAR: 'CHAR'>, <DType.NAME: 'NAME'>, <DType.NCHAR: 'NCHAR'>}
BINARY_TYPES: ClassVar[set[DType]] =
{<DType.MEDIUMBLOB: 'MEDIUMBLOB'>, <DType.LONGBLOB: 'LONGBLOB'>, <DType.VARBINARY: 'VARBINARY'>, <DType.BLOB: 'BLOB'>, <DType.BINARY: 'BINARY'>, <DType.TINYBLOB: 'TINYBLOB'>}
SIGNED_INTEGER_TYPES: ClassVar[set[DType]] =
{<DType.INT256: 'INT256'>, <DType.TINYINT: 'TINYINT'>, <DType.INT128: 'INT128'>, <DType.MEDIUMINT: 'MEDIUMINT'>, <DType.INT: 'INT'>, <DType.BIGINT: 'BIGINT'>, <DType.SMALLINT: 'SMALLINT'>}
UNSIGNED_INTEGER_TYPES: ClassVar[set[DType]] =
{<DType.UINT: 'UINT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.USMALLINT: 'USMALLINT'>, <DType.UINT256: 'UINT256'>, <DType.UINT128: 'UINT128'>, <DType.UTINYINT: 'UTINYINT'>}
INTEGER_TYPES: ClassVar[set[DType]] =
{<DType.INT256: 'INT256'>, <DType.UINT: 'UINT'>, <DType.TINYINT: 'TINYINT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.INT128: 'INT128'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.USMALLINT: 'USMALLINT'>, <DType.MEDIUMINT: 'MEDIUMINT'>, <DType.UINT256: 'UINT256'>, <DType.BIT: 'BIT'>, <DType.INT: 'INT'>, <DType.BIGINT: 'BIGINT'>, <DType.UTINYINT: 'UTINYINT'>, <DType.UINT128: 'UINT128'>, <DType.SMALLINT: 'SMALLINT'>}
REAL_TYPES: ClassVar[set[DType]] =
{<DType.UDECIMAL: 'UDECIMAL'>, <DType.FLOAT: 'FLOAT'>, <DType.MONEY: 'MONEY'>, <DType.DECIMAL128: 'DECIMAL128'>, <DType.DOUBLE: 'DOUBLE'>, <DType.SMALLMONEY: 'SMALLMONEY'>, <DType.DECIMAL32: 'DECIMAL32'>, <DType.DECIMAL256: 'DECIMAL256'>, <DType.UDOUBLE: 'UDOUBLE'>, <DType.DECIMAL: 'DECIMAL'>, <DType.DECIMAL64: 'DECIMAL64'>, <DType.BIGDECIMAL: 'BIGDECIMAL'>, <DType.DECFLOAT: 'DECFLOAT'>}
NUMERIC_TYPES: ClassVar[set[DType]] =
{<DType.UINT: 'UINT'>, <DType.DECIMAL128: 'DECIMAL128'>, <DType.INT128: 'INT128'>, <DType.DOUBLE: 'DOUBLE'>, <DType.DECIMAL64: 'DECIMAL64'>, <DType.MEDIUMINT: 'MEDIUMINT'>, <DType.DECIMAL256: 'DECIMAL256'>, <DType.INT: 'INT'>, <DType.BIGINT: 'BIGINT'>, <DType.USMALLINT: 'USMALLINT'>, <DType.DECIMAL: 'DECIMAL'>, <DType.BIGDECIMAL: 'BIGDECIMAL'>, <DType.UDECIMAL: 'UDECIMAL'>, <DType.TINYINT: 'TINYINT'>, <DType.FLOAT: 'FLOAT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.MONEY: 'MONEY'>, <DType.SMALLMONEY: 'SMALLMONEY'>, <DType.DECIMAL32: 'DECIMAL32'>, <DType.INT256: 'INT256'>, <DType.UTINYINT: 'UTINYINT'>, <DType.DECFLOAT: 'DECFLOAT'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.BIT: 'BIT'>, <DType.UINT256: 'UINT256'>, <DType.UDOUBLE: 'UDOUBLE'>, <DType.UINT128: 'UINT128'>, <DType.SMALLINT: 'SMALLINT'>}
TEMPORAL_TYPES: ClassVar[set[DType]] =
{<DType.DATETIME64: 'DATETIME64'>, <DType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <DType.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <DType.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <DType.TIME: 'TIME'>, <DType.DATETIME: 'DATETIME'>, <DType.TIMESTAMP_S: 'TIMESTAMP_S'>, <DType.DATETIME2: 'DATETIME2'>, <DType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <DType.TIMETZ: 'TIMETZ'>, <DType.TIMESTAMP: 'TIMESTAMP'>, <DType.DATE32: 'DATE32'>, <DType.SMALLDATETIME: 'SMALLDATETIME'>, <DType.DATE: 'DATE'>, <DType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>}
@classmethod
def
build( cls, dtype: Union[str, sqlglot.expressions.core.Identifier, sqlglot.expressions.core.Dot, DataType, DType], dialect: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, udt: bool = False, copy: bool = True, **kwargs: typing_extensions.Unpack[sqlglot._typing.DataTypeArgs]) -> typing_extensions.Self:
348 @classmethod 349 def build( 350 cls, 351 dtype: DATA_TYPE, 352 dialect: DialectType = None, 353 udt: bool = False, 354 copy: bool = True, 355 **kwargs: Unpack[DataTypeArgs], 356 ) -> Self: 357 """ 358 Constructs a DataType object. 359 360 Args: 361 dtype: the data type of interest. 362 dialect: the dialect to use for parsing `dtype`, in case it's a string. 363 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 364 DataType, thus creating a user-defined type. 365 copy: whether to copy the data type. 366 kwargs: additional arguments to pass in the constructor of DataType. 367 368 Returns: 369 The constructed DataType object. 370 """ 371 if isinstance(dtype, str): 372 return cls.from_str(dtype, dialect, udt, **kwargs) 373 elif isinstance(dtype, DType): 374 data_type_exp = cls(this=dtype) 375 if kwargs: 376 for k, v in kwargs.items(): 377 data_type_exp.set(k, v) 378 return data_type_exp 379 elif isinstance(dtype, (Identifier, Dot)) and udt: 380 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 381 elif isinstance(dtype, cls): 382 return maybe_copy(dtype, copy) 383 else: 384 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DType")
Constructs a DataType object.
Arguments:
- dtype: the data type of interest.
- dialect: the dialect to use for parsing
dtype, in case it's a string. - udt: when set to True,
dtypewill be used as-is if it can't be parsed into a DataType, thus creating a user-defined type. - copy: whether to copy the data type.
- kwargs: additional arguments to pass in the constructor of DataType.
Returns:
The constructed DataType object.
@classmethod
def
from_str( cls, dtype: str, dialect: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, udt: bool = False, **kwargs: typing_extensions.Unpack[sqlglot._typing.DataTypeArgs]) -> typing_extensions.Self:
386 @classmethod 387 def from_str( 388 cls, 389 dtype: str, 390 dialect: DialectType = None, 391 udt: bool = False, 392 **kwargs: Unpack[DataTypeArgs], 393 ) -> Self: 394 """ 395 Constructs a `DataType` object from a `str` representation. 396 397 Args: 398 dtype: the data type of interest. 399 dialect: the dialect to use for parsing `dtype`. 400 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 401 `DataType`, thus creating a user-defined type. 402 kwargs: additional arguments to pass in the constructor of `DataType`. 403 404 Returns: 405 The constructed `DataType` object. 406 """ 407 from sqlglot import parse_one 408 409 if dtype.upper() == "UNKNOWN": 410 return cls(this=DType.UNKNOWN, **kwargs) 411 try: 412 return parse_one( 413 dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE 414 ).set_kwargs(kwargs) 415 except ParseError: 416 if udt: 417 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 418 raise
Constructs a DataType object from a str representation.
Arguments:
- dtype: the data type of interest.
- dialect: the dialect to use for parsing
dtype. - udt: when set to True,
dtypewill be used as-is if it can't be parsed into aDataType, thus creating a user-defined type. - kwargs: additional arguments to pass in the constructor of
DataType.
Returns:
The constructed
DataTypeobject.
def
is_type( self, *dtypes: Union[str, sqlglot.expressions.core.Identifier, sqlglot.expressions.core.Dot, DataType, DType], check_nullable: bool = False) -> bool:
420 def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool: 421 """ 422 Checks whether this DataType matches one of the provided data types. Nested types or precision 423 will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>. 424 425 Args: 426 dtypes: the data types to compare this DataType to. 427 check_nullable: whether to take the NULLABLE type constructor into account for the comparison. 428 If false, it means that NULLABLE<INT> is equivalent to INT. 429 430 Returns: 431 True, if and only if there is a type in `dtypes` which is equal to this DataType. 432 """ 433 self_is_nullable: bool | None = self.args.get("nullable") 434 for dtype in dtypes: 435 other_type = DataType.build(dtype, copy=False, udt=True) 436 other_is_nullable: bool | None = other_type.args.get("nullable") 437 if ( 438 other_type.expressions 439 or (check_nullable and (self_is_nullable or other_is_nullable)) 440 or self.this == DType.USERDEFINED 441 or other_type.this == DType.USERDEFINED 442 ): 443 matches = self == other_type 444 else: 445 matches = self.this == other_type.this 446 447 if matches: 448 return True 449 return False
Checks whether this DataType matches one of the provided data types. Nested types or precision
will be compared using "structural equivalence" semantics, so e.g. array
Arguments:
- dtypes: the data types to compare this DataType to.
- check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
If false, it means that NULLABLE
is equivalent to INT.
Returns:
True, if and only if there is a type in
dtypeswhich is equal to this DataType.
Inherited Members
- sqlglot.expressions.core.Expr
- Expr
- is_var_len_args
- is_subquery
- is_cast
- is_primitive
- dump
- load
- pipe
- apply
- sqlglot.expressions.core.Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- to_py
- is_int
- is_star
- alias
- alias_column_names
- name
- alias_or_name
- output_name
- type
- is_leaf
- meta
- meta_get
- copy
- add_comments
- pop_comments
- append
- set
- set_kwargs
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- to_s
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- and_
- or_
- not_
- update_positions
- as_
- isin
- between
- is_
- like
- ilike
- eq
- neq
- rlike
- div
- asc
- desc
- args
- parent
- arg_key
- index
- comments
Inherited Members
- sqlglot.expressions.core.Expr
- Expr
- is_var_len_args
- is_subquery
- is_cast
- is_primitive
- dump
- load
- pipe
- apply
- DataType
- is_data_type
- Type
- STRUCT_TYPES
- ARRAY_TYPES
- NESTED_TYPES
- TEXT_TYPES
- BINARY_TYPES
- SIGNED_INTEGER_TYPES
- UNSIGNED_INTEGER_TYPES
- INTEGER_TYPES
- FLOAT_TYPES
- REAL_TYPES
- NUMERIC_TYPES
- TEMPORAL_TYPES
- build
- from_str
- is_type
- sqlglot.expressions.core.Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- to_py
- is_int
- is_star
- alias
- alias_column_names
- name
- alias_or_name
- output_name
- type
- is_leaf
- meta
- meta_get
- copy
- add_comments
- pop_comments
- append
- set
- set_kwargs
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- to_s
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- and_
- or_
- not_
- update_positions
- as_
- isin
- between
- is_
- like
- ilike
- eq
- neq
- rlike
- div
- asc
- desc
- args
- parent
- arg_key
- index
- comments
Inherited Members
- sqlglot.expressions.core.Expr
- Expr
- is_var_len_args
- is_subquery
- is_cast
- is_primitive
- dump
- load
- pipe
- apply
- DataType
- is_data_type
- Type
- STRUCT_TYPES
- ARRAY_TYPES
- NESTED_TYPES
- TEXT_TYPES
- BINARY_TYPES
- SIGNED_INTEGER_TYPES
- UNSIGNED_INTEGER_TYPES
- INTEGER_TYPES
- FLOAT_TYPES
- REAL_TYPES
- NUMERIC_TYPES
- TEMPORAL_TYPES
- build
- from_str
- is_type
- sqlglot.expressions.core.Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- to_py
- is_int
- is_star
- alias
- alias_column_names
- name
- alias_or_name
- output_name
- type
- is_leaf
- meta
- meta_get
- copy
- add_comments
- pop_comments
- append
- set
- set_kwargs
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- to_s
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- and_
- or_
- not_
- update_positions
- as_
- isin
- between
- is_
- like
- ilike
- eq
- neq
- rlike
- div
- asc
- desc
- args
- parent
- arg_key
- index
- comments
Inherited Members
- sqlglot.expressions.core.Expr
- Expr
- is_var_len_args
- is_subquery
- is_cast
- is_primitive
- dump
- load
- pipe
- apply
- DataType
- is_data_type
- Type
- STRUCT_TYPES
- ARRAY_TYPES
- NESTED_TYPES
- TEXT_TYPES
- BINARY_TYPES
- SIGNED_INTEGER_TYPES
- UNSIGNED_INTEGER_TYPES
- INTEGER_TYPES
- FLOAT_TYPES
- REAL_TYPES
- NUMERIC_TYPES
- TEMPORAL_TYPES
- build
- from_str
- is_type
- sqlglot.expressions.core.Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- to_py
- is_int
- is_star
- alias
- alias_column_names
- name
- alias_or_name
- output_name
- type
- is_leaf
- meta
- meta_get
- copy
- add_comments
- pop_comments
- append
- set
- set_kwargs
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- to_s
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- and_
- or_
- not_
- update_positions
- as_
- isin
- between
- is_
- like
- ilike
- eq
- neq
- rlike
- div
- asc
- desc
- args
- parent
- arg_key
- index
- comments
Inherited Members
- sqlglot.expressions.core.Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- to_py
- is_int
- is_star
- alias
- alias_column_names
- name
- alias_or_name
- output_name
- type
- is_type
- is_leaf
- meta
- meta_get
- copy
- add_comments
- pop_comments
- append
- set
- set_kwargs
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- to_s
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- and_
- or_
- not_
- update_positions
- as_
- isin
- between
- is_
- like
- ilike
- eq
- neq
- rlike
- div
- asc
- desc
- args
- parent
- arg_key
- index
- comments
DATA_TYPE =
typing.Union[str, sqlglot.expressions.core.Identifier, sqlglot.expressions.core.Dot, DataType, DType]