11use crate :: sql:: {
2- dataframe:: { Decimal128Value , ListValue , TimestampValue } ,
2+ dataframe:: { Decimal128Value , ListValue } ,
33 df_type_to_pg_tid,
44} ;
55use bytes:: { BufMut , BytesMut } ;
6- use chrono:: {
7- format:: {
8- Fixed , Item ,
9- Numeric :: { Day , Hour , Minute , Month , Second , Year } ,
10- Pad :: Zero ,
11- } ,
12- prelude:: * ,
13- } ;
146use datafusion:: arrow:: {
157 array:: {
168 Array , BooleanArray , Float16Array , Float32Array , Float64Array , Int16Array , Int32Array ,
@@ -24,87 +16,7 @@ use pg_srv::{
2416 PgTypeId , ProtocolError , ToProtocolValue ,
2517} ;
2618use postgres_types:: { ToSql , Type } ;
27- use std:: { convert:: TryFrom , io, io:: Error } ;
28-
29- // POSTGRES_EPOCH_JDATE
30- fn pg_base_date_epoch ( ) -> NaiveDateTime {
31- NaiveDate :: from_ymd_opt ( 2000 , 1 , 1 )
32- . unwrap ( )
33- . and_hms_opt ( 0 , 0 , 0 )
34- . unwrap ( )
35- }
36-
37- impl ToProtocolValue for TimestampValue {
38- fn to_text ( & self , buf : & mut BytesMut ) -> Result < ( ) , ProtocolError > {
39- let ndt = match self . tz_ref ( ) {
40- None => self . to_naive_datetime ( ) ,
41- Some ( _) => self . to_fixed_datetime ( ) ?. naive_utc ( ) ,
42- } ;
43-
44- // 2022-04-25 15:36:49.39705+00
45- let as_str = ndt
46- . format_with_items (
47- [
48- Item :: Numeric ( Year , Zero ) ,
49- Item :: Literal ( "-" ) ,
50- Item :: Numeric ( Month , Zero ) ,
51- Item :: Literal ( "-" ) ,
52- Item :: Numeric ( Day , Zero ) ,
53- Item :: Literal ( " " ) ,
54- Item :: Numeric ( Hour , Zero ) ,
55- Item :: Literal ( ":" ) ,
56- Item :: Numeric ( Minute , Zero ) ,
57- Item :: Literal ( ":" ) ,
58- Item :: Numeric ( Second , Zero ) ,
59- Item :: Fixed ( Fixed :: Nanosecond6 ) ,
60- ]
61- . iter ( ) ,
62- )
63- . to_string ( ) ;
64-
65- match self . tz_ref ( ) {
66- None => as_str. to_text ( buf) ,
67- Some ( _) => ( as_str + "+00" ) . to_text ( buf) ,
68- }
69- }
70-
71- fn to_binary ( & self , buf : & mut BytesMut ) -> Result < ( ) , ProtocolError > {
72- match self . tz_ref ( ) {
73- None => {
74- let seconds = self
75- . to_naive_datetime ( )
76- . signed_duration_since ( pg_base_date_epoch ( ) )
77- . num_microseconds ( )
78- . ok_or ( Error :: new (
79- io:: ErrorKind :: Other ,
80- "Unable to extract number of seconds from timestamp" ,
81- ) ) ?;
82-
83- buf. put_i32 ( 8_i32 ) ;
84- buf. put_i64 ( seconds)
85- }
86- Some ( tz) => {
87- let seconds = self
88- . to_fixed_datetime ( ) ?
89- . naive_utc ( )
90- . signed_duration_since ( pg_base_date_epoch ( ) )
91- . num_microseconds ( )
92- . ok_or ( Error :: new (
93- io:: ErrorKind :: Other ,
94- format ! (
95- "Unable to extract number of seconds from timestamp with tz: {}" ,
96- tz
97- ) ,
98- ) ) ?;
99-
100- buf. put_i32 ( 8_i32 ) ;
101- buf. put_i64 ( seconds)
102- }
103- } ;
104-
105- Ok ( ( ) )
106- }
107- }
19+ use std:: convert:: TryFrom ;
10820
10921/// https://github.com/postgres/postgres/blob/REL_14_4/src/backend/utils/adt/numeric.c#L1022
11022impl ToProtocolValue for Decimal128Value {
@@ -324,13 +236,13 @@ impl Serialize for BatchWriter {
324236#[ cfg( test) ]
325237mod tests {
326238 use crate :: sql:: {
327- dataframe:: { Decimal128Value , ListValue , TimestampValue } ,
239+ dataframe:: { Decimal128Value , ListValue } ,
328240 shim:: ConnectionError ,
329241 writer:: { BatchWriter , ToProtocolValue } ,
330242 } ;
331243 use bytes:: BytesMut ;
332244 use datafusion:: arrow:: array:: { ArrayRef , Int64Builder } ;
333- use pg_srv:: { buffer, protocol:: Format } ;
245+ use pg_srv:: { buffer, protocol:: Format , TimestampValue } ;
334246 use std:: { io:: Cursor , sync:: Arc } ;
335247
336248 fn assert_text_encode < T : ToProtocolValue > ( value : T , expected : & [ u8 ] ) {
0 commit comments