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
use std::{collections::HashMap, sync::Arc};
use ballista_core::error::{ballista_error, Result};
use datafusion::arrow::{
array::ArrayRef,
datatypes::{DataType, Schema},
record_batch::RecordBatch,
};
use datafusion::scalar::ScalarValue;
pub type MaybeColumnarBatch = Result<Option<ColumnarBatch>>;
#[derive(Debug, Clone)]
pub struct ColumnarBatch {
schema: Arc<Schema>,
columns: HashMap<String, ColumnarValue>,
}
impl ColumnarBatch {
pub fn from_arrow(batch: &RecordBatch) -> Self {
let columns = batch
.columns()
.iter()
.enumerate()
.map(|(i, array)| {
(
batch.schema().field(i).name().clone(),
ColumnarValue::Columnar(array.clone()),
)
})
.collect();
Self {
schema: batch.schema(),
columns,
}
}
pub fn from_values(values: &[ColumnarValue], schema: &Schema) -> Self {
let columns = schema
.fields()
.iter()
.enumerate()
.map(|(i, f)| (f.name().clone(), values[i].clone()))
.collect();
Self {
schema: Arc::new(schema.clone()),
columns,
}
}
pub fn to_arrow(&self) -> Result<RecordBatch> {
let arrays = self
.schema
.fields()
.iter()
.map(|c| {
match self.column(c.name())? {
ColumnarValue::Columnar(array) => Ok(array.clone()),
ColumnarValue::Scalar(_, _) => {
Err(ballista_error("Cannot convert scalar value to Arrow array"))
}
}
})
.collect::<Result<Vec<_>>>()?;
Ok(RecordBatch::try_new(self.schema.clone(), arrays)?)
}
pub fn schema(&self) -> Arc<Schema> {
self.schema.clone()
}
pub fn num_columns(&self) -> usize {
self.columns.len()
}
pub fn num_rows(&self) -> usize {
self.columns[self.schema.field(0).name()].len()
}
pub fn column(&self, name: &str) -> Result<&ColumnarValue> {
Ok(&self.columns[name])
}
pub fn memory_size(&self) -> usize {
self.columns.values().map(|c| c.memory_size()).sum()
}
}
#[derive(Debug, Clone)]
pub enum ColumnarValue {
Scalar(ScalarValue, usize),
Columnar(ArrayRef),
}
impl ColumnarValue {
pub fn len(&self) -> usize {
match self {
ColumnarValue::Scalar(_, n) => *n,
ColumnarValue::Columnar(array) => array.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn data_type(&self) -> &DataType {
match self {
ColumnarValue::Columnar(array) => array.data_type(),
ColumnarValue::Scalar(value, _) => match value {
ScalarValue::UInt8(_) => &DataType::UInt8,
ScalarValue::UInt16(_) => &DataType::UInt16,
ScalarValue::UInt32(_) => &DataType::UInt32,
ScalarValue::UInt64(_) => &DataType::UInt64,
ScalarValue::Int8(_) => &DataType::Int8,
ScalarValue::Int16(_) => &DataType::Int16,
ScalarValue::Int32(_) => &DataType::Int32,
ScalarValue::Int64(_) => &DataType::Int64,
ScalarValue::Float32(_) => &DataType::Float32,
ScalarValue::Float64(_) => &DataType::Float64,
_ => unimplemented!(),
},
}
}
pub fn to_arrow(&self) -> ArrayRef {
match self {
ColumnarValue::Columnar(array) => array.clone(),
ColumnarValue::Scalar(value, n) => value.to_array_of_size(*n),
}
}
pub fn memory_size(&self) -> usize {
match self {
ColumnarValue::Columnar(array) => array.get_array_memory_size(),
_ => 0,
}
}
}