[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r439567609 ## File path: rust/datafusion/src/execution/physical_plan/sort.rs ## @@ -0,0 +1,211 @@ +// 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 the SORT plan + +use std::sync::{Arc, Mutex}; +use std::thread; +use std::thread::JoinHandle; + +use arrow::array::ArrayRef; +pub use arrow::compute::SortOptions; +use arrow::compute::{concat, lexsort_to_indices, take, SortColumn, TakeOptions}; +use arrow::datatypes::Schema; +use arrow::record_batch::RecordBatch; + +use crate::error::Result; +use crate::execution::physical_plan::common::RecordBatchIterator; +use crate::execution::physical_plan::expressions::PhysicalSortExpr; +use crate::execution::physical_plan::{common, BatchIterator, ExecutionPlan, Partition}; + +/// Sort execution plan +pub struct SortExec { +/// Input schema +input: Arc, +expr: Vec, +} + +impl SortExec { +/// Create a new sort execution plan +pub fn try_new( +expr: Vec, +input: Arc, +) -> Result { +Ok(Self { expr, input }) +} +} + +impl ExecutionPlan for SortExec { +fn schema(&self) -> Arc { +self.input.schema().clone() +} + +fn partitions(&self) -> Result>> { +Ok(vec![ +(Arc::new(SortPartition { +input: self.input.partitions()?, +expr: self.expr.clone(), +schema: self.schema(), +})), +]) +} +} + +/// Represents a single partition of a Sort execution plan +struct SortPartition { +schema: Arc, +expr: Vec, +input: Vec>, +} + +impl Partition for SortPartition { +/// Execute the sort +fn execute(&self) -> Result>> { +let threads: Vec>>> = self +.input +.iter() +.map(|p| { +let p = p.clone(); +thread::spawn(move || { +let it = p.execute()?; +common::collect(it) +}) +}) +.collect(); + +// generate record batches from input in parallel +let mut all_batches: Vec> = vec![]; +for thread in threads { +let join = thread.join().expect("Failed to join thread"); +let result = join?; +result +.iter() +.for_each(|batch| all_batches.push(Arc::new(batch.clone(; +} + +// combine all record batches into one for each column +let combined_batch = RecordBatch::try_new( +self.schema.clone(), +self.schema +.fields() +.iter() +.enumerate() +.map(|(i, _)| -> Result { +Ok(concat( +&all_batches +.iter() +.map(|batch| batch.columns()[i].clone()) +.collect::>(), +)?) +}) +.collect::>>()?, +)?; + +// sort combined record batch +let indices = lexsort_to_indices( +&self +.expr +.iter() +.map(|e| e.evaluate_to_sort_column(&combined_batch)) +.collect::>>()?, +)?; + +// reorder all rows based on sorted indices +let sorted_batch = RecordBatch::try_new( +self.schema.clone(), +combined_batch +.columns() +.iter() +.map(|column| -> Result { +Ok(take( +column, +&indices, +// disable bound check overhead since indices are already generated from +// the same record batch +Some(TakeOptions { +check_bounds: false, +}), +)?) +}) +.collect::>>()?, +)?; + +Ok(Arc::new(Mutex::new(RecordBatchIterator::new( Review comment: I have the same question while developing this, curious
[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r437861027 ## File path: rust/arrow/src/array/builder.rs ## @@ -1123,29 +1123,35 @@ fn append_binary_data( } } -for array in data { -// convert string to List to reuse list's cast -let int_data = &array.buffers()[1]; -let int_data = Arc::new(ArrayData::new( -DataType::UInt8, -int_data.len(), -None, -None, -0, -vec![int_data.clone()], -vec![], -)) as ArrayDataRef; -let list_data = Arc::new(ArrayData::new( -DataType::List(Box::new(DataType::UInt8)), -array.len(), -None, -array.null_buffer().map(|buf| buf.clone()), -array.offset(), -vec![(&array.buffers()[0]).clone()], -vec![int_data], -)); -builder.append_data(&[list_data])?; -} +builder.append_data( Review comment: move append_data out of the for loop so memory allocation count for buffers is constant. 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r436252687 ## File path: rust/arrow/src/compute/kernels/concat.rs ## @@ -0,0 +1,395 @@ +// 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 concat kernel for `ArrayRef` +//! +//! Example: +//! +//! ``` +//! use std::sync::Arc; +//! use arrow::array::{ArrayRef, StringArray}; +//! use arrow::compute::concat; +//! +//! let arr = concat(&vec![ +//! Arc::new(StringArray::from(vec!["hello", "world"])) as ArrayRef, +//! Arc::new(StringArray::from(vec!["!"])) as ArrayRef, +//! ]).unwrap(); +//! assert_eq!(arr.len(), 3); +//! ``` + +use std::sync::Arc; + +use crate::array::*; +use crate::datatypes::*; +use crate::error::{ArrowError, Result}; + +use TimeUnit::*; + +/// Concatenate multiple `ArrayRef` with the same type. +/// +/// Returns a new ArrayRef. +pub fn concat(array_list: &Vec) -> Result { +let mut data_type: Option = None; +let array_data_list = &array_list +.iter() +.map(|a| { +let array_data = a.data_ref().clone(); +let curr_data_type = array_data.data_type().clone(); +match &data_type { +Some(t) => { +if t != &curr_data_type { +return Err(ArrowError::ComputeError( +"Cannot concat arrays with data type".to_string(), +)); +} +} +None => { +data_type = Some(curr_data_type); +} +} +Ok(array_data) +}) +.collect::>>()?; + +let data_type = match data_type { +None => { +return Err(ArrowError::ComputeError( +"Cannot concat 0 array".to_string(), +)); +} +Some(t) => t, +}; +match data_type { +DataType::Utf8 => concat_raw_string(array_data_list), +DataType::Boolean => concat_primitive::(array_data_list), +DataType::Int8 => concat_raw_primitive::(array_data_list), +DataType::Int16 => concat_raw_primitive::(array_data_list), +DataType::Int32 => concat_raw_primitive::(array_data_list), +DataType::Int64 => concat_raw_primitive::(array_data_list), +DataType::UInt8 => concat_raw_primitive::(array_data_list), +DataType::UInt16 => concat_raw_primitive::(array_data_list), +DataType::UInt32 => concat_raw_primitive::(array_data_list), +DataType::UInt64 => concat_raw_primitive::(array_data_list), +DataType::Float32 => concat_raw_primitive::(array_data_list), +DataType::Float64 => concat_raw_primitive::(array_data_list), +DataType::Date32(_) => concat_raw_primitive::(array_data_list), +DataType::Date64(_) => concat_raw_primitive::(array_data_list), +DataType::Time32(Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time32(Millisecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Microsecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Nanosecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Second, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Millisecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Microsecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Nanosecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::YearMonth) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::DayTime) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Millisecond) => { +concat_raw_primitiv
[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r435430674 ## File path: rust/arrow/src/compute/kernels/concat.rs ## @@ -0,0 +1,395 @@ +// 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 concat kernel for `ArrayRef` +//! +//! Example: +//! +//! ``` +//! use std::sync::Arc; +//! use arrow::array::{ArrayRef, StringArray}; +//! use arrow::compute::concat; +//! +//! let arr = concat(&vec![ +//! Arc::new(StringArray::from(vec!["hello", "world"])) as ArrayRef, +//! Arc::new(StringArray::from(vec!["!"])) as ArrayRef, +//! ]).unwrap(); +//! assert_eq!(arr.len(), 3); +//! ``` + +use std::sync::Arc; + +use crate::array::*; +use crate::datatypes::*; +use crate::error::{ArrowError, Result}; + +use TimeUnit::*; + +/// Concatenate multiple `ArrayRef` with the same type. +/// +/// Returns a new ArrayRef. +pub fn concat(array_list: &Vec) -> Result { +let mut data_type: Option = None; +let array_data_list = &array_list +.iter() +.map(|a| { +let array_data = a.data_ref().clone(); +let curr_data_type = array_data.data_type().clone(); +match &data_type { +Some(t) => { +if t != &curr_data_type { +return Err(ArrowError::ComputeError( +"Cannot concat arrays with data type".to_string(), +)); +} +} +None => { +data_type = Some(curr_data_type); +} +} +Ok(array_data) +}) +.collect::>>()?; + +let data_type = match data_type { +None => { +return Err(ArrowError::ComputeError( +"Cannot concat 0 array".to_string(), +)); +} +Some(t) => t, +}; +match data_type { +DataType::Utf8 => concat_raw_string(array_data_list), +DataType::Boolean => concat_primitive::(array_data_list), +DataType::Int8 => concat_raw_primitive::(array_data_list), +DataType::Int16 => concat_raw_primitive::(array_data_list), +DataType::Int32 => concat_raw_primitive::(array_data_list), +DataType::Int64 => concat_raw_primitive::(array_data_list), +DataType::UInt8 => concat_raw_primitive::(array_data_list), +DataType::UInt16 => concat_raw_primitive::(array_data_list), +DataType::UInt32 => concat_raw_primitive::(array_data_list), +DataType::UInt64 => concat_raw_primitive::(array_data_list), +DataType::Float32 => concat_raw_primitive::(array_data_list), +DataType::Float64 => concat_raw_primitive::(array_data_list), +DataType::Date32(_) => concat_raw_primitive::(array_data_list), +DataType::Date64(_) => concat_raw_primitive::(array_data_list), +DataType::Time32(Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time32(Millisecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Microsecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Nanosecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Second, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Millisecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Microsecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Nanosecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::YearMonth) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::DayTime) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Millisecond) => { +concat_raw_primitiv
[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r435384974 ## File path: rust/arrow/src/compute/kernels/concat.rs ## @@ -0,0 +1,395 @@ +// 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 concat kernel for `ArrayRef` +//! +//! Example: +//! +//! ``` +//! use std::sync::Arc; +//! use arrow::array::{ArrayRef, StringArray}; +//! use arrow::compute::concat; +//! +//! let arr = concat(&vec![ +//! Arc::new(StringArray::from(vec!["hello", "world"])) as ArrayRef, +//! Arc::new(StringArray::from(vec!["!"])) as ArrayRef, +//! ]).unwrap(); +//! assert_eq!(arr.len(), 3); +//! ``` + +use std::sync::Arc; + +use crate::array::*; +use crate::datatypes::*; +use crate::error::{ArrowError, Result}; + +use TimeUnit::*; + +/// Concatenate multiple `ArrayRef` with the same type. +/// +/// Returns a new ArrayRef. +pub fn concat(array_list: &Vec) -> Result { +let mut data_type: Option = None; +let array_data_list = &array_list +.iter() +.map(|a| { +let array_data = a.data_ref().clone(); +let curr_data_type = array_data.data_type().clone(); +match &data_type { +Some(t) => { +if t != &curr_data_type { +return Err(ArrowError::ComputeError( +"Cannot concat arrays with data type".to_string(), +)); +} +} +None => { +data_type = Some(curr_data_type); +} +} +Ok(array_data) +}) +.collect::>>()?; + +let data_type = match data_type { +None => { +return Err(ArrowError::ComputeError( +"Cannot concat 0 array".to_string(), +)); +} +Some(t) => t, +}; +match data_type { +DataType::Utf8 => concat_raw_string(array_data_list), +DataType::Boolean => concat_primitive::(array_data_list), +DataType::Int8 => concat_raw_primitive::(array_data_list), +DataType::Int16 => concat_raw_primitive::(array_data_list), +DataType::Int32 => concat_raw_primitive::(array_data_list), +DataType::Int64 => concat_raw_primitive::(array_data_list), +DataType::UInt8 => concat_raw_primitive::(array_data_list), +DataType::UInt16 => concat_raw_primitive::(array_data_list), +DataType::UInt32 => concat_raw_primitive::(array_data_list), +DataType::UInt64 => concat_raw_primitive::(array_data_list), +DataType::Float32 => concat_raw_primitive::(array_data_list), +DataType::Float64 => concat_raw_primitive::(array_data_list), +DataType::Date32(_) => concat_raw_primitive::(array_data_list), +DataType::Date64(_) => concat_raw_primitive::(array_data_list), +DataType::Time32(Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time32(Millisecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Microsecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Nanosecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Second, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Millisecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Microsecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Nanosecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::YearMonth) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::DayTime) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Millisecond) => { +concat_raw_primitiv
[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r435027946 ## File path: rust/arrow/src/compute/kernels/concat.rs ## @@ -0,0 +1,395 @@ +// 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 concat kernel for `ArrayRef` +//! +//! Example: +//! +//! ``` +//! use std::sync::Arc; +//! use arrow::array::{ArrayRef, StringArray}; +//! use arrow::compute::concat; +//! +//! let arr = concat(&vec![ +//! Arc::new(StringArray::from(vec!["hello", "world"])) as ArrayRef, +//! Arc::new(StringArray::from(vec!["!"])) as ArrayRef, +//! ]).unwrap(); +//! assert_eq!(arr.len(), 3); +//! ``` + +use std::sync::Arc; + +use crate::array::*; +use crate::datatypes::*; +use crate::error::{ArrowError, Result}; + +use TimeUnit::*; + +/// Concatenate multiple `ArrayRef` with the same type. +/// +/// Returns a new ArrayRef. +pub fn concat(array_list: &Vec) -> Result { +let mut data_type: Option = None; +let array_data_list = &array_list +.iter() +.map(|a| { +let array_data = a.data_ref().clone(); +let curr_data_type = array_data.data_type().clone(); +match &data_type { +Some(t) => { +if t != &curr_data_type { +return Err(ArrowError::ComputeError( +"Cannot concat arrays with data type".to_string(), +)); +} +} +None => { +data_type = Some(curr_data_type); +} +} +Ok(array_data) +}) +.collect::>>()?; + +let data_type = match data_type { +None => { +return Err(ArrowError::ComputeError( +"Cannot concat 0 array".to_string(), +)); +} +Some(t) => t, +}; +match data_type { +DataType::Utf8 => concat_raw_string(array_data_list), +DataType::Boolean => concat_primitive::(array_data_list), +DataType::Int8 => concat_raw_primitive::(array_data_list), +DataType::Int16 => concat_raw_primitive::(array_data_list), +DataType::Int32 => concat_raw_primitive::(array_data_list), +DataType::Int64 => concat_raw_primitive::(array_data_list), +DataType::UInt8 => concat_raw_primitive::(array_data_list), +DataType::UInt16 => concat_raw_primitive::(array_data_list), +DataType::UInt32 => concat_raw_primitive::(array_data_list), +DataType::UInt64 => concat_raw_primitive::(array_data_list), +DataType::Float32 => concat_raw_primitive::(array_data_list), +DataType::Float64 => concat_raw_primitive::(array_data_list), +DataType::Date32(_) => concat_raw_primitive::(array_data_list), +DataType::Date64(_) => concat_raw_primitive::(array_data_list), +DataType::Time32(Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time32(Millisecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Microsecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Nanosecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Second, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Millisecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Microsecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Nanosecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::YearMonth) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::DayTime) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Millisecond) => { +concat_raw_primitiv
[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r435024541 ## File path: rust/arrow/src/compute/kernels/sort.rs ## @@ -122,7 +122,7 @@ impl Default for SortOptions { fn default() -> Self { Self { descending: false, -nulls_first: false, +nulls_first: true, Review comment: this is to match spark's default behavior, i have added a comment. 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [arrow] houqp commented on a change in pull request #7324: ARROW-9005: [Rust] [Datafusion] support sort expression
houqp commented on a change in pull request #7324: URL: https://github.com/apache/arrow/pull/7324#discussion_r435023936 ## File path: rust/arrow/src/compute/kernels/concat.rs ## @@ -0,0 +1,395 @@ +// 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 concat kernel for `ArrayRef` +//! +//! Example: +//! +//! ``` +//! use std::sync::Arc; +//! use arrow::array::{ArrayRef, StringArray}; +//! use arrow::compute::concat; +//! +//! let arr = concat(&vec![ +//! Arc::new(StringArray::from(vec!["hello", "world"])) as ArrayRef, +//! Arc::new(StringArray::from(vec!["!"])) as ArrayRef, +//! ]).unwrap(); +//! assert_eq!(arr.len(), 3); +//! ``` + +use std::sync::Arc; + +use crate::array::*; +use crate::datatypes::*; +use crate::error::{ArrowError, Result}; + +use TimeUnit::*; + +/// Concatenate multiple `ArrayRef` with the same type. +/// +/// Returns a new ArrayRef. +pub fn concat(array_list: &Vec) -> Result { +let mut data_type: Option = None; +let array_data_list = &array_list +.iter() +.map(|a| { +let array_data = a.data_ref().clone(); +let curr_data_type = array_data.data_type().clone(); +match &data_type { +Some(t) => { +if t != &curr_data_type { +return Err(ArrowError::ComputeError( +"Cannot concat arrays with data type".to_string(), +)); +} +} +None => { +data_type = Some(curr_data_type); +} +} +Ok(array_data) +}) +.collect::>>()?; + +let data_type = match data_type { +None => { +return Err(ArrowError::ComputeError( +"Cannot concat 0 array".to_string(), +)); +} +Some(t) => t, +}; +match data_type { +DataType::Utf8 => concat_raw_string(array_data_list), +DataType::Boolean => concat_primitive::(array_data_list), +DataType::Int8 => concat_raw_primitive::(array_data_list), +DataType::Int16 => concat_raw_primitive::(array_data_list), +DataType::Int32 => concat_raw_primitive::(array_data_list), +DataType::Int64 => concat_raw_primitive::(array_data_list), +DataType::UInt8 => concat_raw_primitive::(array_data_list), +DataType::UInt16 => concat_raw_primitive::(array_data_list), +DataType::UInt32 => concat_raw_primitive::(array_data_list), +DataType::UInt64 => concat_raw_primitive::(array_data_list), +DataType::Float32 => concat_raw_primitive::(array_data_list), +DataType::Float64 => concat_raw_primitive::(array_data_list), +DataType::Date32(_) => concat_raw_primitive::(array_data_list), +DataType::Date64(_) => concat_raw_primitive::(array_data_list), +DataType::Time32(Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time32(Millisecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Microsecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Time64(Nanosecond) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Second, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Millisecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Microsecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Timestamp(Nanosecond, _) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::YearMonth) => { +concat_raw_primitive::(array_data_list) +} +DataType::Interval(IntervalUnit::DayTime) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Second) => { +concat_raw_primitive::(array_data_list) +} +DataType::Duration(TimeUnit::Millisecond) => { +concat_raw_primitiv