@@ -2175,14 +2175,19 @@ impl<'a> Parser<'a> {
2175
2175
self.expect_token(&Token::LParen)?;
2176
2176
let expr = self.parse_expr()?;
2177
2177
self.expect_keyword_is(Keyword::AS)?;
2178
- let data_type = self.parse_data_type()?;
2178
+ let dialect = self.dialect;
2179
+ let mysql_style_int = dialect_is!(dialect is MySqlDialect);
2180
+ let data_type = self.parse_data_type_for_cast(mysql_style_int)?;
2181
+ let mysql_style_int = mysql_style_int
2182
+ && (data_type == DataType::BigInt(None) || data_type == DataType::UnsignedBigInt(None));
2179
2183
let format = self.parse_optional_cast_format()?;
2180
2184
self.expect_token(&Token::RParen)?;
2181
2185
Ok(Expr::Cast {
2182
2186
kind,
2183
2187
expr: Box::new(expr),
2184
2188
data_type,
2185
2189
format,
2190
+ mysql_style_int,
2186
2191
})
2187
2192
}
2188
2193
@@ -2884,7 +2889,7 @@ impl<'a> Parser<'a> {
2884
2889
Some(self.parse_identifier()?)
2885
2890
};
2886
2891
2887
- let (field_type, trailing_bracket) = self.parse_data_type_helper()?;
2892
+ let (field_type, trailing_bracket) = self.parse_data_type_helper(false )?;
2888
2893
2889
2894
Ok((
2890
2895
StructField {
@@ -3401,6 +3406,7 @@ impl<'a> Parser<'a> {
3401
3406
expr: Box::new(expr),
3402
3407
data_type: self.parse_data_type()?,
3403
3408
format: None,
3409
+ mysql_style_int: false,
3404
3410
})
3405
3411
} else if Token::ExclamationMark == *tok && self.dialect.supports_factorial_operator() {
3406
3412
Ok(Expr::UnaryOp {
@@ -3641,6 +3647,7 @@ impl<'a> Parser<'a> {
3641
3647
expr: Box::new(expr),
3642
3648
data_type: self.parse_data_type()?,
3643
3649
format: None,
3650
+ mysql_style_int: false,
3644
3651
})
3645
3652
}
3646
3653
@@ -8793,7 +8800,16 @@ impl<'a> Parser<'a> {
8793
8800
8794
8801
/// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
8795
8802
pub fn parse_data_type(&mut self) -> Result<DataType, ParserError> {
8796
- let (ty, trailing_bracket) = self.parse_data_type_helper()?;
8803
+ self.parse_data_type_for_cast(false)
8804
+ }
8805
+
8806
+ /// Parse a SQL datatype, possibly in the context of a CAST expression, for which some dialects
8807
+ /// (i.e. MySQL) have different syntax. See [`Expr::Cast::mysql_style_int`]
8808
+ pub fn parse_data_type_for_cast(
8809
+ &mut self,
8810
+ mysql_style_int: bool,
8811
+ ) -> Result<DataType, ParserError> {
8812
+ let (ty, trailing_bracket) = self.parse_data_type_helper(mysql_style_int)?;
8797
8813
if trailing_bracket.0 {
8798
8814
return parser_err!(
8799
8815
format!("unmatched > after parsing data type {ty}"),
@@ -8806,6 +8822,7 @@ impl<'a> Parser<'a> {
8806
8822
8807
8823
fn parse_data_type_helper(
8808
8824
&mut self,
8825
+ mysql_style_int: bool,
8809
8826
) -> Result<(DataType, MatchedTrailingBracket), ParserError> {
8810
8827
let dialect = self.dialect;
8811
8828
self.advance_token();
@@ -8832,7 +8849,7 @@ impl<'a> Parser<'a> {
8832
8849
))
8833
8850
}
8834
8851
}
8835
- Keyword::TINYINT => {
8852
+ Keyword::TINYINT if !mysql_style_int => {
8836
8853
let optional_precision = self.parse_optional_precision();
8837
8854
if self.parse_keyword(Keyword::UNSIGNED) {
8838
8855
Ok(DataType::UnsignedTinyInt(optional_precision?))
@@ -8848,23 +8865,23 @@ impl<'a> Parser<'a> {
8848
8865
Ok(DataType::Int2(optional_precision?))
8849
8866
}
8850
8867
}
8851
- Keyword::SMALLINT => {
8868
+ Keyword::SMALLINT if !mysql_style_int => {
8852
8869
let optional_precision = self.parse_optional_precision();
8853
8870
if self.parse_keyword(Keyword::UNSIGNED) {
8854
8871
Ok(DataType::UnsignedSmallInt(optional_precision?))
8855
8872
} else {
8856
8873
Ok(DataType::SmallInt(optional_precision?))
8857
8874
}
8858
8875
}
8859
- Keyword::MEDIUMINT => {
8876
+ Keyword::MEDIUMINT if !mysql_style_int => {
8860
8877
let optional_precision = self.parse_optional_precision();
8861
8878
if self.parse_keyword(Keyword::UNSIGNED) {
8862
8879
Ok(DataType::UnsignedMediumInt(optional_precision?))
8863
8880
} else {
8864
8881
Ok(DataType::MediumInt(optional_precision?))
8865
8882
}
8866
8883
}
8867
- Keyword::INT => {
8884
+ Keyword::INT if !mysql_style_int => {
8868
8885
let optional_precision = self.parse_optional_precision();
8869
8886
if self.parse_keyword(Keyword::UNSIGNED) {
8870
8887
Ok(DataType::UnsignedInt(optional_precision?))
@@ -8893,15 +8910,15 @@ impl<'a> Parser<'a> {
8893
8910
Keyword::INT64 => Ok(DataType::Int64),
8894
8911
Keyword::INT128 => Ok(DataType::Int128),
8895
8912
Keyword::INT256 => Ok(DataType::Int256),
8896
- Keyword::INTEGER => {
8913
+ Keyword::INTEGER if !mysql_style_int => {
8897
8914
let optional_precision = self.parse_optional_precision();
8898
8915
if self.parse_keyword(Keyword::UNSIGNED) {
8899
8916
Ok(DataType::UnsignedInteger(optional_precision?))
8900
8917
} else {
8901
8918
Ok(DataType::Integer(optional_precision?))
8902
8919
}
8903
8920
}
8904
- Keyword::BIGINT => {
8921
+ Keyword::BIGINT if !mysql_style_int => {
8905
8922
let optional_precision = self.parse_optional_precision();
8906
8923
if self.parse_keyword(Keyword::UNSIGNED) {
8907
8924
Ok(DataType::UnsignedBigInt(optional_precision?))
@@ -9049,7 +9066,8 @@ impl<'a> Parser<'a> {
9049
9066
})?)
9050
9067
} else {
9051
9068
self.expect_token(&Token::Lt)?;
9052
- let (inside_type, _trailing_bracket) = self.parse_data_type_helper()?;
9069
+ let (inside_type, _trailing_bracket) =
9070
+ self.parse_data_type_helper(mysql_style_int)?;
9053
9071
trailing_bracket = self.expect_closing_angle_bracket(_trailing_bracket)?;
9054
9072
Ok(DataType::Array(ArrayElemTypeDef::AngleBracket(Box::new(
9055
9073
inside_type,
@@ -9110,6 +9128,14 @@ impl<'a> Parser<'a> {
9110
9128
let columns = self.parse_returns_table_columns()?;
9111
9129
Ok(DataType::Table(columns))
9112
9130
}
9131
+ Keyword::UNSIGNED if mysql_style_int => {
9132
+ let _integer = self.parse_keyword(Keyword::INTEGER);
9133
+ Ok(DataType::UnsignedBigInt(None))
9134
+ }
9135
+ Keyword::SIGNED if mysql_style_int => {
9136
+ let _integer = self.parse_keyword(Keyword::INTEGER);
9137
+ Ok(DataType::BigInt(None))
9138
+ }
9113
9139
_ => {
9114
9140
self.prev_token();
9115
9141
let type_name = self.parse_object_name(false)?;
0 commit comments