Skip to content

Commit a49af1d

Browse files
authored
fix: [9018]Fixed RunArray slice offsets(row, cast, eq) (#9213)
# Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. --> - Closes #9018 . # Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> The existing implementation had issue for row or cast ops for RunArray. The equality implementation also did not support logical index-based comparisons. # What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> 1. Fixed RunArray row and cast handling 2. Added logical index based equality check for RunArray. # Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 4. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes # Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out. --> No.
1 parent d0ed407 commit a49af1d

File tree

7 files changed

+299
-64
lines changed

7 files changed

+299
-64
lines changed

arrow-array/src/array/run_array.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ impl<R: RunEndIndexType> RunArray<R> {
141141
///
142142
/// [`values`]: Self::values
143143
pub fn values_slice(&self) -> ArrayRef {
144+
if self.is_empty() {
145+
return self.values.slice(0, 0);
146+
}
144147
let start = self.get_start_physical_index();
145148
let end = self.get_end_physical_index();
146149
self.values.slice(start, end - start + 1)
@@ -653,6 +656,7 @@ mod tests {
653656
use super::*;
654657
use crate::builder::PrimitiveRunBuilder;
655658
use crate::cast::AsArray;
659+
use crate::new_empty_array;
656660
use crate::types::{Int8Type, UInt32Type};
657661
use crate::{Int16Array, Int32Array, StringArray};
658662

@@ -750,6 +754,26 @@ mod tests {
750754
assert_eq!(run_ends.values(), &run_ends_values);
751755
}
752756

757+
#[test]
758+
fn test_run_array_empty() {
759+
let runs = new_empty_array(&DataType::Int16);
760+
let runs = runs.as_primitive::<Int16Type>();
761+
let values = new_empty_array(&DataType::Int64);
762+
let array = RunArray::try_new(runs, &values).unwrap();
763+
764+
fn assertions(array: &RunArray<Int16Type>) {
765+
assert!(array.is_empty());
766+
assert_eq!(array.get_start_physical_index(), 0);
767+
assert_eq!(array.get_end_physical_index(), 0);
768+
assert!(array.get_physical_indices::<i16>(&[]).unwrap().is_empty());
769+
assert!(array.run_ends().is_empty());
770+
assert_eq!(array.run_ends().sliced_values().count(), 0);
771+
}
772+
773+
assertions(&array);
774+
assertions(&array.slice(0, 0));
775+
}
776+
753777
#[test]
754778
fn test_run_array_fmt_debug() {
755779
let mut builder = PrimitiveRunBuilder::<Int16Type, UInt32Type>::with_capacity(3);
@@ -1184,4 +1208,91 @@ mod tests {
11841208
let values_slice2 = values_slice2.as_primitive::<Int32Type>();
11851209
assert_eq!(values_slice2.values(), &[1]);
11861210
}
1211+
1212+
#[test]
1213+
fn test_run_array_values_slice_empty() {
1214+
let run_ends = Int32Array::from(vec![2, 5, 10]);
1215+
let values = StringArray::from(vec!["a", "b", "c"]);
1216+
let array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
1217+
1218+
let slice = array.slice(0, 0);
1219+
assert_eq!(slice.len(), 0);
1220+
1221+
let values_slice = slice.values_slice();
1222+
assert_eq!(values_slice.len(), 0);
1223+
assert_eq!(values_slice.data_type(), &DataType::Utf8);
1224+
}
1225+
1226+
#[test]
1227+
fn test_run_array_eq_empty() {
1228+
let run_ends = Int32Array::from(vec![2, 5, 10]);
1229+
let values = StringArray::from(vec!["a", "b", "c"]);
1230+
let array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
1231+
1232+
let slice1 = array.slice(0, 0);
1233+
let slice2 = array.slice(1, 0);
1234+
let slice3 = array.slice(10, 0);
1235+
1236+
assert_eq!(slice1, slice2);
1237+
assert_eq!(slice2, slice3);
1238+
1239+
let empty_array = new_empty_array(array.data_type());
1240+
let empty_array = crate::cast::as_run_array::<Int32Type>(empty_array.as_ref());
1241+
1242+
assert_eq!(&slice1, empty_array);
1243+
}
1244+
1245+
#[test]
1246+
fn test_run_array_eq_diff_physical_same_logical() {
1247+
let run_ends1 = Int32Array::from(vec![1, 3, 6]);
1248+
let values1 = StringArray::from(vec!["a", "b", "c"]);
1249+
let array1 = RunArray::<Int32Type>::try_new(&run_ends1, &values1).unwrap();
1250+
1251+
let run_ends2 = Int32Array::from(vec![1, 2, 3, 4, 5, 6]);
1252+
let values2 = StringArray::from(vec!["a", "b", "b", "c", "c", "c"]);
1253+
let array2 = RunArray::<Int32Type>::try_new(&run_ends2, &values2).unwrap();
1254+
1255+
assert_eq!(array1, array2);
1256+
}
1257+
1258+
#[test]
1259+
fn test_run_array_eq_sliced() {
1260+
let run_ends1 = Int32Array::from(vec![2, 5, 10]);
1261+
let values1 = StringArray::from(vec!["a", "b", "c"]);
1262+
let array1 = RunArray::<Int32Type>::try_new(&run_ends1, &values1).unwrap();
1263+
// Logical: a, a, b, b, b, c, c, c, c, c
1264+
1265+
let slice1 = array1.slice(1, 6);
1266+
// Logical: a, b, b, b, c, c
1267+
1268+
let run_ends2 = Int32Array::from(vec![1, 4, 6]);
1269+
let values2 = StringArray::from(vec!["a", "b", "c"]);
1270+
let array2 = RunArray::<Int32Type>::try_new(&run_ends2, &values2).unwrap();
1271+
// Logical: a, b, b, b, c, c
1272+
1273+
assert_eq!(slice1, array2);
1274+
1275+
let slice2 = array1.slice(2, 3);
1276+
// Logical: b, b, b
1277+
let run_ends3 = Int32Array::from(vec![3]);
1278+
let values3 = StringArray::from(vec!["b"]);
1279+
let array3 = RunArray::<Int32Type>::try_new(&run_ends3, &values3).unwrap();
1280+
assert_eq!(slice2, array3);
1281+
}
1282+
1283+
#[test]
1284+
fn test_run_array_eq_sliced_different_offsets() {
1285+
let run_ends1 = Int32Array::from(vec![2, 5, 10]);
1286+
let values1 = StringArray::from(vec!["a", "b", "c"]);
1287+
let array1 = RunArray::<Int32Type>::try_new(&run_ends1, &values1).unwrap();
1288+
let array2 = array1.clone();
1289+
assert_eq!(array1, array2);
1290+
1291+
let slice1 = array1.slice(1, 4); // a, b, b, b
1292+
let slice2 = array1.slice(1, 4);
1293+
assert_eq!(slice1, slice2);
1294+
1295+
let slice3 = array1.slice(0, 4); // a, a, b, b
1296+
assert_ne!(slice1, slice3);
1297+
}
11871298
}

arrow-buffer/src/buffer/run.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,16 @@ where
199199
pub fn sliced_values(&self) -> impl Iterator<Item = E> + '_ {
200200
let offset = self.logical_offset;
201201
let len = self.logical_length;
202-
let start = self.get_start_physical_index();
203-
let end = self.get_end_physical_index();
204-
self.run_ends[start..=end].iter().map(move |&val| {
202+
// Doing this roundabout way since the iterator type we return must be
203+
// the same (i.e. cannot use std::iter::empty())
204+
let physical_slice = if self.is_empty() {
205+
&self.run_ends[0..0]
206+
} else {
207+
let start = self.get_start_physical_index();
208+
let end = self.get_end_physical_index();
209+
&self.run_ends[start..=end]
210+
};
211+
physical_slice.iter().map(move |&val| {
205212
let val = val.as_usize().saturating_sub(offset).min(len);
206213
E::from_usize(val).unwrap()
207214
})

arrow-cast/src/cast/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12590,4 +12590,32 @@ mod tests {
1259012590
assert_eq!(casted.as_ref(), &expected);
1259112591
}
1259212592
}
12593+
12594+
#[test]
12595+
fn test_cast_between_sliced_run_end_encoded() {
12596+
let run_ends = Int16Array::from(vec![2, 5, 8]);
12597+
let values = StringArray::from(vec!["a", "b", "c"]);
12598+
12599+
let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12600+
let ree_array = ree_array.slice(1, 2);
12601+
let array_ref = Arc::new(ree_array) as ArrayRef;
12602+
12603+
let target_type = DataType::RunEndEncoded(
12604+
Arc::new(Field::new("run_ends", DataType::Int64, false)),
12605+
Arc::new(Field::new("values", DataType::Utf8, true)),
12606+
);
12607+
let cast_options = CastOptions {
12608+
safe: false,
12609+
format_options: FormatOptions::default(),
12610+
};
12611+
12612+
let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
12613+
let run_array = result.as_run::<Int64Type>();
12614+
let run_array = run_array.downcast::<StringArray>().unwrap();
12615+
12616+
let expected = vec!["a", "b"];
12617+
let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
12618+
12619+
assert_eq!(expected, actual);
12620+
}
1259312621
}

arrow-cast/src/cast/run_array.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ pub(crate) fn run_end_encoded_cast<K: RunEndIndexType>(
3232
.downcast_ref::<RunArray<K>>()
3333
.ok_or_else(|| ArrowError::CastError("Expected RunArray".to_string()))?;
3434

35-
let values = run_array.values();
36-
3735
match to_type {
3836
// Stay as RunEndEncoded, cast only the values
3937
DataType::RunEndEncoded(target_index_field, target_value_field) => {
40-
let cast_values =
41-
cast_with_options(values, target_value_field.data_type(), cast_options)?;
38+
let values = run_array.values_slice();
39+
let cast_values = cast_with_options(
40+
values.as_ref(),
41+
target_value_field.data_type(),
42+
cast_options,
43+
)?;
4244

43-
let run_ends_array = PrimitiveArray::<K>::from_iter_values(
44-
run_array.run_ends().values().iter().copied(),
45-
);
45+
let run_ends_array =
46+
PrimitiveArray::<K>::from_iter_values(run_array.run_ends().sliced_values());
4647
let cast_run_ends = cast_with_options(
4748
&run_ends_array,
4849
target_index_field.data_type(),
@@ -72,6 +73,7 @@ pub(crate) fn run_end_encoded_cast<K: RunEndIndexType>(
7273

7374
// Expand to logical form
7475
_ => {
76+
let values = run_array.values();
7577
let len = run_array.len();
7678
let offset = run_array.offset();
7779
let run_ends = run_array.run_ends().values();

0 commit comments

Comments
 (0)