Skip to content

Apache DataFusion SQL Query Engine

ARROW-4605: [Rust] Move filter and limit code from DataFusion into compute module

Nicolas Trinquiered986ad6f164
diff --git a/rust/datafusion/src/execution/filter.rs b/rust/datafusion/src/execution/filter.rs
index 32c1628fec..8e706705b2 100644
--- a/rust/datafusion/src/execution/filter.rs
+++ b/rust/datafusion/src/execution/filter.rs
@@ -22,7 +22,8 @@ use std::rc::Rc;
use std::sync::Arc;
use arrow::array::*;
-use arrow::datatypes::{DataType, Schema};
+use arrow::compute::array_ops::filter;
+use arrow::datatypes::Schema;
use arrow::record_batch::RecordBatch;
use super::error::{ExecutionError, Result};
@@ -61,7 +62,12 @@ impl Relation for FilterRelation {
Some(filter_bools) => {
let filtered_columns: Result<Vec<ArrayRef>> = (0..batch
.num_columns())
- .map(|i| filter(batch.column(i), &filter_bools))
+ .map(|i| {
+ match filter(batch.column(i).as_ref(), &filter_bools) {
+ Ok(result) => Ok(result),
+ Err(error) => Err(ExecutionError::from(error)),
+ }
+ })
.collect();
let filtered_batch: RecordBatch = RecordBatch::new(
@@ -84,129 +90,3 @@ impl Relation for FilterRelation {
&self.schema
}
}
-
-//TODO: move into Arrow array_ops
-fn filter(array: &Arc<Array>, filter: &BooleanArray) -> Result<ArrayRef> {
- let a = array.as_ref();
-
- //TODO use macros
- match a.data_type() {
- DataType::UInt8 => {
- let b = a.as_any().downcast_ref::<UInt8Array>().unwrap();
- let mut builder = UInt8Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::UInt16 => {
- let b = a.as_any().downcast_ref::<UInt16Array>().unwrap();
- let mut builder = UInt16Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::UInt32 => {
- let b = a.as_any().downcast_ref::<UInt32Array>().unwrap();
- let mut builder = UInt32Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::UInt64 => {
- let b = a.as_any().downcast_ref::<UInt64Array>().unwrap();
- let mut builder = UInt64Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int8 => {
- let b = a.as_any().downcast_ref::<Int8Array>().unwrap();
- let mut builder = Int8Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int16 => {
- let b = a.as_any().downcast_ref::<Int16Array>().unwrap();
- let mut builder = Int16Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int32 => {
- let b = a.as_any().downcast_ref::<Int32Array>().unwrap();
- let mut builder = Int32Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int64 => {
- let b = a.as_any().downcast_ref::<Int64Array>().unwrap();
- let mut builder = Int64Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Float32 => {
- let b = a.as_any().downcast_ref::<Float32Array>().unwrap();
- let mut builder = Float32Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Float64 => {
- let b = a.as_any().downcast_ref::<Float64Array>().unwrap();
- let mut builder = Float64Array::builder(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- builder.append_value(b.value(i))?;
- }
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Utf8 => {
- //TODO: this is inefficient and we should improve the Arrow impl to help make
- // this more concise
- let b = a.as_any().downcast_ref::<BinaryArray>().unwrap();
- let mut values: Vec<String> = Vec::with_capacity(b.len());
- for i in 0..b.len() {
- if filter.value(i) {
- values.push(b.get_string(i));
- }
- }
- let tmp: Vec<&str> = values.iter().map(|s| s.as_str()).collect();
- Ok(Arc::new(BinaryArray::from(tmp)))
- }
- other => Err(ExecutionError::ExecutionError(format!(
- "filter not supported for {:?}",
- other
- ))),
- }
-}
diff --git a/rust/datafusion/src/execution/limit.rs b/rust/datafusion/src/execution/limit.rs
index d6258d63db..888fac5045 100644
--- a/rust/datafusion/src/execution/limit.rs
+++ b/rust/datafusion/src/execution/limit.rs
@@ -22,7 +22,8 @@ use std::rc::Rc;
use std::sync::Arc;
use arrow::array::*;
-use arrow::datatypes::{DataType, Schema};
+use arrow::compute::array_ops::limit;
+use arrow::datatypes::Schema;
use arrow::record_batch::RecordBatch;
use super::error::{ExecutionError, Result};
@@ -58,7 +59,10 @@ impl Relation for LimitRelation {
if batch.num_rows() >= capacity {
let limited_columns: Result<Vec<ArrayRef>> = (0..batch.num_columns())
- .map(|i| limit(batch.column(i).as_ref(), capacity))
+ .map(|i| match limit(batch.column(i).as_ref(), capacity) {
+ Ok(result) => Ok(result),
+ Err(error) => Err(ExecutionError::from(error)),
+ })
.collect();
let limited_batch: RecordBatch =
@@ -79,104 +83,3 @@ impl Relation for LimitRelation {
&self.schema
}
}
-
-//TODO: move into Arrow array_ops
-fn limit(a: &Array, num_rows_to_read: usize) -> Result<ArrayRef> {
- //TODO use macros
- match a.data_type() {
- DataType::UInt8 => {
- let b = a.as_any().downcast_ref::<UInt8Array>().unwrap();
- let mut builder = UInt8Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::UInt16 => {
- let b = a.as_any().downcast_ref::<UInt16Array>().unwrap();
- let mut builder = UInt16Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::UInt32 => {
- let b = a.as_any().downcast_ref::<UInt32Array>().unwrap();
- let mut builder = UInt32Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::UInt64 => {
- let b = a.as_any().downcast_ref::<UInt64Array>().unwrap();
- let mut builder = UInt64Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int8 => {
- let b = a.as_any().downcast_ref::<Int8Array>().unwrap();
- let mut builder = Int8Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int16 => {
- let b = a.as_any().downcast_ref::<Int16Array>().unwrap();
- let mut builder = Int16Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int32 => {
- let b = a.as_any().downcast_ref::<Int32Array>().unwrap();
- let mut builder = Int32Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Int64 => {
- let b = a.as_any().downcast_ref::<Int64Array>().unwrap();
- let mut builder = Int64Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Float32 => {
- let b = a.as_any().downcast_ref::<Float32Array>().unwrap();
- let mut builder = Float32Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Float64 => {
- let b = a.as_any().downcast_ref::<Float64Array>().unwrap();
- let mut builder = Float64Array::builder(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- builder.append_value(b.value(i as usize))?;
- }
- Ok(Arc::new(builder.finish()))
- }
- DataType::Utf8 => {
- //TODO: this is inefficient and we should improve the Arrow impl to help make this more concise
- let b = a.as_any().downcast_ref::<BinaryArray>().unwrap();
- let mut values: Vec<String> = Vec::with_capacity(num_rows_to_read as usize);
- for i in 0..num_rows_to_read {
- values.push(b.get_string(i as usize));
- }
- let tmp: Vec<&str> = values.iter().map(|s| s.as_str()).collect();
- Ok(Arc::new(BinaryArray::from(tmp)))
- }
- other => Err(ExecutionError::ExecutionError(format!(
- "filter not supported for {:?}",
- other
- ))),
- }
-}