Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/fmt.rs
6939 views
1
use std::fmt::{Result, Write};
2
3
use super::Array;
4
use crate::bitmap::Bitmap;
5
use crate::{match_integer_type, with_match_primitive_type_full};
6
7
/// Returns a function that writes the value of the element of `array`
8
/// at position `index` to a [`Write`],
9
/// writing `null` in the null slots.
10
pub fn get_value_display<'a, F: Write + 'a>(
11
array: &'a dyn Array,
12
null: &'static str,
13
) -> Box<dyn Fn(&mut F, usize) -> Result + 'a> {
14
use crate::datatypes::PhysicalType::*;
15
match array.dtype().to_physical_type() {
16
Null => Box::new(move |f, _| write!(f, "{null}")),
17
Boolean => Box::new(|f, index| {
18
super::boolean::fmt::write_value(array.as_any().downcast_ref().unwrap(), index, f)
19
}),
20
Primitive(primitive) => with_match_primitive_type_full!(primitive, |$T| {
21
let writer = super::primitive::fmt::get_write_value::<$T, _>(
22
array.as_any().downcast_ref().unwrap(),
23
);
24
Box::new(move |f, index| writer(f, index))
25
}),
26
Binary => Box::new(|f, index| {
27
super::binary::fmt::write_value::<i32, _>(
28
array.as_any().downcast_ref().unwrap(),
29
index,
30
f,
31
)
32
}),
33
FixedSizeBinary => Box::new(|f, index| {
34
super::fixed_size_binary::fmt::write_value(
35
array.as_any().downcast_ref().unwrap(),
36
index,
37
f,
38
)
39
}),
40
LargeBinary => Box::new(|f, index| {
41
super::binary::fmt::write_value::<i64, _>(
42
array.as_any().downcast_ref().unwrap(),
43
index,
44
f,
45
)
46
}),
47
Utf8 => Box::new(|f, index| {
48
super::utf8::fmt::write_value::<i32, _>(
49
array.as_any().downcast_ref().unwrap(),
50
index,
51
f,
52
)
53
}),
54
LargeUtf8 => Box::new(|f, index| {
55
super::utf8::fmt::write_value::<i64, _>(
56
array.as_any().downcast_ref().unwrap(),
57
index,
58
f,
59
)
60
}),
61
List => Box::new(move |f, index| {
62
super::list::fmt::write_value::<i32, _>(
63
array.as_any().downcast_ref().unwrap(),
64
index,
65
null,
66
f,
67
)
68
}),
69
FixedSizeList => Box::new(move |f, index| {
70
super::fixed_size_list::fmt::write_value(
71
array.as_any().downcast_ref().unwrap(),
72
index,
73
null,
74
f,
75
)
76
}),
77
LargeList => Box::new(move |f, index| {
78
super::list::fmt::write_value::<i64, _>(
79
array.as_any().downcast_ref().unwrap(),
80
index,
81
null,
82
f,
83
)
84
}),
85
Struct => Box::new(move |f, index| {
86
super::struct_::fmt::write_value(array.as_any().downcast_ref().unwrap(), index, null, f)
87
}),
88
Union => Box::new(move |f, index| {
89
super::union::fmt::write_value(array.as_any().downcast_ref().unwrap(), index, null, f)
90
}),
91
Map => Box::new(move |f, index| {
92
super::map::fmt::write_value(array.as_any().downcast_ref().unwrap(), index, null, f)
93
}),
94
BinaryView => Box::new(move |f, index| {
95
super::binview::fmt::write_value::<[u8], _>(
96
array.as_any().downcast_ref().unwrap(),
97
index,
98
f,
99
)
100
}),
101
Utf8View => Box::new(move |f, index| {
102
super::binview::fmt::write_value::<str, _>(
103
array.as_any().downcast_ref().unwrap(),
104
index,
105
f,
106
)
107
}),
108
Dictionary(key_type) => match_integer_type!(key_type, |$T| {
109
Box::new(move |f, index| {
110
super::dictionary::fmt::write_value::<$T,_>(array.as_any().downcast_ref().unwrap(), index, null, f)
111
})
112
}),
113
}
114
}
115
116
/// Returns a function that writes the element of `array`
117
/// at position `index` to a [`Write`], writing `null` to the null slots.
118
pub fn get_display<'a, F: Write + 'a>(
119
array: &'a dyn Array,
120
null: &'static str,
121
) -> Box<dyn Fn(&mut F, usize) -> Result + 'a> {
122
let value_display = get_value_display(array, null);
123
Box::new(move |f, row| {
124
if array.is_null(row) {
125
f.write_str(null)
126
} else {
127
value_display(f, row)
128
}
129
})
130
}
131
132
pub fn write_vec<D, F>(
133
f: &mut F,
134
d: D,
135
validity: Option<&Bitmap>,
136
len: usize,
137
null: &'static str,
138
new_lines: bool,
139
) -> Result
140
where
141
D: Fn(&mut F, usize) -> Result,
142
F: Write,
143
{
144
f.write_char('[')?;
145
write_list(f, d, validity, len, null, new_lines)?;
146
f.write_char(']')?;
147
Ok(())
148
}
149
150
fn write_list<D, F>(
151
f: &mut F,
152
d: D,
153
validity: Option<&Bitmap>,
154
len: usize,
155
null: &'static str,
156
new_lines: bool,
157
) -> Result
158
where
159
D: Fn(&mut F, usize) -> Result,
160
F: Write,
161
{
162
for index in 0..len {
163
if index != 0 {
164
f.write_char(',')?;
165
f.write_char(if new_lines { '\n' } else { ' ' })?;
166
}
167
if let Some(val) = validity {
168
if val.get_bit(index) {
169
d(f, index)
170
} else {
171
write!(f, "{null}")
172
}
173
} else {
174
d(f, index)
175
}?;
176
}
177
Ok(())
178
}
179
180
pub fn write_map<D, F>(
181
f: &mut F,
182
d: D,
183
validity: Option<&Bitmap>,
184
len: usize,
185
null: &'static str,
186
new_lines: bool,
187
) -> Result
188
where
189
D: Fn(&mut F, usize) -> Result,
190
F: Write,
191
{
192
f.write_char('{')?;
193
write_list(f, d, validity, len, null, new_lines)?;
194
f.write_char('}')?;
195
Ok(())
196
}
197
198