Skip to content
Merged
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
23 changes: 4 additions & 19 deletions datafusion/src/physical_plan/expressions/average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,8 @@ impl Accumulator for AvgAccumulator {
Ok(vec![ScalarValue::from(self.count), self.sum.clone()])
}

fn update(&mut self, values: &[ScalarValue]) -> Result<()> {
let values = &values[0];

self.count += (!values.is_null()) as u64;
self.sum = sum::sum(&self.sum, values)?;

Ok(())
fn update(&mut self, _values: &[ScalarValue]) -> Result<()> {
unimplemented!("update_batch is implemented instead");
}

fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
Expand All @@ -188,18 +183,8 @@ impl Accumulator for AvgAccumulator {
Ok(())
}

fn merge(&mut self, states: &[ScalarValue]) -> Result<()> {
let count = &states[0];
// counts are summed
if let ScalarValue::UInt64(Some(c)) = count {
self.count += c
} else {
unreachable!()
};

// sums are summed
self.sum = sum::sum(&self.sum, &states[1])?;
Ok(())
fn merge(&mut self, _states: &[ScalarValue]) -> Result<()> {
unimplemented!("merge_batch is implemented instead");
}

fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
Expand Down
18 changes: 4 additions & 14 deletions datafusion/src/physical_plan/expressions/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,12 @@ impl Accumulator for CountAccumulator {
Ok(())
}

fn update(&mut self, values: &[ScalarValue]) -> Result<()> {
let value = &values[0];
if !value.is_null() {
self.count += 1;
}
Ok(())
fn update(&mut self, _values: &[ScalarValue]) -> Result<()> {
unimplemented!("update_batch is implemented instead");
}

fn merge(&mut self, states: &[ScalarValue]) -> Result<()> {
let count = &states[0];
if let ScalarValue::UInt64(Some(delta)) = count {
self.count += *delta;
} else {
unreachable!()
}
Ok(())
fn merge(&mut self, _states: &[ScalarValue]) -> Result<()> {
unimplemented!("merge_batch is implemented instead");
}

fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
Expand Down
11 changes: 4 additions & 7 deletions datafusion/src/physical_plan/expressions/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,8 @@ impl Accumulator for SumAccumulator {
Ok(vec![self.sum.clone()])
}

fn update(&mut self, values: &[ScalarValue]) -> Result<()> {
// sum(v1, v2, v3) = v1 + v2 + v3
self.sum = sum(&self.sum, &values[0])?;
Ok(())
fn update(&mut self, _values: &[ScalarValue]) -> Result<()> {
unimplemented!("update_batch is implemented instead");
}

fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
Expand All @@ -365,9 +363,8 @@ impl Accumulator for SumAccumulator {
Ok(())
}

fn merge(&mut self, states: &[ScalarValue]) -> Result<()> {
// sum(sum1, sum2) = sum1 + sum2
self.update(states)
fn merge(&mut self, _states: &[ScalarValue]) -> Result<()> {
unimplemented!("merge_batch is implemented instead");
}

fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
Expand Down
118 changes: 4 additions & 114 deletions datafusion/src/physical_plan/expressions/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,122 +302,12 @@ impl Accumulator for VarianceAccumulator {
Ok(())
}

fn update(&mut self, values: &[ScalarValue]) -> Result<()> {
let values = &values[0];
let is_empty = values.is_null();
let mean = ScalarValue::from(self.mean);
let m2 = ScalarValue::from(self.m2);

if !is_empty {
let new_count = self.count + 1;
let delta1 = ScalarValue::add(values, &mean.arithmetic_negate())?;
let new_mean = ScalarValue::add(
&ScalarValue::div(&delta1, &ScalarValue::from(new_count as f64))?,
&mean,
)?;
let delta2 = ScalarValue::add(values, &new_mean.arithmetic_negate())?;
let tmp = ScalarValue::mul(&delta1, &delta2)?;

let new_m2 = ScalarValue::add(&m2, &tmp)?;
self.count += 1;

if let ScalarValue::Float64(Some(c)) = new_mean {
self.mean = c;
} else {
unreachable!()
};
if let ScalarValue::Float64(Some(m)) = new_m2 {
self.m2 = m;
} else {
unreachable!()
};
}

Ok(())
fn update(&mut self, _values: &[ScalarValue]) -> Result<()> {
unimplemented!("update_batch is implemented instead");
}

fn merge(&mut self, states: &[ScalarValue]) -> Result<()> {
let count;
let mean;
let m2;
let mut new_count: u64 = self.count;

if let ScalarValue::UInt64(Some(c)) = states[0] {
count = c;
} else {
unreachable!()
};

if count == 0_u64 {
return Ok(());
}

if let ScalarValue::Float64(Some(m)) = states[1] {
mean = m;
} else {
unreachable!()
};
if let ScalarValue::Float64(Some(n)) = states[2] {
m2 = n;
} else {
unreachable!()
};

if self.count == 0 {
self.count = count;
self.mean = mean;
self.m2 = m2;
return Ok(());
}

new_count += count;

let mean1 = ScalarValue::from(self.mean);
let mean2 = ScalarValue::from(mean);

let new_mean = ScalarValue::add(
&ScalarValue::div(
&ScalarValue::mul(&mean1, &ScalarValue::from(self.count))?,
&ScalarValue::from(new_count as f64),
)?,
&ScalarValue::div(
&ScalarValue::mul(&mean2, &ScalarValue::from(count))?,
&ScalarValue::from(new_count as f64),
)?,
)?;

let delta = ScalarValue::add(&mean2.arithmetic_negate(), &mean1)?;
let delta_sqrt = ScalarValue::mul(&delta, &delta)?;
let new_m2 = ScalarValue::add(
&ScalarValue::add(
&ScalarValue::mul(
&delta_sqrt,
&ScalarValue::div(
&ScalarValue::mul(
&ScalarValue::from(self.count),
&ScalarValue::from(count),
)?,
&ScalarValue::from(new_count as f64),
)?,
)?,
&ScalarValue::from(self.m2),
)?,
&ScalarValue::from(m2),
)?;

self.count = new_count;
if let ScalarValue::Float64(Some(c)) = new_mean {
self.mean = c;
} else {
unreachable!()
};
if let ScalarValue::Float64(Some(m)) = new_m2 {
self.m2 = m;
} else {
unreachable!()
};

Ok(())
fn merge(&mut self, _states: &[ScalarValue]) -> Result<()> {
unimplemented!("merge_batch is implemented instead");
}

fn evaluate(&self) -> Result<ScalarValue> {
Expand Down
Loading