-
Notifications
You must be signed in to change notification settings - Fork 197
feat(array): cast bool and primitive values to Utf8 #9261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
57266ff
61d9744
d56165e
d12b54a
910b12d
2c3825c
b104f51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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; | ||
|
|
@@ -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; | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you explain the trick
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| }; | ||
|
|
@@ -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; | ||
|
|
@@ -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]))] | ||
|
|
||
|
haohuaijin marked this conversation as resolved.
|
Uh oh!
There was an error while loading. Please reload this page.