connectorx/transports/
dummy_arrow.rs1use crate::destinations::arrow::{
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
12pub struct DummyArrowTransport;
14
15#[derive(Error, Debug)]
16pub enum DummyArrowTransportError {
17 #[error(transparent)]
18 Destination(#[from] ArrowDestinationError),
19
20 #[error(transparent)]
21 ConnectorX(#[from] crate::errors::ConnectorXError),
22}
23
24impl_transport!(
25 name = DummyArrowTransport,
26 error = DummyArrowTransportError,
27 systems = DummyTypeSystem => ArrowTypeSystem,
28 route = DummySource => ArrowDestination,
29 mappings = {
30 { F64[f64] => Float64[f64] | conversion auto}
31 { I64[i64] => Int64[i64] | conversion auto}
32 { Bool[bool] => Boolean[bool] | conversion auto}
33 { String[String] => LargeUtf8[String] | conversion auto}
34 { DateTime[DateTime<Utc>] => Date64Micro[NaiveDateTimeWrapperMicro] | conversion option}
35 }
36);
37
38impl TypeConversion<DateTime<Utc>, NaiveDateTimeWrapperMicro> for DummyArrowTransport {
39 fn convert(val: DateTime<Utc>) -> NaiveDateTimeWrapperMicro {
40 NaiveDateTimeWrapperMicro(val.naive_utc())
41 }
42}
43
44impl TypeConversion<NaiveDateTime, DateTime<Utc>> for DummyArrowTransport {
45 fn convert(val: NaiveDateTime) -> DateTime<Utc> {
46 DateTime::from_naive_utc_and_offset(val, Utc)
47 }
48}
49
50impl TypeConversion<NaiveDate, DateTime<Utc>> for DummyArrowTransport {
51 fn convert(val: NaiveDate) -> DateTime<Utc> {
52 DateTime::from_naive_utc_and_offset(
53 val.and_hms_opt(0, 0, 0)
54 .unwrap_or_else(|| panic!("and_hms_opt return None")),
55 Utc,
56 )
57 }
58}
59
60#[cfg(test)]
63#[allow(clippy::duplicate_mod)]
64#[path = "dummy_tests.rs"]
65mod tests;
66
67#[cfg(all(test, feature = "dst_polars"))]
68mod polars_tests {
69 use super::*;
70 use crate::dispatcher::Dispatcher;
71 use crate::sql::CXQuery;
72
73 #[test]
74 fn arrow_destination_converts_to_polars() {
75 let schema = [
76 DummyTypeSystem::I64(false),
77 DummyTypeSystem::F64(true),
78 DummyTypeSystem::Bool(true),
79 DummyTypeSystem::String(false),
80 ];
81 let queries = [CXQuery::naked("3,4"), CXQuery::naked("2,4")];
82 let mut dst = ArrowDestination::new();
83 Dispatcher::<_, _, DummyArrowTransport>::new(
84 DummySource::new(&["i", "f", "b", "s"], &schema),
85 &mut dst,
86 &queries,
87 None,
88 )
89 .run()
90 .unwrap();
91
92 let df = dst.polars().unwrap();
93 assert_eq!(df.shape(), (5, 4));
94 let names: Vec<_> = df
95 .get_column_names()
96 .iter()
97 .map(|s| s.to_string())
98 .collect();
99 assert_eq!(names, ["i", "f", "b", "s"]);
100 let i: Vec<i64> = df
101 .column("i")
102 .unwrap()
103 .i64()
104 .unwrap()
105 .into_no_null_iter()
106 .collect();
107 let mut sorted = i.clone();
108 sorted.sort();
109 assert_eq!(sorted, vec![0, 0, 1, 1, 2]);
110 assert_eq!(df.column("b").unwrap().null_count(), 1);
112 }
113}