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 REF = 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) 178 179 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 450 451 452class PseudoType(DataType): 453 arg_types = {"this": True} 454 455 456class ObjectIdentifier(DataType): 457 arg_types = {"this": True} 458 459 460class IntervalSpan(DataType): 461 arg_types = {"this": True, "expression": True} 462 463 464class Interval(_TimeUnit): 465 # fraction_in_string: a fractional amount was written with its unit in one string, e.g. 466 # INTERVAL '1.5 DAY', which differs from INTERVAL '1.5' DAY in e.g. DuckDB and Postgres 467 arg_types = {"this": False, "unit": False, "fraction_in_string": False} 468 469 470DATA_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
- var_len_arg_key
- 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 REF = auto() 122 ROWVERSION = auto() 123 SERIAL = auto() 124 SET = auto() 125 SMALLDATETIME = auto() 126 SMALLINT = auto() 127 SMALLMONEY = auto() 128 SMALLSERIAL = auto() 129 STRUCT = auto() 130 SUPER = auto() 131 TEXT = auto() 132 TINYBLOB = auto() 133 TINYTEXT = auto() 134 TIME = auto() 135 TIMETZ = auto() 136 TIME_NS = auto() 137 TIMESTAMP = auto() 138 TIMESTAMPNTZ = auto() 139 TIMESTAMPLTZ = auto() 140 TIMESTAMPTZ = auto() 141 TIMESTAMP_S = auto() 142 TIMESTAMP_MS = auto() 143 TIMESTAMP_NS = auto() 144 TINYINT = auto() 145 TSMULTIRANGE = auto() 146 TSRANGE = auto() 147 TSTZMULTIRANGE = auto() 148 TSTZRANGE = auto() 149 UBIGINT = auto() 150 UINT = auto() 151 UINT128 = auto() 152 UINT256 = auto() 153 UMEDIUMINT = auto() 154 UDECIMAL = auto() 155 UDOUBLE = auto() 156 UNION = auto() 157 UNKNOWN = auto() # Sentinel value, useful for type annotation 158 USERDEFINED = "USER-DEFINED" 159 USMALLINT = auto() 160 UTINYINT = auto() 161 UUID = auto() 162 VARBINARY = auto() 163 VARCHAR = auto() 164 VARIANT = auto() 165 VECTOR = auto() 166 XML = auto() 167 YEAR = auto() 168 TDIGEST = auto() 169 170 def into_expr(self, **kwargs: object) -> DataType: 171 """Converts this `DType` into a `DataType` instance. 172 173 Args: 174 **kwargs (object): additional arguments to pass in the constructor of DataType. 175 Returns: 176 DataType: the resulting `DataType` instance. 177 """ 178 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'>
REF =
<DType.REF: 'REF'>
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'>
170 def into_expr(self, **kwargs: object) -> DataType: 171 """Converts this `DType` into a `DataType` instance. 172 173 Args: 174 **kwargs (object): additional arguments to pass in the constructor of DataType. 175 Returns: 176 DataType: the resulting `DataType` instance. 177 """ 178 return DataType(this=self).set_kwargs(kwargs)
181class DataType(Expression): 182 arg_types = { 183 "this": True, 184 "expressions": False, 185 "nested": False, 186 "values": False, 187 "kind": False, 188 "nullable": False, 189 "collate": False, 190 } 191 192 is_data_type: t.ClassVar[bool] = True 193 194 Type: t.ClassVar[Type[DType]] = DType 195 196 STRUCT_TYPES: t.ClassVar[set[DType]] = { 197 DType.FILE, 198 DType.NESTED, 199 DType.OBJECT, 200 DType.STRUCT, 201 DType.UNION, 202 } 203 204 ARRAY_TYPES: t.ClassVar[set[DType]] = { 205 DType.ARRAY, 206 DType.LIST, 207 } 208 209 NESTED_TYPES: t.ClassVar[set[DType]] = { 210 DType.FILE, 211 DType.NESTED, 212 DType.OBJECT, 213 DType.STRUCT, 214 DType.UNION, 215 DType.ARRAY, 216 DType.LIST, 217 DType.MAP, 218 } 219 220 TEXT_TYPES: t.ClassVar[set[DType]] = { 221 DType.CHAR, 222 DType.NCHAR, 223 DType.NVARCHAR, 224 DType.TEXT, 225 DType.TINYTEXT, 226 DType.MEDIUMTEXT, 227 DType.LONGTEXT, 228 DType.VARCHAR, 229 DType.NAME, 230 } 231 232 BINARY_TYPES: t.ClassVar[set[DType]] = { 233 DType.BINARY, 234 DType.VARBINARY, 235 DType.TINYBLOB, 236 DType.BLOB, 237 DType.MEDIUMBLOB, 238 DType.LONGBLOB, 239 } 240 241 SIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = { 242 DType.BIGINT, 243 DType.INT, 244 DType.INT128, 245 DType.INT256, 246 DType.MEDIUMINT, 247 DType.SMALLINT, 248 DType.TINYINT, 249 } 250 251 UNSIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = { 252 DType.UBIGINT, 253 DType.UINT, 254 DType.UINT128, 255 DType.UINT256, 256 DType.UMEDIUMINT, 257 DType.USMALLINT, 258 DType.UTINYINT, 259 } 260 261 INTEGER_TYPES: t.ClassVar[set[DType]] = { 262 DType.BIGINT, 263 DType.INT, 264 DType.INT128, 265 DType.INT256, 266 DType.MEDIUMINT, 267 DType.SMALLINT, 268 DType.TINYINT, 269 DType.UBIGINT, 270 DType.UINT, 271 DType.UINT128, 272 DType.UINT256, 273 DType.UMEDIUMINT, 274 DType.USMALLINT, 275 DType.UTINYINT, 276 DType.BIT, 277 } 278 279 FLOAT_TYPES: t.ClassVar[set[DType]] = { 280 DType.DOUBLE, 281 DType.FLOAT, 282 } 283 284 REAL_TYPES: t.ClassVar[set[DType]] = { 285 DType.DOUBLE, 286 DType.FLOAT, 287 DType.BIGDECIMAL, 288 DType.DECIMAL, 289 DType.DECIMAL32, 290 DType.DECIMAL64, 291 DType.DECIMAL128, 292 DType.DECIMAL256, 293 DType.DECFLOAT, 294 DType.MONEY, 295 DType.SMALLMONEY, 296 DType.UDECIMAL, 297 DType.UDOUBLE, 298 } 299 300 NUMERIC_TYPES: t.ClassVar[set[DType]] = { 301 DType.BIGINT, 302 DType.INT, 303 DType.INT128, 304 DType.INT256, 305 DType.MEDIUMINT, 306 DType.SMALLINT, 307 DType.TINYINT, 308 DType.UBIGINT, 309 DType.UINT, 310 DType.UINT128, 311 DType.UINT256, 312 DType.UMEDIUMINT, 313 DType.USMALLINT, 314 DType.UTINYINT, 315 DType.BIT, 316 DType.DOUBLE, 317 DType.FLOAT, 318 DType.BIGDECIMAL, 319 DType.DECIMAL, 320 DType.DECIMAL32, 321 DType.DECIMAL64, 322 DType.DECIMAL128, 323 DType.DECIMAL256, 324 DType.DECFLOAT, 325 DType.MONEY, 326 DType.SMALLMONEY, 327 DType.UDECIMAL, 328 DType.UDOUBLE, 329 } 330 331 TEMPORAL_TYPES: t.ClassVar[set[DType]] = { 332 DType.DATE, 333 DType.DATE32, 334 DType.DATETIME, 335 DType.DATETIME2, 336 DType.DATETIME64, 337 DType.SMALLDATETIME, 338 DType.TIME, 339 DType.TIMESTAMP, 340 DType.TIMESTAMPNTZ, 341 DType.TIMESTAMPLTZ, 342 DType.TIMESTAMPTZ, 343 DType.TIMESTAMP_MS, 344 DType.TIMESTAMP_NS, 345 DType.TIMESTAMP_S, 346 DType.TIMETZ, 347 } 348 349 @classmethod 350 def build( 351 cls, 352 dtype: DATA_TYPE, 353 dialect: DialectType = None, 354 udt: bool = False, 355 copy: bool = True, 356 **kwargs: Unpack[DataTypeArgs], 357 ) -> Self: 358 """ 359 Constructs a DataType object. 360 361 Args: 362 dtype: the data type of interest. 363 dialect: the dialect to use for parsing `dtype`, in case it's a string. 364 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 365 DataType, thus creating a user-defined type. 366 copy: whether to copy the data type. 367 kwargs: additional arguments to pass in the constructor of DataType. 368 369 Returns: 370 The constructed DataType object. 371 """ 372 if isinstance(dtype, str): 373 return cls.from_str(dtype, dialect, udt, **kwargs) 374 elif isinstance(dtype, DType): 375 data_type_exp = cls(this=dtype) 376 if kwargs: 377 for k, v in kwargs.items(): 378 data_type_exp.set(k, v) 379 return data_type_exp 380 elif isinstance(dtype, (Identifier, Dot)) and udt: 381 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 382 elif isinstance(dtype, cls): 383 return maybe_copy(dtype, copy) 384 else: 385 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DType") 386 387 @classmethod 388 def from_str( 389 cls, 390 dtype: str, 391 dialect: DialectType = None, 392 udt: bool = False, 393 **kwargs: Unpack[DataTypeArgs], 394 ) -> Self: 395 """ 396 Constructs a `DataType` object from a `str` representation. 397 398 Args: 399 dtype: the data type of interest. 400 dialect: the dialect to use for parsing `dtype`. 401 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 402 `DataType`, thus creating a user-defined type. 403 kwargs: additional arguments to pass in the constructor of `DataType`. 404 405 Returns: 406 The constructed `DataType` object. 407 """ 408 from sqlglot import parse_one 409 410 if dtype.upper() == "UNKNOWN": 411 return cls(this=DType.UNKNOWN, **kwargs) 412 try: 413 return parse_one( 414 dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE 415 ).set_kwargs(kwargs) 416 except ParseError: 417 if udt: 418 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 419 raise 420 421 def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool: 422 """ 423 Checks whether this DataType matches one of the provided data types. Nested types or precision 424 will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>. 425 426 Args: 427 dtypes: the data types to compare this DataType to. 428 check_nullable: whether to take the NULLABLE type constructor into account for the comparison. 429 If false, it means that NULLABLE<INT> is equivalent to INT. 430 431 Returns: 432 True, if and only if there is a type in `dtypes` which is equal to this DataType. 433 """ 434 self_is_nullable: bool | None = self.args.get("nullable") 435 for dtype in dtypes: 436 other_type = DataType.build(dtype, copy=False, udt=True) 437 other_is_nullable: bool | None = other_type.args.get("nullable") 438 if ( 439 other_type.expressions 440 or (check_nullable and (self_is_nullable or other_is_nullable)) 441 or self.this == DType.USERDEFINED 442 or other_type.this == DType.USERDEFINED 443 ): 444 matches = self == other_type 445 else: 446 matches = self.this == other_type.this 447 448 if matches: 449 return True 450 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.UNION: 'UNION'>, <DType.NESTED: 'NESTED'>, <DType.OBJECT: 'OBJECT'>, <DType.STRUCT: 'STRUCT'>}
NESTED_TYPES: ClassVar[set[DType]] =
{<DType.FILE: 'FILE'>, <DType.UNION: 'UNION'>, <DType.NESTED: 'NESTED'>, <DType.OBJECT: 'OBJECT'>, <DType.ARRAY: 'ARRAY'>, <DType.STRUCT: 'STRUCT'>, <DType.MAP: 'MAP'>, <DType.LIST: 'LIST'>}
TEXT_TYPES: ClassVar[set[DType]] =
{<DType.LONGTEXT: 'LONGTEXT'>, <DType.NCHAR: 'NCHAR'>, <DType.NVARCHAR: 'NVARCHAR'>, <DType.NAME: 'NAME'>, <DType.TINYTEXT: 'TINYTEXT'>, <DType.CHAR: 'CHAR'>, <DType.TEXT: 'TEXT'>, <DType.MEDIUMTEXT: 'MEDIUMTEXT'>, <DType.VARCHAR: 'VARCHAR'>}
BINARY_TYPES: ClassVar[set[DType]] =
{<DType.TINYBLOB: 'TINYBLOB'>, <DType.LONGBLOB: 'LONGBLOB'>, <DType.VARBINARY: 'VARBINARY'>, <DType.MEDIUMBLOB: 'MEDIUMBLOB'>, <DType.BINARY: 'BINARY'>, <DType.BLOB: 'BLOB'>}
SIGNED_INTEGER_TYPES: ClassVar[set[DType]] =
{<DType.SMALLINT: 'SMALLINT'>, <DType.BIGINT: 'BIGINT'>, <DType.INT: 'INT'>, <DType.INT128: 'INT128'>, <DType.TINYINT: 'TINYINT'>, <DType.INT256: 'INT256'>, <DType.MEDIUMINT: 'MEDIUMINT'>}
UNSIGNED_INTEGER_TYPES: ClassVar[set[DType]] =
{<DType.UTINYINT: 'UTINYINT'>, <DType.UINT: 'UINT'>, <DType.USMALLINT: 'USMALLINT'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.UINT128: 'UINT128'>, <DType.UINT256: 'UINT256'>}
INTEGER_TYPES: ClassVar[set[DType]] =
{<DType.UTINYINT: 'UTINYINT'>, <DType.BIT: 'BIT'>, <DType.UINT: 'UINT'>, <DType.SMALLINT: 'SMALLINT'>, <DType.BIGINT: 'BIGINT'>, <DType.UINT256: 'UINT256'>, <DType.USMALLINT: 'USMALLINT'>, <DType.INT: 'INT'>, <DType.INT128: 'INT128'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.TINYINT: 'TINYINT'>, <DType.UINT128: 'UINT128'>, <DType.INT256: 'INT256'>, <DType.MEDIUMINT: 'MEDIUMINT'>}
REAL_TYPES: ClassVar[set[DType]] =
{<DType.DECIMAL: 'DECIMAL'>, <DType.UDOUBLE: 'UDOUBLE'>, <DType.DECFLOAT: 'DECFLOAT'>, <DType.DECIMAL256: 'DECIMAL256'>, <DType.BIGDECIMAL: 'BIGDECIMAL'>, <DType.MONEY: 'MONEY'>, <DType.SMALLMONEY: 'SMALLMONEY'>, <DType.DECIMAL32: 'DECIMAL32'>, <DType.FLOAT: 'FLOAT'>, <DType.UDECIMAL: 'UDECIMAL'>, <DType.DECIMAL64: 'DECIMAL64'>, <DType.DECIMAL128: 'DECIMAL128'>, <DType.DOUBLE: 'DOUBLE'>}
NUMERIC_TYPES: ClassVar[set[DType]] =
{<DType.DECFLOAT: 'DECFLOAT'>, <DType.UINT256: 'UINT256'>, <DType.INT256: 'INT256'>, <DType.MEDIUMINT: 'MEDIUMINT'>, <DType.DOUBLE: 'DOUBLE'>, <DType.DECIMAL: 'DECIMAL'>, <DType.BIGINT: 'BIGINT'>, <DType.BIGDECIMAL: 'BIGDECIMAL'>, <DType.SMALLMONEY: 'SMALLMONEY'>, <DType.DECIMAL64: 'DECIMAL64'>, <DType.INT: 'INT'>, <DType.INT128: 'INT128'>, <DType.SMALLINT: 'SMALLINT'>, <DType.BIT: 'BIT'>, <DType.UDOUBLE: 'UDOUBLE'>, <DType.DECIMAL256: 'DECIMAL256'>, <DType.MONEY: 'MONEY'>, <DType.FLOAT: 'FLOAT'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.DECIMAL128: 'DECIMAL128'>, <DType.USMALLINT: 'USMALLINT'>, <DType.TINYINT: 'TINYINT'>, <DType.UDECIMAL: 'UDECIMAL'>, <DType.UTINYINT: 'UTINYINT'>, <DType.UINT: 'UINT'>, <DType.DECIMAL32: 'DECIMAL32'>, <DType.UBIGINT: 'UBIGINT'>, <DType.UINT128: 'UINT128'>}
TEMPORAL_TYPES: ClassVar[set[DType]] =
{<DType.DATE: 'DATE'>, <DType.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <DType.TIMESTAMP_S: 'TIMESTAMP_S'>, <DType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <DType.TIMETZ: 'TIMETZ'>, <DType.DATETIME64: 'DATETIME64'>, <DType.DATETIME2: 'DATETIME2'>, <DType.DATE32: 'DATE32'>, <DType.DATETIME: 'DATETIME'>, <DType.TIME: 'TIME'>, <DType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <DType.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <DType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, <DType.SMALLDATETIME: 'SMALLDATETIME'>, <DType.TIMESTAMP: 'TIMESTAMP'>}
@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:
349 @classmethod 350 def build( 351 cls, 352 dtype: DATA_TYPE, 353 dialect: DialectType = None, 354 udt: bool = False, 355 copy: bool = True, 356 **kwargs: Unpack[DataTypeArgs], 357 ) -> Self: 358 """ 359 Constructs a DataType object. 360 361 Args: 362 dtype: the data type of interest. 363 dialect: the dialect to use for parsing `dtype`, in case it's a string. 364 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 365 DataType, thus creating a user-defined type. 366 copy: whether to copy the data type. 367 kwargs: additional arguments to pass in the constructor of DataType. 368 369 Returns: 370 The constructed DataType object. 371 """ 372 if isinstance(dtype, str): 373 return cls.from_str(dtype, dialect, udt, **kwargs) 374 elif isinstance(dtype, DType): 375 data_type_exp = cls(this=dtype) 376 if kwargs: 377 for k, v in kwargs.items(): 378 data_type_exp.set(k, v) 379 return data_type_exp 380 elif isinstance(dtype, (Identifier, Dot)) and udt: 381 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 382 elif isinstance(dtype, cls): 383 return maybe_copy(dtype, copy) 384 else: 385 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:
387 @classmethod 388 def from_str( 389 cls, 390 dtype: str, 391 dialect: DialectType = None, 392 udt: bool = False, 393 **kwargs: Unpack[DataTypeArgs], 394 ) -> Self: 395 """ 396 Constructs a `DataType` object from a `str` representation. 397 398 Args: 399 dtype: the data type of interest. 400 dialect: the dialect to use for parsing `dtype`. 401 udt: when set to True, `dtype` will be used as-is if it can't be parsed into a 402 `DataType`, thus creating a user-defined type. 403 kwargs: additional arguments to pass in the constructor of `DataType`. 404 405 Returns: 406 The constructed `DataType` object. 407 """ 408 from sqlglot import parse_one 409 410 if dtype.upper() == "UNKNOWN": 411 return cls(this=DType.UNKNOWN, **kwargs) 412 try: 413 return parse_one( 414 dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE 415 ).set_kwargs(kwargs) 416 except ParseError: 417 if udt: 418 return cls(this=DType.USERDEFINED, kind=dtype, **kwargs) 419 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:
421 def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool: 422 """ 423 Checks whether this DataType matches one of the provided data types. Nested types or precision 424 will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>. 425 426 Args: 427 dtypes: the data types to compare this DataType to. 428 check_nullable: whether to take the NULLABLE type constructor into account for the comparison. 429 If false, it means that NULLABLE<INT> is equivalent to INT. 430 431 Returns: 432 True, if and only if there is a type in `dtypes` which is equal to this DataType. 433 """ 434 self_is_nullable: bool | None = self.args.get("nullable") 435 for dtype in dtypes: 436 other_type = DataType.build(dtype, copy=False, udt=True) 437 other_is_nullable: bool | None = other_type.args.get("nullable") 438 if ( 439 other_type.expressions 440 or (check_nullable and (self_is_nullable or other_is_nullable)) 441 or self.this == DType.USERDEFINED 442 or other_type.this == DType.USERDEFINED 443 ): 444 matches = self == other_type 445 else: 446 matches = self.this == other_type.this 447 448 if matches: 449 return True 450 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
- var_len_arg_key
- 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
- var_len_arg_key
- 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
- var_len_arg_key
- 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
- var_len_arg_key
- 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
465class Interval(_TimeUnit): 466 # fraction_in_string: a fractional amount was written with its unit in one string, e.g. 467 # INTERVAL '1.5 DAY', which differs from INTERVAL '1.5' DAY in e.g. DuckDB and Postgres 468 arg_types = {"this": False, "unit": False, "fraction_in_string": False}
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]