Skip to main content

connectorx/transports/
mysql_arrow.rs

1//! Transport from MySQL Source to Arrow Destination.
2
3use crate::{
4    destinations::arrow::{
5        typesystem::{ArrowTypeSystem, NaiveDateTimeWrapperMicro, NaiveTimeWrapperMicro},
6        ArrowDestination, ArrowDestinationError,
7    },
8    sources::mysql::{
9        BinaryProtocol, MySQLSource, MySQLSourceError, MySQLTypeSystem, TextProtocol,
10    },
11    typesystem::TypeConversion,
12};
13use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
14use num_traits::ToPrimitive;
15use rust_decimal::Decimal;
16use serde_json::{to_string, Value};
17use std::marker::PhantomData;
18use thiserror::Error;
19
20/// Errors raised while moving data from the MySQL source to the Arrow destination.
21#[derive(Error, Debug)]
22pub enum MySQLArrowTransportError {
23    /// Error from the MySQL source.
24    #[error(transparent)]
25    Source(#[from] MySQLSourceError),
26
27    /// Error from the Arrow destination.
28    #[error(transparent)]
29    Destination(#[from] ArrowDestinationError),
30
31    /// Generic ConnectorX error.
32    #[error(transparent)]
33    ConnectorX(#[from] crate::errors::ConnectorXError),
34}
35
36/// Convert MySQL data types to Arrow data types.
37pub struct MySQLArrowTransport<P>(PhantomData<P>);
38
39impl_transport!(
40    name = MySQLArrowTransport<BinaryProtocol>,
41    error = MySQLArrowTransportError,
42    systems = MySQLTypeSystem => ArrowTypeSystem,
43    route = MySQLSource<BinaryProtocol> => ArrowDestination,
44    mappings = {
45        { Float[f32]                 => Float32[f32]            | conversion auto }
46        { Double[f64]                => Float64[f64]            | conversion auto }
47        { Tiny[i8]                   => Int8[i8]                | conversion auto }
48        { Short[i16]                 => Int16[i16]              | conversion auto }
49        { Int24[i32]                 => Int32[i32]              | conversion none }
50        { Long[i32]                  => Int32[i32]              | conversion auto }
51        { LongLong[i64]              => Int64[i64]              | conversion auto }
52        { UTiny[u8]                  => UInt8[u8]               | conversion auto }
53        { UShort[u16]                => UInt16[u16]             | conversion auto }
54        { UInt24[u32]                => UInt32[u32]             | conversion none }
55        { ULong[u32]                 => UInt32[u32]             | conversion auto }
56        { ULongLong[u64]             => UInt64[u64]             | conversion auto }
57        { Date[NaiveDate]            => Date32[NaiveDate]       | conversion auto }
58        { Time[NaiveTime]            => Time64Micro[NaiveTimeWrapperMicro]       | conversion option }
59        { Datetime[NaiveDateTime]    => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion option }
60        { Year[i16]                  => Int16[i16]              | conversion none }
61        { Timestamp[NaiveDateTime]   => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion none }
62        { Decimal[Decimal]           => Decimal[Decimal]        | conversion auto }
63        { VarChar[String]            => LargeUtf8[String]       | conversion auto }
64        { Char[String]               => LargeUtf8[String]       | conversion none }
65        { Enum[String]               => LargeUtf8[String]       | conversion none }
66        { TinyBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion auto }
67        { Blob[Vec<u8>]              => LargeBinary[Vec<u8>]    | conversion none }
68        { MediumBlob[Vec<u8>]        => LargeBinary[Vec<u8>]    | conversion none }
69        { LongBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion none }
70        { Json[Value]                => LargeUtf8[String]       | conversion option }
71        { Bit[Vec<u8>]               => LargeBinary[Vec<u8>]    | conversion none }
72    }
73);
74
75impl_transport!(
76    name = MySQLArrowTransport<TextProtocol>,
77    error = MySQLArrowTransportError,
78    systems = MySQLTypeSystem => ArrowTypeSystem,
79    route = MySQLSource<TextProtocol> => ArrowDestination,
80    mappings = {
81        { Float[f32]                 => Float32[f32]            | conversion auto }
82        { Double[f64]                => Float64[f64]            | conversion auto }
83        { Tiny[i8]                   => Int8[i8]                | conversion auto }
84        { Short[i16]                 => Int16[i16]              | conversion auto }
85        { Int24[i32]                 => Int32[i32]              | conversion none }
86        { Long[i32]                  => Int32[i32]              | conversion auto }
87        { LongLong[i64]              => Int64[i64]              | conversion auto }
88        { UTiny[u8]                  => UInt8[u8]               | conversion auto }
89        { UShort[u16]                => UInt16[u16]             | conversion auto }
90        { UInt24[u32]                => UInt32[u32]             | conversion none }
91        { ULong[u32]                 => UInt32[u32]             | conversion auto }
92        { ULongLong[u64]             => UInt64[u64]             | conversion auto }
93        { Date[NaiveDate]            => Date32[NaiveDate]       | conversion auto }
94        { Time[NaiveTime]            => Time64Micro[NaiveTimeWrapperMicro]       | conversion option }
95        { Datetime[NaiveDateTime]    => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion option }
96        { Year[i16]                  => Int16[i16]              | conversion none }
97        { Timestamp[NaiveDateTime]   => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion none }
98        { Decimal[Decimal]           => Decimal[Decimal]        | conversion auto }
99        { VarChar[String]            => LargeUtf8[String]       | conversion auto }
100        { Char[String]               => LargeUtf8[String]       | conversion none }
101        { Enum[String]               => LargeUtf8[String]       | conversion none }
102        { TinyBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion auto }
103        { Blob[Vec<u8>]              => LargeBinary[Vec<u8>]    | conversion none }
104        { MediumBlob[Vec<u8>]        => LargeBinary[Vec<u8>]    | conversion none }
105        { LongBlob[Vec<u8>]          => LargeBinary[Vec<u8>]    | conversion none }
106        { Json[Value]                => LargeUtf8[String]       | conversion option }
107        { Bit[Vec<u8>]               => LargeBinary[Vec<u8>]    | conversion none }
108    }
109);
110
111impl<P> TypeConversion<NaiveTime, NaiveTimeWrapperMicro> for MySQLArrowTransport<P> {
112    fn convert(val: NaiveTime) -> NaiveTimeWrapperMicro {
113        NaiveTimeWrapperMicro(val)
114    }
115}
116
117impl<P> TypeConversion<NaiveDateTime, NaiveDateTimeWrapperMicro> for MySQLArrowTransport<P> {
118    fn convert(val: NaiveDateTime) -> NaiveDateTimeWrapperMicro {
119        NaiveDateTimeWrapperMicro(val)
120    }
121}
122
123impl<P> TypeConversion<Decimal, f64> for MySQLArrowTransport<P> {
124    fn convert(val: Decimal) -> f64 {
125        val.to_f64()
126            .unwrap_or_else(|| panic!("cannot convert decimal {:?} to float64", val))
127    }
128}
129
130impl<P> TypeConversion<Value, String> for MySQLArrowTransport<P> {
131    fn convert(val: Value) -> String {
132        to_string(&val).unwrap()
133    }
134}