Skip to content

Commit 45f84af

Browse files
committed
Add bool8
1 parent 2aab559 commit 45f84af

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use crate::error::_internal_err;
19+
use crate::types::extension::DFExtensionType;
20+
use arrow::array::{Array, Int8Array};
21+
use arrow::datatypes::DataType;
22+
use arrow::util::display::{ArrayFormatter, DisplayIndex, FormatOptions, FormatResult};
23+
use std::fmt::Write;
24+
25+
/// Defines the extension type logic for the canonical `arrow.uuid` extension type.
26+
///
27+
/// See [`DFExtensionType`] for information on DataFusion's extension type mechanism.
28+
impl DFExtensionType for arrow_schema::extension::Bool8 {
29+
fn storage_type(&self) -> DataType {
30+
DataType::Int8
31+
}
32+
33+
fn serialize_metadata(&self) -> Option<String> {
34+
None
35+
}
36+
37+
fn create_array_formatter<'fmt>(
38+
&self,
39+
array: &'fmt dyn Array,
40+
options: &FormatOptions<'fmt>,
41+
) -> crate::Result<Option<ArrayFormatter<'fmt>>> {
42+
if array.data_type() != &DataType::Int8 {
43+
return _internal_err!("Wrong array type for Bool8");
44+
}
45+
46+
let display_index = Bool8ValueDisplayIndex {
47+
array: array.as_any().downcast_ref().unwrap(),
48+
null_str: options.null(),
49+
};
50+
Ok(Some(ArrayFormatter::new(
51+
Box::new(display_index),
52+
options.safe(),
53+
)))
54+
}
55+
}
56+
57+
/// Pretty printer for binary UUID values.
58+
#[derive(Debug, Clone, Copy)]
59+
struct Bool8ValueDisplayIndex<'a> {
60+
array: &'a Int8Array,
61+
null_str: &'a str,
62+
}
63+
64+
impl DisplayIndex for Bool8ValueDisplayIndex<'_> {
65+
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
66+
if self.array.is_null(idx) {
67+
write!(f, "{}", self.null_str)?;
68+
return Ok(());
69+
}
70+
71+
let bytes = self.array.value(idx);
72+
write!(f, "{}", bytes != 0)?;
73+
Ok(())
74+
}
75+
}
76+
77+
#[cfg(test)]
78+
mod tests {
79+
use super::*;
80+
81+
#[test]
82+
pub fn test_pretty_bool8() {
83+
let values = Int8Array::from_iter([Some(0), Some(1), Some(-20), None]);
84+
85+
let extension_type = arrow_schema::extension::Bool8 {};
86+
let formatter = extension_type
87+
.create_array_formatter(&values, &FormatOptions::default().with_null("NULL"))
88+
.unwrap()
89+
.unwrap();
90+
91+
assert_eq!(formatter.value(0).to_string(), "false");
92+
assert_eq!(formatter.value(1).to_string(), "true");
93+
assert_eq!(formatter.value(2).to_string(), "true");
94+
assert_eq!(formatter.value(3).to_string(), "NULL");
95+
}
96+
}

datafusion/common/src/types/canonical_extensions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
mod bool8;
1819
mod uuid;

0 commit comments

Comments
 (0)