@@ -19,6 +19,7 @@ use bytes::{Buf, BufMut, BytesMut};
19
19
use jsonbb:: { Value , ValueRef } ;
20
20
use postgres_types:: { accepts, to_sql_checked, FromSql , IsNull , ToSql , Type } ;
21
21
use risingwave_common_estimate_size:: EstimateSize ;
22
+ use thiserror_ext:: AsReport ;
22
23
23
24
use super :: {
24
25
Datum , IntoOrdered , ListValue , MapType , MapValue , ScalarImpl , StructRef , ToOwnedDatum , F64 ,
@@ -408,37 +409,19 @@ impl<'a> JsonbRef<'a> {
408
409
409
410
/// Convert the jsonb value to a datum.
410
411
pub fn to_datum ( self , ty : & DataType ) -> Result < Datum , String > {
411
- if !matches ! (
412
- ty,
413
- DataType :: Jsonb
414
- | DataType :: Boolean
415
- | DataType :: Int16
416
- | DataType :: Int32
417
- | DataType :: Int64
418
- | DataType :: Float32
419
- | DataType :: Float64
420
- | DataType :: Varchar
421
- | DataType :: List ( _)
422
- | DataType :: Struct ( _)
423
- ) {
424
- return Err ( format ! ( "cannot cast jsonb to {ty}" ) ) ;
425
- }
426
412
if self . 0 . as_null ( ) . is_some ( ) {
427
413
return Ok ( None ) ;
428
414
}
429
- Ok ( Some ( match ty {
415
+ let datum = match ty {
430
416
DataType :: Jsonb => ScalarImpl :: Jsonb ( self . into ( ) ) ,
431
- DataType :: Boolean => ScalarImpl :: Bool ( self . as_bool ( ) ?) ,
432
- DataType :: Int16 => ScalarImpl :: Int16 ( self . as_number ( ) ?. try_into ( ) ?) ,
433
- DataType :: Int32 => ScalarImpl :: Int32 ( self . as_number ( ) ?. try_into ( ) ?) ,
434
- DataType :: Int64 => ScalarImpl :: Int64 ( self . as_number ( ) ?. try_into ( ) ?) ,
435
- DataType :: Float32 => ScalarImpl :: Float32 ( self . as_number ( ) ?. try_into ( ) ?) ,
436
- DataType :: Float64 => ScalarImpl :: Float64 ( self . as_number ( ) ?) ,
437
- DataType :: Varchar => ScalarImpl :: Utf8 ( self . force_string ( ) . into ( ) ) ,
438
417
DataType :: List ( t) => ScalarImpl :: List ( self . to_list ( t) ?) ,
439
418
DataType :: Struct ( s) => ScalarImpl :: Struct ( self . to_struct ( s) ?) ,
440
- _ => unreachable ! ( ) ,
441
- } ) )
419
+ _ => {
420
+ let s = self . force_string ( ) ;
421
+ ScalarImpl :: from_text ( & s, ty) . map_err ( |e| format ! ( "{}" , e. as_report( ) ) ) ?
422
+ }
423
+ } ;
424
+ Ok ( Some ( datum) )
442
425
}
443
426
444
427
/// Convert the jsonb value to a list value.
0 commit comments