Skip to content

Commit 4deed26

Browse files
alexander-beedieAlexander Beedie
and
Alexander Beedie
authored
Support additional DuckDB integer types such as HUGEINT, UHUGEINT, etc (#1797)
Co-authored-by: Alexander Beedie <[email protected]>
1 parent 610096c commit 4deed26

File tree

7 files changed

+274
-201
lines changed

7 files changed

+274
-201
lines changed

src/ast/data_type.rs

+214-177
Large diffs are not rendered by default.

src/ast/ddl.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1276,9 +1276,9 @@ impl fmt::Display for IndexOption {
12761276
}
12771277
}
12781278

1279-
/// [Postgres] unique index nulls handling option: `[ NULLS [ NOT ] DISTINCT ]`
1279+
/// [PostgreSQL] unique index nulls handling option: `[ NULLS [ NOT ] DISTINCT ]`
12801280
///
1281-
/// [Postgres]: https://www.postgresql.org/docs/17/sql-altertable.html
1281+
/// [PostgreSQL]: https://www.postgresql.org/docs/17/sql-altertable.html
12821282
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
12831283
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12841284
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -2175,15 +2175,15 @@ pub struct CreateFunction {
21752175
///
21762176
/// IMMUTABLE | STABLE | VOLATILE
21772177
///
2178-
/// [Postgres](https://www.postgresql.org/docs/current/sql-createfunction.html)
2178+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
21792179
pub behavior: Option<FunctionBehavior>,
21802180
/// CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
21812181
///
2182-
/// [Postgres](https://www.postgresql.org/docs/current/sql-createfunction.html)
2182+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
21832183
pub called_on_null: Option<FunctionCalledOnNull>,
21842184
/// PARALLEL { UNSAFE | RESTRICTED | SAFE }
21852185
///
2186-
/// [Postgres](https://www.postgresql.org/docs/current/sql-createfunction.html)
2186+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
21872187
pub parallel: Option<FunctionParallel>,
21882188
/// USING ... (Hive only)
21892189
pub using: Option<CreateFunctionUsing>,

src/ast/mod.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl fmt::Display for Interval {
410410

411411
/// A field definition within a struct
412412
///
413-
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
413+
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
414414
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
415415
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
416416
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -431,7 +431,7 @@ impl fmt::Display for StructField {
431431

432432
/// A field definition within a union
433433
///
434-
/// [duckdb]: https://duckdb.org/docs/sql/data_types/union.html
434+
/// [DuckDB]: https://duckdb.org/docs/sql/data_types/union.html
435435
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
436436
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
437437
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -448,7 +448,7 @@ impl fmt::Display for UnionField {
448448

449449
/// A dictionary field within a dictionary.
450450
///
451-
/// [duckdb]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
451+
/// [DuckDB]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
452452
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
453453
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
454454
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -479,7 +479,7 @@ impl Display for Map {
479479

480480
/// A map field within a map.
481481
///
482-
/// [duckdb]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
482+
/// [DuckDB]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
483483
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
484484
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
485485
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -2385,10 +2385,10 @@ impl fmt::Display for DeclareAssignment {
23852385
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
23862386
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
23872387
pub enum DeclareType {
2388-
/// Cursor variable type. e.g. [Snowflake] [Postgres]
2388+
/// Cursor variable type. e.g. [Snowflake] [PostgreSQL]
23892389
///
23902390
/// [Snowflake]: https://docs.snowflake.com/en/developer-guide/snowflake-scripting/cursors#declaring-a-cursor
2391-
/// [Postgres]: https://www.postgresql.org/docs/current/plpgsql-cursors.html
2391+
/// [PostgreSQL]: https://www.postgresql.org/docs/current/plpgsql-cursors.html
23922392
Cursor,
23932393

23942394
/// Result set variable type. [Snowflake]
@@ -2427,15 +2427,15 @@ impl fmt::Display for DeclareType {
24272427
}
24282428

24292429
/// A `DECLARE` statement.
2430-
/// [Postgres] [Snowflake] [BigQuery]
2430+
/// [PostgreSQL] [Snowflake] [BigQuery]
24312431
///
24322432
/// Examples:
24332433
/// ```sql
24342434
/// DECLARE variable_name := 42
24352435
/// DECLARE liahona CURSOR FOR SELECT * FROM films;
24362436
/// ```
24372437
///
2438-
/// [Postgres]: https://www.postgresql.org/docs/current/sql-declare.html
2438+
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-declare.html
24392439
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare
24402440
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#declare
24412441
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -3020,7 +3020,7 @@ pub enum Statement {
30203020
/// ```sql
30213021
/// CREATE ROLE
30223022
/// ```
3023-
/// See [postgres](https://www.postgresql.org/docs/current/sql-createrole.html)
3023+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createrole.html)
30243024
CreateRole {
30253025
names: Vec<ObjectName>,
30263026
if_not_exists: bool,
@@ -3046,7 +3046,7 @@ pub enum Statement {
30463046
/// ```sql
30473047
/// CREATE SECRET
30483048
/// ```
3049-
/// See [duckdb](https://duckdb.org/docs/sql/statements/create_secret.html)
3049+
/// See [DuckDB](https://duckdb.org/docs/sql/statements/create_secret.html)
30503050
CreateSecret {
30513051
or_replace: bool,
30523052
temporary: Option<bool>,
@@ -3550,7 +3550,7 @@ pub enum Statement {
35503550
///
35513551
/// Supported variants:
35523552
/// 1. [Hive](https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction)
3553-
/// 2. [Postgres](https://www.postgresql.org/docs/15/sql-createfunction.html)
3553+
/// 2. [PostgreSQL](https://www.postgresql.org/docs/15/sql-createfunction.html)
35543554
/// 3. [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement)
35553555
CreateFunction(CreateFunction),
35563556
/// CREATE TRIGGER
@@ -8281,7 +8281,7 @@ impl fmt::Display for FunctionDeterminismSpecifier {
82818281
/// where within the statement, the body shows up.
82828282
///
82838283
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_11
8284-
/// [Postgres]: https://www.postgresql.org/docs/15/sql-createfunction.html
8284+
/// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
82858285
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
82868286
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
82878287
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -8319,7 +8319,7 @@ pub enum CreateFunctionBody {
83198319
/// RETURN a + b;
83208320
/// ```
83218321
///
8322-
/// [Postgres]: https://www.postgresql.org/docs/current/sql-createfunction.html
8322+
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html
83238323
Return(Expr),
83248324
}
83258325

@@ -8625,9 +8625,9 @@ impl Display for CreateViewParams {
86258625
}
86268626
}
86278627

8628-
/// Engine of DB. Some warehouse has parameters of engine, e.g. [clickhouse]
8628+
/// Engine of DB. Some warehouse has parameters of engine, e.g. [ClickHouse]
86298629
///
8630-
/// [clickhouse]: https://clickhouse.com/docs/en/engines/table-engines
8630+
/// [ClickHouse]: https://clickhouse.com/docs/en/engines/table-engines
86318631
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
86328632
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
86338633
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]

src/dialect/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ pub trait Dialect: Debug + Any {
995995
/// Returns true if the dialect supports `SET NAMES <charset_name> [COLLATE <collation_name>]`.
996996
///
997997
/// - [MySQL](https://dev.mysql.com/doc/refman/8.4/en/set-names.html)
998-
/// - [Postgres](https://www.postgresql.org/docs/17/sql-set.html)
998+
/// - [PostgreSQL](https://www.postgresql.org/docs/17/sql-set.html)
999999
///
10001000
/// Note: Postgres doesn't support the `COLLATE` clause, but we permissively parse it anyway.
10011001
fn supports_set_names(&self) -> bool {

src/keywords.rs

+5
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ define_keywords!(
411411
HOSTS,
412412
HOUR,
413413
HOURS,
414+
HUGEINT,
414415
ICEBERG,
415416
ID,
416417
IDENTITY,
@@ -908,7 +909,9 @@ define_keywords!(
908909
TRY_CONVERT,
909910
TUPLE,
910911
TYPE,
912+
UBIGINT,
911913
UESCAPE,
914+
UHUGEINT,
912915
UINT128,
913916
UINT16,
914917
UINT256,
@@ -942,6 +945,8 @@ define_keywords!(
942945
USER,
943946
USER_RESOURCES,
944947
USING,
948+
USMALLINT,
949+
UTINYINT,
945950
UUID,
946951
VACUUM,
947952
VALID,

src/parser/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -3779,7 +3779,7 @@ impl<'a> Parser<'a> {
37793779
})
37803780
}
37813781

3782-
/// Parse a postgresql casting style which is in the form of `expr::datatype`.
3782+
/// Parse a PostgreSQL casting style which is in the form of `expr::datatype`.
37833783
pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
37843784
Ok(Expr::Cast {
37853785
kind: CastKind::DoubleColon,
@@ -4873,9 +4873,9 @@ impl<'a> Parser<'a> {
48734873
}
48744874
}
48754875

4876-
/// Parse `CREATE FUNCTION` for [Postgres]
4876+
/// Parse `CREATE FUNCTION` for [PostgreSQL]
48774877
///
4878-
/// [Postgres]: https://www.postgresql.org/docs/15/sql-createfunction.html
4878+
/// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
48794879
fn parse_postgres_create_function(
48804880
&mut self,
48814881
or_replace: bool,
@@ -9171,6 +9171,11 @@ impl<'a> Parser<'a> {
91719171
Ok(DataType::BigInt(optional_precision?))
91729172
}
91739173
}
9174+
Keyword::HUGEINT => Ok(DataType::HugeInt),
9175+
Keyword::UBIGINT => Ok(DataType::UBigInt),
9176+
Keyword::UHUGEINT => Ok(DataType::UHugeInt),
9177+
Keyword::USMALLINT => Ok(DataType::USmallInt),
9178+
Keyword::UTINYINT => Ok(DataType::UTinyInt),
91749179
Keyword::UINT8 => Ok(DataType::UInt8),
91759180
Keyword::UINT16 => Ok(DataType::UInt16),
91769181
Keyword::UINT32 => Ok(DataType::UInt32),

tests/sqlparser_duckdb.rs

+26
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,32 @@ fn test_duckdb_load_extension() {
352352
);
353353
}
354354

355+
#[test]
356+
fn test_duckdb_specific_int_types() {
357+
let duckdb_dtypes = vec![
358+
("UTINYINT", DataType::UTinyInt),
359+
("USMALLINT", DataType::USmallInt),
360+
("UBIGINT", DataType::UBigInt),
361+
("UHUGEINT", DataType::UHugeInt),
362+
("HUGEINT", DataType::HugeInt),
363+
];
364+
for (dtype_string, data_type) in duckdb_dtypes {
365+
let sql = format!("SELECT 123::{}", dtype_string);
366+
let select = duckdb().verified_only_select(&sql);
367+
assert_eq!(
368+
&Expr::Cast {
369+
kind: CastKind::DoubleColon,
370+
expr: Box::new(Expr::Value(
371+
Value::Number("123".parse().unwrap(), false).with_empty_span()
372+
)),
373+
data_type: data_type.clone(),
374+
format: None,
375+
},
376+
expr_from_projection(&select.projection[0])
377+
);
378+
}
379+
}
380+
355381
#[test]
356382
fn test_duckdb_struct_literal() {
357383
//struct literal syntax https://duckdb.org/docs/sql/data_types/struct#creating-structs

0 commit comments

Comments
 (0)