Skip to main content

connectorx/transports/
sqlite_arrow.rs

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