wiedld commented on code in PR #6387: URL: https://github.com/apache/arrow-rs/pull/6387#discussion_r1768922477
########## arrow-select/src/union_extract.rs: ########## @@ -0,0 +1,1221 @@ +// 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. + +//! Defines union_extract kernel for [UnionArray] + +use crate::take::take; +use arrow_array::{ + make_array, new_empty_array, new_null_array, Array, ArrayRef, BooleanArray, Int32Array, Scalar, + UnionArray, +}; +use arrow_buffer::{bit_util, BooleanBuffer, MutableBuffer, NullBuffer, ScalarBuffer}; +use arrow_data::layout; +use arrow_schema::{ArrowError, DataType, UnionFields}; +use std::cmp::Ordering; +use std::sync::Arc; + +/// Returns the value of the target field when selected, or NULL otherwise. +/// ```text +/// ┌─────────────────┐ ┌─────────────────┐ +/// │ A=1 │ │ 1 │ +/// ├─────────────────┤ ├─────────────────┤ +/// │ A=NULL │ │ NULL │ +/// ├─────────────────┤ union_extract(values, 'A') ├─────────────────┤ +/// │ B='t' │ ────────────────────────────▶ │ NULL │ +/// ├─────────────────┤ ├─────────────────┤ +/// │ A=3 │ │ 3 │ +/// ├─────────────────┤ ├─────────────────┤ +/// │ B=NULL │ │ NULL │ +/// └─────────────────┘ └─────────────────┘ +/// union array result +/// ``` +/// # Errors +/// +/// Returns error if target field is not found +/// +/// # Examples +/// ``` +/// # use std::sync::Arc; +/// # use arrow_schema::{DataType, Field, UnionFields}; +/// # use arrow_array::{UnionArray, StringArray, Int32Array}; +/// # use arrow_select::union_extract::union_extract; +/// let fields = UnionFields::new( +/// [1, 3], +/// [ +/// Field::new("A", DataType::Int32, true), +/// Field::new("B", DataType::Utf8, true) +/// ] +/// ); +/// +/// let union = UnionArray::try_new( +/// fields, +/// vec![1, 1, 3, 1, 3].into(), +/// None, +/// vec![ +/// Arc::new(Int32Array::from(vec![Some(1), None, None, Some(3), Some(0)])), +/// Arc::new(StringArray::from(vec![None, None, Some("t"), Some("."), None])) Review Comment: Learning question, unrelated to the changes in this PR. The UnionArray::try_new permits multiple logical values (for the same slot) for sparse union modes. In the example above this is `[{A=1}, {}, {B='t'}, {A=3, B="."}, {A=0}]`. Does this mean that sparse unions can be used as json equivalents, whereas dense unions are for dynamic typing? If yes, is this documented somewhere? -- 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]
