sqlparser/ast/data_type.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#[cfg(not(feature = "std"))]
19use alloc::{boxed::Box, format, string::String, vec::Vec};
20use core::fmt;
21
22#[cfg(feature = "serde")]
23use serde::{Deserialize, Serialize};
24
25#[cfg(feature = "visitor")]
26use sqlparser_derive::{Visit, VisitMut};
27
28use crate::ast::{display_comma_separated, Expr, ObjectName, StructField, UnionField};
29
30use super::{value::escape_single_quote_string, ColumnDef};
31
32#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
33#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
34#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
35/// A member of an ENUM type.
36pub enum EnumMember {
37 /// Just a name.
38 Name(String),
39 /// ClickHouse allows to specify an integer value for each enum value.
40 ///
41 /// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/data-types/enum)
42 NamedValue(String, Expr),
43}
44
45/// SQL data types
46#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
49pub enum DataType {
50 /// Table type in [PostgreSQL], e.g. CREATE FUNCTION RETURNS TABLE(...).
51 ///
52 /// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
53 /// [MsSQL]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16#c-create-a-multi-statement-table-valued-function
54 Table(Option<Vec<ColumnDef>>),
55 /// Table type with a name, e.g. CREATE FUNCTION RETURNS @result TABLE(...).
56 ///
57 /// [MsSQl]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16#table
58 NamedTable {
59 /// Table name.
60 name: ObjectName,
61 /// Table columns.
62 columns: Vec<ColumnDef>,
63 },
64 /// Fixed-length character type, e.g. CHARACTER(10).
65 Character(Option<CharacterLength>),
66 /// Fixed-length char type, e.g. CHAR(10).
67 Char(Option<CharacterLength>),
68 /// Character varying type, e.g. CHARACTER VARYING(10).
69 CharacterVarying(Option<CharacterLength>),
70 /// Char varying type, e.g. CHAR VARYING(10).
71 CharVarying(Option<CharacterLength>),
72 /// Variable-length character type, e.g. VARCHAR(10).
73 Varchar(Option<CharacterLength>),
74 /// Variable-length character type, e.g. NVARCHAR(10).
75 Nvarchar(Option<CharacterLength>),
76 /// Uuid type.
77 Uuid,
78 /// Large character object with optional length,
79 /// e.g. CHARACTER LARGE OBJECT, CHARACTER LARGE OBJECT(1000), [SQL Standard].
80 ///
81 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
82 CharacterLargeObject(Option<u64>),
83 /// Large character object with optional length,
84 /// e.g. CHAR LARGE OBJECT, CHAR LARGE OBJECT(1000), [SQL Standard].
85 ///
86 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
87 CharLargeObject(Option<u64>),
88 /// Large character object with optional length,
89 /// e.g. CLOB, CLOB(1000), [SQL Standard].
90 ///
91 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
92 /// [Oracle]: https://docs.oracle.com/javadb/10.10.1.2/ref/rrefclob.html
93 Clob(Option<u64>),
94 /// Fixed-length binary type with optional length,
95 /// see [SQL Standard], [MS SQL Server].
96 ///
97 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
98 /// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
99 Binary(Option<u64>),
100 /// Variable-length binary with optional length type,
101 /// see [SQL Standard], [MS SQL Server].
102 ///
103 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
104 /// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
105 Varbinary(Option<BinaryLength>),
106 /// Large binary object with optional length,
107 /// see [SQL Standard], [Oracle].
108 ///
109 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-large-object-string-type
110 /// [Oracle]: https://docs.oracle.com/javadb/10.8.3.0/ref/rrefblob.html
111 Blob(Option<u64>),
112 /// [MySQL] blob with up to 2**8 bytes.
113 ///
114 /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
115 TinyBlob,
116 /// [MySQL] blob with up to 2**24 bytes.
117 ///
118 /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
119 MediumBlob,
120 /// [MySQL] blob with up to 2**32 bytes.
121 ///
122 /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
123 LongBlob,
124 /// Variable-length binary data with optional length.
125 ///
126 /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#bytes_type
127 Bytes(Option<u64>),
128 /// Numeric type with optional precision and scale, e.g. NUMERIC(10,2), [SQL Standard][1].
129 ///
130 /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
131 Numeric(ExactNumberInfo),
132 /// Decimal type with optional precision and scale, e.g. DECIMAL(10,2), [SQL Standard][1].
133 ///
134 /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
135 Decimal(ExactNumberInfo),
136 /// [MySQL] unsigned decimal with optional precision and scale, e.g. DECIMAL UNSIGNED or DECIMAL(10,2) UNSIGNED.
137 /// Note: Using UNSIGNED with DECIMAL is deprecated in recent versions of MySQL.
138 ///
139 /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html
140 DecimalUnsigned(ExactNumberInfo),
141 /// [BigNumeric] type used in BigQuery.
142 ///
143 /// [BigNumeric]: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#bignumeric_literals
144 BigNumeric(ExactNumberInfo),
145 /// This is alias for `BigNumeric` type used in BigQuery.
146 ///
147 /// [BigDecimal]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types
148 BigDecimal(ExactNumberInfo),
149 /// Dec type with optional precision and scale, e.g. DEC(10,2), [SQL Standard][1].
150 ///
151 /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
152 Dec(ExactNumberInfo),
153 /// [MySQL] unsigned decimal (DEC alias) with optional precision and scale, e.g. DEC UNSIGNED or DEC(10,2) UNSIGNED.
154 /// Note: Using UNSIGNED with DEC is deprecated in recent versions of MySQL.
155 ///
156 /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html
157 DecUnsigned(ExactNumberInfo),
158 /// Floating point with optional precision and scale, e.g. FLOAT, FLOAT(8), or FLOAT(8,2).
159 Float(ExactNumberInfo),
160 /// [MySQL] unsigned floating point with optional precision and scale, e.g.
161 /// FLOAT UNSIGNED, FLOAT(10) UNSIGNED or FLOAT(10,2) UNSIGNED.
162 /// Note: Using UNSIGNED with FLOAT is deprecated in recent versions of MySQL.
163 ///
164 /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html
165 FloatUnsigned(ExactNumberInfo),
166 /// Tiny integer with optional display width, e.g. TINYINT or TINYINT(3).
167 TinyInt(Option<u64>),
168 /// Unsigned tiny integer with optional display width,
169 /// e.g. TINYINT UNSIGNED or TINYINT(3) UNSIGNED.
170 TinyIntUnsigned(Option<u64>),
171 /// Unsigned tiny integer, e.g. UTINYINT
172 UTinyInt,
173 /// Int2 is an alias for SmallInt in [PostgreSQL].
174 /// Note: Int2 means 2 bytes in PostgreSQL (not 2 bits).
175 /// Int2 with optional display width, e.g. INT2 or INT2(5).
176 ///
177 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
178 Int2(Option<u64>),
179 /// Unsigned Int2 with optional display width, e.g. INT2 UNSIGNED or INT2(5) UNSIGNED.
180 Int2Unsigned(Option<u64>),
181 /// Small integer with optional display width, e.g. SMALLINT or SMALLINT(5).
182 SmallInt(Option<u64>),
183 /// Unsigned small integer with optional display width,
184 /// e.g. SMALLINT UNSIGNED or SMALLINT(5) UNSIGNED.
185 SmallIntUnsigned(Option<u64>),
186 /// Unsigned small integer, e.g. USMALLINT.
187 USmallInt,
188 /// MySQL medium integer ([1]) with optional display width,
189 /// e.g. MEDIUMINT or MEDIUMINT(5).
190 ///
191 /// [1]: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
192 MediumInt(Option<u64>),
193 /// Unsigned medium integer ([1]) with optional display width,
194 /// e.g. MEDIUMINT UNSIGNED or MEDIUMINT(5) UNSIGNED.
195 ///
196 /// [1]: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
197 MediumIntUnsigned(Option<u64>),
198 /// Int with optional display width, e.g. INT or INT(11).
199 Int(Option<u64>),
200 /// Int4 is an alias for Integer in [PostgreSQL].
201 /// Note: Int4 means 4 bytes in PostgreSQL (not 4 bits).
202 /// Int4 with optional display width, e.g. Int4 or Int4(11).
203 ///
204 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
205 Int4(Option<u64>),
206 /// Int8 is an alias for BigInt in [PostgreSQL] and Integer type in [ClickHouse].
207 /// Int8 with optional display width, e.g. INT8 or INT8(11).
208 /// Note: Int8 means 8 bytes in [PostgreSQL], but 8 bits in [ClickHouse].
209 ///
210 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
211 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
212 Int8(Option<u64>),
213 /// Integer type in [ClickHouse].
214 /// Note: Int16 means 16 bits in [ClickHouse].
215 ///
216 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
217 Int16,
218 /// Integer type in [ClickHouse].
219 /// Note: Int32 means 32 bits in [ClickHouse].
220 ///
221 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
222 Int32,
223 /// Integer type in [BigQuery], [ClickHouse].
224 ///
225 /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#integer_types
226 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
227 Int64,
228 /// Integer type in [ClickHouse].
229 /// Note: Int128 means 128 bits in [ClickHouse].
230 ///
231 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
232 Int128,
233 /// Integer type in [ClickHouse].
234 /// Note: Int256 means 256 bits in [ClickHouse].
235 ///
236 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
237 Int256,
238 /// Integer with optional display width, e.g. INTEGER or INTEGER(11).
239 Integer(Option<u64>),
240 /// Unsigned int with optional display width, e.g. INT UNSIGNED or INT(11) UNSIGNED.
241 IntUnsigned(Option<u64>),
242 /// Unsigned int4 with optional display width, e.g. INT4 UNSIGNED or INT4(11) UNSIGNED.
243 Int4Unsigned(Option<u64>),
244 /// Unsigned integer with optional display width, e.g. INTEGER UNSIGNED or INTEGER(11) UNSIGNED.
245 IntegerUnsigned(Option<u64>),
246 /// 128-bit integer type, e.g. HUGEINT.
247 HugeInt,
248 /// Unsigned 128-bit integer type, e.g. UHUGEINT.
249 UHugeInt,
250 /// Unsigned integer type in [ClickHouse].
251 /// Note: UInt8 means 8 bits in [ClickHouse].
252 ///
253 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
254 UInt8,
255 /// Unsigned integer type in [ClickHouse].
256 /// Note: UInt16 means 16 bits in [ClickHouse].
257 ///
258 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
259 UInt16,
260 /// Unsigned integer type in [ClickHouse].
261 /// Note: UInt32 means 32 bits in [ClickHouse].
262 ///
263 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
264 UInt32,
265 /// Unsigned integer type in [ClickHouse].
266 /// Note: UInt64 means 64 bits in [ClickHouse].
267 ///
268 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
269 UInt64,
270 /// Unsigned integer type in [ClickHouse].
271 /// Note: UInt128 means 128 bits in [ClickHouse].
272 ///
273 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
274 UInt128,
275 /// Unsigned integer type in [ClickHouse].
276 /// Note: UInt256 means 256 bits in [ClickHouse].
277 ///
278 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
279 UInt256,
280 /// Big integer with optional display width, e.g. BIGINT or BIGINT(20).
281 BigInt(Option<u64>),
282 /// Unsigned big integer with optional display width, e.g. BIGINT UNSIGNED or BIGINT(20) UNSIGNED.
283 BigIntUnsigned(Option<u64>),
284 /// Unsigned big integer, e.g. UBIGINT.
285 UBigInt,
286 /// Unsigned Int8 with optional display width, e.g. INT8 UNSIGNED or INT8(11) UNSIGNED.
287 Int8Unsigned(Option<u64>),
288 /// Signed integer as used in [MySQL CAST] target types, without optional `INTEGER` suffix,
289 /// e.g. `SIGNED`
290 ///
291 /// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
292 Signed,
293 /// Signed integer as used in [MySQL CAST] target types, with optional `INTEGER` suffix,
294 /// e.g. `SIGNED INTEGER`
295 ///
296 /// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
297 SignedInteger,
298 /// Signed integer as used in [MySQL CAST] target types, without optional `INTEGER` suffix,
299 /// e.g. `SIGNED`
300 ///
301 /// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
302 Unsigned,
303 /// Unsigned integer as used in [MySQL CAST] target types, with optional `INTEGER` suffix,
304 /// e.g. `UNSIGNED INTEGER`.
305 ///
306 /// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
307 UnsignedInteger,
308 /// Float4 is an alias for Real in [PostgreSQL].
309 ///
310 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
311 Float4,
312 /// Floating point in [ClickHouse].
313 ///
314 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
315 Float32,
316 /// Floating point in [BigQuery].
317 ///
318 /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#floating_point_types
319 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
320 Float64,
321 /// Floating point, e.g. REAL.
322 Real,
323 /// [MySQL] unsigned real, e.g. REAL UNSIGNED.
324 /// Note: Using UNSIGNED with REAL is deprecated in recent versions of MySQL.
325 ///
326 /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html
327 RealUnsigned,
328 /// Float8 is an alias for Double in [PostgreSQL].
329 ///
330 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
331 Float8,
332 /// Double
333 Double(ExactNumberInfo),
334 /// [MySQL] unsigned double precision with optional precision, e.g. DOUBLE UNSIGNED or DOUBLE(10,2) UNSIGNED.
335 /// Note: Using UNSIGNED with DOUBLE is deprecated in recent versions of MySQL.
336 ///
337 /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html
338 DoubleUnsigned(ExactNumberInfo),
339 /// Double Precision, see [SQL Standard], [PostgreSQL].
340 ///
341 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#approximate-numeric-type
342 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-numeric.html
343 DoublePrecision,
344 /// [MySQL] unsigned double precision, e.g. DOUBLE PRECISION UNSIGNED.
345 /// Note: Using UNSIGNED with DOUBLE PRECISION is deprecated in recent versions of MySQL.
346 ///
347 /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html
348 DoublePrecisionUnsigned,
349 /// Bool is an alias for Boolean, see [PostgreSQL].
350 ///
351 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
352 Bool,
353 /// Boolean type.
354 Boolean,
355 /// Date type.
356 Date,
357 /// Date32 with the same range as Datetime64.
358 ///
359 /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/date32
360 Date32,
361 /// Time with optional time precision and time zone information, see [SQL Standard][1].
362 ///
363 /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
364 Time(Option<u64>, TimezoneInfo),
365 /// Datetime with optional time precision, see [MySQL][1].
366 ///
367 /// [1]: https://dev.mysql.com/doc/refman/8.0/en/datetime.html
368 Datetime(Option<u64>),
369 /// Datetime with time precision and optional timezone, see [ClickHouse][1].
370 ///
371 /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/datetime64
372 Datetime64(u64, Option<String>),
373 /// Timestamp with optional time precision and time zone information, see [SQL Standard][1].
374 ///
375 /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
376 Timestamp(Option<u64>, TimezoneInfo),
377 /// Databricks timestamp without time zone. See [1].
378 ///
379 /// [1]: https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type
380 TimestampNtz(Option<u64>),
381 /// Interval type.
382 Interval {
383 /// [PostgreSQL] fields specification like `INTERVAL YEAR TO MONTH`.
384 ///
385 /// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-datetime.html
386 fields: Option<IntervalFields>,
387 /// [PostgreSQL] subsecond precision like `INTERVAL HOUR TO SECOND(3)`
388 ///
389 /// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-datetime.html
390 precision: Option<u64>,
391 },
392 /// JSON type.
393 JSON,
394 /// Binary JSON type.
395 JSONB,
396 /// Regclass used in [PostgreSQL] serial.
397 ///
398 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
399 Regclass,
400 /// Text type.
401 Text,
402 /// [MySQL] text with up to 2**8 bytes.
403 ///
404 /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
405 TinyText,
406 /// [MySQL] text with up to 2**24 bytes.
407 ///
408 /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
409 MediumText,
410 /// [MySQL] text with up to 2**32 bytes.
411 ///
412 /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
413 LongText,
414 /// String with optional length.
415 String(Option<u64>),
416 /// A fixed-length string e.g [ClickHouse][1].
417 ///
418 /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/fixedstring
419 FixedString(u64),
420 /// Bytea type, see [PostgreSQL].
421 ///
422 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-bit.html
423 Bytea,
424 /// Bit string, see [PostgreSQL], [MySQL], or [MSSQL].
425 ///
426 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-bit.html
427 /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/bit-type.html
428 /// [MSSQL]: https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-ver16
429 Bit(Option<u64>),
430 /// `BIT VARYING(n)`: Variable-length bit string, see [PostgreSQL].
431 ///
432 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-bit.html
433 BitVarying(Option<u64>),
434 /// `VARBIT(n)`: Variable-length bit string. [PostgreSQL] alias for `BIT VARYING`.
435 ///
436 /// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
437 VarBit(Option<u64>),
438 /// Custom types.
439 Custom(ObjectName, Vec<String>),
440 /// Arrays.
441 Array(ArrayElemTypeDef),
442 /// Map, see [ClickHouse].
443 ///
444 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/map
445 Map(Box<DataType>, Box<DataType>),
446 /// Tuple, see [ClickHouse].
447 ///
448 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
449 Tuple(Vec<StructField>),
450 /// Nested type, see [ClickHouse].
451 ///
452 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nested-data-structures/nested
453 Nested(Vec<ColumnDef>),
454 /// Enum type.
455 Enum(Vec<EnumMember>, Option<u8>),
456 /// Set type.
457 Set(Vec<String>),
458 /// Struct type, see [Hive], [BigQuery].
459 ///
460 /// [Hive]: https://docs.cloudera.com/cdw-runtime/cloud/impala-sql-reference/topics/impala-struct.html
461 /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
462 Struct(Vec<StructField>, StructBracketKind),
463 /// Union type, see [DuckDB].
464 ///
465 /// [DuckDB]: https://duckdb.org/docs/sql/data_types/union.html
466 Union(Vec<UnionField>),
467 /// Nullable - special marker NULL represents in ClickHouse as a data type.
468 ///
469 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nullable
470 Nullable(Box<DataType>),
471 /// LowCardinality - changes the internal representation of other data types to be dictionary-encoded.
472 ///
473 /// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/lowcardinality
474 LowCardinality(Box<DataType>),
475 /// No type specified - only used with
476 /// [`SQLiteDialect`](crate::dialect::SQLiteDialect), from statements such
477 /// as `CREATE TABLE t1 (a)`.
478 Unspecified,
479 /// Trigger data type, returned by functions associated with triggers, see [PostgreSQL].
480 ///
481 /// [PostgreSQL]: https://www.postgresql.org/docs/current/plpgsql-trigger.html
482 Trigger,
483 /// SETOF type modifier for [PostgreSQL] function return types,
484 /// e.g. `CREATE FUNCTION ... RETURNS SETOF text`.
485 ///
486 /// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html
487 SetOf(Box<DataType>),
488 /// Any data type, used in BigQuery UDF definitions for templated parameters, see [BigQuery].
489 ///
490 /// [BigQuery]: https://cloud.google.com/bigquery/docs/user-defined-functions#templated-sql-udf-parameters
491 AnyType,
492 /// Geometric type, see [PostgreSQL].
493 ///
494 /// [PostgreSQL]: https://www.postgresql.org/docs/9.5/functions-geometry.html
495 GeometricType(GeometricTypeKind),
496 /// PostgreSQL text search vectors, see [PostgreSQL].
497 ///
498 /// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
499 TsVector,
500 /// PostgreSQL text search query, see [PostgreSQL].
501 ///
502 /// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
503 TsQuery,
504}
505
506impl fmt::Display for DataType {
507 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
508 match self {
509 DataType::Character(size) => format_character_string_type(f, "CHARACTER", size),
510 DataType::Char(size) => format_character_string_type(f, "CHAR", size),
511 DataType::CharacterVarying(size) => {
512 format_character_string_type(f, "CHARACTER VARYING", size)
513 }
514 DataType::CharVarying(size) => format_character_string_type(f, "CHAR VARYING", size),
515 DataType::Varchar(size) => format_character_string_type(f, "VARCHAR", size),
516 DataType::Nvarchar(size) => format_character_string_type(f, "NVARCHAR", size),
517 DataType::Uuid => write!(f, "UUID"),
518 DataType::CharacterLargeObject(size) => {
519 format_type_with_optional_length(f, "CHARACTER LARGE OBJECT", size, false)
520 }
521 DataType::CharLargeObject(size) => {
522 format_type_with_optional_length(f, "CHAR LARGE OBJECT", size, false)
523 }
524 DataType::Clob(size) => format_type_with_optional_length(f, "CLOB", size, false),
525 DataType::Binary(size) => format_type_with_optional_length(f, "BINARY", size, false),
526 DataType::Varbinary(size) => format_varbinary_type(f, "VARBINARY", size),
527 DataType::Blob(size) => format_type_with_optional_length(f, "BLOB", size, false),
528 DataType::TinyBlob => write!(f, "TINYBLOB"),
529 DataType::MediumBlob => write!(f, "MEDIUMBLOB"),
530 DataType::LongBlob => write!(f, "LONGBLOB"),
531 DataType::Bytes(size) => format_type_with_optional_length(f, "BYTES", size, false),
532 DataType::Numeric(info) => {
533 write!(f, "NUMERIC{info}")
534 }
535 DataType::Decimal(info) => {
536 write!(f, "DECIMAL{info}")
537 }
538 DataType::DecimalUnsigned(info) => {
539 write!(f, "DECIMAL{info} UNSIGNED")
540 }
541 DataType::Dec(info) => {
542 write!(f, "DEC{info}")
543 }
544 DataType::DecUnsigned(info) => {
545 write!(f, "DEC{info} UNSIGNED")
546 }
547 DataType::BigNumeric(info) => write!(f, "BIGNUMERIC{info}"),
548 DataType::BigDecimal(info) => write!(f, "BIGDECIMAL{info}"),
549 DataType::Float(info) => write!(f, "FLOAT{info}"),
550 DataType::FloatUnsigned(info) => write!(f, "FLOAT{info} UNSIGNED"),
551 DataType::TinyInt(zerofill) => {
552 format_type_with_optional_length(f, "TINYINT", zerofill, false)
553 }
554 DataType::TinyIntUnsigned(zerofill) => {
555 format_type_with_optional_length(f, "TINYINT", zerofill, true)
556 }
557 DataType::Int2(zerofill) => {
558 format_type_with_optional_length(f, "INT2", zerofill, false)
559 }
560 DataType::Int2Unsigned(zerofill) => {
561 format_type_with_optional_length(f, "INT2", zerofill, true)
562 }
563 DataType::SmallInt(zerofill) => {
564 format_type_with_optional_length(f, "SMALLINT", zerofill, false)
565 }
566 DataType::SmallIntUnsigned(zerofill) => {
567 format_type_with_optional_length(f, "SMALLINT", zerofill, true)
568 }
569 DataType::MediumInt(zerofill) => {
570 format_type_with_optional_length(f, "MEDIUMINT", zerofill, false)
571 }
572 DataType::MediumIntUnsigned(zerofill) => {
573 format_type_with_optional_length(f, "MEDIUMINT", zerofill, true)
574 }
575 DataType::Int(zerofill) => format_type_with_optional_length(f, "INT", zerofill, false),
576 DataType::IntUnsigned(zerofill) => {
577 format_type_with_optional_length(f, "INT", zerofill, true)
578 }
579 DataType::Int4(zerofill) => {
580 format_type_with_optional_length(f, "INT4", zerofill, false)
581 }
582 DataType::Int8(zerofill) => {
583 format_type_with_optional_length(f, "INT8", zerofill, false)
584 }
585 DataType::Int16 => {
586 write!(f, "Int16")
587 }
588 DataType::Int32 => {
589 write!(f, "Int32")
590 }
591 DataType::Int64 => {
592 write!(f, "INT64")
593 }
594 DataType::Int128 => {
595 write!(f, "Int128")
596 }
597 DataType::Int256 => {
598 write!(f, "Int256")
599 }
600 DataType::HugeInt => {
601 write!(f, "HUGEINT")
602 }
603 DataType::Int4Unsigned(zerofill) => {
604 format_type_with_optional_length(f, "INT4", zerofill, true)
605 }
606 DataType::Integer(zerofill) => {
607 format_type_with_optional_length(f, "INTEGER", zerofill, false)
608 }
609 DataType::IntegerUnsigned(zerofill) => {
610 format_type_with_optional_length(f, "INTEGER", zerofill, true)
611 }
612 DataType::BigInt(zerofill) => {
613 format_type_with_optional_length(f, "BIGINT", zerofill, false)
614 }
615 DataType::BigIntUnsigned(zerofill) => {
616 format_type_with_optional_length(f, "BIGINT", zerofill, true)
617 }
618 DataType::Int8Unsigned(zerofill) => {
619 format_type_with_optional_length(f, "INT8", zerofill, true)
620 }
621 DataType::UTinyInt => {
622 write!(f, "UTINYINT")
623 }
624 DataType::USmallInt => {
625 write!(f, "USMALLINT")
626 }
627 DataType::UBigInt => {
628 write!(f, "UBIGINT")
629 }
630 DataType::UHugeInt => {
631 write!(f, "UHUGEINT")
632 }
633 DataType::UInt8 => {
634 write!(f, "UInt8")
635 }
636 DataType::UInt16 => {
637 write!(f, "UInt16")
638 }
639 DataType::UInt32 => {
640 write!(f, "UInt32")
641 }
642 DataType::UInt64 => {
643 write!(f, "UInt64")
644 }
645 DataType::UInt128 => {
646 write!(f, "UInt128")
647 }
648 DataType::UInt256 => {
649 write!(f, "UInt256")
650 }
651 DataType::Signed => {
652 write!(f, "SIGNED")
653 }
654 DataType::SignedInteger => {
655 write!(f, "SIGNED INTEGER")
656 }
657 DataType::Unsigned => {
658 write!(f, "UNSIGNED")
659 }
660 DataType::UnsignedInteger => {
661 write!(f, "UNSIGNED INTEGER")
662 }
663 DataType::Real => write!(f, "REAL"),
664 DataType::RealUnsigned => write!(f, "REAL UNSIGNED"),
665 DataType::Float4 => write!(f, "FLOAT4"),
666 DataType::Float32 => write!(f, "Float32"),
667 DataType::Float64 => write!(f, "FLOAT64"),
668 DataType::Double(info) => write!(f, "DOUBLE{info}"),
669 DataType::DoubleUnsigned(info) => write!(f, "DOUBLE{info} UNSIGNED"),
670 DataType::Float8 => write!(f, "FLOAT8"),
671 DataType::DoublePrecision => write!(f, "DOUBLE PRECISION"),
672 DataType::DoublePrecisionUnsigned => write!(f, "DOUBLE PRECISION UNSIGNED"),
673 DataType::Bool => write!(f, "BOOL"),
674 DataType::Boolean => write!(f, "BOOLEAN"),
675 DataType::Date => write!(f, "DATE"),
676 DataType::Date32 => write!(f, "Date32"),
677 DataType::Time(precision, timezone_info) => {
678 format_datetime_precision_and_tz(f, "TIME", precision, timezone_info)
679 }
680 DataType::Datetime(precision) => {
681 format_type_with_optional_length(f, "DATETIME", precision, false)
682 }
683 DataType::Timestamp(precision, timezone_info) => {
684 format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info)
685 }
686 DataType::TimestampNtz(precision) => {
687 format_type_with_optional_length(f, "TIMESTAMP_NTZ", precision, false)
688 }
689 DataType::Datetime64(precision, timezone) => {
690 format_clickhouse_datetime_precision_and_timezone(
691 f,
692 "DateTime64",
693 precision,
694 timezone,
695 )
696 }
697 DataType::Interval { fields, precision } => {
698 write!(f, "INTERVAL")?;
699 if let Some(fields) = fields {
700 write!(f, " {fields}")?;
701 }
702 if let Some(precision) = precision {
703 write!(f, "({precision})")?;
704 }
705 Ok(())
706 }
707 DataType::JSON => write!(f, "JSON"),
708 DataType::JSONB => write!(f, "JSONB"),
709 DataType::Regclass => write!(f, "REGCLASS"),
710 DataType::Text => write!(f, "TEXT"),
711 DataType::TinyText => write!(f, "TINYTEXT"),
712 DataType::MediumText => write!(f, "MEDIUMTEXT"),
713 DataType::LongText => write!(f, "LONGTEXT"),
714 DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false),
715 DataType::Bytea => write!(f, "BYTEA"),
716 DataType::Bit(size) => format_type_with_optional_length(f, "BIT", size, false),
717 DataType::BitVarying(size) => {
718 format_type_with_optional_length(f, "BIT VARYING", size, false)
719 }
720 DataType::VarBit(size) => format_type_with_optional_length(f, "VARBIT", size, false),
721 DataType::Array(ty) => match ty {
722 ArrayElemTypeDef::None => write!(f, "ARRAY"),
723 ArrayElemTypeDef::SquareBracket(t, None) => write!(f, "{t}[]"),
724 ArrayElemTypeDef::SquareBracket(t, Some(size)) => write!(f, "{t}[{size}]"),
725 ArrayElemTypeDef::AngleBracket(t) => write!(f, "ARRAY<{t}>"),
726 ArrayElemTypeDef::Parenthesis(t) => write!(f, "Array({t})"),
727 },
728 DataType::Custom(ty, modifiers) => {
729 if modifiers.is_empty() {
730 write!(f, "{ty}")
731 } else {
732 write!(f, "{}({})", ty, modifiers.join(", "))
733 }
734 }
735 DataType::Enum(vals, bits) => {
736 match bits {
737 Some(bits) => write!(f, "ENUM{bits}"),
738 None => write!(f, "ENUM"),
739 }?;
740 write!(f, "(")?;
741 for (i, v) in vals.iter().enumerate() {
742 if i != 0 {
743 write!(f, ", ")?;
744 }
745 match v {
746 EnumMember::Name(name) => {
747 write!(f, "'{}'", escape_single_quote_string(name))?
748 }
749 EnumMember::NamedValue(name, value) => {
750 write!(f, "'{}' = {}", escape_single_quote_string(name), value)?
751 }
752 }
753 }
754 write!(f, ")")
755 }
756 DataType::Set(vals) => {
757 write!(f, "SET(")?;
758 for (i, v) in vals.iter().enumerate() {
759 if i != 0 {
760 write!(f, ", ")?;
761 }
762 write!(f, "'{}'", escape_single_quote_string(v))?;
763 }
764 write!(f, ")")
765 }
766 DataType::Struct(fields, bracket) => {
767 if !fields.is_empty() {
768 match bracket {
769 StructBracketKind::Parentheses => {
770 write!(f, "STRUCT({})", display_comma_separated(fields))
771 }
772 StructBracketKind::AngleBrackets => {
773 write!(f, "STRUCT<{}>", display_comma_separated(fields))
774 }
775 }
776 } else {
777 write!(f, "STRUCT")
778 }
779 }
780 DataType::Union(fields) => {
781 write!(f, "UNION({})", display_comma_separated(fields))
782 }
783 // ClickHouse
784 DataType::Nullable(data_type) => {
785 write!(f, "Nullable({data_type})")
786 }
787 DataType::FixedString(character_length) => {
788 write!(f, "FixedString({character_length})")
789 }
790 DataType::LowCardinality(data_type) => {
791 write!(f, "LowCardinality({data_type})")
792 }
793 DataType::Map(key_data_type, value_data_type) => {
794 write!(f, "Map({key_data_type}, {value_data_type})")
795 }
796 DataType::Tuple(fields) => {
797 write!(f, "Tuple({})", display_comma_separated(fields))
798 }
799 DataType::Nested(fields) => {
800 write!(f, "Nested({})", display_comma_separated(fields))
801 }
802 DataType::Unspecified => Ok(()),
803 DataType::Trigger => write!(f, "TRIGGER"),
804 DataType::SetOf(inner) => write!(f, "SETOF {inner}"),
805 DataType::AnyType => write!(f, "ANY TYPE"),
806 DataType::Table(fields) => match fields {
807 Some(fields) => {
808 write!(f, "TABLE({})", display_comma_separated(fields))
809 }
810 None => {
811 write!(f, "TABLE")
812 }
813 },
814 DataType::NamedTable { name, columns } => {
815 write!(f, "{} TABLE ({})", name, display_comma_separated(columns))
816 }
817 DataType::GeometricType(kind) => write!(f, "{kind}"),
818 DataType::TsVector => write!(f, "TSVECTOR"),
819 DataType::TsQuery => write!(f, "TSQUERY"),
820 }
821 }
822}
823
824fn format_type_with_optional_length(
825 f: &mut fmt::Formatter,
826 sql_type: &'static str,
827 len: &Option<u64>,
828 unsigned: bool,
829) -> fmt::Result {
830 write!(f, "{sql_type}")?;
831 if let Some(len) = len {
832 write!(f, "({len})")?;
833 }
834 if unsigned {
835 write!(f, " UNSIGNED")?;
836 }
837 Ok(())
838}
839
840fn format_character_string_type(
841 f: &mut fmt::Formatter,
842 sql_type: &str,
843 size: &Option<CharacterLength>,
844) -> fmt::Result {
845 write!(f, "{sql_type}")?;
846 if let Some(size) = size {
847 write!(f, "({size})")?;
848 }
849 Ok(())
850}
851
852fn format_varbinary_type(
853 f: &mut fmt::Formatter,
854 sql_type: &str,
855 size: &Option<BinaryLength>,
856) -> fmt::Result {
857 write!(f, "{sql_type}")?;
858 if let Some(size) = size {
859 write!(f, "({size})")?;
860 }
861 Ok(())
862}
863
864fn format_datetime_precision_and_tz(
865 f: &mut fmt::Formatter,
866 sql_type: &'static str,
867 len: &Option<u64>,
868 time_zone: &TimezoneInfo,
869) -> fmt::Result {
870 write!(f, "{sql_type}")?;
871 let len_fmt = len.as_ref().map(|l| format!("({l})")).unwrap_or_default();
872
873 match time_zone {
874 TimezoneInfo::Tz => {
875 write!(f, "{time_zone}{len_fmt}")?;
876 }
877 _ => {
878 write!(f, "{len_fmt}{time_zone}")?;
879 }
880 }
881
882 Ok(())
883}
884
885fn format_clickhouse_datetime_precision_and_timezone(
886 f: &mut fmt::Formatter,
887 sql_type: &'static str,
888 len: &u64,
889 time_zone: &Option<String>,
890) -> fmt::Result {
891 write!(f, "{sql_type}({len}")?;
892
893 if let Some(time_zone) = time_zone {
894 write!(f, ", '{time_zone}'")?;
895 }
896
897 write!(f, ")")?;
898
899 Ok(())
900}
901
902/// Type of brackets used for `STRUCT` literals.
903#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
904#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
905#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
906pub enum StructBracketKind {
907 /// Example: `STRUCT(a INT, b STRING)`
908 Parentheses,
909 /// Example: `STRUCT<a INT, b STRING>`
910 AngleBrackets,
911}
912
913/// Timestamp and Time data types information about TimeZone formatting.
914///
915/// This is more related to a display information than real differences between each variant. To
916/// guarantee compatibility with the input query we must maintain its exact information.
917#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
918#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
919#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
920pub enum TimezoneInfo {
921 /// No information about time zone, e.g. TIMESTAMP
922 None,
923 /// Temporal type 'WITH TIME ZONE', e.g. TIMESTAMP WITH TIME ZONE, [SQL Standard], [Oracle]
924 ///
925 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
926 /// [Oracle]: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/nlspg/datetime-data-types-and-time-zone-support.html#GUID-3F1C388E-C651-43D5-ADBC-1A49E5C2CA05
927 WithTimeZone,
928 /// Temporal type 'WITHOUT TIME ZONE', e.g. TIME WITHOUT TIME ZONE, [SQL Standard], [Postgresql]
929 ///
930 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
931 /// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
932 WithoutTimeZone,
933 /// Postgresql specific `WITH TIME ZONE` formatting, for both TIME and TIMESTAMP, e.g. TIMETZ, [Postgresql]
934 ///
935 /// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
936 Tz,
937}
938
939impl fmt::Display for TimezoneInfo {
940 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
941 match self {
942 TimezoneInfo::None => {
943 write!(f, "")
944 }
945 TimezoneInfo::WithTimeZone => {
946 write!(f, " WITH TIME ZONE")
947 }
948 TimezoneInfo::WithoutTimeZone => {
949 write!(f, " WITHOUT TIME ZONE")
950 }
951 TimezoneInfo::Tz => {
952 // TZ is the only one that is displayed BEFORE the precision, so the datatype display
953 // must be aware of that. Check <https://www.postgresql.org/docs/14/datatype-datetime.html>
954 // for more information
955 write!(f, "TZ")
956 }
957 }
958 }
959}
960
961/// Fields for [Postgres] `INTERVAL` type.
962///
963/// [Postgres]: https://www.postgresql.org/docs/17/datatype-datetime.html
964#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
965#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
966#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
967pub enum IntervalFields {
968 /// `YEAR` field
969 Year,
970 /// `MONTH` field
971 Month,
972 /// `DAY` field
973 Day,
974 /// `HOUR` field
975 Hour,
976 /// `MINUTE` field
977 Minute,
978 /// `SECOND` field
979 Second,
980 /// `YEAR TO MONTH` field
981 YearToMonth,
982 /// `DAY TO HOUR` field
983 DayToHour,
984 /// `DAY TO MINUTE` field
985 DayToMinute,
986 /// `DAY TO SECOND` field
987 DayToSecond,
988 /// `HOUR TO MINUTE` field
989 HourToMinute,
990 /// `HOUR TO SECOND` field
991 HourToSecond,
992 /// `MINUTE TO SECOND` field
993 MinuteToSecond,
994}
995
996impl fmt::Display for IntervalFields {
997 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
998 match self {
999 IntervalFields::Year => write!(f, "YEAR"),
1000 IntervalFields::Month => write!(f, "MONTH"),
1001 IntervalFields::Day => write!(f, "DAY"),
1002 IntervalFields::Hour => write!(f, "HOUR"),
1003 IntervalFields::Minute => write!(f, "MINUTE"),
1004 IntervalFields::Second => write!(f, "SECOND"),
1005 IntervalFields::YearToMonth => write!(f, "YEAR TO MONTH"),
1006 IntervalFields::DayToHour => write!(f, "DAY TO HOUR"),
1007 IntervalFields::DayToMinute => write!(f, "DAY TO MINUTE"),
1008 IntervalFields::DayToSecond => write!(f, "DAY TO SECOND"),
1009 IntervalFields::HourToMinute => write!(f, "HOUR TO MINUTE"),
1010 IntervalFields::HourToSecond => write!(f, "HOUR TO SECOND"),
1011 IntervalFields::MinuteToSecond => write!(f, "MINUTE TO SECOND"),
1012 }
1013 }
1014}
1015
1016/// Additional information for `NUMERIC`, `DECIMAL`, and `DEC` data types
1017/// following the 2016 [SQL Standard].
1018///
1019/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
1020#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1021#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1022#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1023pub enum ExactNumberInfo {
1024 /// No additional information, e.g. `DECIMAL`.
1025 None,
1026 /// Only precision information, e.g. `DECIMAL(10)`.
1027 Precision(u64),
1028 /// Precision and scale information, e.g. `DECIMAL(10,2)`.
1029 PrecisionAndScale(u64, i64),
1030}
1031
1032impl fmt::Display for ExactNumberInfo {
1033 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1034 match self {
1035 ExactNumberInfo::None => {
1036 write!(f, "")
1037 }
1038 ExactNumberInfo::Precision(p) => {
1039 write!(f, "({p})")
1040 }
1041 ExactNumberInfo::PrecisionAndScale(p, s) => {
1042 write!(f, "({p},{s})")
1043 }
1044 }
1045 }
1046}
1047
1048/// Information about [character length][1], including length and possibly unit.
1049///
1050/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-length
1051#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1052#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1053#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1054pub enum CharacterLength {
1055 /// Integer length with optional unit (e.g. `CHAR(10)` or `VARCHAR(10 CHARACTERS)`).
1056 IntegerLength {
1057 /// Default (if VARYING) or maximum (if not VARYING) length
1058 length: u64,
1059 /// Optional unit. If not informed, the ANSI handles it as CHARACTERS implicitly
1060 unit: Option<CharLengthUnits>,
1061 },
1062 /// VARCHAR(MAX) or NVARCHAR(MAX), used in T-SQL (Microsoft SQL Server).
1063 Max,
1064}
1065
1066impl fmt::Display for CharacterLength {
1067 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1068 match self {
1069 CharacterLength::IntegerLength { length, unit } => {
1070 write!(f, "{length}")?;
1071 if let Some(unit) = unit {
1072 write!(f, " {unit}")?;
1073 }
1074 }
1075 CharacterLength::Max => {
1076 write!(f, "MAX")?;
1077 }
1078 }
1079 Ok(())
1080 }
1081}
1082
1083/// Possible units for characters, initially based on 2016 ANSI [SQL Standard][1].
1084///
1085/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#char-length-units
1086#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1087#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1088#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1089pub enum CharLengthUnits {
1090 /// CHARACTERS unit
1091 Characters,
1092 /// OCTETS unit
1093 Octets,
1094}
1095
1096impl fmt::Display for CharLengthUnits {
1097 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1098 match self {
1099 Self::Characters => {
1100 write!(f, "CHARACTERS")
1101 }
1102 Self::Octets => {
1103 write!(f, "OCTETS")
1104 }
1105 }
1106 }
1107}
1108
1109#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1110#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1111#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1112/// Information about [binary length][1], including length and possibly unit.
1113///
1114/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-length
1115pub enum BinaryLength {
1116 /// Integer length for binary types (e.g. `VARBINARY(100)`).
1117 IntegerLength {
1118 /// Default (if VARYING)
1119 length: u64,
1120 },
1121 /// VARBINARY(MAX) used in T-SQL (Microsoft SQL Server).
1122 Max,
1123}
1124
1125impl fmt::Display for BinaryLength {
1126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1127 match self {
1128 BinaryLength::IntegerLength { length } => {
1129 write!(f, "{length}")?;
1130 }
1131 BinaryLength::Max => {
1132 write!(f, "MAX")?;
1133 }
1134 }
1135 Ok(())
1136 }
1137}
1138
1139/// Represents the data type of the elements in an array (if any) as well as
1140/// the syntax used to declare the array.
1141///
1142/// For example: Bigquery/Hive use `ARRAY<INT>` whereas snowflake uses ARRAY.
1143#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1144#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1145#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1146pub enum ArrayElemTypeDef {
1147 /// Use `ARRAY` style without an explicit element type.
1148 None,
1149 /// Angle-bracket style, e.g. `ARRAY<INT>`.
1150 AngleBracket(Box<DataType>),
1151 /// Square-bracket style, e.g. `INT[]` or `INT[2]`.
1152 SquareBracket(Box<DataType>, Option<u64>),
1153 /// Parenthesis style, e.g. `Array(Int64)`.
1154 Parenthesis(Box<DataType>),
1155}
1156
1157/// Represents different types of geometric shapes which are commonly used in
1158/// PostgreSQL/Redshift for spatial operations and geometry-related computations.
1159///
1160/// [PostgreSQL]: https://www.postgresql.org/docs/9.5/functions-geometry.html
1161#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1162#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1163#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1164pub enum GeometricTypeKind {
1165 /// Point geometry
1166 Point,
1167 /// Line geometry
1168 Line,
1169 /// Line segment geometry
1170 LineSegment,
1171 /// Box geometry
1172 GeometricBox,
1173 /// Path geometry
1174 GeometricPath,
1175 /// Polygon geometry
1176 Polygon,
1177 /// Circle geometry
1178 Circle,
1179}
1180
1181impl fmt::Display for GeometricTypeKind {
1182 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1183 match self {
1184 GeometricTypeKind::Point => write!(f, "point"),
1185 GeometricTypeKind::Line => write!(f, "line"),
1186 GeometricTypeKind::LineSegment => write!(f, "lseg"),
1187 GeometricTypeKind::GeometricBox => write!(f, "box"),
1188 GeometricTypeKind::GeometricPath => write!(f, "path"),
1189 GeometricTypeKind::Polygon => write!(f, "polygon"),
1190 GeometricTypeKind::Circle => write!(f, "circle"),
1191 }
1192 }
1193}