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

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.

def is_type( self, *dtypes: Union[str, sqlglot.expressions.core.Identifier, sqlglot.expressions.core.Dot, DataType, DType], check_nullable: bool = False) -> bool:
376    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
377        """
378        Checks whether this DataType matches one of the provided data types. Nested types or precision
379        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
380
381        Args:
382            dtypes: the data types to compare this DataType to.
383            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
384                If false, it means that NULLABLE<INT> is equivalent to INT.
385
386        Returns:
387            True, if and only if there is a type in `dtypes` which is equal to this DataType.
388        """
389        self_is_nullable = self.args.get("nullable")
390        for dtype in dtypes:
391            other_type = DataType.build(dtype, copy=False, udt=True)
392            other_is_nullable = other_type.args.get("nullable")
393            if (
394                other_type.expressions
395                or (check_nullable and (self_is_nullable or other_is_nullable))
396                or self.this == DType.USERDEFINED
397                or other_type.this == DType.USERDEFINED
398            ):
399                matches = self == other_type
400            else:
401                matches = self.this == other_type.this
402
403            if matches:
404                return True
405        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):
408class PseudoType(DataType):
409    arg_types = {"this": True}
arg_types = {'this': True}
key: ClassVar[str] = 'pseudotype'
required_args: 't.ClassVar[set[str]]' = {'this'}
class ObjectIdentifier(DataType):
412class ObjectIdentifier(DataType):
413    arg_types = {"this": True}
arg_types = {'this': True}
key: ClassVar[str] = 'objectidentifier'
required_args: 't.ClassVar[set[str]]' = {'this'}
class IntervalSpan(DataType):
416class IntervalSpan(DataType):
417    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key: ClassVar[str] = 'intervalspan'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
class Interval(sqlglot.expressions.core._TimeUnit):
420class Interval(_TimeUnit):
421    arg_types = {"this": False, "unit": False}
arg_types = {'this': False, 'unit': False}
key: ClassVar[str] = 'interval'
required_args: 't.ClassVar[set[str]]' = set()