Skip to content

Commit c7bdd76

Browse files
alexander-beedieAlexander Beedie
authored andcommitted
Support additional DuckDB integer types such as HUGEINT, UHUGEINT, etc (apache#1797)
Co-authored-by: Alexander Beedie <[email protected]>
1 parent 9419e22 commit c7bdd76

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))]
@@ -2403,10 +2403,10 @@ impl fmt::Display for DeclareAssignment {
24032403
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24042404
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
24052405
pub enum DeclareType {
2406-
/// Cursor variable type. e.g. [Snowflake] [Postgres]
2406+
/// Cursor variable type. e.g. [Snowflake] [PostgreSQL]
24072407
///
24082408
/// [Snowflake]: https://docs.snowflake.com/en/developer-guide/snowflake-scripting/cursors#declaring-a-cursor
2409-
/// [Postgres]: https://www.postgresql.org/docs/current/plpgsql-cursors.html
2409+
/// [PostgreSQL]: https://www.postgresql.org/docs/current/plpgsql-cursors.html
24102410
Cursor,
24112411

24122412
/// Result set variable type. [Snowflake]
@@ -2445,15 +2445,15 @@ impl fmt::Display for DeclareType {
24452445
}
24462446

24472447
/// A `DECLARE` statement.
2448-
/// [Postgres] [Snowflake] [BigQuery]
2448+
/// [PostgreSQL] [Snowflake] [BigQuery]
24492449
///
24502450
/// Examples:
24512451
/// ```sql
24522452
/// DECLARE variable_name := 42
24532453
/// DECLARE liahona CURSOR FOR SELECT * FROM films;
24542454
/// ```
24552455
///
2456-
/// [Postgres]: https://www.postgresql.org/docs/current/sql-declare.html
2456+
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-declare.html
24572457
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare
24582458
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#declare
24592459
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -3038,7 +3038,7 @@ pub enum Statement {
30383038
/// ```sql
30393039
/// CREATE ROLE
30403040
/// ```
3041-
/// See [postgres](https://www.postgresql.org/docs/current/sql-createrole.html)
3041+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createrole.html)
30423042
CreateRole {
30433043
names: Vec<ObjectName>,
30443044
if_not_exists: bool,
@@ -3064,7 +3064,7 @@ pub enum Statement {
30643064
/// ```sql
30653065
/// CREATE SECRET
30663066
/// ```
3067-
/// See [duckdb](https://duckdb.org/docs/sql/statements/create_secret.html)
3067+
/// See [DuckDB](https://duckdb.org/docs/sql/statements/create_secret.html)
30683068
CreateSecret {
30693069
or_replace: bool,
30703070
temporary: Option<bool>,
@@ -3568,7 +3568,7 @@ pub enum Statement {
35683568
///
35693569
/// Supported variants:
35703570
/// 1. [Hive](https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction)
3571-
/// 2. [Postgres](https://www.postgresql.org/docs/15/sql-createfunction.html)
3571+
/// 2. [PostgreSQL](https://www.postgresql.org/docs/15/sql-createfunction.html)
35723572
/// 3. [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement)
35733573
CreateFunction(CreateFunction),
35743574
/// CREATE TRIGGER
@@ -8299,7 +8299,7 @@ impl fmt::Display for FunctionDeterminismSpecifier {
82998299
/// where within the statement, the body shows up.
83008300
///
83018301
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_11
8302-
/// [Postgres]: https://www.postgresql.org/docs/15/sql-createfunction.html
8302+
/// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
83038303
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
83048304
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
83058305
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -8337,7 +8337,7 @@ pub enum CreateFunctionBody {
83378337
/// RETURN a + b;
83388338
/// ```
83398339
///
8340-
/// [Postgres]: https://www.postgresql.org/docs/current/sql-createfunction.html
8340+
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html
83418341
Return(Expr),
83428342
}
83438343

@@ -8643,9 +8643,9 @@ impl Display for CreateViewParams {
86438643
}
86448644
}
86458645

8646-
/// Engine of DB. Some warehouse has parameters of engine, e.g. [clickhouse]
8646+
/// Engine of DB. Some warehouse has parameters of engine, e.g. [ClickHouse]
86478647
///
8648-
/// [clickhouse]: https://clickhouse.com/docs/en/engines/table-engines
8648+
/// [ClickHouse]: https://clickhouse.com/docs/en/engines/table-engines
86498649
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
86508650
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
86518651
#[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
@@ -3789,7 +3789,7 @@ impl<'a> Parser<'a> {
37893789
})
37903790
}
37913791

3792-
/// Parse a postgresql casting style which is in the form of `expr::datatype`.
3792+
/// Parse a PostgreSQL casting style which is in the form of `expr::datatype`.
37933793
pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
37943794
Ok(Expr::Cast {
37953795
kind: CastKind::DoubleColon,
@@ -4883,9 +4883,9 @@ impl<'a> Parser<'a> {
48834883
}
48844884
}
48854885

4886-
/// Parse `CREATE FUNCTION` for [Postgres]
4886+
/// Parse `CREATE FUNCTION` for [PostgreSQL]
48874887
///
4888-
/// [Postgres]: https://www.postgresql.org/docs/15/sql-createfunction.html
4888+
/// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
48894889
fn parse_postgres_create_function(
48904890
&mut self,
48914891
or_replace: bool,
@@ -9181,6 +9181,11 @@ impl<'a> Parser<'a> {
91819181
Ok(DataType::BigInt(optional_precision?))
91829182
}
91839183
}
9184+
Keyword::HUGEINT => Ok(DataType::HugeInt),
9185+
Keyword::UBIGINT => Ok(DataType::UBigInt),
9186+
Keyword::UHUGEINT => Ok(DataType::UHugeInt),
9187+
Keyword::USMALLINT => Ok(DataType::USmallInt),
9188+
Keyword::UTINYINT => Ok(DataType::UTinyInt),
91849189
Keyword::UINT8 => Ok(DataType::UInt8),
91859190
Keyword::UINT16 => Ok(DataType::UInt16),
91869191
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)