Skip to content

Commit 36572d4

Browse files
committed
Handle Snowflake TEXT cast modifiers
1 parent 913cf0e commit 36572d4

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/parser/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12170,7 +12170,20 @@ impl<'a> Parser<'a> {
1217012170
self.expect_token(&Token::RParen)?;
1217112171
Ok(DataType::FixedString(character_length))
1217212172
}
12173-
Keyword::TEXT => Ok(DataType::Text),
12173+
Keyword::TEXT => {
12174+
if dialect_is!(dialect is SnowflakeDialect) {
12175+
if let Some(modifiers) = self.parse_optional_type_modifiers()? {
12176+
Ok(DataType::Custom(
12177+
ObjectName::from(vec![Ident::new("TEXT")]),
12178+
modifiers,
12179+
))
12180+
} else {
12181+
Ok(DataType::Text)
12182+
}
12183+
} else {
12184+
Ok(DataType::Text)
12185+
}
12186+
}
1217412187
Keyword::TINYTEXT => Ok(DataType::TinyText),
1217512188
Keyword::MEDIUMTEXT => Ok(DataType::MediumText),
1217612189
Keyword::LONGTEXT => Ok(DataType::LongText),

tests/sqlparser_snowflake.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5010,3 +5010,52 @@ fn test_select_dollar_column_from_stage() {
50105010
// With table function args, without alias
50115011
snowflake().verified_stmt("SELECT $1, $2 FROM @mystage1(file_format => 'myformat')");
50125012
}
5013+
5014+
#[test]
5015+
fn test_parse_pg_style_cast_to_text_with_length() {
5016+
let select = snowflake().verified_only_select(
5017+
"SELECT _ID::TEXT(16777216) AS _ID FROM INCARE_ANALYTICS.USER_DETAILS",
5018+
);
5019+
match only(&select.projection) {
5020+
SelectItem::ExprWithAlias {
5021+
expr:
5022+
Expr::Cast {
5023+
kind: CastKind::DoubleColon,
5024+
data_type,
5025+
..
5026+
},
5027+
alias,
5028+
} => {
5029+
assert_eq!(alias.value, "_ID");
5030+
assert_eq!(
5031+
*data_type,
5032+
DataType::Custom(
5033+
ObjectName::from(vec![Ident::new("TEXT")]),
5034+
vec!["16777216".to_string()],
5035+
)
5036+
);
5037+
}
5038+
_ => unreachable!(),
5039+
}
5040+
}
5041+
5042+
#[test]
5043+
fn test_parse_cast_function_to_text_with_length() {
5044+
let select = snowflake().verified_only_select("SELECT CAST(_ID AS TEXT(42)) FROM USER_DETAILS");
5045+
match expr_from_projection(only(&select.projection)) {
5046+
Expr::Cast {
5047+
kind: CastKind::Cast,
5048+
data_type,
5049+
..
5050+
} => {
5051+
assert_eq!(
5052+
*data_type,
5053+
DataType::Custom(
5054+
ObjectName::from(vec![Ident::new("TEXT")]),
5055+
vec!["42".to_string()],
5056+
)
5057+
);
5058+
}
5059+
_ => unreachable!(),
5060+
}
5061+
}

0 commit comments

Comments
 (0)