Skip to content

Optimize count_distinct.size #5377

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

Merged
merged 6 commits into from
Feb 28, 2023
Merged
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
59 changes: 42 additions & 17 deletions datafusion/physical-expr/src/aggregate/count_distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,43 @@ impl DistinctCountAccumulator {
self.update(&row_values)
})
}

// calculating the size for fixed length values, taking first batch size * number of batches
// This method is faster than .full_size(), however it is not suitable for variable length values like strings or complex types
fn fixed_size(&self) -> usize {
std::mem::size_of_val(self)
+ (std::mem::size_of::<DistinctScalarValues>() * self.values.capacity())
+ self
.values
.iter()
.next()
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 that is nice

.map(|vals| {
(ScalarValue::size_of_vec(&vals.0) - std::mem::size_of_val(&vals.0))
* self.values.capacity()
})
.unwrap_or(0)
}

// calculates the size as accurate as possible, call to this method is expensive
fn full_size(&self) -> usize {
std::mem::size_of_val(self)
+ (std::mem::size_of::<DistinctScalarValues>() * self.values.capacity())
+ self
.values
.iter()
.map(|vals| {
ScalarValue::size_of_vec(&vals.0) - std::mem::size_of_val(&vals.0)
})
.sum::<usize>()
+ (std::mem::size_of::<DataType>() * self.state_data_types.capacity())
+ self
.state_data_types
.iter()
.map(|dt| dt.size() - std::mem::size_of_val(dt))
.sum::<usize>()
+ self.count_data_type.size()
- std::mem::size_of_val(&self.count_data_type)
}
}

impl Accumulator for DistinctCountAccumulator {
Expand Down Expand Up @@ -216,23 +253,11 @@ impl Accumulator for DistinctCountAccumulator {
}

fn size(&self) -> usize {
std::mem::size_of_val(self)
+ (std::mem::size_of::<DistinctScalarValues>() * self.values.capacity())
+ self
.values
.iter()
.map(|vals| {
ScalarValue::size_of_vec(&vals.0) - std::mem::size_of_val(&vals.0)
})
.sum::<usize>()
+ (std::mem::size_of::<DataType>() * self.state_data_types.capacity())
+ self
.state_data_types
.iter()
.map(|dt| dt.size() - std::mem::size_of_val(dt))
.sum::<usize>()
+ self.count_data_type.size()
- std::mem::size_of_val(&self.count_data_type)
if self.count_data_type.is_primitive() {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

self.fixed_size()
} else {
self.full_size()
}
}
}

Expand Down