print_arrays/
print_arrays.rs

1//! ---------------------------------------------------------
2//! Builds a demo table and prints it.
3//!
4//! Run with:
5//!     cargo run --example print_arrays
6//! ---------------------------------------------------------
7
8use std::sync::Arc;
9
10use minarrow::aliases::{BoolArr, CatArr, FltArr, IntArr, StrArr};
11use minarrow::enums::array::Array;
12use minarrow::{Bitmask, MaskedArray, NumericArray, Print, TextArray};
13
14#[cfg(feature = "views")]
15use minarrow::{ArrayV, BitmaskV, NumericArrayV, TextArrayV};
16
17#[cfg(feature = "datetime")]
18use minarrow::DatetimeArray;
19
20fn main() {
21    // Numeric (Integer, Float, all sizes)
22    let col_i32 = IntArr::<i32>::from_slice(&[1, 2, 3, 4, 5]);
23    let col_u32 = IntArr::<u32>::from_slice(&[100, 200, 300, 400, 500]);
24    let col_f32 = FltArr::<f32>::from_slice(&[1.1, 2.2, 3.3, 4.4, 5.5]);
25
26    // Boolean with nulls
27    let mut col_bool = BoolArr::from_slice(&[true, false, true, false, true]);
28    col_bool.set_null_mask(Some(Bitmask::from_bools(&[true, true, true, false, true])));
29
30    // String and Dictionary/Categorical
31    let col_str32 = StrArr::from_slice(&["red", "blue", "green", "yellow", "purple"]);
32
33    let col_cat32 = CatArr::<u32>::from_values(
34        ["apple", "banana", "cherry", "banana", "apple"]
35            .iter()
36            .copied(),
37    );
38
39    // --- Print NumericArray, TextArray, TemporalArray enums
40    println!("\n--- Enums: NumericArray, TextArray, TemporalArray ---");
41    NumericArray::Int32(Arc::new(col_i32.clone())).print();
42    println!("\n");
43    NumericArray::UInt32(Arc::new(col_u32.clone())).print();
44    println!("\n");
45    NumericArray::Float32(Arc::new(col_f32.clone())).print();
46    println!("\n");
47    TextArray::String32(Arc::new(col_str32.clone())).print();
48    println!("\n");
49    let _ = &TextArray::Categorical32(Arc::new(col_cat32.clone())).print();
50
51    println!("\n--- Array (top-level) ---");
52    Array::from_int32(col_i32.clone()).print();
53    println!("\n");
54    Array::from_uint32(col_u32.clone()).print();
55    println!("\n");
56    Array::from_float32(col_f32.clone()).print();
57    println!("\n");
58    Array::from_string32(col_str32.clone()).print();
59    println!("\n");
60    Array::from_categorical32(col_cat32.clone()).print();
61    println!("\n");
62    // --- Print Array Views (ArrayV, NumericArrayV, TextArrayV, TemporalArrayV)
63    #[cfg(feature = "views")]
64    println!("\n--- Array Views ---");
65    #[cfg(feature = "views")]
66    ArrayV::new(Array::from_int32(col_i32.clone()), 1, 3).print();
67
68    let num_arr = NumericArray::Int32(Arc::new(col_i32.clone()));
69    num_arr.print();
70
71    #[cfg(feature = "views")]
72    let num_view = NumericArrayV::new(num_arr, 1, 3);
73    #[cfg(feature = "views")]
74    num_view.print();
75
76    let txt_arr = TextArray::String32(Arc::new(col_str32.clone()));
77    txt_arr.print();
78
79    #[cfg(feature = "views")]
80    let txt_view = TextArrayV::new(txt_arr, 1, 3);
81    #[cfg(feature = "views")]
82    txt_view.print();
83
84    // --- Print Bitmask and BitmaskV
85    println!("\n--- Bitmask & BitmaskV ---");
86    let bm = Bitmask::from_bools(&[true, false, true, true, false]);
87    bm.print();
88    #[cfg(feature = "views")]
89    BitmaskV::new(bm.clone(), 1, 3).print();
90
91    // Datetime - various time units
92    #[cfg(feature = "datetime")]
93    {
94        use minarrow::enums::time_units::TimeUnit;
95
96        println!("\n--- Datetime Arrays (various time units) ---");
97
98        // Seconds since Unix epoch (1970-01-01 00:00:00 UTC)
99        let dt_seconds = DatetimeArray::<i64>::from_slice(
100            &[
101                1_700_000_000, // 2023-11-14 22:13:20 UTC
102                1_700_086_400, // 2023-11-15 22:13:20 UTC
103                1_700_172_800, // 2023-11-16 22:13:20 UTC
104            ],
105            Some(TimeUnit::Seconds),
106        );
107        println!("Seconds:");
108        dt_seconds.print();
109        println!();
110
111        // Milliseconds
112        let dt_millis = DatetimeArray::<i64>::from_slice(
113            &[
114                1_700_000_000_000, // 2023-11-14 22:13:20.000 UTC
115                1_700_086_400_000, // 2023-11-15 22:13:20.000 UTC
116                1_700_172_800_000, // 2023-11-16 22:13:20.000 UTC
117            ],
118            Some(TimeUnit::Milliseconds),
119        );
120        println!("Milliseconds:");
121        dt_millis.print();
122        println!();
123
124        // Microseconds
125        let dt_micros = DatetimeArray::<i64>::from_slice(
126            &[
127                1_700_000_000_000_000, // 2023-11-14 22:13:20.000000 UTC
128                1_700_086_400_000_000, // 2023-11-15 22:13:20.000000 UTC
129                1_700_172_800_000_000, // 2023-11-16 22:13:20.000000 UTC
130            ],
131            Some(TimeUnit::Microseconds),
132        );
133        println!("Microseconds:");
134        dt_micros.print();
135        println!();
136
137        // Nanoseconds
138        let dt_nanos = DatetimeArray::<i64>::from_slice(
139            &[
140                1_700_000_000_000_000_000, // 2023-11-14 22:13:20.000000000 UTC
141                1_700_086_400_000_000_000, // 2023-11-15 22:13:20.000000000 UTC
142                1_700_172_800_000_000_000, // 2023-11-16 22:13:20.000000000 UTC
143            ],
144            Some(TimeUnit::Nanoseconds),
145        );
146        println!("Nanoseconds:");
147        dt_nanos.print();
148        println!();
149
150        // Days since Unix epoch
151        let dt_days = DatetimeArray::<i32>::from_slice(
152            &[
153                19_670, // 2023-11-14
154                19_671, // 2023-11-15
155                19_672, // 2023-11-16
156            ],
157            Some(TimeUnit::Days),
158        );
159        println!("Days:");
160        dt_days.print();
161        println!();
162
163        // With timezone operations (requires datetime_ops feature)
164        #[cfg(feature = "datetime_ops")]
165        {
166            println!(
167                "--- Datetime with Timezone Conversions (requires 'datetime_ops' feature) ---"
168            );
169
170            // UTC datetime
171            let utc_dt =
172                DatetimeArray::<i64>::from_slice(&[1_700_000_000], Some(TimeUnit::Seconds));
173
174            // Test IANA timezone identifiers
175            println!("IANA Timezone Identifiers:");
176            println!("America/New_York:");
177            utc_dt.tz("America/New_York").print();
178            println!();
179
180            println!("Australia/Sydney:");
181            utc_dt.tz("Australia/Sydney").print();
182            println!();
183
184            println!("Europe/London:");
185            utc_dt.tz("Europe/London").print();
186            println!();
187
188            println!("Asia/Tokyo:");
189            utc_dt.tz("Asia/Tokyo").print();
190            println!();
191
192            // Test timezone abbreviations
193            println!("\nTimezone Abbreviations:");
194            println!("EST:");
195            utc_dt.tz("EST").print();
196            println!();
197
198            println!("AEST:");
199            utc_dt.tz("AEST").print();
200            println!();
201
202            println!("JST:");
203            utc_dt.tz("JST").print();
204            println!();
205
206            // Test direct offset strings
207            println!("\nDirect Offset Strings:");
208            println!("UTC:");
209            utc_dt.tz("UTC").print();
210            println!();
211
212            println!("+05:30 (India):");
213            utc_dt.tz("+05:30").print();
214            println!();
215
216            println!("-03:30 (Newfoundland):");
217            utc_dt.tz("-03:30").print();
218            println!();
219        }
220
221        #[cfg(not(feature = "datetime_ops"))]
222        {
223            println!(
224                "Note: Enable 'datetime_ops' feature for timezone conversions and datetime operations."
225            );
226            println!();
227        }
228    }
229}