print_table/
print_table.rs

1//! ---------------------------------------------------------
2//! Builds a demo table and prints it.
3//!
4//! Run with:
5//!     cargo run --example print_table
6//! ---------------------------------------------------------
7
8use minarrow::aliases::{BoolArr, CatArr, FltArr, IntArr, StrArr};
9use minarrow::{Bitmask, FieldArray, MaskedArray, Print, Table};
10#[cfg(feature = "datetime")]
11use minarrow::{DatetimeArray, enums::time_units::TimeUnit};
12
13fn main() {
14    // Inner arrays
15
16    // Numeric
17    let col_i32 = IntArr::<i32>::from_slice(&[1, 2, 3, 4, 5]);
18    let col_u32 = IntArr::<u32>::from_slice(&[100, 200, 300, 400, 500]);
19    let col_i64 = IntArr::<i64>::from_slice(&[10, 20, 30, 40, 50]);
20    let col_u64 = IntArr::<u64>::from_slice(&[101, 201, 301, 401, 501]);
21    let col_f32 = FltArr::<f32>::from_slice(&[1.1, 2.2, 3.3, 4.4, 5.5]);
22    let col_f64 = FltArr::<f64>::from_slice(&[2.2, 3.3, 4.4, 5.5, 6.6]);
23
24    // Boolean with nulls
25    let mut col_bool = BoolArr::from_slice(&[true, false, true, false, true]);
26    col_bool.set_null_mask(Some(Bitmask::from_bools(&[true, true, true, false, true])));
27
28    // String and Dictionary/Categorical
29    let col_str32 = StrArr::<u32>::from_slice(&["red", "blue", "green", "yellow", "purple"]);
30    let col_cat32 = CatArr::<u32>::from_values(
31        ["apple", "banana", "cherry", "banana", "apple"].iter().copied()
32    );
33
34    // Datetime
35    #[cfg(feature = "datetime")]
36    let col_dt32 = DatetimeArray::<i32>::from_slice(
37        &[1000, 2000, 3000, 4000, 5000],
38        Some(TimeUnit::Milliseconds)
39    );
40    #[cfg(feature = "datetime")]
41    let col_dt64 = DatetimeArray::<i64>::from_slice(
42        &[1_000_000_000, 2_000_000_000, 3_000_000_000, 4_000_000_000, 5_000_000_000],
43        Some(TimeUnit::Nanoseconds)
44    );
45
46    // FieldArray (column) construction
47    let fa_i32 = FieldArray::from_inner("int32_col", col_i32);
48    let fa_u32 = FieldArray::from_inner("uint32_col", col_u32);
49    let fa_i64 = FieldArray::from_inner("int64_col", col_i64);
50    let fa_u64 = FieldArray::from_inner("uint64_col", col_u64);
51    let fa_f32 = FieldArray::from_inner("float32_col", col_f32);
52    let fa_f64 = FieldArray::from_inner("float64_col", col_f64);
53    let fa_bool = FieldArray::from_inner("bool_col", col_bool);
54    let fa_str32 = FieldArray::from_inner("utf8_col", col_str32);
55    let fa_cat32 = FieldArray::from_inner("dict32_col", col_cat32);
56    #[cfg(feature = "datetime")]
57    let fa_dt32 = FieldArray::from_inner("datetime32_col", col_dt32);
58    #[cfg(feature = "datetime")]
59    let fa_dt64 = FieldArray::from_inner("datetime64_col", col_dt64);
60
61    // Build Table
62    let mut tbl = Table::new("MyTable".to_string(), None);
63    tbl.add_col(fa_i32);
64    tbl.add_col(fa_u32);
65    tbl.add_col(fa_i64);
66    tbl.add_col(fa_u64);
67    tbl.add_col(fa_f32);
68    tbl.add_col(fa_f64);
69    tbl.add_col(fa_bool);
70    tbl.add_col(fa_str32);
71    tbl.add_col(fa_cat32);
72    #[cfg(feature = "datetime")]
73    tbl.add_col(fa_dt32);
74    #[cfg(feature = "datetime")]
75    tbl.add_col(fa_dt64);
76
77    // Print the table
78    tbl.print();
79}