@@ -5343,6 +5343,35 @@ macro_rules! format_option {
53435343 } } ;
53445344}
53455345
5346+ macro_rules! format_decimal {
5347+ ( $F: expr, $TYPE: ty, $VALUE: expr, $PRECISION: expr, $SCALE: expr) => { {
5348+ match $VALUE {
5349+ Some ( value) => write!(
5350+ $F,
5351+ "{}" ,
5352+ <$TYPE>:: format_decimal( * value, * $PRECISION, * $SCALE)
5353+ ) ,
5354+ None => write!( $F, "NULL" ) ,
5355+ }
5356+ } } ;
5357+ }
5358+
5359+ macro_rules! format_decimal_debug {
5360+ ( $F: expr, $TYPE_NAME: literal, $TYPE: ty, $VALUE: expr, $PRECISION: expr, $SCALE: expr) => { {
5361+ match $VALUE {
5362+ Some ( value) => write!(
5363+ $F,
5364+ "{}({},{},{})" ,
5365+ $TYPE_NAME,
5366+ <$TYPE>:: format_decimal( * value, * $PRECISION, * $SCALE) ,
5367+ $PRECISION,
5368+ $SCALE
5369+ ) ,
5370+ None => write!( $F, "{}(NULL,{},{})" , $TYPE_NAME, $PRECISION, $SCALE) ,
5371+ }
5372+ } } ;
5373+ }
5374+
53465375// Implement Display trait for ScalarValue
53475376//
53485377// # Panics
@@ -5352,16 +5381,16 @@ impl fmt::Display for ScalarValue {
53525381 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
53535382 match self {
53545383 ScalarValue :: Decimal32 ( v, p, s) => {
5355- write ! ( f, "{v:?},{p:?},{s:?}" ) ? ;
5384+ format_decimal ! ( f, Decimal32Type , v , p , s ) ?
53565385 }
53575386 ScalarValue :: Decimal64 ( v, p, s) => {
5358- write ! ( f, "{v:?},{p:?},{s:?}" ) ? ;
5387+ format_decimal ! ( f, Decimal64Type , v , p , s ) ?
53595388 }
53605389 ScalarValue :: Decimal128 ( v, p, s) => {
5361- write ! ( f, "{v:?},{p:?},{s:?}" ) ? ;
5390+ format_decimal ! ( f, Decimal128Type , v , p , s ) ?
53625391 }
53635392 ScalarValue :: Decimal256 ( v, p, s) => {
5364- write ! ( f, "{v:?},{p:?},{s:?}" ) ? ;
5393+ format_decimal ! ( f, Decimal256Type , v , p , s ) ?
53655394 }
53665395 ScalarValue :: Boolean ( e) => format_option ! ( f, e) ?,
53675396 ScalarValue :: Float16 ( e) => format_option ! ( f, e) ?,
@@ -5535,8 +5564,9 @@ fn fmt_list(arr: &dyn Array, f: &mut fmt::Formatter) -> fmt::Result {
55355564 write ! ( f, "{value_formatter}" )
55365565}
55375566
5538- /// writes a byte array to formatter. `[1, 2, 3]` ==> `"1,2,3"`
5539- fn fmt_binary ( data : & [ u8 ] , f : & mut fmt:: Formatter ) -> fmt:: Result {
5567+ /// Writes a byte array for ScalarValue Debug formatting.
5568+ /// `[1, 2, 3]` -> `"1,2,3"`
5569+ fn fmt_binary_debug ( data : & [ u8 ] , f : & mut fmt:: Formatter ) -> fmt:: Result {
55405570 let mut iter = data. iter ( ) ;
55415571 if let Some ( b) = iter. next ( ) {
55425572 write ! ( f, "{b}" ) ?;
@@ -5550,10 +5580,46 @@ fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
55505580impl fmt:: Debug for ScalarValue {
55515581 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
55525582 match self {
5553- ScalarValue :: Decimal32 ( _, _, _) => write ! ( f, "Decimal32({self})" ) ,
5554- ScalarValue :: Decimal64 ( _, _, _) => write ! ( f, "Decimal64({self})" ) ,
5555- ScalarValue :: Decimal128 ( _, _, _) => write ! ( f, "Decimal128({self})" ) ,
5556- ScalarValue :: Decimal256 ( _, _, _) => write ! ( f, "Decimal256({self})" ) ,
5583+ ScalarValue :: Decimal32 ( value, precision, scale) => {
5584+ format_decimal_debug ! (
5585+ f,
5586+ "Decimal32" ,
5587+ Decimal32Type ,
5588+ value,
5589+ precision,
5590+ scale
5591+ )
5592+ }
5593+ ScalarValue :: Decimal64 ( value, precision, scale) => {
5594+ format_decimal_debug ! (
5595+ f,
5596+ "Decimal64" ,
5597+ Decimal64Type ,
5598+ value,
5599+ precision,
5600+ scale
5601+ )
5602+ }
5603+ ScalarValue :: Decimal128 ( value, precision, scale) => {
5604+ format_decimal_debug ! (
5605+ f,
5606+ "Decimal128" ,
5607+ Decimal128Type ,
5608+ value,
5609+ precision,
5610+ scale
5611+ )
5612+ }
5613+ ScalarValue :: Decimal256 ( value, precision, scale) => {
5614+ format_decimal_debug ! (
5615+ f,
5616+ "Decimal256" ,
5617+ Decimal256Type ,
5618+ value,
5619+ precision,
5620+ scale
5621+ )
5622+ }
55575623 ScalarValue :: Boolean ( _) => write ! ( f, "Boolean({self})" ) ,
55585624 ScalarValue :: Float16 ( _) => write ! ( f, "Float16({self})" ) ,
55595625 ScalarValue :: Float32 ( _) => write ! ( f, "Float32({self})" ) ,
@@ -5587,27 +5653,27 @@ impl fmt::Debug for ScalarValue {
55875653 ScalarValue :: Binary ( None ) => write ! ( f, "Binary({self})" ) ,
55885654 ScalarValue :: Binary ( Some ( b) ) => {
55895655 write ! ( f, "Binary(\" " ) ?;
5590- fmt_binary ( b. as_slice ( ) , f) ?;
5656+ fmt_binary_debug ( b. as_slice ( ) , f) ?;
55915657 write ! ( f, "\" )" )
55925658 }
55935659 ScalarValue :: BinaryView ( None ) => write ! ( f, "BinaryView({self})" ) ,
55945660 ScalarValue :: BinaryView ( Some ( b) ) => {
55955661 write ! ( f, "BinaryView(\" " ) ?;
5596- fmt_binary ( b. as_slice ( ) , f) ?;
5662+ fmt_binary_debug ( b. as_slice ( ) , f) ?;
55975663 write ! ( f, "\" )" )
55985664 }
55995665 ScalarValue :: FixedSizeBinary ( size, None ) => {
56005666 write ! ( f, "FixedSizeBinary({size}, {self})" )
56015667 }
56025668 ScalarValue :: FixedSizeBinary ( size, Some ( b) ) => {
56035669 write ! ( f, "FixedSizeBinary({size}, \" " ) ?;
5604- fmt_binary ( b. as_slice ( ) , f) ?;
5670+ fmt_binary_debug ( b. as_slice ( ) , f) ?;
56055671 write ! ( f, "\" )" )
56065672 }
56075673 ScalarValue :: LargeBinary ( None ) => write ! ( f, "LargeBinary({self})" ) ,
56085674 ScalarValue :: LargeBinary ( Some ( b) ) => {
56095675 write ! ( f, "LargeBinary(\" " ) ?;
5610- fmt_binary ( b. as_slice ( ) , f) ?;
5676+ fmt_binary_debug ( b. as_slice ( ) , f) ?;
56115677 write ! ( f, "\" )" )
56125678 }
56135679 ScalarValue :: FixedSizeList ( _) => write ! ( f, "FixedSizeList({self})" ) ,
@@ -9600,6 +9666,36 @@ mod tests {
96009666 ) ;
96019667 }
96029668
9669+ #[ test]
9670+ fn test_decimal_display_and_debug ( ) {
9671+ let decimal32 = ScalarValue :: Decimal32 ( Some ( 123 ) , 3 , 2 ) ;
9672+ assert_eq ! ( decimal32. to_string( ) , "1.23" ) ;
9673+ assert_eq ! ( format!( "{decimal32:?}" ) , "Decimal32(1.23,3,2)" ) ;
9674+
9675+ let decimal64 = ScalarValue :: Decimal64 ( Some ( -12345 ) , 5 , 3 ) ;
9676+ assert_eq ! ( decimal64. to_string( ) , "-12.345" ) ;
9677+ assert_eq ! ( format!( "{decimal64:?}" ) , "Decimal64(-12.345,5,3)" ) ;
9678+
9679+ let decimal128 = ScalarValue :: Decimal128 ( Some ( 1 ) , 1 , 1 ) ;
9680+ assert_eq ! ( decimal128. to_string( ) , "0.1" ) ;
9681+ assert_eq ! ( format!( "{decimal128:?}" ) , "Decimal128(0.1,1,1)" ) ;
9682+
9683+ let decimal128_trailing_zero = ScalarValue :: Decimal128 ( Some ( 120 ) , 3 , 2 ) ;
9684+ assert_eq ! ( decimal128_trailing_zero. to_string( ) , "1.20" ) ;
9685+ assert_eq ! (
9686+ format!( "{decimal128_trailing_zero:?}" ) ,
9687+ "Decimal128(1.20,3,2)"
9688+ ) ;
9689+
9690+ let decimal256 = ScalarValue :: Decimal256 ( Some ( i256:: from ( 100123 ) ) , 28 , 3 ) ;
9691+ assert_eq ! ( decimal256. to_string( ) , "100.123" ) ;
9692+ assert_eq ! ( format!( "{decimal256:?}" ) , "Decimal256(100.123,28,3)" ) ;
9693+
9694+ let null_decimal = ScalarValue :: Decimal128 ( None , 10 , 2 ) ;
9695+ assert_eq ! ( null_decimal. to_string( ) , "NULL" ) ;
9696+ assert_eq ! ( format!( "{null_decimal:?}" ) , "Decimal128(NULL,10,2)" ) ;
9697+ }
9698+
96039699 #[ test]
96049700 fn test_struct_display_null ( ) {
96059701 let fields = vec ! [ Field :: new( "a" , DataType :: Int32 , false ) ] ;
0 commit comments