Skip to main content

connectorx/transports/
oracle_arrow.rs

1use crate::{
2    destinations::arrow::{
3        typesystem::{ArrowTypeSystem, DateTimeWrapperMicro, NaiveDateTimeWrapperMicro},
4        ArrowDestination, ArrowDestinationError,
5    },
6    sources::oracle::{OracleSource, OracleSourceError, OracleTypeSystem},
7    typesystem::TypeConversion,
8};
9use chrono::{DateTime, NaiveDateTime, Utc};
10use rust_decimal::Decimal;
11use thiserror::Error;
12
13/// Errors raised while moving data from the Oracle source to the Arrow destination.
14#[derive(Error, Debug)]
15pub enum OracleArrowTransportError {
16    /// Error from the Oracle source.
17    #[error(transparent)]
18    Source(#[from] OracleSourceError),
19
20    /// Error from the Arrow destination.
21    #[error(transparent)]
22    Destination(#[from] ArrowDestinationError),
23
24    /// Generic ConnectorX error.
25    #[error(transparent)]
26    ConnectorX(#[from] crate::errors::ConnectorXError),
27}
28
29/// Transport from the Oracle source to the Arrow destination.
30pub struct OracleArrowTransport;
31
32impl_transport!(
33    name = OracleArrowTransport,
34    error = OracleArrowTransportError,
35    systems = OracleTypeSystem => ArrowTypeSystem,
36    route = OracleSource => ArrowDestination,
37    mappings = {
38        { NumFloat[f64]                     => Float64[f64]                             | conversion auto }
39        { NumDecimal[Decimal]               => Decimal[Decimal]                         | conversion auto }
40        { Float[f64]                        => Float64[f64]                             | conversion none }
41        { BinaryFloat[f64]                  => Float64[f64]                             | conversion none }
42        { BinaryDouble[f64]                 => Float64[f64]                             | conversion none }
43        { NumInt[i64]                       => Int64[i64]                               | conversion auto }
44        { Blob[Vec<u8>]                     => LargeBinary[Vec<u8>]                     | conversion auto }
45        { Clob[String]                      => LargeUtf8[String]                        | conversion none }
46        { VarChar[String]                   => LargeUtf8[String]                        | conversion auto }
47        { Char[String]                      => LargeUtf8[String]                        | conversion none }
48        { NVarChar[String]                  => LargeUtf8[String]                        | conversion none }
49        { NChar[String]                     => LargeUtf8[String]                        | conversion none }
50        { Date[NaiveDateTime]               => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion option }
51        { Timestamp[NaiveDateTime]          => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion none }
52        { TimestampNano[NaiveDateTime]      => Date64[NaiveDateTime]                    | conversion auto }
53        { TimestampTz[DateTime<Utc>]        => DateTimeTzMicro[DateTimeWrapperMicro]    | conversion option }
54        { TimestampTzNano[DateTime<Utc>]    => DateTimeTz[DateTime<Utc>]                | conversion auto }
55        { Boolean[bool]                     => Boolean[bool]                            | conversion auto }
56    }
57);
58
59impl TypeConversion<NaiveDateTime, NaiveDateTimeWrapperMicro> for OracleArrowTransport {
60    fn convert(val: NaiveDateTime) -> NaiveDateTimeWrapperMicro {
61        NaiveDateTimeWrapperMicro(val)
62    }
63}
64
65impl TypeConversion<DateTime<Utc>, DateTimeWrapperMicro> for OracleArrowTransport {
66    fn convert(val: DateTime<Utc>) -> DateTimeWrapperMicro {
67        DateTimeWrapperMicro(val)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use crate::typesystem::{Transport, TypeSystem};
75
76    /// The destination type of each Oracle date and time type must hold the value the conversion produces: a
77    /// mismatch is only caught at run time, on the first row ("Data type unexpected"). `TIMESTAMP WITH TIME ZONE`
78    /// produced a `DateTimeWrapperMicro` into a `DateTimeTz` column, which holds a `DateTime<Utc>`.
79    /// Checks that `ts` holds values of type `T` (`Option<T>` when nullable).
80    fn holds<T>(ts: ArrowTypeSystem, nullable: bool) -> bool
81    where
82        T: crate::typesystem::TypeAssoc<ArrowTypeSystem>,
83        Option<T>: crate::typesystem::TypeAssoc<ArrowTypeSystem>,
84    {
85        if nullable {
86            ts.check::<Option<T>>().is_ok()
87        } else {
88            ts.check::<T>().is_ok()
89        }
90    }
91
92    #[test]
93    fn date_and_time_types_hold_the_converted_values() {
94        let dst = |ts| OracleArrowTransport::convert_typesystem(ts).unwrap();
95        for nullable in [true, false] {
96            let tz = dst(OracleTypeSystem::TimestampTz(nullable));
97            assert!(
98                holds::<DateTimeWrapperMicro>(tz, nullable),
99                "TIMESTAMP WITH TIME ZONE -> {tz:?}"
100            );
101
102            let tz_nano = dst(OracleTypeSystem::TimestampTzNano(nullable));
103            assert!(
104                holds::<DateTime<Utc>>(tz_nano, nullable),
105                "TIMESTAMP(7..9) WITH TIME ZONE -> {tz_nano:?}"
106            );
107
108            for naive in [
109                OracleTypeSystem::Date(nullable),
110                OracleTypeSystem::Timestamp(nullable),
111            ] {
112                let ts = dst(naive);
113                assert!(
114                    holds::<NaiveDateTimeWrapperMicro>(ts, nullable),
115                    "{naive:?} -> {ts:?}"
116                );
117            }
118            let nano = dst(OracleTypeSystem::TimestampNano(nullable));
119            assert!(
120                holds::<NaiveDateTime>(nano, nullable),
121                "TIMESTAMP(7..9) -> {nano:?}"
122            );
123        }
124    }
125
126    #[test]
127    fn timestamp_with_time_zone_converts_at_microsecond_precision() {
128        let utc = DateTime::from_timestamp_micros(944_074_800_123_456).unwrap();
129        let converted: Option<DateTimeWrapperMicro> = <OracleArrowTransport as TypeConversion<
130            Option<DateTime<Utc>>,
131            Option<DateTimeWrapperMicro>,
132        >>::convert(Some(utc));
133        assert_eq!(converted.map(|w| w.0), Some(utc));
134    }
135}