Edit on GitHub

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.VARCHAR,
224        DType.NAME,
225    }
226
227    SIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = {
228        DType.BIGINT,
229        DType.INT,
230        DType.INT128,
231        DType.INT256,
232        DType.MEDIUMINT,
233        DType.SMALLINT,
234        DType.TINYINT,
235    }
236
237    UNSIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = {
238        DType.UBIGINT,
239        DType.UINT,
240        DType.UINT128,
241        DType.UINT256,
242        DType.UMEDIUMINT,
243        DType.USMALLINT,
244        DType.UTINYINT,
245    }
246
247    INTEGER_TYPES: t.ClassVar[set[DType]] = {
248        DType.BIGINT,
249        DType.INT,
250        DType.INT128,
251        DType.INT256,
252        DType.MEDIUMINT,
253        DType.SMALLINT,
254        DType.TINYINT,
255        DType.UBIGINT,
256        DType.UINT,
257        DType.UINT128,
258        DType.UINT256,
259        DType.UMEDIUMINT,
260        DType.USMALLINT,
261        DType.UTINYINT,
262        DType.BIT,
263    }
264
265    FLOAT_TYPES: t.ClassVar[set[DType]] = {
266        DType.DOUBLE,
267        DType.FLOAT,
268    }
269
270    REAL_TYPES: t.ClassVar[set[DType]] = {
271        DType.DOUBLE,
272        DType.FLOAT,
273        DType.BIGDECIMAL,
274        DType.DECIMAL,
275        DType.DECIMAL32,
276        DType.DECIMAL64,
277        DType.DECIMAL128,
278        DType.DECIMAL256,
279        DType.DECFLOAT,
280        DType.MONEY,
281        DType.SMALLMONEY,
282        DType.UDECIMAL,
283        DType.UDOUBLE,
284    }
285
286    NUMERIC_TYPES: t.ClassVar[set[DType]] = {
287        DType.BIGINT,
288        DType.INT,
289        DType.INT128,
290        DType.INT256,
291        DType.MEDIUMINT,
292        DType.SMALLINT,
293        DType.TINYINT,
294        DType.UBIGINT,
295        DType.UINT,
296        DType.UINT128,
297        DType.UINT256,
298        DType.UMEDIUMINT,
299        DType.USMALLINT,
300        DType.UTINYINT,
301        DType.BIT,
302        DType.DOUBLE,
303        DType.FLOAT,
304        DType.BIGDECIMAL,
305        DType.DECIMAL,
306        DType.DECIMAL32,
307        DType.DECIMAL64,
308        DType.DECIMAL128,
309        DType.DECIMAL256,
310        DType.DECFLOAT,
311        DType.MONEY,
312        DType.SMALLMONEY,
313        DType.UDECIMAL,
314        DType.UDOUBLE,
315    }
316
317    TEMPORAL_TYPES: t.ClassVar[set[DType]] = {
318        DType.DATE,
319        DType.DATE32,
320        DType.DATETIME,
321        DType.DATETIME2,
322        DType.DATETIME64,
323        DType.SMALLDATETIME,
324        DType.TIME,
325        DType.TIMESTAMP,
326        DType.TIMESTAMPNTZ,
327        DType.TIMESTAMPLTZ,
328        DType.TIMESTAMPTZ,
329        DType.TIMESTAMP_MS,
330        DType.TIMESTAMP_NS,
331        DType.TIMESTAMP_S,
332        DType.TIMETZ,
333    }
334
335    @classmethod
336    def build(
337        cls,
338        dtype: DATA_TYPE,
339        dialect: DialectType = None,
340        udt: bool = False,
341        copy: bool = True,
342        **kwargs: Unpack[DataTypeArgs],
343    ) -> Self:
344        """
345        Constructs a DataType object.
346
347        Args:
348            dtype: the data type of interest.
349            dialect: the dialect to use for parsing `dtype`, in case it's a string.
350            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
351                DataType, thus creating a user-defined type.
352            copy: whether to copy the data type.
353            kwargs: additional arguments to pass in the constructor of DataType.
354
355        Returns:
356            The constructed DataType object.
357        """
358        if isinstance(dtype, str):
359            return cls.from_str(dtype, dialect, udt, **kwargs)
360        elif isinstance(dtype, DType):
361            data_type_exp = cls(this=dtype)
362            if kwargs:
363                for k, v in kwargs.items():
364                    data_type_exp.set(k, v)
365            return data_type_exp
366        elif isinstance(dtype, (Identifier, Dot)) and udt:
367            return cls(this=DType.USERDEFINED, kind=dtype, **kwargs)
368        elif isinstance(dtype, cls):
369            return maybe_copy(dtype, copy)
370        else:
371            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DType")
372
373    @classmethod
374    def from_str(
375        cls,
376        dtype: str,
377        dialect: DialectType = None,
378        udt: bool = False,
379        **kwargs: Unpack[DataTypeArgs],
380    ) -> Self:
381        """
382        Constructs a `DataType` object from a `str` representation.
383
384        Args:
385            dtype: the data type of interest.
386            dialect: the dialect to use for parsing `dtype`.
387            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
388                `DataType`, thus creating a user-defined type.
389            kwargs: additional arguments to pass in the constructor of `DataType`.
390
391        Returns:
392            The constructed `DataType` object.
393        """
394        from sqlglot import parse_one
395
396        if dtype.upper() == "UNKNOWN":
397            return cls(this=DType.UNKNOWN, **kwargs)
398        try:
399            return parse_one(
400                dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE
401            ).set_kwargs(kwargs)
402        except ParseError:
403            if udt:
404                return cls(this=DType.USERDEFINED, kind=dtype, **kwargs)
405            raise
406
407    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
408        """
409        Checks whether this DataType matches one of the provided data types. Nested types or precision
410        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
411
412        Args:
413            dtypes: the data types to compare this DataType to.
414            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
415                If false, it means that NULLABLE<INT> is equivalent to INT.
416
417        Returns:
418            True, if and only if there is a type in `dtypes` which is equal to this DataType.
419        """
420        self_is_nullable: bool | None = self.args.get("nullable")
421        for dtype in dtypes:
422            other_type = DataType.build(dtype, copy=False, udt=True)
423            other_is_nullable: bool | None = other_type.args.get("nullable")
424            if (
425                other_type.expressions
426                or (check_nullable and (self_is_nullable or other_is_nullable))
427                or self.this == DType.USERDEFINED
428                or other_type.this == DType.USERDEFINED
429            ):
430                matches = self == other_type
431            else:
432                matches = self.this == other_type.this
433
434            if matches:
435                return True
436        return False
437
438
439class PseudoType(DataType):
440    arg_types = {"this": True}
441
442
443class ObjectIdentifier(DataType):
444    arg_types = {"this": True}
445
446
447class IntervalSpan(DataType):
448    arg_types = {"this": True, "expression": True}
449
450
451class Interval(_TimeUnit):
452    arg_types = {"this": False, "unit": False}
453
454
455DATA_TYPE = t.Union[str, Identifier, Dot, DataType, DType]
class DataTypeParam(sqlglot.expressions.core.Expression):
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
arg_types = {'this': True, 'expression': False}
this: sqlglot.expressions.core.Expr
30    @property
31    def this(self) -> Expr:
32        return self.args["this"]

