MCPcopy Create free account
hub / github.com/apache/arrow-rs / bit_width

Function bit_width

arrow-array/src/ffi.rs:145–248  ·  view source on GitHub ↗

returns the number of bits that buffer `i` (in the C data interface) is expected to have. This is set by the Arrow specification

(data_type: &DataType, i: usize)

Source from the content-addressed store, hash-verified

143/// returns the number of bits that buffer `i` (in the C data interface) is expected to have.
144/// This is set by the Arrow specification
145fn bit_width(data_type: &DataType, i: usize) -> Result<usize> {
146 if let Some(primitive) = data_type.primitive_width() {
147 return match i {
148 0 => Err(ArrowError::CDataInterface(format!(
149 "The datatype \"{data_type}\" doesn't expect buffer at index 0. Please verify that the C data interface is correctly implemented."
150 ))),
151 1 => Ok(primitive * 8),
152 i => Err(ArrowError::CDataInterface(format!(
153 "The datatype \"{data_type}\" expects 2 buffers, but requested {i}. Please verify that the C data interface is correctly implemented."
154 ))),
155 };
156 }
157
158 Ok(match (data_type, i) {
159 (DataType::Boolean, 1) => 1,
160 (DataType::Boolean, _) => {
161 return Err(ArrowError::CDataInterface(format!(
162 "The datatype \"{data_type}\" expects 2 buffers, but requested {i}. Please verify that the C data interface is correctly implemented."
163 )));
164 }
165 (DataType::FixedSizeBinary(num_bytes), 1) => {
166 TryInto::<usize>::try_into(*num_bytes).map_err(|_| {
167 ArrowError::InvalidArgumentError(format!(
168 "cannot determine bit_width for FixedSizeBinary({num_bytes})"
169 ))
170 })? * u8::BITS as usize
171 }
172 (DataType::FixedSizeList(f, num_elems), 1) => {
173 let child_bit_width = bit_width(f.data_type(), 1)?;
174 child_bit_width * (*num_elems as usize)
175 }
176 (DataType::FixedSizeBinary(_), _) | (DataType::FixedSizeList(_, _), _) => {
177 return Err(ArrowError::CDataInterface(format!(
178 "The datatype \"{data_type}\" expects 2 buffers, but requested {i}. Please verify that the C data interface is correctly implemented."
179 )));
180 }
181 // Variable-size list and map have one i32 buffer.
182 // Variable-sized binaries: have two buffers.
183 // "small": first buffer is i32, second is in bytes
184 (DataType::Utf8, 1)
185 | (DataType::Binary, 1)
186 | (DataType::List(_), 1)
187 | (DataType::Map(_, _), 1) => i32::BITS as _,
188 (DataType::Utf8, 2) | (DataType::Binary, 2) => u8::BITS as _,
189 // List views have two i32 buffers, offsets and sizes
190 (DataType::ListView(_), 1) | (DataType::ListView(_), 2) => i32::BITS as _,
191 // Large list views have two i64 buffers, offsets and sizes
192 (DataType::LargeListView(_), 1) | (DataType::LargeListView(_), 2) => i64::BITS as _,
193 (DataType::List(_), _) | (DataType::Map(_, _), _) => {
194 return Err(ArrowError::CDataInterface(format!(
195 "The datatype \"{data_type}\" expects 2 buffers, but requested {i}. Please verify that the C data interface is correctly implemented."
196 )));
197 }
198 (DataType::Utf8, _) | (DataType::Binary, _) => {
199 return Err(ArrowError::CDataInterface(format!(
200 "The datatype \"{data_type}\" expects 3 buffers, but requested {i}. Please verify that the C data interface is correctly implemented."
201 )));
202 }

Callers 1

buffer_lenMethod · 0.85

Calls 2

primitive_widthMethod · 0.80
data_typeMethod · 0.45

Tested by

no test coverage detected