Skip to content
Open
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ indicatif = "0.18.0"
insta = "1.43"
inventory = "0.3.20"
itertools = "0.14.0"
itoa = "1.0.18"
jiff = "0.2.28"
jni = { version = "0.22.0" }
kanal = "0.1.1"
Expand Down Expand Up @@ -238,6 +239,7 @@ rstest = "0.26.1"
rstest_reuse = "0.7.0"
rustc-hash = "2.1.1"
rustix = { version = "1.1", features = ["fs"] }
ryu = "1.0.23"
serde = "1.0.221"
serde_json = "1.0.138"
serde_test = "1.0.176"
Expand Down
2 changes: 2 additions & 0 deletions vortex-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ half = { workspace = true, features = ["num-traits"] }
humansize = { workspace = true }
inventory = { workspace = true }
itertools = { workspace = true }
itoa = { workspace = true }
jiff = { workspace = true }
memchr = { workspace = true }
num-traits = { workspace = true }
Expand All @@ -53,6 +54,7 @@ regex-syntax = { workspace = true }
rstest = { workspace = true, optional = true }
rstest_reuse = { workspace = true, optional = true }
rustc-hash = { workspace = true }
ryu = { workspace = true }
serde = { workspace = true, optional = true, features = ["derive", "rc"] }
simdutf8 = { workspace = true }
smallvec = { workspace = true }
Expand Down
116 changes: 116 additions & 0 deletions vortex-array/src/arrays/bool/compute/cast.rs
Comment thread
haohuaijin marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::sync::Arc;

use num_traits::One;
use num_traits::Zero;
use vortex_buffer::BufferMut;
Expand All @@ -13,7 +15,9 @@ use crate::array::ArrayView;
use crate::arrays::Bool;
use crate::arrays::BoolArray;
use crate::arrays::PrimitiveArray;
use crate::arrays::VarBinViewArray;
use crate::arrays::bool::BoolArrayExt;
use crate::arrays::varbinview::BinaryView;
use crate::dtype::DType;
use crate::match_each_native_ptype;
use crate::scalar_fn::fns::cast::CastKernel;
Expand Down Expand Up @@ -53,6 +57,40 @@ impl CastKernel for Bool {
));
}

if let DType::Utf8(new_nullability) = dtype {
let len = array.len();
let new_validity = array
.validity()?
.cast_nullability(*new_nullability, len, ctx)?;

let values = array.to_bit_buffer();
let true_view = BinaryView::new_inlined(b"true");
let false_view = BinaryView::new_inlined(b"false");
let true_count = values.true_count();

let views = if true_count <= len - true_count {
let mut views = BufferMut::full(false_view, len);
values.for_each_set_index(|index| views[index] = true_view);
views
} else {
let mut views = BufferMut::full(true_view, len);
(!&values).for_each_set_index(|index| views[index] = false_view);
views
};

Comment on lines +71 to +80

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain the trick

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first init the full buffer with more frequent value(true/false), then only overwrite the another(false/true) positions, that minimize the index write.

// SAFETY: every view is one of two known-valid inlined UTF-8 strings, no view
// references an external buffer, and cast_nullability returns matching validity.
return Ok(Some(unsafe {
VarBinViewArray::new_unchecked(
views.freeze(),
Arc::from([]),
dtype.clone(),
new_validity,
)
.into_array()
}));
}

let DType::Primitive(new_ptype, new_nullability) = dtype else {
return Ok(None);
};
Expand All @@ -79,12 +117,15 @@ mod tests {
use std::sync::LazyLock;

use rstest::rstest;
use vortex_error::VortexResult;
use vortex_session::VortexSession;

use crate::Canonical;
use crate::IntoArray;
use crate::VortexSessionExecute;
use crate::arrays::BoolArray;
use crate::arrays::VarBinViewArray;
use crate::assert_arrays_eq;
use crate::builtins::ArrayBuiltins;
use crate::compute::conformance::cast::test_cast_conformance;
use crate::dtype::DType;
Expand Down Expand Up @@ -117,6 +158,81 @@ mod tests {
assert!(result.is_err(), "Expected error, got: {result:?}");
}

#[test]
fn cast_bool_to_utf8() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let actual = BoolArray::from_iter([true, false, true])
.into_array()
.cast(DType::Utf8(Nullability::NonNullable))?;
let expected = VarBinViewArray::from_iter_str(["true", "false", "true"]);

assert_arrays_eq!(actual, expected, &mut ctx);
Ok(())
}

