SabrinaZhaozyf commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r955295521


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/customobject/CovarianceTuple.java:
##########
@@ -0,0 +1,121 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.pinot.segment.local.customobject;
+
+import java.nio.ByteBuffer;
+import javax.annotation.Nonnull;
+
+
+/**
+ * Intermediate state used by CovarianceAggregationFunction which helps 
calculate
+ * population covariance and sample covariance
+ */
+public class CovarianceTuple implements Comparable<CovarianceTuple> {
+
+  private double _sumX;
+  private double _sumY;
+  private double _sumXY;
+  private long _count;
+
+  public CovarianceTuple(double sumX, double sumY, double sumXY, long count) {
+    _sumX = sumX;
+    _sumY = sumY;
+    _sumXY = sumXY;
+    _count = count;
+  }
+
+  public void apply(double sumX, double sumY, double sumXY, long count) {
+    _sumX += sumX;
+    _sumY += sumY;
+    _sumXY += sumXY;
+    _count += count;
+  }
+
+  public void apply(@Nonnull CovarianceTuple covarianceTuple) {
+    _sumX += covarianceTuple._sumX;
+    _sumY += covarianceTuple._sumY;
+    _sumXY += covarianceTuple._sumXY;
+    _count += covarianceTuple._count;
+  }
+
+  public double getSumX() {
+    return _sumX;
+  }
+
+  public double getSumY() {
+    return _sumY;
+  }
+
+  public double getSumXY() {
+    return _sumXY;
+  }
+
+  public long getCount() {
+    return _count;
+  }
+
+  @Nonnull
+  public byte[] toBytes() {
+    ByteBuffer byteBuffer = ByteBuffer.allocate(Double.BYTES + Double.BYTES + 
Double.BYTES + Long.BYTES);
+    byteBuffer.putDouble(_sumX);
+    byteBuffer.putDouble(_sumY);
+    byteBuffer.putDouble(_sumXY);
+    byteBuffer.putLong(_count);
+    return byteBuffer.array();
+  }
+
+  @Nonnull
+  public static CovarianceTuple fromBytes(byte[] bytes) {
+    return fromByteBuffer(ByteBuffer.wrap(bytes));
+  }
+
+  @Nonnull
+  public static CovarianceTuple fromByteBuffer(ByteBuffer byteBuffer) {
+    return new CovarianceTuple(byteBuffer.getDouble(), byteBuffer.getDouble(), 
byteBuffer.getDouble(),
+        byteBuffer.getLong());
+  }
+
+  @Override
+  public int compareTo(@Nonnull CovarianceTuple covarianceTuple) {

Review Comment:
   I will leave this for now. We can revisit this if needed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to