Skip to content

Commit

Permalink
Improve performance of collapse, from @akurmustafa
Browse files Browse the repository at this point in the history
alamb#26

fix formatting
  • Loading branch information
akurmustafa authored and alamb committed Jan 7, 2025
1 parent d2fe8a4 commit a44acfd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions datafusion-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions datafusion/physical-expr-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ arrow = { workspace = true }
datafusion-common = { workspace = true, default-features = true }
datafusion-expr-common = { workspace = true }
hashbrown = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
15 changes: 12 additions & 3 deletions datafusion/physical-expr-common/src/sort_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use arrow::datatypes::Schema;
use arrow::record_batch::RecordBatch;
use datafusion_common::Result;
use datafusion_expr_common::columnar_value::ColumnarValue;
use itertools::Itertools;
use indexmap::IndexSet;
use itertools::{izip, Itertools};

/// Represents Sort operation for a column in a RecordBatch
///
Expand Down Expand Up @@ -568,11 +569,19 @@ impl LexRequirement {
/// `vec![a Some(ASC)]`.
pub fn collapse(self) -> Self {
let mut output = Vec::<PhysicalSortRequirement>::new();
let mut exprs = IndexSet::new();
let mut reqs = vec![];
for item in self {
if !output.iter().any(|req| req.expr.eq(&item.expr)) {
output.push(item);
let PhysicalSortRequirement { expr, options: req } = item;
// new insertion
if exprs.insert(expr) {
reqs.push(req);
}
}
debug_assert_eq!(reqs.len(), exprs.len());
for (expr, req) in izip!(exprs, reqs) {
output.push(PhysicalSortRequirement::new(expr, req));
}
LexRequirement::new(output)
}
}
Expand Down

0 comments on commit a44acfd

Please sign in to comment.