#[test]
fn cast_nullable_bool_to_utf8() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let actual = BoolArray::from_iter([Some(true), None, Some(false)])
.into_array()
.cast(DType::Utf8(Nullability::Nullable))?;
let expected = VarBinViewArray::from_iter_nullable_str([Some("true"), None, Some("false")]);

assert_arrays_eq!(actual, expected, &mut ctx);
Ok(())
}

#[test]
fn cast_all_null_bool_to_utf8() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let actual = BoolArray::from_iter([None, None])
.into_array()
.cast(DType::Utf8(Nullability::Nullable))?;
let expected = VarBinViewArray::from_iter_nullable_str([None::<&str>, None]);

assert_arrays_eq!(actual, expected, &mut ctx);
Ok(())
}

#[test]
fn cast_nullable_bool_with_null_to_non_nullable_utf8_fails() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let result = BoolArray::from_iter([Some(true), None])
.into_array()
.cast(DType::Utf8(Nullability::NonNullable))?
.execute::<Canonical>(&mut ctx);

assert!(result.is_err(), "Expected error, got: {result:?}");
Ok(())
}

#[test]
fn cast_all_valid_nullable_bool_to_non_nullable_utf8() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let actual = BoolArray::from_iter([Some(true), Some(false)])
.into_array()
.cast(DType::Utf8(Nullability::NonNullable))?;
let expected = VarBinViewArray::from_iter_str(["true", "false"]);

assert_arrays_eq!(actual, expected, &mut ctx);
Ok(())
}

#[test]
fn cast_bool_to_binary_is_unsupported() {
let mut ctx = SESSION.create_execution_ctx();
let result = BoolArray::from_iter([true, false])
.into_array()
.cast(DType::Binary(Nullability::NonNullable))
.and_then(|array| {
array
.execute::<Canonical>(&mut ctx)
.map(|canonical| canonical.into_array())
});

assert!(result.is_err(), "Expected error, got: {result:?}");
}

#[rstest]
#[case(BoolArray::from_iter(vec![true, false, true, true, false]))]
#[case(BoolArray::from_iter(vec![Some(true), Some(false), None, Some(true), None]))]
Expand Down
24 changes: 24 additions & 0 deletions vortex-array/src/arrays/constant/compute/cast.rs
Comment thread
haohuaijin marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl CastReduce for Constant {
#[cfg(test)]
mod tests {
use rstest::rstest;
use vortex_error::VortexResult;

use crate::IntoArray;
use crate::VortexSessionExecute;
Expand Down Expand Up @@ -65,4 +66,27 @@ mod tests {
Some(DecimalValue::I128(4200))
);
}

#[rstest]
#[case(
Scalar::from(false),
DType::Utf8(Nullability::Nullable),
Scalar::utf8("false", Nullability::Nullable)
)]
#[case(
Scalar::from(-42i64),
DType::Utf8(Nullability::NonNullable),
Scalar::utf8("-42", Nullability::NonNullable)
)]
fn test_cast_bool_and_primitive_constants_to_utf8(
#[case] source: Scalar,
#[case] target: DType,
#[case] expected: Scalar,
) -> VortexResult<()> {
let casted = ConstantArray::new(source, 5).into_array().cast(target)?;

assert_eq!(casted.len(), 5);
assert_eq!(casted.as_constant(), Some(expected));
Ok(())
}
}
Loading