connectorx/transports/
mysql_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
//! Transport from MySQL Source to Arrow Destination.

use crate::{
    destinations::arrow::{
        typesystem::{ArrowTypeSystem, NaiveDateTimeWrapperMicro, NaiveTimeWrapperMicro},
        ArrowDestination, ArrowDestinationError,
    },
    impl_transport,
    sources::mysql::{
        BinaryProtocol, MySQLSource, MySQLSourceError, MySQLTypeSystem, TextProtocol,
    },
    typesystem::TypeConversion,
};
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use num_traits::ToPrimitive;
use rust_decimal::Decimal;
use serde_json::{to_string, Value};
use std::marker::PhantomData;
use thiserror::Error;

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

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

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

/// Convert MySQL data types to Arrow data types.
pub struct MySQLArrowTransport<P>(PhantomData<P>);

impl_transport!(
    name = MySQLArrowTransport<BinaryProtocol>,
    error = MySQLArrowTransportError,
    systems = MySQLTypeSystem => ArrowTypeSystem,
    route = MySQLSource<BinaryProtocol> => ArrowDestination,
    mappings = {
        { Float[f32]                 => Float64[f64]            | conversion auto }
        { Double[f64]                => Float64[f64]            | conversion auto }
        { Tiny[i8]                   => Boolean[bool]           | conversion option }
        { Short[i16]                 => Int64[i64]              | conversion auto }
        { Int24[i32]                 => Int64[i64]              | conversion none }
        { Long[i32]                  => Int64[i64]              | conversion auto }
        { LongLong[i64]              => Int64[i64]              | conversion auto }
        { UTiny[u8]                  => Int64[i64]              | conversion auto }
        { UShort[u16]                => Int64[i64]              | conversion auto }
        { ULong[u32]                 => Int64[i64]              | conversion auto }
        { UInt24[u32]                => Int64[i64]              | conversion none }
        { ULongLong[u64]             => Float64[f64]            | conversion auto }
        { Date[NaiveDate]            => Date32[NaiveDate]       | conversion auto }
        { Time[NaiveTime]            => Time64Micro[NaiveTimeWrapperMicro]       | conversion option }
        { Datetime[NaiveDateTime]    => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion option }
        { Year[i16]                  => Int64[i64]              | conversion none}
        { Timestamp[NaiveDateTime]   => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion none }
        { Decimal[Decimal]           => Float64[f64]            | conversion option }
        { VarChar[String]            => LargeUtf8[String]       | conversion auto }
        { Char[String]               => LargeUtf8[String]       | conversion none }
        { Enum[String]               => LargeUtf8[String]       | conversion none }
        { TinyBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion auto }
        { Blob[Vec<u8>]              => LargeBinary[Vec<u8>]    | conversion none }
        { MediumBlob[Vec<u8>]        => LargeBinary[Vec<u8>]    | conversion none }
        { LongBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion none }
        { Json[Value]                => LargeUtf8[String]       | conversion option }
        { Bit[Vec<u8>]               => LargeBinary[Vec<u8>]    | conversion none }
    }
);

impl_transport!(
    name = MySQLArrowTransport<TextProtocol>,
    error = MySQLArrowTransportError,
    systems = MySQLTypeSystem => ArrowTypeSystem,
    route = MySQLSource<TextProtocol> => ArrowDestination,
    mappings = {
        { Float[f32]                 => Float64[f64]            | conversion auto }
        { Double[f64]                => Float64[f64]            | conversion auto }
        { Tiny[i8]                   => Boolean[bool]           | conversion option }
        { Short[i16]                 => Int64[i64]              | conversion auto }
        { Int24[i32]                 => Int64[i64]              | conversion none }
        { Long[i32]                  => Int64[i64]              | conversion auto }
        { LongLong[i64]              => Int64[i64]              | conversion auto }
        { UTiny[u8]                  => Int64[i64]              | conversion auto }
        { UShort[u16]                => Int64[i64]              | conversion auto }
        { ULong[u32]                 => Int64[i64]              | conversion auto }
        { UInt24[u32]                => Int64[i64]              | conversion none }
        { ULongLong[u64]             => Float64[f64]            | conversion auto }
        { Date[NaiveDate]            => Date32[NaiveDate]       | conversion auto }
        { Time[NaiveTime]            => Time64Micro[NaiveTimeWrapperMicro]       | conversion option }
        { Datetime[NaiveDateTime]    => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion option }
        { Year[i16]                  => Int64[i64]              | conversion none}
        { Timestamp[NaiveDateTime]   => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion none }
        { Decimal[Decimal]           => Float64[f64]            | conversion option }
        { VarChar[String]            => LargeUtf8[String]       | conversion auto }
        { Char[String]               => LargeUtf8[String]       | conversion none }
        { Enum[String]               => LargeUtf8[String]       | conversion none }
        { TinyBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion auto }
        { Blob[Vec<u8>]              => LargeBinary[Vec<u8>]    | conversion none }
        { MediumBlob[Vec<u8>]        => LargeBinary[Vec<u8>]    | conversion none }
        { LongBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion none }
        { Json[Value]                => LargeUtf8[String]       | conversion option }
        { Bit[Vec<u8>]               => LargeBinary[Vec<u8>]    | conversion none }
    }
);

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

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

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

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

impl<P> TypeConversion<i8, bool> for MySQLArrowTransport<P> {
    fn convert(val: i8) -> bool {
        val != 0
    }
}