1+ use std:: error:: Error ;
12use std:: io:: Write ;
23use std:: str:: FromStr ;
34use std:: sync:: Arc ;
45
6+ use arrow:: array:: * ;
7+ use arrow:: datatypes:: * ;
58use bytes:: BufMut ;
69use bytes:: BytesMut ;
710use chrono:: { NaiveDate , NaiveDateTime } ;
8- use datafusion:: arrow:: array:: * ;
9- use datafusion:: arrow:: datatypes:: * ;
10- use list_encoder:: encode_list;
1111use pgwire:: api:: results:: DataRowEncoder ;
1212use pgwire:: api:: results:: FieldFormat ;
13- use pgwire:: error:: { ErrorInfo , PgWireError , PgWireResult } ;
13+ use pgwire:: error:: PgWireError ;
14+ use pgwire:: error:: PgWireResult ;
1415use pgwire:: types:: ToSqlText ;
1516use postgres_types:: { ToSql , Type } ;
1617use rust_decimal:: Decimal ;
17- use struct_encoder:: encode_struct;
1818use timezone:: Tz ;
1919
20- pub mod list_encoder ;
21- pub mod row_encoder ;
22- pub mod struct_encoder;
20+ use crate :: error :: ToSqlError ;
21+ use crate :: list_encoder :: encode_list ;
22+ use crate :: struct_encoder:: encode_struct ;
2323
24- trait Encoder {
24+ pub trait Encoder {
2525 fn encode_field_with_type_and_format < T > (
2626 & mut self ,
2727 value : & T ,
@@ -61,7 +61,7 @@ impl ToSql for EncodedValue {
6161 & self ,
6262 _ty : & Type ,
6363 out : & mut BytesMut ,
64- ) -> Result < postgres_types:: IsNull , Box < dyn std :: error :: Error + Sync + Send > >
64+ ) -> Result < postgres_types:: IsNull , Box < dyn Error + Send + Sync > >
6565 where
6666 Self : Sized ,
6767 {
@@ -80,7 +80,7 @@ impl ToSql for EncodedValue {
8080 & self ,
8181 ty : & Type ,
8282 out : & mut BytesMut ,
83- ) -> Result < postgres_types:: IsNull , Box < dyn std :: error :: Error + Sync + Send > > {
83+ ) -> Result < postgres_types:: IsNull , Box < dyn Error + Send + Sync > > {
8484 self . to_sql ( ty, out)
8585 }
8686}
@@ -90,7 +90,7 @@ impl ToSqlText for EncodedValue {
9090 & self ,
9191 _ty : & Type ,
9292 out : & mut BytesMut ,
93- ) -> Result < postgres_types:: IsNull , Box < dyn std :: error :: Error + Sync + Send > >
93+ ) -> Result < postgres_types:: IsNull , Box < dyn Error + Send + Sync > >
9494 where
9595 Self : Sized ,
9696 {
@@ -261,16 +261,13 @@ fn get_numeric_128_value(
261261 }
262262 _ => unreachable ! ( ) ,
263263 } ;
264- PgWireError :: UserError ( Box :: new ( ErrorInfo :: new (
265- "ERROR" . to_owned ( ) ,
266- "XX000" . to_owned ( ) ,
267- message. to_owned ( ) ,
268- ) ) )
264+ // TODO: add error type in PgWireError
265+ PgWireError :: ApiError ( ToSqlError :: from ( message) )
269266 } )
270267 . map ( Some )
271268}
272269
273- fn encode_value < T : Encoder > (
270+ pub fn encode_value < T : Encoder > (
274271 encoder : & mut T ,
275272 arr : & Arc < dyn Array > ,
276273 idx : usize ,
@@ -387,8 +384,7 @@ fn encode_value<T: Encoder>(
387384 }
388385 let ts_array = arr. as_any ( ) . downcast_ref :: < TimestampSecondArray > ( ) . unwrap ( ) ;
389386 if let Some ( tz) = timezone {
390- let tz = Tz :: from_str ( tz. as_ref ( ) )
391- . map_err ( |e| PgWireError :: ApiError ( Box :: new ( e) ) ) ?;
387+ let tz = Tz :: from_str ( tz. as_ref ( ) ) . map_err ( ToSqlError :: from) ?;
392388 let value = ts_array
393389 . value_as_datetime_with_tz ( idx, tz)
394390 . map ( |d| d. fixed_offset ( ) ) ;
@@ -411,8 +407,7 @@ fn encode_value<T: Encoder>(
411407 . downcast_ref :: < TimestampMillisecondArray > ( )
412408 . unwrap ( ) ;
413409 if let Some ( tz) = timezone {
414- let tz = Tz :: from_str ( tz. as_ref ( ) )
415- . map_err ( |e| PgWireError :: ApiError ( Box :: new ( e) ) ) ?;
410+ let tz = Tz :: from_str ( tz. as_ref ( ) ) . map_err ( ToSqlError :: from) ?;
416411 let value = ts_array
417412 . value_as_datetime_with_tz ( idx, tz)
418413 . map ( |d| d. fixed_offset ( ) ) ;
@@ -435,8 +430,7 @@ fn encode_value<T: Encoder>(
435430 . downcast_ref :: < TimestampMicrosecondArray > ( )
436431 . unwrap ( ) ;
437432 if let Some ( tz) = timezone {
438- let tz = Tz :: from_str ( tz. as_ref ( ) )
439- . map_err ( |e| PgWireError :: ApiError ( Box :: new ( e) ) ) ?;
433+ let tz = Tz :: from_str ( tz. as_ref ( ) ) . map_err ( ToSqlError :: from) ?;
440434 let value = ts_array
441435 . value_as_datetime_with_tz ( idx, tz)
442436 . map ( |d| d. fixed_offset ( ) ) ;
@@ -459,8 +453,7 @@ fn encode_value<T: Encoder>(
459453 . downcast_ref :: < TimestampNanosecondArray > ( )
460454 . unwrap ( ) ;
461455 if let Some ( tz) = timezone {
462- let tz = Tz :: from_str ( tz. as_ref ( ) )
463- . map_err ( |e| PgWireError :: ApiError ( Box :: new ( e) ) ) ?;
456+ let tz = Tz :: from_str ( tz. as_ref ( ) ) . map_err ( ToSqlError :: from) ?;
464457 let value = ts_array
465458 . value_as_datetime_with_tz ( idx, tz)
466459 . map ( |d| d. fixed_offset ( ) ) ;
@@ -483,11 +476,10 @@ fn encode_value<T: Encoder>(
483476 let fields = match type_. kind ( ) {
484477 postgres_types:: Kind :: Composite ( fields) => fields,
485478 _ => {
486- return Err ( PgWireError :: UserError ( Box :: new ( ErrorInfo :: new (
487- "ERROR" . to_owned ( ) ,
488- "XX000" . to_owned ( ) ,
489- format ! ( "Failed to unwrap a composite type from type {}" , type_) ,
490- ) ) ) )
479+ return Err ( PgWireError :: ApiError ( ToSqlError :: from ( format ! (
480+ "Failed to unwrap a composite type from type {}" ,
481+ type_
482+ ) ) ) ) ;
491483 }
492484 } ;
493485 let value = encode_struct ( arr, idx, fields, format) ?;
@@ -517,14 +509,10 @@ fn encode_value<T: Encoder>(
517509 . or_else ( || get_dict_values ! ( UInt32Type ) )
518510 . or_else ( || get_dict_values ! ( UInt64Type ) )
519511 . ok_or_else ( || {
520- PgWireError :: UserError ( Box :: new ( ErrorInfo :: new (
521- "ERROR" . to_owned ( ) ,
522- "XX000" . to_owned ( ) ,
523- format ! (
524- "Unsupported dictionary key type for value type {}" ,
525- value_type
526- ) ,
527- ) ) )
512+ ToSqlError :: from ( format ! (
513+ "Unsupported dictionary key type for value type {}" ,
514+ value_type
515+ ) )
528516 } ) ?;
529517
530518 // If the dictionary has only one value, treat it as a primitive
@@ -536,15 +524,11 @@ fn encode_value<T: Encoder>(
536524 }
537525 }
538526 _ => {
539- return Err ( PgWireError :: UserError ( Box :: new ( ErrorInfo :: new (
540- "ERROR" . to_owned ( ) ,
541- "XX000" . to_owned ( ) ,
542- format ! (
543- "Unsupported Datatype {} and array {:?}" ,
544- arr. data_type( ) ,
545- & arr
546- ) ,
547- ) ) ) )
527+ return Err ( PgWireError :: ApiError ( ToSqlError :: from ( format ! (
528+ "Unsupported Datatype {} and array {:?}" ,
529+ arr. data_type( ) ,
530+ & arr
531+ ) ) ) ) ;
548532 }
549533 }
550534
0 commit comments