Skip to main content

connectorx/transports/
sqlite_arrowstream.rs

1//! Transport from SQLite Source to Arrow Destination.
2
3use crate::{
4    destinations::arrowstream::{
5        typesystem::{ArrowTypeSystem, NaiveDateTimeWrapperMicro, NaiveTimeWrapperMicro},
6        ArrowDestination, ArrowDestinationError,
7    },
8    sources::sqlite::{SQLiteSource, SQLiteSourceError, SQLiteTypeSystem},
9    typesystem::TypeConversion,
10};
11use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
12use thiserror::Error;
13
14/// Errors raised while moving data from the SQLite source to the streaming Arrow destination.
15#[derive(Error, Debug)]
16pub enum SQLiteArrowTransportError {
17    /// Error from the SQLite source.
18    #[error(transparent)]
19    Source(#[from] SQLiteSourceError),
20
21    /// Error from the streaming Arrow destination.
22    #[error(transparent)]
23    Destination(#[from] ArrowDestinationError),
24
25    /// Generic ConnectorX error.
26    #[error(transparent)]
27    ConnectorX(#[from] crate::errors::ConnectorXError),
28}
29
30/// Convert SQLite data types to Arrow data types.
31pub struct SQLiteArrowTransport;
32
33impl_transport!(
34    name = SQLiteArrowTransport,
35    error = SQLiteArrowTransportError,
36    systems = SQLiteTypeSystem => ArrowTypeSystem,
37    route = SQLiteSource => ArrowDestination,
38    mappings = {
39        { Bool[bool]                 => Boolean[bool]           | conversion auto }
40        { Int8[i64]                  => Int64[i64]              | conversion auto }
41        { Int4[i32]                  => Int64[i64]              | conversion auto }
42        { Int2[i16]                  => Int64[i64]              | conversion auto }
43        { Real[f64]                  => Float64[f64]            | conversion auto }
44        { Text[Box<str>]             => LargeUtf8[String]       | conversion option }
45        { Blob[Vec<u8>]              => LargeBinary[Vec<u8>]    | conversion auto }
46        { Date[NaiveDate]            => Date32[NaiveDate]       | conversion auto }
47        { Time[NaiveTime]            => Time64Micro[NaiveTimeWrapperMicro]       | conversion option }
48        { Timestamp[NaiveDateTime]   => Date64Micro[NaiveDateTimeWrapperMicro]   | conversion option }
49    }
50);
51
52impl TypeConversion<Box<str>, String> for SQLiteArrowTransport {
53    fn convert(val: Box<str>) -> String {
54        val.to_string()
55    }
56}
57
58impl TypeConversion<NaiveTime, NaiveTimeWrapperMicro> for SQLiteArrowTransport {
59    fn convert(val: NaiveTime) -> NaiveTimeWrapperMicro {
60        NaiveTimeWrapperMicro(val)
61    }
62}
63
64impl TypeConversion<NaiveDateTime, NaiveDateTimeWrapperMicro> for SQLiteArrowTransport {
65    fn convert(val: NaiveDateTime) -> NaiveDateTimeWrapperMicro {
66        NaiveDateTimeWrapperMicro(val)
67    }
68}