1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::any::Any;
use std::sync::Arc;
use crate::core::column::{Column, ColumnTrait, ColumnType};
use crate::core::error::{Error, Result};
/// Structure representing an Int64 column
#[derive(Debug, Clone)]
pub struct Int64Column {
pub(crate) data: Arc<[i64]>,
pub(crate) null_mask: Option<Arc<[u8]>>,
pub(crate) name: Option<String>,
}
impl Int64Column {
/// Create a new Int64Column
pub fn new(data: Vec<i64>) -> Self {
Self {
data: data.into(),
null_mask: None,
name: None,
}
}
/// Create an Int64Column with a name
pub fn with_name(data: Vec<i64>, name: impl Into<String>) -> Self {
Self {
data: data.into(),
null_mask: None,
name: Some(name.into()),
}
}
/// Create an Int64Column with NULL values
pub fn with_nulls(data: Vec<i64>, nulls: Vec<bool>) -> Self {
let null_mask = if nulls.iter().any(|&is_null| is_null) {
Some(crate::column::common::utils::create_bitmask(&nulls))
} else {
None
};
Self {
data: data.into(),
null_mask,
name: None,
}
}
/// Set the name
pub fn set_name(&mut self, name: impl Into<String>) {
self.name = Some(name.into());
}
/// Get the name
pub fn get_name(&self) -> Option<&str> {
self.name.as_deref()
}
/// Get a reference to the underlying data (for testing and demonstration)
pub fn data(&self) -> &[i64] {
&self.data
}
/// Get data at the specified index
pub fn get(&self, index: usize) -> Result<Option<i64>> {
if index >= self.data.len() {
return Err(Error::IndexOutOfBounds {
index,
size: self.data.len(),
});
}
// Check for NULL value
if let Some(ref mask) = self.null_mask {
let byte_idx = index / 8;
let bit_idx = index % 8;
if byte_idx < mask.len() && (mask[byte_idx] & (1 << bit_idx)) != 0 {
return Ok(None);
}
}
Ok(Some(self.data[index]))
}
/// Calculate the sum of data (excluding NULL values)
pub fn sum(&self) -> i64 {
if self.data.is_empty() {
return 0;
}
match &self.null_mask {
None => {
// Simple sum if there are no NULLs
self.data.iter().sum()
}
Some(mask) => {
// Sum excluding NULL values
let mut sum = 0;
for i in 0..self.data.len() {
let byte_idx = i / 8;
let bit_idx = i % 8;
if byte_idx >= mask.len() || (mask[byte_idx] & (1 << bit_idx)) == 0 {
sum += self.data[i];
}
}
sum
}
}
}
/// Calculate the mean (average) of data (excluding NULL values)
pub fn mean(&self) -> Option<f64> {
if self.data.is_empty() {
return None;
}
let (sum, count) = match &self.null_mask {
None => {
// Case with no NULL values
let sum: i64 = self.data.iter().sum();
(sum, self.data.len())
}
Some(mask) => {
// Calculate excluding NULL values
let mut sum = 0;
let mut count = 0;
for i in 0..self.data.len() {
let byte_idx = i / 8;
let bit_idx = i % 8;
if byte_idx >= mask.len() || (mask[byte_idx] & (1 << bit_idx)) == 0 {
sum += self.data[i];
count += 1;
}
}
(sum, count)
}
};
if count == 0 {
None
} else {
Some(sum as f64 / count as f64)
}
}
/// Calculate the minimum value of data (excluding NULL values)
pub fn min(&self) -> Option<i64> {
if self.data.is_empty() {
return None;
}
match &self.null_mask {
None => {
// Case with no NULL values
self.data.iter().copied().min()
}
Some(mask) => {
// Calculate excluding NULL values
let mut min_val = None;
for i in 0..self.data.len() {
let byte_idx = i / 8;
let bit_idx = i % 8;
if byte_idx >= mask.len() || (mask[byte_idx] & (1 << bit_idx)) == 0 {
let val = self.data[i];
min_val = Some(min_val.map_or(val, |m: i64| m.min(val)));
}
}
min_val
}
}
}
/// Calculate the maximum value of data (excluding NULL values)
pub fn max(&self) -> Option<i64> {
if self.data.is_empty() {
return None;
}
match &self.null_mask {
None => {
// Case with no NULL values
self.data.iter().copied().max()
}
Some(mask) => {
// Calculate excluding NULL values
let mut max_val = None;
for i in 0..self.data.len() {
let byte_idx = i / 8;
let bit_idx = i % 8;
if byte_idx >= mask.len() || (mask[byte_idx] & (1 << bit_idx)) == 0 {
let val = self.data[i];
max_val = Some(max_val.map_or(val, |m: i64| m.max(val)));
}
}
max_val
}
}
}
/// Create a new column by applying a mapping function
pub fn map<F>(&self, f: F) -> Self
where
F: Fn(i64) -> i64,
{
let mapped_data: Vec<i64> = self.data.iter().map(|&x| f(x)).collect();
Self {
data: mapped_data.into(),
null_mask: self.null_mask.clone(),
name: self.name.clone(),
}
}
/// Create a new column based on filtering conditions
pub fn filter<F>(&self, predicate: F) -> Self
where
F: Fn(Option<i64>) -> bool,
{
let mut filtered_data = Vec::new();
let mut filtered_nulls = Vec::new();
let has_nulls = self.null_mask.is_some();
for i in 0..self.data.len() {
let value = self.get(i).unwrap_or(None);
if predicate(value) {
filtered_data.push(value.unwrap_or_default());
if has_nulls {
filtered_nulls.push(value.is_none());
}
}
}
if has_nulls {
Self::with_nulls(filtered_data, filtered_nulls)
} else {
Self::new(filtered_data)
}
}
}
impl ColumnTrait for Int64Column {
fn len(&self) -> usize {
self.data.len()
}
fn column_type(&self) -> ColumnType {
ColumnType::Int64
}
fn name(&self) -> Option<&str> {
self.name.as_deref()
}
fn clone_column(&self) -> crate::core::column::Column {
// Convert the legacy Column type to the core Column type
let legacy_column = Column::Int64(self.clone());
// This is a temporary workaround - in a complete solution,
// we would implement proper conversion between column types
crate::core::column::Column::from_any(Box::new(legacy_column))
}
fn as_any(&self) -> &dyn Any {
self
}
}