Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP use into iter to minimize array concats #579

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions datafusion/src/physical_plan/expressions/nth_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::error::{DataFusionError, Result};
use crate::physical_plan::{window_functions::BuiltInWindowFunctionExpr, PhysicalExpr};
use crate::scalar::ScalarValue;
use arrow::array::{new_empty_array, new_null_array, ArrayRef};
use arrow::array::{new_null_array, ArrayRef};
use arrow::datatypes::{DataType, Field};
use std::any::Any;
use std::sync::Arc;
Expand Down Expand Up @@ -127,19 +127,19 @@ impl BuiltInWindowFunctionExpr for NthValue {
value.len()
)));
}
if num_rows == 0 {
return Ok(new_empty_array(value.data_type()));
}
assert!(num_rows > 0, "Impossibly got empty values");
let index: usize = match self.kind {
NthValueKind::First => 0,
NthValueKind::Last => (num_rows as usize) - 1,
NthValueKind::Nth(n) => (n as usize) - 1,
};

Ok(if index >= num_rows {
new_null_array(value.data_type(), num_rows)
let data_type: &DataType = value.data_type();
new_null_array(data_type, num_rows)
} else {
let value = ScalarValue::try_from_array(value, index)?;
value.to_array_of_size(num_rows)
let scalar = ScalarValue::try_from_array(value, num_rows)?;
scalar.to_array_of_size(num_rows)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/src/physical_plan/expressions/row_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl BuiltInWindowFunctionExpr for RowNumber {

fn evaluate(&self, num_rows: usize, _values: &[ArrayRef]) -> Result<ArrayRef> {
Ok(Arc::new(UInt64Array::from_iter_values(
(1..num_rows + 1).map(|i| i as u64),
1..(num_rows as u64) + 1,
)))
}
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/src/physical_plan/window_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
//!
//! see also https://www.postgresql.org/docs/current/functions-window.html

use crate::arrow::array::ArrayRef;
use crate::arrow::datatypes::Field;
use crate::error::{DataFusionError, Result};
use crate::physical_plan::{
aggregates, aggregates::AggregateFunction, functions::Signature,
type_coercion::data_types, PhysicalExpr,
};
use arrow::array::ArrayRef;
use arrow::datatypes::DataType;
use std::any::Any;
use std::sync::Arc;
Expand Down
40 changes: 24 additions & 16 deletions datafusion/src/physical_plan/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::physical_plan::{
Accumulator, AggregateExpr, Distribution, ExecutionPlan, Partitioning, PhysicalExpr,
RecordBatchStream, SendableRecordBatchStream, WindowExpr,
};
use crate::scalar::ScalarValue;
use arrow::compute::concat;
use arrow::{
array::ArrayRef,
Expand All @@ -42,6 +43,7 @@ use futures::Future;
use pin_project_lite::pin_project;
use std::any::Any;
use std::convert::TryInto;
use std::iter;
use std::ops::Range;
use std::pin::Pin;
use std::sync::Arc;
Expand Down Expand Up @@ -187,11 +189,13 @@ impl WindowExpr for BuiltInWindowExpr {
.collect::<Vec<_>>();
self.window.evaluate(len, &values)
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.collect::<Vec<ArrayRef>>();
let results = results.iter().map(|i| i.as_ref()).collect::<Vec<_>>();
concat(&results).map_err(DataFusionError::ArrowError)
.collect::<Result<Vec<_>>>()?;
if results.len() == 1 {
Ok(results[0].clone())
} else {
let results = results.iter().map(|i| i.as_ref()).collect::<Vec<_>>();
concat(&results).map_err(DataFusionError::ArrowError)
}
}
}

Expand Down Expand Up @@ -246,21 +250,25 @@ impl AggregateWindowExpr {
let values = self.evaluate_args(batch)?;
let results = partition_points
.iter()
.map(|partition_range| {
.map::<Result<_>, _>(|partition_range| {
let sort_partition_points =
find_ranges_in_range(partition_range, &sort_partition_points);
let mut window_accumulators = self.create_accumulator()?;
sort_partition_points
let result = sort_partition_points
.iter()
.map(|range| window_accumulators.scan_peers(&values, range))
.collect::<Result<Vec<_>>>()
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten();
ScalarValue::iter_to_array(result)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dandandan i find that changing to use this, even if would be saving ArrayRef creating, makes the performance much worse, because it changes from iterating trusted len iterator to this boxed type with unbounded untrusted iterator.

})
.collect::<Result<Vec<Vec<ArrayRef>>>>()?
.into_iter()
.flatten()
.collect::<Vec<ArrayRef>>();
let results = results.iter().map(|i| i.as_ref()).collect::<Vec<_>>();
concat(&results).map_err(DataFusionError::ArrowError)
.collect::<Result<Vec<_>>>()?;
if results.len() == 1 {
Ok(results[0].clone())
} else {
let results = results.iter().map(|i| i.as_ref()).collect::<Vec<_>>();
concat(&results).map_err(DataFusionError::ArrowError)
}
}

fn group_based_evaluate(&self, _batch: &RecordBatch) -> Result<ArrayRef> {
Expand Down Expand Up @@ -328,7 +336,7 @@ impl AggregateWindowAccumulator {
&mut self,
values: &[ArrayRef],
value_range: &Range<usize>,
) -> Result<ArrayRef> {
) -> Result<impl IntoIterator<Item = ScalarValue>> {
if value_range.is_empty() {
return Err(DataFusionError::Internal(
"Value range cannot be empty".to_owned(),
Expand All @@ -341,7 +349,7 @@ impl AggregateWindowAccumulator {
.collect::<Vec<_>>();
self.accumulator.update_batch(&values)?;
let value = self.accumulator.evaluate()?;
Ok(value.to_array_of_size(len))
Ok(iter::repeat(value).take(len))
}
}

Expand Down