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

refactor: make sum_distinct not to have update and merge #5474

Merged
merged 1 commit into from
Mar 4, 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
58 changes: 24 additions & 34 deletions datafusion/physical-expr/src/aggregate/sum_distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,6 @@ impl DistinctSumAccumulator {
data_type: data_type.clone(),
})
}

fn update(&mut self, values: &[ScalarValue]) -> Result<()> {
values.iter().for_each(|v| {
// If the value is NULL, it is not included in the final sum.
if !v.is_null() {
self.hash_values.insert(v.clone());
}
});

Ok(())
}

fn merge(&mut self, states: &[ScalarValue]) -> Result<()> {
if states.is_empty() {
return Ok(());
}

states.iter().try_for_each(|state| match state {
ScalarValue::List(Some(values), _) => self.update(values.as_ref()),
_ => Err(DataFusionError::Internal(format!(
"Unexpected accumulator state {state:?}"
))),
})
}
}

impl Accumulator for DistinctSumAccumulator {
Expand All @@ -147,23 +123,37 @@ impl Accumulator for DistinctSumAccumulator {
return Ok(());
}

let scalar_values = (0..values[0].len())
.map(|index| ScalarValue::try_from_array(&values[0], index))
.collect::<Result<Vec<_>>>()?;
self.update(&scalar_values)
let arr = &values[0];
(0..values[0].len()).try_for_each(|index| {
if !arr.is_null(index) {
let v = ScalarValue::try_from_array(arr, index)?;
self.hash_values.insert(v);
}
Ok(())
})
}

fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
if states.is_empty() {
return Ok(());
}

(0..states[0].len()).try_for_each(|index| {
let v = states
.iter()
.map(|array| ScalarValue::try_from_array(array, index))
.collect::<Result<Vec<_>>>()?;
self.merge(&v)
let arr = &states[0];
(0..arr.len()).try_for_each(|index| {
let scalar = ScalarValue::try_from_array(arr, index)?;

if let ScalarValue::List(Some(scalar), _) = scalar {
scalar.iter().for_each(|scalar| {
if !ScalarValue::is_null(scalar) {
self.hash_values.insert(scalar.clone());
}
});
} else {
return Err(DataFusionError::Internal(
"Unexpected accumulator state".into(),
));
}
Ok(())
})
}

Expand Down