Skip to content

Commit 9b20bfd

Browse files
committed
Implement removeIntermediate method for CentralMoment, Correlation, Covariance, and Regression accumulators to handle state removal
1 parent 2a748b1 commit 9b20bfd

4 files changed

Lines changed: 181 additions & 4 deletions

File tree

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/aggregation/CentralMomentAccumulator.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,66 @@ public void outputFinal(ColumnBuilder columnBuilder) {
198198

199199
@Override
200200
public void removeIntermediate(Column[] input) {
201-
throw new UnsupportedOperationException();
201+
checkArgument(input.length == 1, "Input of CentralMoment should be 1");
202+
if (input[0].isNull(0)) {
203+
return;
204+
}
205+
206+
byte[] bytes = input[0].getBinary(0).getValues();
207+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
208+
209+
long nB = buffer.getLong();
210+
if (nB == 0) {
211+
return;
212+
}
213+
checkArgument(count >= nB, "CentralMoment state count is smaller than removed state count");
214+
215+
if (count == nB) {
216+
reset();
217+
return;
218+
}
219+
220+
double meanB = buffer.getDouble();
221+
double m2B = buffer.getDouble();
222+
double m3B = buffer.getDouble();
223+
double m4B = buffer.getDouble();
224+
225+
long nTotal = count;
226+
long nA = nTotal - nB;
227+
228+
double meanA = ((double) nTotal * mean - (double) nB * meanB) / nA;
229+
230+
double delta = meanB - meanA;
231+
double delta2 = delta * delta;
232+
double delta3 = delta * delta2;
233+
double delta4 = delta2 * delta2;
234+
235+
double m2A = m2 - m2B - delta2 * nA * nB / nTotal;
236+
double m3A =
237+
m3
238+
- m3B
239+
- delta3 * nA * nB * (nA - nB) / ((double) nTotal * nTotal)
240+
- 3.0 * delta * (nA * m2B - nB * m2A) / nTotal;
241+
242+
double m4A =
243+
m4
244+
- m4B
245+
- delta4
246+
* nA
247+
* nB
248+
* ((double) nA * nA - (double) nA * nB + (double) nB * nB)
249+
/ ((double) nTotal * nTotal * nTotal)
250+
- 6.0
251+
* delta2
252+
* ((double) nA * nA * m2B + (double) nB * nB * m2A)
253+
/ ((double) nTotal * nTotal)
254+
- 4.0 * delta * (nA * m3B - nB * m3A) / nTotal;
255+
256+
count = nA;
257+
mean = meanA;
258+
m2 = m2A;
259+
m3 = m3A;
260+
m4 = m4A;
202261
}
203262

204263
@Override

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/aggregation/CorrelationAccumulator.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,49 @@ public void outputFinal(ColumnBuilder columnBuilder) {
187187

188188
@Override
189189
public void removeIntermediate(Column[] input) {
190+
checkArgument(input.length == 1, "Input of Correlation should be 1");
191+
if (input[0].isNull(0)) {
192+
return;
193+
}
194+
195+
byte[] bytes = input[0].getBinary(0).getValues();
196+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
197+
198+
long otherCount = buffer.getLong();
199+
if (otherCount == 0) {
200+
return;
201+
}
202+
checkArgument(
203+
count >= otherCount, "Correlation state count is smaller than removed state count");
204+
205+
if (count == otherCount) {
206+
reset();
207+
return;
208+
}
190209

191-
throw new UnsupportedOperationException("Remove not implemented for Correlation");
210+
double otherMeanX = buffer.getDouble();
211+
double otherMeanY = buffer.getDouble();
212+
double otherM2X = buffer.getDouble();
213+
double otherM2Y = buffer.getDouble();
214+
double otherC2 = buffer.getDouble();
215+
216+
long totalCount = count;
217+
long newCount = totalCount - otherCount;
218+
219+
double newMeanX = (totalCount * meanX - otherCount * otherMeanX) / newCount;
220+
double newMeanY = (totalCount * meanY - otherCount * otherMeanY) / newCount;
221+
222+
double deltaX = otherMeanX - newMeanX;
223+
double deltaY = otherMeanY - newMeanY;
224+
double correction = ((double) newCount * otherCount) / totalCount;
225+
226+
c2 = c2 - otherC2 - deltaX * deltaY * correction;
227+
m2X = m2X - otherM2X - deltaX * deltaX * correction;
228+
m2Y = m2Y - otherM2Y - deltaY * deltaY * correction;
229+
230+
meanX = newMeanX;
231+
meanY = newMeanY;
232+
count = newCount;
192233
}
193234

194235
@Override

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/aggregation/CovarianceAccumulator.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,44 @@ public void outputFinal(ColumnBuilder columnBuilder) {
174174

175175
@Override
176176
public void removeIntermediate(Column[] input) {
177-
throw new UnsupportedOperationException("Remove not implemented for Covariance");
177+
checkArgument(input.length == 1, "Input of Covariance should be 1");
178+
if (input[0].isNull(0)) {
179+
return;
180+
}
181+
182+
byte[] bytes = input[0].getBinary(0).getValues();
183+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
184+
185+
long otherCount = buffer.getLong();
186+
if (otherCount == 0) {
187+
return;
188+
}
189+
checkArgument(
190+
count >= otherCount, "Covariance state count is smaller than removed state count");
191+
192+
if (count == otherCount) {
193+
reset();
194+
return;
195+
}
196+
197+
double otherMeanX = buffer.getDouble();
198+
double otherMeanY = buffer.getDouble();
199+
double otherC2 = buffer.getDouble();
200+
201+
long totalCount = count;
202+
long newCount = totalCount - otherCount;
203+
204+
double newMeanX = ((double) totalCount * meanX - (double) otherCount * otherMeanX) / newCount;
205+
double newMeanY = ((double) totalCount * meanY - (double) otherCount * otherMeanY) / newCount;
206+
207+
double deltaX = otherMeanX - newMeanX;
208+
double deltaY = otherMeanY - newMeanY;
209+
double correction = ((double) newCount * otherCount) / totalCount;
210+
211+
c2 = c2 - otherC2 - deltaX * deltaY * correction;
212+
meanX = newMeanX;
213+
meanY = newMeanY;
214+
count = newCount;
178215
}
179216

180217
@Override

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/aggregation/RegressionAccumulator.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,47 @@ public void outputFinal(ColumnBuilder columnBuilder) {
183183

184184
@Override
185185
public void removeIntermediate(Column[] input) {
186-
throw new UnsupportedOperationException();
186+
checkArgument(input.length == 1, "Input of Regression should be 1");
187+
if (input[0].isNull(0)) {
188+
return;
189+
}
190+
191+
byte[] bytes = input[0].getBinary(0).getValues();
192+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
193+
194+
long otherCount = buffer.getLong();
195+
if (otherCount == 0) {
196+
return;
197+
}
198+
checkArgument(
199+
count >= otherCount, "Regression state count is smaller than removed state count");
200+
201+
if (count == otherCount) {
202+
reset();
203+
return;
204+
}
205+
206+
double otherMeanX = buffer.getDouble();
207+
double otherMeanY = buffer.getDouble();
208+
double otherM2X = buffer.getDouble();
209+
double otherC2 = buffer.getDouble();
210+
211+
long totalCount = count;
212+
long newCount = totalCount - otherCount;
213+
214+
double newMeanX = ((double) totalCount * meanX - (double) otherCount * otherMeanX) / newCount;
215+
double newMeanY = ((double) totalCount * meanY - (double) otherCount * otherMeanY) / newCount;
216+
217+
double deltaX = otherMeanX - newMeanX;
218+
double deltaY = otherMeanY - newMeanY;
219+
double correction = ((double) newCount * otherCount) / totalCount;
220+
221+
c2 = c2 - otherC2 - deltaX * deltaY * correction;
222+
m2X = m2X - otherM2X - deltaX * deltaX * correction;
223+
224+
meanX = newMeanX;
225+
meanY = newMeanY;
226+
count = newCount;
187227
}
188228

189229
@Override

0 commit comments

Comments
 (0)