sqlparser/ast/
data_type.rs1// 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 /// Any data type, used in BigQuery UDF definitions for templated parameters, see [BigQuery].
484 ///
485 /// [BigQuery]: https://cloud.google.com/bigquery/docs/user-defined-functions#templated-sql-udf-parameters
486 AnyType,
487 /// Geometric type, see [PostgreSQL].
488 ///
489 /// [PostgreSQL]: https://www.postgresql.org/docs/9.5/functions-geometry.html
490 GeometricType(GeometricTypeKind),
491 /// PostgreSQL text search vectors, see [PostgreSQL].
492 ///
493 /// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
494 TsVector,
495 /// PostgreSQL text search query, see [PostgreSQL].
496 ///
497 /// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
498 TsQuery,
499}
500
501impl fmt::Display for DataType {
502 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
503 match self {
504 DataType::Character(size) => format_character_string_type(f, "CHARACTER", size),
505 DataType::Char(size) => format_character_string_type(f, "CHAR", size),
506 DataType::CharacterVarying(size) => {
507 format_character_string_type(f, "CHARACTER VARYING", size)
508 }
509 DataType::CharVarying(size) => format_character_string_type(f, "CHAR VARYING", size),
510 DataType::Varchar(size) => format_character_string_type(f, "VARCHAR", size),
511 DataType::Nvarchar(size) => format_character_string_type(f, "NVARCHAR", size),
512 DataType::Uuid => write!(f, "UUID"),
513 DataType::CharacterLargeObject(size) => {
514 format_type_with_optional_length(f, "CHARACTER LARGE OBJECT", size, false)
515 }
516 DataType::CharLargeObject(size) => {
517 format_type_with_optional_length(f, "CHAR LARGE OBJECT", size, false)
518 }
519 DataType::Clob(size) => format_type_with_optional_length(f, "CLOB", size, false),
520 DataType::Binary(size) => format_type_with_optional_length(f, "BINARY", size, false),
521 DataType::Varbinary(size) => format_varbinary_type(f, "VARBINARY", size),
522 DataType::Blob(size) => format_type_with_optional_length(f, "BLOB", size, false),
523 DataType::TinyBlob => write!(f, "TINYBLOB"),
524 DataType::MediumBlob => write!(f, "MEDIUMBLOB"),
525 DataType::LongBlob => write!(f, "LONGBLOB"),
526 DataType::Bytes(size) => format_type_with_optional_length(f, "BYTES", size, false),
527 DataType::Numeric(info) => {
528 write!(f, "NUMERIC{info}")
529 }
530 DataType::Decimal(info) => {
531 write!(f, "DECIMAL{info}")
532 }
533 DataType::DecimalUnsigned(info) => {
534 write!(f, "DECIMAL{info} UNSIGNED")
535 }
536 DataType::Dec(info) => {
537 write!(f, "DEC{info}")
538 }
539 DataType::DecUnsigned(info) => {
540 write!(f, "DEC{info} UNSIGNED")
541 }
542 DataType::BigNumeric(info) => write!(f, "BIGNUMERIC{info}"),
543 DataType::BigDecimal(info) => write!(f, "BIGDECIMAL{info}"),
544 DataType::Float(info) => write!(f, "FLOAT{info}"),
545 DataType::FloatUnsigned(info) => write!(f, "FLOAT{info} UNSIGNED"),
546 DataType::TinyInt(zerofill) => {
547 format_type_with_optional_length(f, "TINYINT", zerofill, false)
548 }
549 DataType::TinyIntUnsigned(zerofill) => {
550 format_type_with_optional_length(f, "TINYINT", zerofill, true)
551 }
552 DataType::Int2(zerofill) => {
553 format_type_with_optional_length(f, "INT2", zerofill, false)
554 }
555 DataType::Int2Unsigned(zerofill) => {
556 format_type_with_optional_length(f, "INT2", zerofill, true)
557 }
558 DataType::SmallInt(zerofill) => {
559 format_type_with_optional_length(f, "SMALLINT", zerofill, false)
560 }
561 DataType::SmallIntUnsigned(zerofill) => {
562 format_type_with_optional_length(f, "SMALLINT", zerofill, true)
563 }
564 DataType::MediumInt(zerofill) => {
565 format_type_with_optional_length(f, "MEDIUMINT", zerofill, false)
566 }
567 DataType::MediumIntUnsigned(zerofill) => {
568 format_type_with_optional_length(f, "MEDIUMINT", zerofill, true)
569 }
570 DataType::Int(zerofill) => format_type_with_optional_length(f, "INT", zerofill, false),
571 DataType::IntUnsigned(zerofill) => {
572 format_type_with_optional_length(f, "INT", zerofill, true)
573 }
574 DataType::Int4(zerofill) => {
575 format_type_with_optional_length(f, "INT4", zerofill, false)
576 }
577 DataType::Int8(zerofill) => {
578 format_type_with_optional_length(f, "INT8", zerofill, false)
579 }
580 DataType::Int16 => {
581 write!(f, "Int16")
582 }
583 DataType::Int32 => {
584 write!(f, "Int32")
585 }
586 DataType::Int64 => {
587 write!(f, "INT64")
588 }
589 DataType::Int128 => {
590 write!(f, "Int128")
591 }
592 DataType::Int256 => {
593 write!(f, "Int256")
594 }
595 DataType::HugeInt => {
596 write!(f, "HUGEINT")
597 }
598 DataType::Int4Unsigned(zerofill) => {
599 format_type_with_optional_length(f, "INT4", zerofill, true)
600 }
601 DataType::Integer(zerofill) => {
602 format_type_with_optional_length(f, "INTEGER", zerofill, false)
603 }
604 DataType::IntegerUnsigned(zerofill) => {
605 format_type_with_optional_length(f, "INTEGER", zerofill, true)
606 }
607 DataType::BigInt(zerofill) => {
608 format_type_with_optional_length(f, "BIGINT", zerofill, false)
609 }
610 DataType::BigIntUnsigned(zerofill) => {
611 format_type_with_optional_length(f, "BIGINT", zerofill, true)
612 }
613 DataType::Int8Unsigned(zerofill) => {
614 format_type_with_optional_length(f, "INT8", zerofill, true)
615 }
616 DataType::UTinyInt => {
617 write!(f, "UTINYINT")
618 }
619 DataType::USmallInt => {
620 write!(f, "USMALLINT")
621 }
622 DataType::UBigInt => {
623 write!(f, "UBIGINT")
624 }
625 DataType::UHugeInt => {
626 write!(f, "UHUGEINT")
627 }
628 DataType::UInt8 => {
629 write!(f, "UInt8")
630 }
631 DataType::UInt16 => {
632 write!(f, "UInt16")
633 }
634 DataType::UInt32 => {
635 write!(f, "UInt32")
636 }
637 DataType::UInt64 => {
638 write!(f, "UInt64")
639 }
640 DataType::UInt128 => {
641 write!(f, "UInt128")
642 }
643 DataType::UInt256 => {
644 write!(f, "UInt256")
645 }
646 DataType::Signed => {
647 write!(f, "SIGNED")
648 }
649 DataType::SignedInteger => {
650 write!(f, "SIGNED INTEGER")
651 }
652 DataType::Unsigned => {
653 write!(f, "UNSIGNED")
654 }
655 DataType::UnsignedInteger => {
656 write!(f, "UNSIGNED INTEGER")
657 }
658 DataType::Real => write!(f, "REAL"),
659 DataType::RealUnsigned => write!(f, "REAL UNSIGNED"),
660 DataType::Float4 => write!(f, "FLOAT4"),
661 DataType::Float32 => write!(f, "Float32"),
662 DataType::Float64 => write!(f, "FLOAT64"),
663 DataType::Double(info) => write!(f, "DOUBLE{info}"),
664 DataType::DoubleUnsigned(info) => write!(f, "DOUBLE{info} UNSIGNED"),
665 DataType::Float8 => write!(f, "FLOAT8"),
666 DataType::DoublePrecision => write!(f, "DOUBLE PRECISION"),
667 DataType::DoublePrecisionUnsigned => write!(f, "DOUBLE PRECISION UNSIGNED"),
668 DataType::Bool => write!(f, "BOOL"),
669 DataType::Boolean => write!(f, "BOOLEAN"),
670 DataType::Date => write!(f, "DATE"),
671 DataType::Date32 => write!(f, "Date32"),
672 DataType::Time(precision, timezone_info) => {
673 format_datetime_precision_and_tz(f, "TIME", precision, timezone_info)
674 }
675 DataType::Datetime(precision) => {
676 format_type_with_optional_length(f, "DATETIME", precision, false)
677 }
678 DataType::Timestamp(precision, timezone_info) => {
679 format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info)
680 }
681 DataType::TimestampNtz(precision) => {
682 format_type_with_optional_length(f, "TIMESTAMP_NTZ", precision, false)
683 }
684 DataType::Datetime64(precision, timezone) => {
685 format_clickhouse_datetime_precision_and_timezone(
686 f,
687 "DateTime64",
688 precision,
689 timezone,
690 )
691 }
692 DataType::Interval { fields, precision } => {
693 write!(f, "INTERVAL")?;
694 if let Some(fields) = fields {
695 write!(f, " {fields}")?;
696 }
697 if let Some(precision) = precision {
698 write!(f, "({precision})")?;
699 }
700 Ok(())
701 }
702 DataType::JSON => write!(f, "JSON"),
703 DataType::JSONB => write!(f, "JSONB"),
704 DataType::Regclass => write!(f, "REGCLASS"),
705 DataType::Text => write!(f, "TEXT"),
706 DataType::TinyText => write!(f, "TINYTEXT"),
707 DataType::MediumText => write!(f, "MEDIUMTEXT"),
708 DataType::LongText => write!(f, "LONGTEXT"),
709 DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false),
710 DataType::Bytea => write!(f, "BYTEA"),
711 DataType::Bit(size) => format_type_with_optional_length(f, "BIT", size, false),
712 DataType::BitVarying(size) => {
713 format_type_with_optional_length(f, "BIT VARYING", size, false)
714 }
715 DataType::VarBit(size) => format_type_with_optional_length(f, "VARBIT", size, false),
716 DataType::Array(ty) => match ty {
717 ArrayElemTypeDef::None => write!(f, "ARRAY"),
718 ArrayElemTypeDef::SquareBracket(t, None) => write!(f, "{t}[]"),
719 ArrayElemTypeDef::SquareBracket(t, Some(size)) => write!(f, "{t}[{size}]"),
720 ArrayElemTypeDef::AngleBracket(t) => write!(f, "ARRAY<{t}>"),
721 ArrayElemTypeDef::Parenthesis(t) => write!(f, "Array({t})"),
722 },
723 DataType::Custom(ty, modifiers) => {
724 if modifiers.is_empty() {
725 write!(f, "{ty}")
726 } else {
727 write!(f, "{}({})", ty, modifiers.join(", "))
728 }
729 }
730 DataType::Enum(vals, bits) => {
731 match bits {
732 Some(bits) => write!(f, "ENUM{bits}"),
733 None => write!(f, "ENUM"),
734 }?;
735 write!(f, "(")?;
736 for (i, v) in vals.iter().enumerate() {
737 if i != 0 {
738 write!(f, ", ")?;
739 }
740 match v {
741 EnumMember::Name(name) => {
742 write!(f, "'{}'", escape_single_quote_string(name))?
743 }
744 EnumMember::NamedValue(name, value) => {
745 write!(f, "'{}' = {}", escape_single_quote_string(name), value)?
746 }
747 }
748 }
749 write!(f, ")")
750 }
751 DataType::Set(vals) => {
752 write!(f, "SET(")?;
753 for (i, v) in vals.iter().enumerate() {
754 if i != 0 {
755 write!(f, ", ")?;
756 }
757 write!(f, "'{}'", escape_single_quote_string(v))?;
758 }
759 write!(f, ")")
760 }
761 DataType::Struct(fields, bracket) => {
762 if !fields.is_empty() {
763 match bracket {
764 StructBracketKind::Parentheses => {
765 write!(f, "STRUCT({})", display_comma_separated(fields))
766 }
767 StructBracketKind::AngleBrackets => {
768 write!(f, "STRUCT<{}>", display_comma_separated(fields))
769 }
770 }
771 } else {
772 write!(f, "STRUCT")
773 }
774 }
775 DataType::Union(fields) => {
776 write!(f, "UNION({})", display_comma_separated(fields))
777 }
778 // ClickHouse
779 DataType::Nullable(data_type) => {
780 write!(f, "Nullable({data_type})")
781 }
782 DataType::FixedString(character_length) => {
783 write!(f, "FixedString({character_length})")
784 }
785 DataType::LowCardinality(data_type) => {
786 write!(f, "LowCardinality({data_type})")
787 }
788 DataType::Map(key_data_type, value_data_type) => {
789 write!(f, "Map({key_data_type}, {value_data_type})")
790 }
791 DataType::Tuple(fields) => {
792 write!(f, "Tuple({})", display_comma_separated(fields))
793 }
794 DataType::Nested(fields) => {
795 write!(f, "Nested({})", display_comma_separated(fields))
796 }
797 DataType::Unspecified => Ok(()),
798 DataType::Trigger => write!(f, "TRIGGER"),
799 DataType::AnyType => write!(f, "ANY TYPE"),
800 DataType::Table(fields) => match fields {
801 Some(fields) => {
802 write!(f, "TABLE({})", display_comma_separated(fields))
803 }
804 None => {
805 write!(f, "TABLE")
806 }
807 },
808 DataType::NamedTable { name, columns } => {
809 write!(f, "{} TABLE ({})", name, display_comma_separated(columns))
810 }
811 DataType::GeometricType(kind) => write!(f, "{kind}"),
812 DataType::TsVector => write!(f, "TSVECTOR"),
813 DataType::TsQuery => write!(f, "TSQUERY"),
814 }
815 }
816}
817
818fn format_type_with_optional_length(
819 f: &mut fmt::Formatter,
820 sql_type: &'static str,
821 len: &Option<u64>,
822 unsigned: bool,
823) -> fmt::Result {
824 write!(f, "{sql_type}")?;
825 if let Some(len) = len {
826 write!(f, "({len})")?;
827 }
828 if unsigned {
829 write!(f, " UNSIGNED")?;
830 }
831 Ok(())
832}
833
834fn format_character_string_type(
835 f: &mut fmt::Formatter,
836 sql_type: &str,
837 size: &Option<CharacterLength>,
838) -> fmt::Result {
839 write!(f, "{sql_type}")?;
840 if let Some(size) = size {
841 write!(f, "({size})")?;
842 }
843 Ok(())
844}
845
846fn format_varbinary_type(
847 f: &mut fmt::Formatter,
848 sql_type: &str,
849 size: &Option<BinaryLength>,
850) -> fmt::Result {
851 write!(f, "{sql_type}")?;
852 if let Some(size) = size {
853 write!(f, "({size})")?;
854 }
855 Ok(())
856}
857
858fn format_datetime_precision_and_tz(
859 f: &mut fmt::Formatter,
860 sql_type: &'static str,
861 len: &Option<u64>,
862 time_zone: &TimezoneInfo,
863) -> fmt::Result {
864 write!(f, "{sql_type}")?;
865 let len_fmt = len.as_ref().map(|l| format!("({l})")).unwrap_or_default();
866
867 match time_zone {
868 TimezoneInfo::Tz => {
869 write!(f, "{time_zone}{len_fmt}")?;
870 }
871 _ => {
872 write!(f, "{len_fmt}{time_zone}")?;
873 }
874 }
875
876 Ok(())
877}
878
879fn format_clickhouse_datetime_precision_and_timezone(
880 f: &mut fmt::Formatter,
881 sql_type: &'static str,
882 len: &u64,
883 time_zone: &Option<String>,
884) -> fmt::Result {
885 write!(f, "{sql_type}({len}")?;
886
887 if let Some(time_zone) = time_zone {
888 write!(f, ", '{time_zone}'")?;
889 }
890
891 write!(f, ")")?;
892
893 Ok(())
894}
895
896/// Type of brackets used for `STRUCT` literals.
897#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
898#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
899#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
900pub enum StructBracketKind {
901 /// Example: `STRUCT(a INT, b STRING)`
902 Parentheses,
903 /// Example: `STRUCT<a INT, b STRING>`
904 AngleBrackets,
905}
906
907/// Timestamp and Time data types information about TimeZone formatting.
908///
909/// This is more related to a display information than real differences between each variant. To
910/// guarantee compatibility with the input query we must maintain its exact information.
911#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
912#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
913#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
914pub enum TimezoneInfo {
915 /// No information about time zone, e.g. TIMESTAMP
916 None,
917 /// Temporal type 'WITH TIME ZONE', e.g. TIMESTAMP WITH TIME ZONE, [SQL Standard], [Oracle]
918 ///
919 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
920 /// [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
921 WithTimeZone,
922 /// Temporal type 'WITHOUT TIME ZONE', e.g. TIME WITHOUT TIME ZONE, [SQL Standard], [Postgresql]
923 ///
924 /// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
925 /// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
926 WithoutTimeZone,
927 /// Postgresql specific `WITH TIME ZONE` formatting, for both TIME and TIMESTAMP, e.g. TIMETZ, [Postgresql]
928 ///
929 /// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
930 Tz,
931}
932
933impl fmt::Display for TimezoneInfo {
934 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
935 match self {
936 TimezoneInfo::None => {
937 write!(f, "")
938 }
939 TimezoneInfo::WithTimeZone => {
940 write!(f, " WITH TIME ZONE")
941 }
942 TimezoneInfo::WithoutTimeZone => {
943 write!(f, " WITHOUT TIME ZONE")
944 }
945 TimezoneInfo::Tz => {
946 // TZ is the only one that is displayed BEFORE the precision, so the datatype display
947 // must be aware of that. Check <https://www.postgresql.org/docs/14/datatype-datetime.html>
948 // for more information
949 write!(f, "TZ")
950 }
951 }
952 }
953}
954
955/// Fields for [Postgres] `INTERVAL` type.
956///
957/// [Postgres]: https://www.postgresql.org/docs/17/datatype-datetime.html
958#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
959#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
960#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
961pub enum IntervalFields {
962 /// `YEAR` field
963 Year,
964 /// `MONTH` field
965 Month,
966 /// `DAY` field
967 Day,
968 /// `HOUR` field
969 Hour,
970 /// `MINUTE` field
971 Minute,
972 /// `SECOND` field
973 Second,
974 /// `YEAR TO MONTH` field
975 YearToMonth,
976 /// `DAY TO HOUR` field
977 DayToHour,
978 /// `DAY TO MINUTE` field
979 DayToMinute,
980 /// `DAY TO SECOND` field
981 DayToSecond,
982 /// `HOUR TO MINUTE` field
983 HourToMinute,
984 /// `HOUR TO SECOND` field
985 HourToSecond,
986 /// `MINUTE TO SECOND` field
987 MinuteToSecond,
988}
989
990impl fmt::Display for IntervalFields {
991 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
992 match self {
993 IntervalFields::Year => write!(f, "YEAR"),
994 IntervalFields::Month => write!(f, "MONTH"),
995 IntervalFields::Day => write!(f, "DAY"),
996 IntervalFields::Hour => write!(f, "HOUR"),
997 IntervalFields::Minute => write!(f, "MINUTE"),
998 IntervalFields::Second => write!(f, "SECOND"),
999 IntervalFields::YearToMonth => write!(f, "YEAR TO MONTH"),
1000 IntervalFields::DayToHour => write!(f, "DAY TO HOUR"),
1001 IntervalFields::DayToMinute => write!(f, "DAY TO MINUTE"),
1002 IntervalFields::DayToSecond => write!(f, "DAY TO SECOND"),
1003 IntervalFields::HourToMinute => write!(f, "HOUR TO MINUTE"),
1004 IntervalFields::HourToSecond => write!(f, "HOUR TO SECOND"),
1005 IntervalFields::MinuteToSecond => write!(f, "MINUTE TO SECOND"),
1006 }
1007 }
1008}
1009
1010/// Additional information for `NUMERIC`, `DECIMAL`, and `DEC` data types
1011/// following the 2016 [SQL Standard].
1012///
1013/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
1014#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1015#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1016#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1017pub enum ExactNumberInfo {
1018 /// No additional information, e.g. `DECIMAL`.
1019 None,
1020 /// Only precision information, e.g. `DECIMAL(10)`.
1021 Precision(u64),
1022 /// Precision and scale information, e.g. `DECIMAL(10,2)`.
1023 PrecisionAndScale(u64, i64),
1024}
1025
1026impl fmt::Display for ExactNumberInfo {
1027 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1028 match self {
1029 ExactNumberInfo::None => {
1030 write!(f, "")
1031 }
1032 ExactNumberInfo::Precision(p) => {
1033 write!(f, "({p})")
1034 }
1035 ExactNumberInfo::PrecisionAndScale(p, s) => {
1036 write!(f, "({p},{s})")
1037 }
1038 }
1039 }
1040}
1041
1042/// Information about [character length][1], including length and possibly unit.
1043///
1044/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-length
1045#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1046#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1047#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1048pub enum CharacterLength {
1049 /// Integer length with optional unit (e.g. `CHAR(10)` or `VARCHAR(10 CHARACTERS)`).
1050 IntegerLength {
1051 /// Default (if VARYING) or maximum (if not VARYING) length
1052 length: u64,
1053 /// Optional unit. If not informed, the ANSI handles it as CHARACTERS implicitly
1054 unit: Option<CharLengthUnits>,
1055 },
1056 /// VARCHAR(MAX) or NVARCHAR(MAX), used in T-SQL (Microsoft SQL Server).
1057 Max,
1058}
1059
1060impl fmt::Display for CharacterLength {
1061 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1062 match self {
1063 CharacterLength::IntegerLength { length, unit } => {
1064 write!(f, "{length}")?;
1065 if let Some(unit) = unit {
1066 write!(f, " {unit}")?;
1067 }
1068 }
1069 CharacterLength::Max => {
1070 write!(f, "MAX")?;
1071 }
1072 }
1073 Ok(())
1074 }
1075}
1076
1077/// Possible units for characters, initially based on 2016 ANSI [SQL Standard][1].
1078///
1079/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#char-length-units
1080#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1081#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1082#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1083pub enum CharLengthUnits {
1084 /// CHARACTERS unit
1085 Characters,
1086 /// OCTETS unit
1087 Octets,
1088}
1089
1090impl fmt::Display for CharLengthUnits {
1091 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1092 match self {
1093 Self::Characters => {
1094 write!(f, "CHARACTERS")
1095 }
1096 Self::Octets => {
1097 write!(f, "OCTETS")
1098 }
1099 }
1100 }
1101}
1102
1103#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1104#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1105#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1106/// Information about [binary length][1], including length and possibly unit.
1107///
1108/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-length
1109pub enum BinaryLength {
1110 /// Integer length for binary types (e.g. `VARBINARY(100)`).
1111 IntegerLength {
1112 /// Default (if VARYING)
1113 length: u64,
1114 },
1115 /// VARBINARY(MAX) used in T-SQL (Microsoft SQL Server).
1116 Max,
1117}
1118
1119impl fmt::Display for BinaryLength {
1120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1121 match self {
1122 BinaryLength::IntegerLength { length } => {
1123 write!(f, "{length}")?;
1124 }
1125 BinaryLength::Max => {
1126 write!(f, "MAX")?;
1127 }
1128 }
1129 Ok(())
1130 }
1131}
1132
1133/// Represents the data type of the elements in an array (if any) as well as
1134/// the syntax used to declare the array.
1135///
1136/// For example: Bigquery/Hive use `ARRAY<INT>` whereas snowflake uses ARRAY.
1137#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1138#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1139#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1140pub enum ArrayElemTypeDef {
1141 /// Use `ARRAY` style without an explicit element type.
1142 None,
1143 /// Angle-bracket style, e.g. `ARRAY<INT>`.
1144 AngleBracket(Box<DataType>),
1145 /// Square-bracket style, e.g. `INT[]` or `INT[2]`.
1146 SquareBracket(Box<DataType>, Option<u64>),
1147 /// Parenthesis style, e.g. `Array(Int64)`.
1148 Parenthesis(Box<DataType>),
1149}
1150
1151/// Represents different types of geometric shapes which are commonly used in
1152/// PostgreSQL/Redshift for spatial operations and geometry-related computations.
1153///
1154/// [PostgreSQL]: https://www.postgresql.org/docs/9.5/functions-geometry.html
1155#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1156#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1157#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1158pub enum GeometricTypeKind {
1159 /// Point geometry
1160 Point,
1161 /// Line geometry
1162 Line,
1163 /// Line segment geometry
1164 LineSegment,
1165 /// Box geometry
1166 GeometricBox,
1167 /// Path geometry
1168 GeometricPath,
1169 /// Polygon geometry
1170 Polygon,
1171 /// Circle geometry
1172 Circle,
1173}
1174
1175impl fmt::Display for GeometricTypeKind {
1176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1177 match self {
1178 GeometricTypeKind::Point => write!(f, "point"),
1179 GeometricTypeKind::Line => write!(f, "line"),
1180 GeometricTypeKind::LineSegment => write!(f, "lseg"),
1181 GeometricTypeKind::GeometricBox => write!(f, "box"),
1182 GeometricTypeKind::GeometricPath => write!(f, "path"),
1183 GeometricTypeKind::Polygon => write!(f, "polygon"),
1184 GeometricTypeKind::Circle => write!(f, "circle"),
1185 }
1186 }
1187}