@@ -2703,10 +2703,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
27032703 } else if let Ok ( n) = n. parse :: < i64 > ( ) {
27042704 Ok ( lit ( n) )
27052705 } else if self . options . parse_float_as_decimal {
2706- if let Some ( i) = n. find ( '.' ) {
2707- let p = n. len ( ) - 1 ;
2708- let s = n. len ( ) - i - 1 ;
2709- let str = n. replace ( '.' , "" ) ;
2706+ // remove leading zeroes
2707+ let str = n. trim_start_matches ( '0' ) ;
2708+ if let Some ( i) = str. find ( '.' ) {
2709+ // remove trailing zeroes
2710+ let str = str. trim_end_matches ( '0' ) ;
2711+ let p = str. len ( ) - 1 ;
2712+ let s = str. len ( ) - i - 1 ;
2713+ let str = str. replace ( '.' , "" ) ;
27102714 let n = str. parse :: < i128 > ( ) . map_err ( |_| {
27112715 DataFusionError :: from ( ParserError ( format ! (
27122716 "Cannot parse {} as i128 when building decimal" ,
@@ -3093,15 +3097,29 @@ mod tests {
30933097
30943098 #[ test]
30953099 fn parse_decimals ( ) {
3096- let options = ParserOptions {
3097- parse_float_as_decimal : true ,
3098- } ;
3099- quick_test_with_options (
3100- "SELECT 1, 1.0, 0.1, .1, 12.34" ,
3101- "Projection: Int64(1), Decimal128(Some(10),2,1), Decimal128(Some(1),2,1), Decimal128(Some(1),1,1), Decimal128(Some(1234),4,2)\
3102- \n EmptyRelation",
3103- options
3104- ) ;
3100+ let test_data = [
3101+ ( "1" , "Int64(1)" ) ,
3102+ ( "001" , "Int64(1)" ) ,
3103+ ( "0.1" , "Decimal128(Some(1),1,1)" ) ,
3104+ ( "0.01" , "Decimal128(Some(1),2,2)" ) ,
3105+ ( "1.0" , "Decimal128(Some(1),1,0)" ) ,
3106+ ( "10.01" , "Decimal128(Some(1001),4,2)" ) ,
3107+ (
3108+ "10000000000000000000.00" ,
3109+ "Decimal128(Some(10000000000000000000),20,0)" ,
3110+ ) ,
3111+ ] ;
3112+ for ( a, b) in test_data {
3113+ let sql = format ! ( "SELECT {}" , a) ;
3114+ let expected = format ! ( "Projection: {}\n EmptyRelation" , b) ;
3115+ quick_test_with_options (
3116+ & sql,
3117+ & expected,
3118+ ParserOptions {
3119+ parse_float_as_decimal : true ,
3120+ } ,
3121+ ) ;
3122+ }
31053123 }
31063124
31073125 #[ test]
0 commit comments