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"]
32            .iter()
33            .copied(),
34    );
35
36    // Datetime
37    #[cfg(feature = "datetime")]
38    let col_dt32 = DatetimeArray::<i32>::from_slice(
39        &[1000, 2000, 3000, 4000, 5000],
40        Some(TimeUnit::Milliseconds),
41    );
42    #[cfg(feature = "datetime")]
43    let col_dt64 = DatetimeArray::<i64>::from_slice(
44        &[
45            1_000_000_000,
46            2_000_000_000,
47            3_000_000_000,
48            4_000_000_000,
49            5_000_000_000,
50        ],
51        Some(TimeUnit::Nanoseconds),
52    );
53
54    // FieldArray (column) construction
55    let fa_i32 = FieldArray::from_arr("int32_col", col_i32);
56    let fa_u32 = FieldArray::from_arr("uint32_col", col_u32);
57    let fa_i64 = FieldArray::from_arr("int64_col", col_i64);
58    let fa_u64 = FieldArray::from_arr("uint64_col", col_u64);
59    let fa_f32 = FieldArray::from_arr("float32_col", col_f32);
60    let fa_f64 = FieldArray::from_arr("float64_col", col_f64);
61    let fa_bool = FieldArray::from_arr("bool_col", col_bool);
62    let fa_str32 = FieldArray::from_arr("utf8_col", col_str32);
63    let fa_cat32 = FieldArray::from_arr("dict32_col", col_cat32);
64    #[cfg(feature = "datetime")]
65    let fa_dt32 = FieldArray::from_arr("datetime32_col", col_dt32);
66    #[cfg(feature = "datetime")]
67    let fa_dt64 = FieldArray::from_arr("datetime64_col", col_dt64);
68
69    // Build Table
70    let mut tbl = Table::new("MyTable".to_string(), None);
71    tbl.add_col(fa_i32);
72    tbl.add_col(fa_u32);
73    tbl.add_col(fa_i64);
74    tbl.add_col(fa_u64);
75    tbl.add_col(fa_f32);
76    tbl.add_col(fa_f64);
77    tbl.add_col(fa_bool);
78    tbl.add_col(fa_str32);
79    tbl.add_col(fa_cat32);
80    #[cfg(feature = "datetime")]
81    tbl.add_col(fa_dt32);
82    #[cfg(feature = "datetime")]
83    tbl.add_col(fa_dt64);
84
85    // Print the table
86    tbl.print();
87}