connectorx/transports/
postgres_arrow.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Transport from Postgres Source to Arrow Destination.

use crate::destinations::arrow::{
    typesystem::{
        ArrowTypeSystem, DateTimeWrapperMicro, NaiveDateTimeWrapperMicro, NaiveTimeWrapperMicro,
    },
    ArrowDestination, ArrowDestinationError,
};
use crate::sources::postgres::{
    BinaryProtocol, CSVProtocol, CursorProtocol, PostgresSource, PostgresSourceError,
    PostgresTypeSystem, SimpleProtocol,
};
use crate::typesystem::TypeConversion;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use num_traits::ToPrimitive;
use postgres::NoTls;
use postgres_openssl::MakeTlsConnector;
use rust_decimal::Decimal;
use serde_json::Value;
use std::marker::PhantomData;
use thiserror::Error;
use uuid::Uuid;

#[derive(Error, Debug)]
pub enum PostgresArrowTransportError {
    #[error(transparent)]
    Source(#[from] PostgresSourceError),

    #[error(transparent)]
    Destination(#[from] ArrowDestinationError),

    #[error(transparent)]
    ConnectorX(#[from] crate::errors::ConnectorXError),
}

/// Convert Postgres data types to Arrow data types.
pub struct PostgresArrowTransport<P, C>(PhantomData<P>, PhantomData<C>);

macro_rules! impl_postgres_transport {
    ($proto:ty, $tls:ty) => {
        impl_transport!(
            name = PostgresArrowTransport<$proto, $tls>,
            error = PostgresArrowTransportError,
            systems = PostgresTypeSystem => ArrowTypeSystem,
            route = PostgresSource<$proto, $tls> => ArrowDestination,
            mappings = {
                { Float4[f32]                => Float64[f64]                           | conversion auto   }
                { Float8[f64]                => Float64[f64]                           | conversion auto   }
                { Numeric[Decimal]           => Float64[f64]                           | conversion option }
                { Int2[i16]                  => Int64[i64]                             | conversion auto   }
                { Int4[i32]                  => Int64[i64]                             | conversion auto   }
                { Int8[i64]                  => Int64[i64]                             | conversion auto   }
                { Bool[bool]                 => Boolean[bool]                          | conversion auto   }
                { Text[&'r str]              => LargeUtf8[String]                      | conversion owned  }
                { BpChar[&'r str]            => LargeUtf8[String]                      | conversion none   }
                { VarChar[&'r str]           => LargeUtf8[String]                      | conversion none   }
                { Name[&'r str]              => LargeUtf8[String]                      | conversion none   }
                { Timestamp[NaiveDateTime]   => Date64Micro[NaiveDateTimeWrapperMicro] | conversion option }
                { Date[NaiveDate]            => Date32[NaiveDate]                      | conversion auto   }
                { Time[NaiveTime]            => Time64Micro[NaiveTimeWrapperMicro]     | conversion option }
                { TimestampTz[DateTime<Utc>] => DateTimeTzMicro[DateTimeWrapperMicro]  | conversion option }
                { UUID[Uuid]                 => LargeUtf8[String]                      | conversion option }
                { Char[&'r str]              => LargeUtf8[String]                      | conversion none   }
                { ByteA[Vec<u8>]             => LargeBinary[Vec<u8>]                   | conversion auto   }
                { JSON[Value]                => LargeUtf8[String]                      | conversion option }
                { JSONB[Value]               => LargeUtf8[String]                      | conversion none   }
                { BoolArray[Vec<bool>]       => BoolArray[Vec<Option<bool>>]           | conversion option }
                { VarcharArray[Vec<String>]  => Utf8Array[Vec<Option<String>>]         | conversion option }
                { TextArray[Vec<String>]     => Utf8Array[Vec<Option<String>>]         | conversion none   }
                { Int2Array[Vec<i16>]        => Int64Array[Vec<Option<i64>>]           | conversion option }
                { Int4Array[Vec<i32>]        => Int64Array[Vec<Option<i64>>]           | conversion option }
                { Int8Array[Vec<i64>]        => Int64Array[Vec<Option<i64>>]           | conversion option }
                { Float4Array[Vec<f32>]      => Float64Array[Vec<Option<f64>>]         | conversion option }
                { Float8Array[Vec<f64>]      => Float64Array[Vec<Option<f64>>]         | conversion option }
                { NumericArray[Vec<Decimal>] => Float64Array[Vec<Option<f64>>]         | conversion option }
            }
        );
    }
}

impl_postgres_transport!(BinaryProtocol, NoTls);
impl_postgres_transport!(BinaryProtocol, MakeTlsConnector);
impl_postgres_transport!(CSVProtocol, NoTls);
impl_postgres_transport!(CSVProtocol, MakeTlsConnector);
impl_postgres_transport!(CursorProtocol, NoTls);
impl_postgres_transport!(CursorProtocol, MakeTlsConnector);
impl_postgres_transport!(SimpleProtocol, NoTls);
impl_postgres_transport!(SimpleProtocol, MakeTlsConnector);

impl<P, C> TypeConversion<NaiveTime, NaiveTimeWrapperMicro> for PostgresArrowTransport<P, C> {
    fn convert(val: NaiveTime) -> NaiveTimeWrapperMicro {
        NaiveTimeWrapperMicro(val)
    }
}

impl<P, C> TypeConversion<NaiveDateTime, NaiveDateTimeWrapperMicro>
    for PostgresArrowTransport<P, C>
{
    fn convert(val: NaiveDateTime) -> NaiveDateTimeWrapperMicro {
        NaiveDateTimeWrapperMicro(val)
    }
}

impl<P, C> TypeConversion<DateTime<Utc>, DateTimeWrapperMicro> for PostgresArrowTransport<P, C> {
    fn convert(val: DateTime<Utc>) -> DateTimeWrapperMicro {
        DateTimeWrapperMicro(val)
    }
}

impl<P, C> TypeConversion<Uuid, String> for PostgresArrowTransport<P, C> {
    fn convert(val: Uuid) -> String {
        val.to_string()
    }
}

impl<P, C> TypeConversion<Decimal, f64> for PostgresArrowTransport<P, C> {
    fn convert(val: Decimal) -> f64 {
        val.to_f64()
            .unwrap_or_else(|| panic!("cannot convert decimal {:?} to float64", val))
    }
}

impl<P, C> TypeConversion<Value, String> for PostgresArrowTransport<P, C> {
    fn convert(val: Value) -> String {
        val.to_string()
    }
}

impl<P, C> TypeConversion<Vec<bool>, Vec<Option<bool>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<bool>) -> Vec<Option<bool>> {
        val.into_iter().map(Some).collect()
    }
}

impl<P, C> TypeConversion<Vec<String>, Vec<Option<String>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<String>) -> Vec<Option<String>> {
        val.into_iter().map(Some).collect()
    }
}

impl<P, C> TypeConversion<Vec<i16>, Vec<Option<i64>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<i16>) -> Vec<Option<i64>> {
        val.into_iter().map(|v| Some(v as i64)).collect()
    }
}

impl<P, C> TypeConversion<Vec<i32>, Vec<Option<i64>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<i32>) -> Vec<Option<i64>> {
        val.into_iter().map(|v| Some(v as i64)).collect()
    }
}

impl<P, C> TypeConversion<Vec<i64>, Vec<Option<i64>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<i64>) -> Vec<Option<i64>> {
        val.into_iter().map(Some).collect()
    }
}

impl<P, C> TypeConversion<Vec<Decimal>, Vec<Option<f64>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<Decimal>) -> Vec<Option<f64>> {
        val.into_iter()
            .map(|v| {
                Some(
                    v.to_f64()
                        .unwrap_or_else(|| panic!("cannot convert decimal {:?} to float64", v)),
                )
            })
            .collect()
    }
}

impl<P, C> TypeConversion<Vec<f32>, Vec<Option<f64>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<f32>) -> Vec<Option<f64>> {
        val.into_iter().map(|v| Some(v as f64)).collect()
    }
}

impl<P, C> TypeConversion<Vec<f64>, Vec<Option<f64>>> for PostgresArrowTransport<P, C> {
    fn convert(val: Vec<f64>) -> Vec<Option<f64>> {
        val.into_iter().map(Some).collect()
    }
}