Retrieves the argument with key "this".

name: str
34    @property
35    def name(self) -> str:
36        return self.this.name
key: ClassVar[str] = 'datatypeparam'
required_args: 't.ClassVar[set[str]]' = {'this'}
class DType(sqlglot.helper.AutoName):
 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'>
def into_expr(self, **kwargs: object) -> DataType:
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)

Converts this DType into a DataType instance.

Arguments:
  • **kwargs (object): additional arguments to pass in the constructor of DataType.
Returns:

DataType: the resulting DataType instance.

class DataType(sqlglot.expressions.core.Expression):
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.VARCHAR,
225        DType.NAME,
226    }
227
228    SIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = {
229        DType.BIGINT,
230        DType.INT,
231        DType.INT128,
232        DType.INT256,
233        DType.MEDIUMINT,
234        DType.SMALLINT,
235        DType.TINYINT,
236    }
237
238    UNSIGNED_INTEGER_TYPES: t.ClassVar[set[DType]] = {
239        DType.UBIGINT,
240        DType.UINT,
241        DType.UINT128,
242        DType.UINT256,
243        DType.UMEDIUMINT,
244        DType.USMALLINT,
245        DType.UTINYINT,
246    }
247
248    INTEGER_TYPES: t.ClassVar[set[DType]] = {
249        DType.BIGINT,
250        DType.INT,
251        DType.INT128,
252        DType.INT256,
253        DType.MEDIUMINT,
254        DType.SMALLINT,
255        DType.TINYINT,
256        DType.UBIGINT,
257        DType.UINT,
258        DType.UINT128,
259        DType.UINT256,
260        DType.UMEDIUMINT,
261        DType.USMALLINT,
262        DType.UTINYINT,
263        DType.BIT,
264    }
265
266    FLOAT_TYPES: t.ClassVar[set[DType]] = {
267        DType.DOUBLE,
268        DType.FLOAT,
269    }
270
271    REAL_TYPES: t.ClassVar[set[DType]] = {
272        DType.DOUBLE,
273        DType.FLOAT,
274        DType.BIGDECIMAL,
275        DType.DECIMAL,
276        DType.DECIMAL32,
277        DType.DECIMAL64,
278        DType.DECIMAL128,
279        DType.DECIMAL256,
280        DType.DECFLOAT,
281        DType.MONEY,
282        DType.SMALLMONEY,
283        DType.UDECIMAL,
284        DType.UDOUBLE,
285    }
286
287    NUMERIC_TYPES: t.ClassVar[set[DType]] = {
288        DType.BIGINT,
289        DType.INT,
290        DType.INT128,
291        DType.INT256,
292        DType.MEDIUMINT,
293        DType.SMALLINT,
294        DType.TINYINT,
295        DType.UBIGINT,
296        DType.UINT,
297        DType.UINT128,
298        DType.UINT256,
299        DType.UMEDIUMINT,
300        DType.USMALLINT,
301        DType.UTINYINT,
302        DType.BIT,
303        DType.DOUBLE,
304        DType.FLOAT,
305        DType.BIGDECIMAL,
306        DType.DECIMAL,
307        DType.DECIMAL32,
308        DType.DECIMAL64,
309        DType.DECIMAL128,
310        DType.DECIMAL256,
311        DType.DECFLOAT,
312        DType.MONEY,
313        DType.SMALLMONEY,
314        DType.UDECIMAL,
315        DType.UDOUBLE,
316    }
317
318    TEMPORAL_TYPES: t.ClassVar[set[DType]] = {
319        DType.DATE,
320        DType.DATE32,
321        DType.DATETIME,
322        DType.DATETIME2,
323        DType.DATETIME64,
324        DType.SMALLDATETIME,
325        DType.TIME,
326        DType.TIMESTAMP,
327        DType.TIMESTAMPNTZ,
328        DType.TIMESTAMPLTZ,
329        DType.TIMESTAMPTZ,
330        DType.TIMESTAMP_MS,
331        DType.TIMESTAMP_NS,
332        DType.TIMESTAMP_S,
333        DType.TIMETZ,
334    }
335
336    @classmethod
337    def build(
338        cls,
339        dtype: DATA_TYPE,
340        dialect: DialectType = None,
341        udt: bool = False,
342        copy: bool = True,
343        **kwargs: Unpack[DataTypeArgs],
344    ) -> Self:
345        """
346        Constructs a DataType object.
347
348        Args:
349            dtype: the data type of interest.
350            dialect: the dialect to use for parsing `dtype`, in case it's a string.
351            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
352                DataType, thus creating a user-defined type.
353            copy: whether to copy the data type.
354            kwargs: additional arguments to pass in the constructor of DataType.
355
356        Returns:
357            The constructed DataType object.
358        """
359        if isinstance(dtype, str):
360            return cls.from_str(dtype, dialect, udt, **kwargs)
361        elif isinstance(dtype, DType):
362            data_type_exp = cls(this=dtype)
363            if kwargs:
364                for k, v in kwargs.items():
365                    data_type_exp.set(k, v)
366            return data_type_exp
367        elif isinstance(dtype, (Identifier, Dot)) and udt:
368            return cls(this=DType.USERDEFINED, kind=dtype, **kwargs)
369        elif isinstance(dtype, cls):
370            return maybe_copy(dtype, copy)
371        else:
372            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DType")
373
374    @classmethod
375    def from_str(
376        cls,
377        dtype: str,
378        dialect: DialectType = None,
379        udt: bool = False,
380        **kwargs: Unpack[DataTypeArgs],
381    ) -> Self:
382        """
383        Constructs a `DataType` object from a `str` representation.
384
385        Args:
386            dtype: the data type of interest.
387            dialect: the dialect to use for parsing `dtype`.
388            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
389                `DataType`, thus creating a user-defined type.
390            kwargs: additional arguments to pass in the constructor of `DataType`.
391
392        Returns:
393            The constructed `DataType` object.
394        """
395        from sqlglot import parse_one
396
397        if dtype.upper() == "UNKNOWN":
398            return cls(this=DType.UNKNOWN, **kwargs)
399        try:
400            return parse_one(
401                dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE
402            ).set_kwargs(kwargs)
403        except ParseError:
404            if udt:
405                return cls(this=DType.USERDEFINED, kind=dtype, **kwargs)
406            raise
407
408    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
409        """
410        Checks whether this DataType matches one of the provided data types. Nested types or precision
411        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
412
413        Args:
414            dtypes: the data types to compare this DataType to.
415            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
416                If false, it means that NULLABLE<INT> is equivalent to INT.
417
418        Returns:
419            True, if and only if there is a type in `dtypes` which is equal to this DataType.
420        """
421        self_is_nullable: bool | None = self.args.get("nullable")
422        for dtype in dtypes:
423            other_type = DataType.build(dtype, copy=False, udt=True)
424            other_is_nullable: bool | None = other_type.args.get("nullable")
425            if (
426                other_type.expressions
427                or (check_nullable and (self_is_nullable or other_is_nullable))
428                or self.this == DType.USERDEFINED
429                or other_type.this == DType.USERDEFINED
430            ):
431                matches = self == other_type
432            else:
433                matches = self.this == other_type.this
434
435            if matches:
436                return True
437        return False
arg_types = {'this': True, 'expressions': False, 'nested': False, 'values': False, 'kind': False, 'nullable': False, 'collate': False}
is_data_type: ClassVar[bool] = True
Type: 't.ClassVar[Type[DType]]' = <enum 'DType'>
STRUCT_TYPES: ClassVar[set[DType]] = {<DType.NESTED: 'NESTED'>, <DType.OBJECT: 'OBJECT'>, <DType.UNION: 'UNION'>, <DType.STRUCT: 'STRUCT'>, <DType.FILE: 'FILE'>}
ARRAY_TYPES: ClassVar[set[DType]] = {<DType.LIST: 'LIST'>, <DType.ARRAY: 'ARRAY'>}
NESTED_TYPES: ClassVar[set[DType]] = {<DType.LIST: 'LIST'>, <DType.NESTED: 'NESTED'>, <DType.MAP: 'MAP'>, <DType.ARRAY: 'ARRAY'>, <DType.OBJECT: 'OBJECT'>, <DType.UNION: 'UNION'>, <DType.STRUCT: 'STRUCT'>, <DType.FILE: 'FILE'>}
TEXT_TYPES: ClassVar[set[DType]] = {<DType.TEXT: 'TEXT'>, <DType.NCHAR: 'NCHAR'>, <DType.CHAR: 'CHAR'>, <DType.NVARCHAR: 'NVARCHAR'>, <DType.NAME: 'NAME'>, <DType.VARCHAR: 'VARCHAR'>}
SIGNED_INTEGER_TYPES: ClassVar[set[DType]] = {<DType.INT128: 'INT128'>, <DType.MEDIUMINT: 'MEDIUMINT'>, <DType.BIGINT: 'BIGINT'>, <DType.INT: 'INT'>, <DType.INT256: 'INT256'>, <DType.TINYINT: 'TINYINT'>, <DType.SMALLINT: 'SMALLINT'>}
UNSIGNED_INTEGER_TYPES: ClassVar[set[DType]] = {<DType.USMALLINT: 'USMALLINT'>, <DType.UTINYINT: 'UTINYINT'>, <DType.UINT128: 'UINT128'>, <DType.UINT256: 'UINT256'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.UINT: 'UINT'>}
INTEGER_TYPES: ClassVar[set[DType]] = {<DType.USMALLINT: 'USMALLINT'>, <DType.UTINYINT: 'UTINYINT'>, <DType.INT128: 'INT128'>, <DType.MEDIUMINT: 'MEDIUMINT'>, <DType.UINT128: 'UINT128'>, <DType.BIT: 'BIT'>, <DType.BIGINT: 'BIGINT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.INT: 'INT'>, <DType.UINT256: 'UINT256'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.INT256: 'INT256'>, <DType.TINYINT: 'TINYINT'>, <DType.UINT: 'UINT'>, <DType.SMALLINT: 'SMALLINT'>}
FLOAT_TYPES: ClassVar[set[DType]] = {<DType.DOUBLE: 'DOUBLE'>, <DType.FLOAT: 'FLOAT'>}
REAL_TYPES: ClassVar[set[DType]] = {<DType.MONEY: 'MONEY'>, <DType.SMALLMONEY: 'SMALLMONEY'>, <DType.UDECIMAL: 'UDECIMAL'>, <DType.DECIMAL32: 'DECIMAL32'>, <DType.DECIMAL128: 'DECIMAL128'>, <DType.DECIMAL64: 'DECIMAL64'>, <DType.DECIMAL: 'DECIMAL'>, <DType.UDOUBLE: 'UDOUBLE'>, <DType.DECFLOAT: 'DECFLOAT'>, <DType.BIGDECIMAL: 'BIGDECIMAL'>, <DType.DOUBLE: 'DOUBLE'>, <DType.DECIMAL256: 'DECIMAL256'>, <DType.FLOAT: 'FLOAT'>}
NUMERIC_TYPES: ClassVar[set[DType]] = {<DType.INT128: 'INT128'>, <DType.MEDIUMINT: 'MEDIUMINT'>, <DType.UINT256: 'UINT256'>, <DType.BIGDECIMAL: 'BIGDECIMAL'>, <DType.DECFLOAT: 'DECFLOAT'>, <DType.MONEY: 'MONEY'>, <DType.UINT128: 'UINT128'>, <DType.DECIMAL64: 'DECIMAL64'>, <DType.UMEDIUMINT: 'UMEDIUMINT'>, <DType.TINYINT: 'TINYINT'>, <DType.DOUBLE: 'DOUBLE'>, <DType.SMALLINT: 'SMALLINT'>, <DType.SMALLMONEY: 'SMALLMONEY'>, <DType.DECIMAL32: 'DECIMAL32'>, <DType.DECIMAL: 'DECIMAL'>, <DType.UDOUBLE: 'UDOUBLE'>, <DType.BIGINT: 'BIGINT'>, <DType.UBIGINT: 'UBIGINT'>, <DType.INT256: 'INT256'>, <DType.DECIMAL256: 'DECIMAL256'>, <DType.USMALLINT: 'USMALLINT'>, <DType.UTINYINT: 'UTINYINT'>, <DType.UDECIMAL: 'UDECIMAL'>, <DType.DECIMAL128: 'DECIMAL128'>, <DType.INT: 'INT'>, <DType.BIT: 'BIT'>, <DType.UINT: 'UINT'>, <DType.FLOAT: 'FLOAT'>}
TEMPORAL_TYPES: ClassVar[set[DType]] = {<DType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <DType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, <DType.DATETIME: 'DATETIME'>, <DType.TIME: 'TIME'>, <DType.DATE: 'DATE'>, <DType.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <DType.DATE32: 'DATE32'>, <DType.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <DType.DATETIME2: 'DATETIME2'>, <DType.DATETIME64: 'DATETIME64'>, <DType.TIMETZ: 'TIMETZ'>, <DType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <DType.SMALLDATETIME: 'SMALLDATETIME'>, <DType.TIMESTAMP: 'TIMESTAMP'>, <DType.TIMESTAMP_S: 'TIMESTAMP_S'>}
@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:
336    @classmethod
337    def build(
338        cls,
339        dtype: DATA_TYPE,
340        dialect: DialectType = None,
341        udt: bool = False,
342        copy: bool = True,
343        **kwargs: Unpack[DataTypeArgs],
344    ) -> Self:
345        """
346        Constructs a DataType object.
347
348        Args:
349            dtype: the data type of interest.
350            dialect: the dialect to use for parsing `dtype`, in case it's a string.
351            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
352                DataType, thus creating a user-defined type.
353            copy: whether to copy the data type.
354            kwargs: additional arguments to pass in the constructor of DataType.
355
356        Returns:
357            The constructed DataType object.
358        """
359        if isinstance(dtype, str):
360            return cls.from_str(dtype, dialect, udt, **kwargs)
361        elif isinstance(dtype, DType):
362            data_type_exp = cls(this=dtype)
363            if kwargs:
364                for k, v in kwargs.items():
365                    data_type_exp.set(k, v)
366            return data_type_exp
367        elif isinstance(dtype, (Identifier, Dot)) and udt:
368            return cls(this=DType.USERDEFINED, kind=dtype, **kwargs)
369        elif isinstance(dtype, cls):
370            return maybe_copy(dtype, copy)
371        else:
372            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, dtype will 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:
374    @classmethod
375    def from_str(
376        cls,
377        dtype: str,
378        dialect: DialectType = None,
379        udt: bool = False,
380        **kwargs: Unpack[DataTypeArgs],
381    ) -> Self:
382        """
383        Constructs a `DataType` object from a `str` representation.
384
385        Args:
386            dtype: the data type of interest.
387            dialect: the dialect to use for parsing `dtype`.
388            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
389                `DataType`, thus creating a user-defined type.
390            kwargs: additional arguments to pass in the constructor of `DataType`.
391
392        Returns:
393            The constructed `DataType` object.
394        """
395        from sqlglot import parse_one
396
397        if dtype.upper() == "UNKNOWN":
398            return cls(this=DType.UNKNOWN, **kwargs)
399        try:
400            return parse_one(
401                dtype, read=dialect, into=cls, error_level=ErrorLevel.IGNORE
402            ).set_kwargs(kwargs)
403        except ParseError:
404            if udt:
405                return cls(this=DType.USERDEFINED, kind=dtype, **kwargs)
406            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, dtype will be used as-is if it can't be parsed into a DataType, thus creating a user-defined type.
  • kwargs: additional arguments to pass in the constructor of DataType.
Returns:

The constructed DataType object.

def is_type( self, *dtypes: Union[str, sqlglot.expressions.core.Identifier, sqlglot.expressions.core.Dot, DataType, DType], check_nullable: bool = False) -> bool:
408    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
409        """
410        Checks whether this DataType matches one of the provided data types. Nested types or precision
411        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
412
413        Args:
414            dtypes: the data types to compare this DataType to.
415            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
416                If false, it means that NULLABLE<INT> is equivalent to INT.
417
418        Returns:
419            True, if and only if there is a type in `dtypes` which is equal to this DataType.
420        """
421        self_is_nullable: bool | None = self.args.get("nullable")
422        for dtype in dtypes:
423            other_type = DataType.build(dtype, copy=False, udt=True)
424            other_is_nullable: bool | None = other_type.args.get("nullable")
425            if (
426                other_type.expressions
427                or (check_nullable and (self_is_nullable or other_is_nullable))
428                or self.this == DType.USERDEFINED
429                or other_type.this == DType.USERDEFINED
430            ):
431                matches = self == other_type
432            else:
433                matches = self.this == other_type.this
434
435            if matches:
436                return True
437        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 != 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 dtypes which is equal to this DataType.

key: ClassVar[str] = 'datatype'
required_args: 't.ClassVar[set[str]]' = {'this'}
class PseudoType(DataType):
440class PseudoType(DataType):
441    arg_types = {"this": True}
arg_types = {'this': True}
key: ClassVar[str] = 'pseudotype'
required_args: 't.ClassVar[set[str]]' = {'this'}
class ObjectIdentifier(DataType):
444class ObjectIdentifier(DataType):
445    arg_types = {"this": True}
arg_types = {'this': True}
key: ClassVar[str] = 'objectidentifier'
required_args: 't.ClassVar[set[str]]' = {'this'}
class IntervalSpan(DataType):
448class IntervalSpan(DataType):
449    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key: ClassVar[str] = 'intervalspan'
required_args: 't.ClassVar[set[str]]' = {'this', 'expression'}
class Interval(sqlglot.expressions.core._TimeUnit):
452class Interval(_TimeUnit):
453    arg_types = {"this": False, "unit": False}
arg_types = {'this': False, 'unit': False}
key: ClassVar[str] = 'interval'
required_args: 't.ClassVar[set[str]]' = set()