Skip to content

Commit 278345d

Browse files
yuval-illumexalamb
andauthored
Add support of NVARCHAR data type (apache#462)
* Add support of nvarchar data type * Change the format type with capitals Co-authored-by: Andrew Lamb <[email protected]> * Add Test Co-authored-by: Andrew Lamb <[email protected]>
1 parent d035784 commit 278345d

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

src/ast/data_type.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum DataType {
2929
Char(Option<u64>),
3030
/// Variable-length character type e.g. VARCHAR(10)
3131
Varchar(Option<u64>),
32+
/// Variable-length character type e.g. NVARCHAR(10)
33+
Nvarchar(Option<u64>),
3234
/// Uuid type
3335
Uuid,
3436
/// Large character object e.g. CLOB(1000)
@@ -98,6 +100,9 @@ impl fmt::Display for DataType {
98100
DataType::Varchar(size) => {
99101
format_type_with_optional_length(f, "CHARACTER VARYING", size, false)
100102
}
103+
DataType::Nvarchar(size) => {
104+
format_type_with_optional_length(f, "NVARCHAR", size, false)
105+
}
101106
DataType::Uuid => write!(f, "UUID"),
102107
DataType::Clob(size) => write!(f, "CLOB({})", size),
103108
DataType::Binary(size) => write!(f, "BINARY({})", size),

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ define_keywords!(
336336
NULLIF,
337337
NULLS,
338338
NUMERIC,
339+
NVARCHAR,
339340
OBJECT,
340341
OCCURRENCES_REGEX,
341342
OCTET_LENGTH,

src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,6 +2569,7 @@ impl<'a> Parser<'a> {
25692569
}
25702570
}
25712571
Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_precision()?)),
2572+
Keyword::NVARCHAR => Ok(DataType::Nvarchar(self.parse_optional_precision()?)),
25722573
Keyword::CHAR | Keyword::CHARACTER => {
25732574
if self.parse_keyword(Keyword::VARYING) {
25742575
Ok(DataType::Varchar(self.parse_optional_precision()?))

tests/sqlparser_common.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,16 @@ fn parse_cast() {
13651365
"SELECT CAST(id AS DECIMAL) FROM customer",
13661366
"SELECT CAST(id AS NUMERIC) FROM customer",
13671367
);
1368+
1369+
let sql = "SELECT CAST(id AS NVARCHAR(50)) FROM customer";
1370+
let select = verified_only_select(sql);
1371+
assert_eq!(
1372+
&Expr::Cast {
1373+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
1374+
data_type: DataType::Nvarchar(Some(50))
1375+
},
1376+
expr_from_projection(only(&select.projection))
1377+
);
13681378
}
13691379

13701380
#[test]

0 commit comments

Comments
 (0)