micromegas_datafusion_extensions/
binary_column_accessor.rs1use anyhow::{Result, anyhow};
2use datafusion::arrow::array::{
3 Array, ArrayRef, BinaryArray, BinaryViewArray, DictionaryArray, RecordBatch,
4};
5use datafusion::arrow::datatypes::{DataType, Int32Type};
6use std::sync::Arc;
7
8pub trait BinaryColumnAccessor: Send {
9 fn value(&self, index: usize) -> &[u8];
10
11 fn len(&self) -> usize;
12
13 fn is_null(&self, index: usize) -> bool;
14
15 fn is_empty(&self) -> bool {
16 self.len() == 0
17 }
18}
19
20struct BinaryArrayAccessor {
21 array: Arc<BinaryArray>,
22}
23
24impl BinaryArrayAccessor {
25 fn new(array: Arc<BinaryArray>) -> Self {
26 Self { array }
27 }
28}
29
30impl BinaryColumnAccessor for BinaryArrayAccessor {
31 fn value(&self, index: usize) -> &[u8] {
32 self.array.value(index)
33 }
34
35 fn len(&self) -> usize {
36 self.array.len()
37 }
38
39 fn is_null(&self, index: usize) -> bool {
40 self.array.is_null(index)
41 }
42}
43
44struct BinaryViewAccessor {
45 array: Arc<BinaryViewArray>,
46}
47
48impl BinaryColumnAccessor for BinaryViewAccessor {
49 fn value(&self, index: usize) -> &[u8] {
50 self.array.value(index)
51 }
52
53 fn len(&self) -> usize {
54 self.array.len()
55 }
56
57 fn is_null(&self, index: usize) -> bool {
58 self.array.is_null(index)
59 }
60}
61
62struct DictionaryBinaryAccessor {
63 array: Arc<DictionaryArray<Int32Type>>,
64 values: Arc<BinaryArray>,
65}
66
67impl DictionaryBinaryAccessor {
68 fn new(array: Arc<DictionaryArray<Int32Type>>) -> Result<Self> {
69 let values = array
70 .values()
71 .as_any()
72 .downcast_ref::<BinaryArray>()
73 .ok_or_else(|| anyhow!("Dictionary values are not BinaryArray"))?
74 .clone();
75
76 Ok(Self {
77 array,
78 values: Arc::new(values),
79 })
80 }
81}
82
83impl BinaryColumnAccessor for DictionaryBinaryAccessor {
84 fn value(&self, index: usize) -> &[u8] {
85 let key = self.array.keys().value(index);
86 self.values.value(key as usize)
87 }
88
89 fn len(&self) -> usize {
90 self.array.len()
91 }
92
93 fn is_null(&self, index: usize) -> bool {
94 self.array.is_null(index)
95 }
96}
97
98pub fn create_binary_accessor(array: &ArrayRef) -> Result<Box<dyn BinaryColumnAccessor + Send>> {
99 match array.data_type() {
100 DataType::Binary => {
101 let binary_array = array
102 .as_any()
103 .downcast_ref::<BinaryArray>()
104 .ok_or_else(|| anyhow!("Failed to downcast to BinaryArray"))?
105 .clone();
106 Ok(Box::new(BinaryArrayAccessor::new(Arc::new(binary_array))))
107 }
108 DataType::BinaryView => {
109 let array = array
110 .as_any()
111 .downcast_ref::<BinaryViewArray>()
112 .ok_or_else(|| anyhow!("Failed to downcast to BinaryViewArray"))?
113 .clone();
114 Ok(Box::new(BinaryViewAccessor {
115 array: Arc::new(array),
116 }))
117 }
118 DataType::Dictionary(key_type, value_type) => {
119 if !matches!(value_type.as_ref(), DataType::Binary) {
120 return Err(anyhow!("Dictionary values must be Binary"));
121 }
122
123 match key_type.as_ref() {
124 DataType::Int32 => {
125 let dict_array = array
126 .as_any()
127 .downcast_ref::<DictionaryArray<Int32Type>>()
128 .ok_or_else(|| anyhow!("Failed to downcast to DictionaryArray<Int32>"))?
129 .clone();
130 Ok(Box::new(DictionaryBinaryAccessor::new(Arc::new(
131 dict_array,
132 ))?))
133 }
134 _ => Err(anyhow!("Unsupported dictionary key type: {:?}", key_type)),
135 }
136 }
137 _ => Err(anyhow!(
138 "Unsupported array type for binary accessor: {:?}",
139 array.data_type()
140 )),
141 }
142}
143
144pub fn binary_column_by_name(
145 batch: &RecordBatch,
146 name: &str,
147) -> Result<Box<dyn BinaryColumnAccessor + Send>> {
148 let column = batch
149 .column_by_name(name)
150 .ok_or_else(|| anyhow!("Column '{}' not found", name))?;
151 create_binary_accessor(column)
152}