@@ -7,6 +7,7 @@ use std::sync::atomic::Ordering::{Acquire, Release};
77use serde:: { Deserialize , Serialize } ;
88
99use integer_encoding:: FixedInt ;
10+ use sqlparser:: ast:: ExactNumberInfo ;
1011use strum_macros:: AsRefStr ;
1112
1213use crate :: types:: errors:: TypeError ;
@@ -57,6 +58,8 @@ pub enum LogicalType {
5758 Varchar ( Option < u32 > ) ,
5859 Date ,
5960 DateTime ,
61+ // decimal (precision, scale)
62+ Decimal ( Option < u8 > , Option < u8 > ) ,
6063}
6164
6265impl LogicalType {
@@ -75,8 +78,9 @@ impl LogicalType {
7578 LogicalType :: UBigint => Some ( 8 ) ,
7679 LogicalType :: Float => Some ( 4 ) ,
7780 LogicalType :: Double => Some ( 8 ) ,
78- /// Note: The non-fixed length type's raw_len is None
79- LogicalType :: Varchar ( _) =>None ,
81+ /// Note: The non-fixed length type's raw_len is None e.g. Varchar and Decimal
82+ LogicalType :: Varchar ( _) => None ,
83+ LogicalType :: Decimal ( _, _) => Some ( 16 ) ,
8084 LogicalType :: Date => Some ( 4 ) ,
8185 LogicalType :: DateTime => Some ( 8 ) ,
8286 }
@@ -269,6 +273,7 @@ impl LogicalType {
269273 LogicalType :: Varchar ( _) => false ,
270274 LogicalType :: Date => matches ! ( to, LogicalType :: DateTime | LogicalType :: Varchar ( _) ) ,
271275 LogicalType :: DateTime => matches ! ( to, LogicalType :: Date | LogicalType :: Varchar ( _) ) ,
276+ LogicalType :: Decimal ( _, _) => false ,
272277 }
273278 }
274279}
@@ -296,6 +301,13 @@ impl TryFrom<sqlparser::ast::DataType> for LogicalType {
296301 sqlparser:: ast:: DataType :: UnsignedBigInt ( _) => Ok ( LogicalType :: UBigint ) ,
297302 sqlparser:: ast:: DataType :: Boolean => Ok ( LogicalType :: Boolean ) ,
298303 sqlparser:: ast:: DataType :: Datetime ( _) => Ok ( LogicalType :: DateTime ) ,
304+ sqlparser:: ast:: DataType :: Decimal ( info) => match info {
305+ ExactNumberInfo :: None => Ok ( Self :: Decimal ( None , None ) ) ,
306+ ExactNumberInfo :: Precision ( p) => Ok ( Self :: Decimal ( Some ( p as u8 ) , None ) ) ,
307+ ExactNumberInfo :: PrecisionAndScale ( p, s) => {
308+ Ok ( Self :: Decimal ( Some ( p as u8 ) , Some ( s as u8 ) ) )
309+ }
310+ } ,
299311 other => Err ( TypeError :: NotImplementedSqlparserDataType (
300312 other. to_string ( ) ,
301313 ) ) ,
@@ -313,7 +325,7 @@ impl std::fmt::Display for LogicalType {
313325mod test {
314326 use std:: sync:: atomic:: Ordering :: Release ;
315327
316- use crate :: types:: { IdGenerator , ID_BUF , LogicalType } ;
328+ use crate :: types:: { IdGenerator , ID_BUF } ;
317329
318330 /// Tips: 由于IdGenerator为static全局性质生成的id,因此需要单独测试避免其他测试方法干扰
319331 #[ test]
0 commit comments