Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 0.14.1 (development)

- Allow to build bools from integers. Nonzero integers are mapped to true

## 0.14.0

- Add `arrow=58` support
Expand Down
40 changes: 40 additions & 0 deletions serde_arrow/src/internal/serialization/bool_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ impl<'a> Serializer for &'a mut BoolBuilder {
'a, BoolBuilder;
override serialize_none,
override serialize_bool,
override serialize_i8,
override serialize_i16,
override serialize_i32,
override serialize_i64,
override serialize_u8,
override serialize_u16,
override serialize_u32,
override serialize_u64,
);

fn serialize_none(self) -> Result<()> {
Expand All @@ -110,4 +118,36 @@ impl<'a> Serializer for &'a mut BoolBuilder {
self.array.len += 1;
Ok(())
}

fn serialize_i8(self, v: i8) -> Result<()> {
self.serialize_bool(v != 0)
}

fn serialize_i16(self, v: i16) -> Result<()> {
self.serialize_bool(v != 0)
}

fn serialize_i32(self, v: i32) -> Result<()> {
self.serialize_bool(v != 0)
}

fn serialize_i64(self, v: i64) -> Result<()> {
self.serialize_bool(v != 0)
}

fn serialize_u8(self, v: u8) -> Result<()> {
self.serialize_bool(v != 0)
}

fn serialize_u16(self, v: u16) -> Result<()> {
self.serialize_bool(v != 0)
}

fn serialize_u32(self, v: u32) -> Result<()> {
self.serialize_bool(v != 0)
}

fn serialize_u64(self, v: u64) -> Result<()> {
self.serialize_bool(v != 0)
}
}
88 changes: 88 additions & 0 deletions serde_arrow/src/test_with_arrow/impls/arrow_bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use serde_json::json;

use crate::internal::{schema::TracingOptions, utils::Item};

use super::utils::Test;

#[test]
fn bool() {
let items = &[Item(true), Item(false)];
Test::new()
.with_schema(json!([{"name": "item", "data_type": "Bool"}]))
.trace_schema_from_samples(items, TracingOptions::default())
.trace_schema_from_type::<Item<bool>>(TracingOptions::default())
.serialize(items)
.deserialize(items)
.check_nulls(&[&[false, false]]);
}

#[test]
fn nullable_bool() {
let items = &[Item(Some(true)), Item(None), Item(Some(false))];
Test::new()
.with_schema(json!([{"name": "item", "data_type": "Bool", "nullable": true}]))
.trace_schema_from_samples(items, TracingOptions::default())
.trace_schema_from_type::<Item<Option<bool>>>(TracingOptions::default())
.serialize(items)
.deserialize(items)
.check_nulls(&[&[false, true, false]]);
}

fn test_bool_from_int<T: serde::Serialize>(zero: T, nonzero: T) {
Test::new()
.with_schema(json!([{"name": "item", "data_type": "Bool"}]))
.serialize(&[Item(zero), Item(nonzero)])
.deserialize(&[Item(false), Item(true)]);
}

#[test]
fn bool_from_i8() {
test_bool_from_int(0_i8, 1_i8);
test_bool_from_int(0_i8, 32_i8);
test_bool_from_int(0_i8, -1_i8);
}

#[test]
fn bool_from_i16() {
test_bool_from_int(0_i16, 1_i16);
test_bool_from_int(0_i16, 32_i16);
test_bool_from_int(0_i16, -1_i16);
}

#[test]
fn bool_from_i32() {
test_bool_from_int(0_i32, 1_i32);
test_bool_from_int(0_i32, 32_i32);
test_bool_from_int(0_i32, -1_i32);
}

#[test]
fn bool_from_i64() {
test_bool_from_int(0_i64, 1_i64);
test_bool_from_int(0_i64, 32_i64);
test_bool_from_int(0_i64, -1_i64);
}

#[test]
fn bool_from_u8() {
test_bool_from_int(0_u8, 1_u8);
test_bool_from_int(0_u8, 32_u8);
}

#[test]
fn bool_from_u16() {
test_bool_from_int(0_u16, 1_u16);
test_bool_from_int(0_u16, 32_u16);
}

#[test]
fn bool_from_u32() {
test_bool_from_int(0_u32, 1_u32);
test_bool_from_int(0_u32, 32_u32);
}

#[test]
fn bool_from_u64() {
test_bool_from_int(0_u64, 1_u64);
test_bool_from_int(0_u64, 32_u64);
}
1 change: 1 addition & 0 deletions serde_arrow/src/test_with_arrow/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod utils;
mod arrow_binary;
#[cfg(has_arrow_bytes_view_support)]
mod arrow_binary_view;
mod arrow_bool;
mod arrow_date;
mod arrow_decimal;
mod arrow_dictionary;
Expand Down
24 changes: 0 additions & 24 deletions serde_arrow/src/test_with_arrow/impls/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,6 @@ fn null() {
// nulls = [true, true, true],
}

#[test]
fn bool() {
let items = &[Item(true), Item(false)];
Test::new()
.with_schema(json!([{"name": "item", "data_type": "Bool"}]))
.trace_schema_from_samples(items, TracingOptions::default())
.trace_schema_from_type::<Item<bool>>(TracingOptions::default())
.serialize(items)
.deserialize(items)
.check_nulls(&[&[false, false]]);
}

#[test]
fn nullable_bool() {
let items = &[Item(Some(true)), Item(None), Item(Some(false))];
Test::new()
.with_schema(json!([{"name": "item", "data_type": "Bool", "nullable": true}]))
.trace_schema_from_samples(items, TracingOptions::default())
.trace_schema_from_type::<Item<Option<bool>>>(TracingOptions::default())
.serialize(items)
.deserialize(items)
.check_nulls(&[&[false, true, false]]);
}

#[test]
fn u8() {
let items: &[Item<u8>] = &[Item(1), Item(2), Item(3), Item(4)];
Expand Down