Skip to main content

connectorx/transports/
mssql_arrow.rs

1//! Transport from MsSQL Source to Arrow Destination.
2
3use crate::destinations::arrow::{
4    typesystem::{DateTimeWrapperMicro, NaiveDateTimeWrapperMicro, NaiveTimeWrapperMicro},
5    ArrowDestination, ArrowDestinationError, ArrowTypeSystem,
6};
7use crate::sources::mssql::{FloatN, IntN, MsSQLSource, MsSQLSourceError, MsSQLTypeSystem};
8use crate::typesystem::TypeConversion;
9use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
10use num_traits::ToPrimitive;
11use rust_decimal::Decimal;
12use thiserror::Error;
13use uuid_old::Uuid;
14
15/// Convert MsSQL data types to Arrow data types.
16pub struct MsSQLArrowTransport;
17
18/// Errors raised while moving data from the SQL Server source to the Arrow destination.
19#[derive(Error, Debug)]
20pub enum MsSQLArrowTransportError {
21    /// Error from the SQL Server source.
22    #[error(transparent)]
23    Source(#[from] MsSQLSourceError),
24
25    /// Error from the Arrow destination.
26    #[error(transparent)]
27    Destination(#[from] ArrowDestinationError),
28
29    /// Generic ConnectorX error.
30    #[error(transparent)]
31    ConnectorX(#[from] crate::errors::ConnectorXError),
32}
33
34impl_transport!(
35    name = MsSQLArrowTransport,
36    error = MsSQLArrowTransportError,
37    systems = MsSQLTypeSystem => ArrowTypeSystem,
38    route = MsSQLSource => ArrowDestination,
39    mappings = {
40        { Tinyint[u8]                   => Int64[i64]                | conversion auto }
41        { Smallint[i16]                 => Int64[i64]                | conversion auto }
42        { Int[i32]                      => Int64[i64]                | conversion auto }
43        { Bigint[i64]                   => Int64[i64]                | conversion auto }
44        { Intn[IntN]                    => Int64[i64]                | conversion option }
45        { Float24[f32]                  => Float32[f32]              | conversion auto }
46        { Float53[f64]                  => Float64[f64]              | conversion auto }
47        { Floatn[FloatN]                => Float64[f64]              | conversion option }
48        { Bit[bool]                     => Boolean[bool]             | conversion auto  }
49        { Nvarchar[&'r str]             => LargeUtf8[String]         | conversion owned }
50        { Varchar[&'r str]              => LargeUtf8[String]         | conversion none }
51        { Nchar[&'r str]                => LargeUtf8[String]         | conversion none }
52        { Char[&'r str]                 => LargeUtf8[String]         | conversion none }
53        { Text[&'r str]                 => LargeUtf8[String]         | conversion none }
54        { Ntext[&'r str]                => LargeUtf8[String]         | conversion none }
55        { Binary[&'r [u8]]              => LargeBinary[Vec<u8>]      | conversion owned }
56        { Varbinary[&'r [u8]]           => LargeBinary[Vec<u8>]      | conversion none }
57        { Image[&'r [u8]]               => LargeBinary[Vec<u8>]      | conversion none }
58        { Numeric[Decimal]              => Decimal[Decimal]          | conversion auto }
59        { Decimal[Decimal]              => Decimal[Decimal]          | conversion none }
60        { Datetime[NaiveDateTime]       => Date64Micro[NaiveDateTimeWrapperMicro]     | conversion option }
61        { Datetime2[NaiveDateTime]      => Date64Micro[NaiveDateTimeWrapperMicro]     | conversion none }
62        { Smalldatetime[NaiveDateTime]  => Date64Micro[NaiveDateTimeWrapperMicro]     | conversion none }
63        { Date[NaiveDate]               => Date32[NaiveDate]         | conversion auto }
64        { Datetimeoffset[DateTime<Utc>] => DateTimeTzMicro[DateTimeWrapperMicro] | conversion option }
65        { Uniqueidentifier[Uuid]        => LargeUtf8[String]         | conversion option }
66        { Time[NaiveTime]               => Time64Micro[NaiveTimeWrapperMicro]         | conversion option }
67        { SmallMoney[f32]               => Float32[f32]              | conversion none }
68        { Money[f64]                    => Float64[f64]              | conversion none }
69    }
70);
71
72impl TypeConversion<NaiveTime, NaiveTimeWrapperMicro> for MsSQLArrowTransport {
73    fn convert(val: NaiveTime) -> NaiveTimeWrapperMicro {
74        NaiveTimeWrapperMicro(val)
75    }
76}
77
78impl TypeConversion<NaiveDateTime, NaiveDateTimeWrapperMicro> for MsSQLArrowTransport {
79    fn convert(val: NaiveDateTime) -> NaiveDateTimeWrapperMicro {
80        NaiveDateTimeWrapperMicro(val)
81    }
82}
83
84impl TypeConversion<DateTime<Utc>, DateTimeWrapperMicro> for MsSQLArrowTransport {
85    fn convert(val: DateTime<Utc>) -> DateTimeWrapperMicro {
86        DateTimeWrapperMicro(val)
87    }
88}
89
90impl TypeConversion<Uuid, String> for MsSQLArrowTransport {
91    fn convert(val: Uuid) -> String {
92        val.to_string()
93    }
94}
95
96impl TypeConversion<IntN, i64> for MsSQLArrowTransport {
97    fn convert(val: IntN) -> i64 {
98        val.0
99    }
100}
101
102impl TypeConversion<FloatN, f64> for MsSQLArrowTransport {
103    fn convert(val: FloatN) -> f64 {
104        val.0
105    }
106}
107
108impl TypeConversion<Decimal, f64> for MsSQLArrowTransport {
109    fn convert(val: Decimal) -> f64 {
110        val.to_f64()
111            .unwrap_or_else(|| panic!("cannot convert decimal {:?} to float64", val))
112    }
113}