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
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,15 @@ public static void mergeVarianceState(VarianceState state, VarianceState otherSt
if (count == 0) {
return;
}
if (state.getCount() == 0) {
state.setCount(count);
state.setMean(mean);
state.setM2(m2);
return;
}
long newCount = count + state.getCount();
double newMean = ((count * mean) + (state.getCount() * state.getMean())) / (double) newCount;
double delta = mean - state.getMean();
double newMean = state.getMean() + delta / newCount * count;
state.setM2(state.getM2() + m2 + delta * delta * count * state.getCount() / (double) newCount);
state.setCount(newCount);
state.setMean(newMean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,30 @@ public void testVariance()
assertQuery("SELECT VAR_SAMP(totalprice) FROM (SELECT totalprice FROM orders LIMIT 0) T");
}

@Test
public void testZeroVarianceSamp()
{
assertQuery("SELECT VAR_SAMP(6523763181031200) FROM LINEITEM");
}

@Test
public void testZeroVariancePop()
{
assertQuery("SELECT VAR_POP(6523763181031200) FROM LINEITEM");
}

@Test
public void testZeroStddevSamp()
{
assertQuery("SELECT STDDEV_SAMP(6523763181031200) FROM LINEITEM");
}

@Test
public void testZeroStddevPop()
{
assertQuery("SELECT STDDEV_POP(6523763181031200) FROM LINEITEM");
}

@Test
public void testVariancePop()
{
Expand Down