geoffreyclaude commented on code in PR #11771:
URL: https://github.com/apache/arrow-rs/pull/11771#discussion_r4563697554
##########
arrow-row/src/lib.rs:
##########
@@ -944,6 +966,7 @@ enum Encoder<'a> {
field_to_type_ids: Vec<i8>,
type_ids: ScalarBuffer<i8>,
offsets: Option<ScalarBuffer<i32>>,
+ nulls: Option<arrow_buffer::NullBuffer>,
Review Comment:
Updated the documentation to list the encoded child rows, type IDs, optional
dense offsets, and logical nulls.
##########
arrow-row/tests/union_ordering.rs:
##########
@@ -0,0 +1,375 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use arrow_array::cast::AsArray;
+use arrow_array::{
+ Array, ArrayRef, Int32Array, ListArray, StringArray, StructArray,
UnionArray, new_empty_array,
+ new_null_array,
+};
+use arrow_buffer::{NullBuffer, OffsetBuffer};
+use arrow_ord::sort::{LexicographicalComparator, SortColumn};
+use arrow_row::{RowConverter, Rows, SortField};
+use arrow_schema::{DataType, Field, SortOptions, UnionFields, UnionMode};
+
+// Keep the test data in terms of values: Int(None) is a null from the integer
+// field, while Str(None) is a null from the string field.
+#[derive(Clone, Copy)]
+enum Value {
+ Int(Option<i32>),
+ Str(Option<&'static str>),
+}
+use Value::{Int, Str};
+
+#[rustfmt::skip]
+const INPUT: [(Value, Value, Option<&str>); 7] = [
+ // Union input Expected after decoding Following sort column
+ (Int(Some(2)), Int(Some(2)), None), // row 0
+ (Str(Some("z")), Str(Some("z")), Some("z")), // row 1
+ (Int(None), Int(None), Some("a")), // row 2
+ (Str(None), Int(None), Some("a")), // row 3
+ (Int(Some(-1)), Int(Some(-1)), None), // row 4
+ (Str(Some("a")), Str(Some("a")), Some("z")), // row 5
+ (Str(Some("z")), Str(Some("z")), Some("a")), // row 6
+];
+
+// The integers below are row numbers in INPUT. The following column always
sorts
+// descending, with its nulls last; these options apply to the Union column
only.
+#[rustfmt::skip]
+const SORT_CASES: [(&str, SortOptions, [usize; 7]); 4] = [
+ ("ascending, nulls first", SortOptions { descending: false, nulls_first:
true }, [2, 3, 5, 1, 6, 4, 0]),
+ ("ascending, nulls last", SortOptions { descending: false, nulls_first:
false }, [5, 1, 6, 4, 0, 2, 3]),
+ ("descending, nulls first", SortOptions { descending: true, nulls_first:
true }, [2, 3, 0, 4, 1, 6, 5]),
+ ("descending, nulls last", SortOptions { descending: true, nulls_first:
false }, [0, 4, 1, 6, 5, 2, 3]),
+];
+
+fn int_string_fields() -> UnionFields {
+ // Put integer first in the schema, but give string the smaller type ID.
+ // Ordering uses type IDs; choosing a field for decoded nulls uses schema
order.
+ UnionFields::try_new(
+ [127, 0],
+ [
+ Field::new("int", DataType::Int32, true),
+ Field::new("str", DataType::Utf8, true),
+ ],
+ )
+ .unwrap()
+}
+
+fn int_string_union(mode: UnionMode, fields: &UnionFields, values: &[Value])
-> UnionArray {
+ let mut fields_iter = fields.iter();
+ let int_id = fields_iter.next().unwrap().0;
+ let str_id = fields_iter.next().unwrap().0;
+ let mut type_ids = Vec::new();
+ let mut offsets = Vec::new();
+ let mut ints = Vec::new();
+ let mut strings = Vec::new();
+ for &value in values {
+ // Dense children keep only their selected values. Sparse children
have a
+ // slot for every row, so fill the unselected child's slot with a null.
+ match value {
+ Int(value) => {
+ type_ids.push(int_id);
+ offsets.push(ints.len() as i32);
+ ints.push(value);
+ if mode == UnionMode::Sparse {
+ strings.push(None);
+ }
+ }
+ Str(value) => {
+ type_ids.push(str_id);
+ offsets.push(strings.len() as i32);
+ strings.push(value);
+ if mode == UnionMode::Sparse {
+ ints.push(None);
+ }
+ }
+ }
+ }
+ UnionArray::try_new(
+ fields.clone(),
+ type_ids.into(),
+ (mode == UnionMode::Dense).then(|| offsets.into()),
+ vec![
+ Arc::new(Int32Array::from(ints)),
+ Arc::new(StringArray::from(strings)),
+ ],
+ )
+ .unwrap()
+}
+
+fn check_roundtrip(columns: &[ArrayRef], options: &[SortOptions]) ->
Vec<ArrayRef> {
+ let fields = |columns: &[ArrayRef]| {
+ columns
+ .iter()
+ .zip(options)
+ .map(|(column, &options)| {
+ SortField::new_with_options(column.data_type().clone(),
options)
+ })
+ .collect()
+ };
+ let converter = RowConverter::new(fields(columns)).unwrap();
+ let rows = converter.convert_columns(columns).unwrap();
+ let decoded = converter.convert_rows(&rows).unwrap();
+ for column in &decoded {
+ column.to_data().validate_full().unwrap();
+ }
+
+ // Nulls may change Union fields, and dictionaries decode to their value
types.
Review Comment:
Updated the comment to say “the selected Union field,” making clear that the
set of fields stays the same.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]