Skip to main content

connectorx/transports/
dummy_arrowstream.rs

1//! Transport from Dummy Source to Arrow Destination.
2
3use crate::destinations::arrowstream::{
4    typesystem::{ArrowTypeSystem, NaiveDateTimeWrapperMicro},
5    ArrowDestination, ArrowDestinationError,
6};
7use crate::sources::dummy::{DummySource, DummyTypeSystem};
8use crate::typesystem::TypeConversion;
9use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};
10use thiserror::Error;
11
12/// Convert Dummy data types to Arrow data types.
13pub struct DummyArrowTransport;
14
15/// Errors raised while moving data from the dummy source to the streaming Arrow destination.
16#[derive(Error, Debug)]
17pub enum DummyArrowTransportError {
18    /// Error from the streaming Arrow destination.
19    #[error(transparent)]
20    Destination(#[from] ArrowDestinationError),
21
22    /// Generic ConnectorX error.
23    #[error(transparent)]
24    ConnectorX(#[from] crate::errors::ConnectorXError),
25}
26
27impl_transport!(
28    name = DummyArrowTransport,
29    error = DummyArrowTransportError,
30    systems = DummyTypeSystem => ArrowTypeSystem,
31    route = DummySource => ArrowDestination,
32    mappings = {
33        { F64[f64]                => Float64[f64]               | conversion auto}
34        { I64[i64]                => Int64[i64]                 | conversion auto}
35        { Bool[bool]              => Boolean[bool]              | conversion auto}
36        { String[String]          => LargeUtf8[String]          | conversion auto}
37        { DateTime[DateTime<Utc>] => Date64Micro[NaiveDateTimeWrapperMicro]      | conversion option}
38    }
39);
40
41impl TypeConversion<DateTime<Utc>, NaiveDateTimeWrapperMicro> for DummyArrowTransport {
42    fn convert(val: DateTime<Utc>) -> NaiveDateTimeWrapperMicro {
43        NaiveDateTimeWrapperMicro(val.naive_utc())
44    }
45}
46
47impl TypeConversion<NaiveDateTime, DateTime<Utc>> for DummyArrowTransport {
48    fn convert(val: NaiveDateTime) -> DateTime<Utc> {
49        DateTime::from_naive_utc_and_offset(val, Utc)
50    }
51}
52
53impl TypeConversion<NaiveDate, DateTime<Utc>> for DummyArrowTransport {
54    fn convert(val: NaiveDate) -> DateTime<Utc> {
55        DateTime::from_naive_utc_and_offset(
56            val.and_hms_opt(0, 0, 0)
57                .unwrap_or_else(|| panic!("and_hms_opt return None")),
58            Utc,
59        )
60    }
61}
62
63// The arrow and arrowstream versions of this module share one API, so they
64// share their tests: both include the same file.
65#[cfg(test)]
66#[allow(clippy::duplicate_mod)]
67#[path = "dummy_tests.rs"]
68mod tests;