You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2022/08/17 22:55:46 UTC
[GitHub] [pinot] SabrinaZhaozyf opened a new pull request, #9236: [WIP] Add Support for Covariance Function
SabrinaZhaozyf opened a new pull request, #9236:
URL: https://github.com/apache/pinot/pull/9236
Label: `feature`
This PR adds support for COV_POP function to pinot.
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] amrishlal commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
amrishlal commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954116254
##########
pinot-core/src/test/java/org/apache/pinot/queries/BaseQueriesTest.java:
##########
@@ -146,14 +160,21 @@ private BrokerResponseNative getBrokerResponse(String query, PlanMaker planMaker
/**
* Run query on multiple index segments with custom plan maker.
* <p>Use this to test the whole flow from server to broker.
- * <p>The result should be equivalent to querying 4 identical index segments.
+ * <p>Unless explicitly override getDistinctInstances or initialize 2 distinct index segments in test, the result
+ * should be equivalent to querying 4 identical index segments. Otherwise, the result will be equivalent to
+ * querying 2 distinct instances each having 2 segments.
*/
private BrokerResponseNative getBrokerResponse(PinotQuery pinotQuery, PlanMaker planMaker) {
PinotQuery serverPinotQuery = GapfillUtils.stripGapfill(pinotQuery);
QueryContext queryContext = QueryContextConverterUtils.getQueryContext(pinotQuery);
QueryContext serverQueryContext =
serverPinotQuery == pinotQuery ? queryContext : QueryContextConverterUtils.getQueryContext(serverPinotQuery);
+ List<List<IndexSegment>> instances = getDistinctInstances();
+ if (instances.size() == 2) {
+ return getBrokerResponseDistinctInstances(pinotQuery, planMaker);
+ }
+
Review Comment:
This appears to be hardcoding how a query will be executed when there are two segments. If I understand correctly there are two problems here: 1) the result is being duplicated which is causing problem with covariance values, and 2) ability to run queries against multiple segment. I ran into the same issue while testing a feature that I am working on. The first problem can be solved by adding a boolean variable for not duplicating the query results. The second (executing queries against multiple segments can be solved by local setup in the `CovarianceQueriesTest.java`. I will share the code with you offline. Let's see if we can do this without hardcoding for 2 segments.
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on PR #9236:
URL: https://github.com/apache/pinot/pull/9236#issuecomment-1224910533
> Can you please clarify or add a link in the description to the formula you are using to calculate covariance? I don't see "average value" being used in your calculation? It will also be good to add support for "covariance of sample" (which is probably more commonly used) along with "covariance of population"
Yes. This should be simple if we extend `CovarianceAggregationFunction.java` and add Bessel's correction to `extract result`. We probably want to add that @SabrinaZhaozyf ?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] amrishlal commented on pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
amrishlal commented on PR #9236:
URL: https://github.com/apache/pinot/pull/9236#issuecomment-1224884718
Can you please clarify or add a link in the description to the formula you are using to calculate covariance? I don't see "average value" being used in your calculation? It will also be good to add support for "covariance of sample" (which is probably more commonly used) along with "covariance of population"
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] SabrinaZhaozyf commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
SabrinaZhaozyf commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954368611
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
Review Comment:
Done
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] siddharthteotia commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r953361062
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
Review Comment:
This should be changed if we are planning to use the same function/class for both population based and sample based
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] siddharthteotia commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r953361062
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
Review Comment:
This should be changed if we are planning to use the same function for both population based and sample based
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r952958168
##########
pinot-core/src/test/java/org/apache/pinot/queries/CovarianceQueriesTest.java:
##########
@@ -0,0 +1,444 @@
+/**
+ * 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.queries;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.math3.stat.correlation.Covariance;
+import org.apache.commons.math3.util.Precision;
+import org.apache.pinot.common.response.broker.BrokerResponseNative;
+import org.apache.pinot.common.response.broker.ResultTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.operator.blocks.IntermediateResultsBlock;
+import org.apache.pinot.core.operator.query.AggregationGroupByOrderByOperator;
+import org.apache.pinot.core.operator.query.AggregationOperator;
+import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader;
+import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.spi.ImmutableSegment;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.utils.ReadMode;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+
+/**
+ * Queries test for covariance queries.
+ */
+public class CovarianceQueriesTest extends BaseQueriesTest {
+ private static final File INDEX_DIR = new File(FileUtils.getTempDirectory(), "CovarianceQueriesTest");
+ private static final String RAW_TABLE_NAME = "testTable";
+ private static final String SEGMENT_NAME = "testSegment";
+
+ // test segments 1-4 evenly divide testSegment into 4 distinct segments
+ private static final String SEGMENT_NAME_1 = "testSegment1";
+ private static final String SEGMENT_NAME_2 = "testSegment2";
+ private static final String SEGMENT_NAME_3 = "testSegment3";
+ private static final String SEGMENT_NAME_4 = "testSegment4";
+
+ private static final int NUM_RECORDS = 2000;
+ private static final int NUM_GROUPS = 10;
+ private static final int MAX_VALUE = 500;
+ private static final double RELATIVE_EPSILON = 0.0001;
+ private static final double DELTA = 0.0001;
+
+ private static final String INT_COLUMN_X = "intColumnX";
+ private static final String INT_COLUMN_Y = "intColumnY";
+ private static final String DOUBLE_COLUMN_X = "doubleColumnX";
+ private static final String DOUBLE_COLUMN_Y = "doubleColumnY";
+ private static final String LONG_COLUMN = "longColumn";
+ private static final String FLOAT_COLUMN = "floatColumn";
+ private static final String GROUP_BY_COLUMN = "groupByColumn";
+
+ private static final Schema SCHEMA =
+ new Schema.SchemaBuilder().addSingleValueDimension(INT_COLUMN_X, FieldSpec.DataType.INT)
+ .addSingleValueDimension(INT_COLUMN_Y, FieldSpec.DataType.INT)
+ .addSingleValueDimension(DOUBLE_COLUMN_X, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(DOUBLE_COLUMN_Y, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(LONG_COLUMN, FieldSpec.DataType.LONG)
+ .addSingleValueDimension(FLOAT_COLUMN, FieldSpec.DataType.FLOAT)
+ .addSingleValueDimension(GROUP_BY_COLUMN, FieldSpec.DataType.DOUBLE).build();
+ private static final TableConfig TABLE_CONFIG =
+ new TableConfigBuilder(TableType.OFFLINE).setTableName(RAW_TABLE_NAME).build();
+
+ private IndexSegment _indexSegment;
+ private List<IndexSegment> _indexSegments;
+ private List<List<IndexSegment>> _instances;
+ private int _sumIntX = 0;
+ private int _sumIntY = 0;
+ private int _sumIntXY = 0;
+
+ private double _sumDoubleX = 0;
+ private double _sumDoubleY = 0;
+ private double _sumDoubleXY = 0;
+
+ private long _sumLong = 0L;
+ private double _sumFloat = 0;
+
+ private double _sumIntDouble = 0;
+ private long _sumIntLong = 0L;
+ private double _sumIntFloat = 0;
+ private double _sumDoubleLong = 0;
+ private double _sumDoubleFloat = 0;
+ private double _sumLongFloat = 0;
+
+ private double _expectedCovIntXY;
+ private double _expectedCovDoubleXY;
+ private double _expectedCovIntDouble;
+ private double _expectedCovIntLong;
+ private double _expectedCovIntFloat;
+ private double _expectedCovDoubleLong;
+ private double _expectedCovDoubleFloat;
+ private double _expectedCovLongFloat;
+
+ private double _expectedCovWithFilter;
+
+ private CovarianceTuple[] _expectedGroupByResultVer1 = new CovarianceTuple[NUM_GROUPS];
+ private CovarianceTuple[] _expectedGroupByResultVer2 = new CovarianceTuple[NUM_GROUPS];
+ private double[] _expectedFinalResultVer1 = new double[NUM_GROUPS];
+ private double[] _expectedFinalResultVer2 = new double[NUM_GROUPS];
+
+ @Override
+ protected String getFilter() {
+ // filter out half of the rows based on group id
+ return " WHERE groupByColumn < " + (NUM_GROUPS / 2);
+ }
+
+ @Override
+ protected IndexSegment getIndexSegment() {
+ return _indexSegment;
+ }
+
+ @Override
+ protected List<IndexSegment> getIndexSegments() {
+ return _indexSegments;
+ }
+
+ @BeforeClass
+ public void setUp()
+ throws Exception {
+ FileUtils.deleteDirectory(INDEX_DIR);
+
+ List<GenericRow> records = new ArrayList<>(NUM_RECORDS);
+
+ Random rand = new Random();
+ int[] intColX = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ int[] intColY = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColX = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColY = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ long[] longCol = rand.longs(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] floatCol = new double[NUM_RECORDS];
+ double[] groupByCol = new double[NUM_RECORDS];
+
+ int groupSize = NUM_RECORDS / NUM_GROUPS;
+ double sumX = 0;
+ double sumY = 0;
+ double sumGroupBy = 0;
+ double sumXY = 0;
+ double sumXGroupBy = 0;
+ int groupByVal = 0;
+
+ for (int i = 0; i < NUM_RECORDS; i++) {
+ GenericRow record = new GenericRow();
+ int intX = intColX[i];
+ int intY = intColY[i];
+ double doubleX = doubleColX[i];
+ double doubleY = doubleColY[i];
+ long longVal = longCol[i];
+ float floatVal = -MAX_VALUE + rand.nextFloat() * 2 * MAX_VALUE;
+
+ // set up inner segment group by results
+ groupByVal = (int) Math.floor(i / groupSize);
+ if (i % groupSize == 0 && groupByVal > 0) {
+ _expectedGroupByResultVer1[groupByVal - 1] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal - 1] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+ sumX = 0;
+ sumY = 0;
+ sumGroupBy = 0;
+ sumXY = 0;
+ sumXGroupBy = 0;
+ }
+
+ sumX += doubleX;
+ sumY += doubleY;
+ sumGroupBy += groupByVal;
+ sumXY += doubleX * doubleY;
+ sumXGroupBy += doubleX * groupByVal;
+
+ floatCol[i] = floatVal;
+ groupByCol[i] = groupByVal;
+
+ // calculate inner segment results
+ _sumIntX += intX;
+ _sumIntY += intY;
+ _sumDoubleX += doubleX;
+ _sumDoubleY += doubleY;
+ _sumLong += longVal;
+ _sumFloat += floatVal;
+ _sumIntXY += intX * intY;
+ _sumDoubleXY += doubleX * doubleY;
+ _sumIntDouble += intX * doubleX;
+ _sumIntLong += intX * longVal;
+ _sumIntFloat += intX * floatCol[i];
+ _sumDoubleLong += doubleX * longVal;
+ _sumDoubleFloat += doubleX * floatCol[i];
+ _sumLongFloat += longVal * floatCol[i];
+
+ record.putValue(INT_COLUMN_X, intX);
+ record.putValue(INT_COLUMN_Y, intY);
+ record.putValue(DOUBLE_COLUMN_X, doubleX);
+ record.putValue(DOUBLE_COLUMN_Y, doubleY);
+ record.putValue(LONG_COLUMN, longVal);
+ record.putValue(FLOAT_COLUMN, floatVal);
+ record.putValue(GROUP_BY_COLUMN, groupByVal);
+ records.add(record);
+ }
+ _expectedGroupByResultVer1[groupByVal] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+
+ // calculate inter segment result
+ Covariance cov = new Covariance();
+ double[] newIntColX = Arrays.stream(intColX).asDoubleStream().toArray();
+ double[] newIntColY = Arrays.stream(intColY).asDoubleStream().toArray();
+ double[] newLongCol = Arrays.stream(longCol).asDoubleStream().toArray();
+ _expectedCovIntXY = cov.covariance(newIntColX, newIntColY, false);
+ _expectedCovDoubleXY = cov.covariance(doubleColX, doubleColY, false);
+ _expectedCovIntDouble = cov.covariance(newIntColX, doubleColX, false);
+ _expectedCovIntLong = cov.covariance(newIntColX, newLongCol, false);
+ _expectedCovIntFloat = cov.covariance(newIntColX, floatCol, false);
+ _expectedCovDoubleLong = cov.covariance(doubleColX, newLongCol, false);
+ _expectedCovDoubleFloat = cov.covariance(doubleColX, floatCol, false);
+ _expectedCovLongFloat = cov.covariance(newLongCol, floatCol, false);
+
+ double[] filteredX = Arrays.copyOfRange(doubleColX, 0, NUM_RECORDS / 2);
+ double[] filteredY = Arrays.copyOfRange(doubleColY, 0, NUM_RECORDS / 2);
+ _expectedCovWithFilter = cov.covariance(filteredX, filteredY, false);
+
+ // calculate inter segment group by results
+ for (int i = 0; i < NUM_GROUPS; i++) {
+ double[] colX = Arrays.copyOfRange(doubleColX, i * groupSize, (i + 1) * groupSize);
+ double[] colGroupBy = Arrays.copyOfRange(groupByCol, i * groupSize, (i + 1) * groupSize);
+ double[] colY = Arrays.copyOfRange(doubleColY, i * groupSize, (i + 1) * groupSize);
+ _expectedFinalResultVer1[i] = cov.covariance(colX, colGroupBy, false);
+ _expectedFinalResultVer2[i] = cov.covariance(colX, colY, false);
+ }
+
+ // generate testSegment
+ ImmutableSegment immutableSegment = setUpSingleSegment(records, SEGMENT_NAME);
+ _indexSegment = immutableSegment;
+ _indexSegments = Arrays.asList(immutableSegment, immutableSegment);
+
+ // divide testSegment into 4 distinct segments for distinct inter segment tests
+ // by doing so, we can avoid calculating global covariance again
+ _instances = new ArrayList<>();
Review Comment:
We can probably create an
1. add function `getDistinctInstances` in baseQueriesTest and pass this in
2. or add a new code path in `getBrokerResponse`: if we pass 4 segments in `getIndexSegments`, run with two instances using (1, 2) (3, 4) (combining `getBrokerResponseDistinctInstance` with `getBrokerResponse`)
discussed offline
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954440149
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
`(sumX / count) * (sumY / count)` can be rewritten as `sumX * sumY / (count * count)` which also avoid another divide. but I am no expert in JIT compiler so not sure if it would be smart enough to optimize that
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954154389
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
Review Comment:
+1 also add ref here for https://en.wikipedia.org/wiki/Bessel%27s_correction
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r953101634
##########
pinot-core/src/test/java/org/apache/pinot/queries/CovarianceQueriesTest.java:
##########
@@ -0,0 +1,444 @@
+/**
+ * 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.queries;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.math3.stat.correlation.Covariance;
+import org.apache.commons.math3.util.Precision;
+import org.apache.pinot.common.response.broker.BrokerResponseNative;
+import org.apache.pinot.common.response.broker.ResultTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.operator.blocks.IntermediateResultsBlock;
+import org.apache.pinot.core.operator.query.AggregationGroupByOrderByOperator;
+import org.apache.pinot.core.operator.query.AggregationOperator;
+import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader;
+import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.spi.ImmutableSegment;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.utils.ReadMode;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+
+/**
+ * Queries test for covariance queries.
+ */
+public class CovarianceQueriesTest extends BaseQueriesTest {
+ private static final File INDEX_DIR = new File(FileUtils.getTempDirectory(), "CovarianceQueriesTest");
+ private static final String RAW_TABLE_NAME = "testTable";
+ private static final String SEGMENT_NAME = "testSegment";
+
+ // test segments 1-4 evenly divide testSegment into 4 distinct segments
+ private static final String SEGMENT_NAME_1 = "testSegment1";
+ private static final String SEGMENT_NAME_2 = "testSegment2";
+ private static final String SEGMENT_NAME_3 = "testSegment3";
+ private static final String SEGMENT_NAME_4 = "testSegment4";
+
+ private static final int NUM_RECORDS = 2000;
+ private static final int NUM_GROUPS = 10;
+ private static final int MAX_VALUE = 500;
+ private static final double RELATIVE_EPSILON = 0.0001;
+ private static final double DELTA = 0.0001;
+
+ private static final String INT_COLUMN_X = "intColumnX";
+ private static final String INT_COLUMN_Y = "intColumnY";
+ private static final String DOUBLE_COLUMN_X = "doubleColumnX";
+ private static final String DOUBLE_COLUMN_Y = "doubleColumnY";
+ private static final String LONG_COLUMN = "longColumn";
+ private static final String FLOAT_COLUMN = "floatColumn";
+ private static final String GROUP_BY_COLUMN = "groupByColumn";
+
+ private static final Schema SCHEMA =
+ new Schema.SchemaBuilder().addSingleValueDimension(INT_COLUMN_X, FieldSpec.DataType.INT)
+ .addSingleValueDimension(INT_COLUMN_Y, FieldSpec.DataType.INT)
+ .addSingleValueDimension(DOUBLE_COLUMN_X, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(DOUBLE_COLUMN_Y, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(LONG_COLUMN, FieldSpec.DataType.LONG)
+ .addSingleValueDimension(FLOAT_COLUMN, FieldSpec.DataType.FLOAT)
+ .addSingleValueDimension(GROUP_BY_COLUMN, FieldSpec.DataType.DOUBLE).build();
+ private static final TableConfig TABLE_CONFIG =
+ new TableConfigBuilder(TableType.OFFLINE).setTableName(RAW_TABLE_NAME).build();
+
+ private IndexSegment _indexSegment;
+ private List<IndexSegment> _indexSegments;
+ private List<List<IndexSegment>> _instances;
+ private int _sumIntX = 0;
+ private int _sumIntY = 0;
+ private int _sumIntXY = 0;
+
+ private double _sumDoubleX = 0;
+ private double _sumDoubleY = 0;
+ private double _sumDoubleXY = 0;
+
+ private long _sumLong = 0L;
+ private double _sumFloat = 0;
+
+ private double _sumIntDouble = 0;
+ private long _sumIntLong = 0L;
+ private double _sumIntFloat = 0;
+ private double _sumDoubleLong = 0;
+ private double _sumDoubleFloat = 0;
+ private double _sumLongFloat = 0;
+
+ private double _expectedCovIntXY;
+ private double _expectedCovDoubleXY;
+ private double _expectedCovIntDouble;
+ private double _expectedCovIntLong;
+ private double _expectedCovIntFloat;
+ private double _expectedCovDoubleLong;
+ private double _expectedCovDoubleFloat;
+ private double _expectedCovLongFloat;
+
+ private double _expectedCovWithFilter;
+
+ private CovarianceTuple[] _expectedGroupByResultVer1 = new CovarianceTuple[NUM_GROUPS];
+ private CovarianceTuple[] _expectedGroupByResultVer2 = new CovarianceTuple[NUM_GROUPS];
+ private double[] _expectedFinalResultVer1 = new double[NUM_GROUPS];
+ private double[] _expectedFinalResultVer2 = new double[NUM_GROUPS];
+
+ @Override
+ protected String getFilter() {
+ // filter out half of the rows based on group id
+ return " WHERE groupByColumn < " + (NUM_GROUPS / 2);
+ }
+
+ @Override
+ protected IndexSegment getIndexSegment() {
+ return _indexSegment;
+ }
+
+ @Override
+ protected List<IndexSegment> getIndexSegments() {
+ return _indexSegments;
+ }
+
+ @BeforeClass
+ public void setUp()
+ throws Exception {
+ FileUtils.deleteDirectory(INDEX_DIR);
+
+ List<GenericRow> records = new ArrayList<>(NUM_RECORDS);
+
+ Random rand = new Random();
+ int[] intColX = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ int[] intColY = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColX = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColY = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ long[] longCol = rand.longs(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] floatCol = new double[NUM_RECORDS];
+ double[] groupByCol = new double[NUM_RECORDS];
+
+ int groupSize = NUM_RECORDS / NUM_GROUPS;
+ double sumX = 0;
+ double sumY = 0;
+ double sumGroupBy = 0;
+ double sumXY = 0;
+ double sumXGroupBy = 0;
+ int groupByVal = 0;
+
+ for (int i = 0; i < NUM_RECORDS; i++) {
+ GenericRow record = new GenericRow();
+ int intX = intColX[i];
+ int intY = intColY[i];
+ double doubleX = doubleColX[i];
+ double doubleY = doubleColY[i];
+ long longVal = longCol[i];
+ float floatVal = -MAX_VALUE + rand.nextFloat() * 2 * MAX_VALUE;
+
+ // set up inner segment group by results
+ groupByVal = (int) Math.floor(i / groupSize);
+ if (i % groupSize == 0 && groupByVal > 0) {
+ _expectedGroupByResultVer1[groupByVal - 1] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal - 1] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+ sumX = 0;
+ sumY = 0;
+ sumGroupBy = 0;
+ sumXY = 0;
+ sumXGroupBy = 0;
+ }
+
+ sumX += doubleX;
+ sumY += doubleY;
+ sumGroupBy += groupByVal;
+ sumXY += doubleX * doubleY;
+ sumXGroupBy += doubleX * groupByVal;
+
+ floatCol[i] = floatVal;
+ groupByCol[i] = groupByVal;
+
+ // calculate inner segment results
+ _sumIntX += intX;
+ _sumIntY += intY;
+ _sumDoubleX += doubleX;
+ _sumDoubleY += doubleY;
+ _sumLong += longVal;
+ _sumFloat += floatVal;
+ _sumIntXY += intX * intY;
+ _sumDoubleXY += doubleX * doubleY;
+ _sumIntDouble += intX * doubleX;
+ _sumIntLong += intX * longVal;
+ _sumIntFloat += intX * floatCol[i];
+ _sumDoubleLong += doubleX * longVal;
+ _sumDoubleFloat += doubleX * floatCol[i];
+ _sumLongFloat += longVal * floatCol[i];
+
+ record.putValue(INT_COLUMN_X, intX);
+ record.putValue(INT_COLUMN_Y, intY);
+ record.putValue(DOUBLE_COLUMN_X, doubleX);
+ record.putValue(DOUBLE_COLUMN_Y, doubleY);
+ record.putValue(LONG_COLUMN, longVal);
+ record.putValue(FLOAT_COLUMN, floatVal);
+ record.putValue(GROUP_BY_COLUMN, groupByVal);
+ records.add(record);
+ }
+ _expectedGroupByResultVer1[groupByVal] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+
+ // calculate inter segment result
+ Covariance cov = new Covariance();
+ double[] newIntColX = Arrays.stream(intColX).asDoubleStream().toArray();
+ double[] newIntColY = Arrays.stream(intColY).asDoubleStream().toArray();
+ double[] newLongCol = Arrays.stream(longCol).asDoubleStream().toArray();
+ _expectedCovIntXY = cov.covariance(newIntColX, newIntColY, false);
+ _expectedCovDoubleXY = cov.covariance(doubleColX, doubleColY, false);
+ _expectedCovIntDouble = cov.covariance(newIntColX, doubleColX, false);
+ _expectedCovIntLong = cov.covariance(newIntColX, newLongCol, false);
+ _expectedCovIntFloat = cov.covariance(newIntColX, floatCol, false);
+ _expectedCovDoubleLong = cov.covariance(doubleColX, newLongCol, false);
+ _expectedCovDoubleFloat = cov.covariance(doubleColX, floatCol, false);
+ _expectedCovLongFloat = cov.covariance(newLongCol, floatCol, false);
+
+ double[] filteredX = Arrays.copyOfRange(doubleColX, 0, NUM_RECORDS / 2);
+ double[] filteredY = Arrays.copyOfRange(doubleColY, 0, NUM_RECORDS / 2);
+ _expectedCovWithFilter = cov.covariance(filteredX, filteredY, false);
+
+ // calculate inter segment group by results
+ for (int i = 0; i < NUM_GROUPS; i++) {
+ double[] colX = Arrays.copyOfRange(doubleColX, i * groupSize, (i + 1) * groupSize);
+ double[] colGroupBy = Arrays.copyOfRange(groupByCol, i * groupSize, (i + 1) * groupSize);
+ double[] colY = Arrays.copyOfRange(doubleColY, i * groupSize, (i + 1) * groupSize);
+ _expectedFinalResultVer1[i] = cov.covariance(colX, colGroupBy, false);
+ _expectedFinalResultVer2[i] = cov.covariance(colX, colY, false);
+ }
+
+ // generate testSegment
+ ImmutableSegment immutableSegment = setUpSingleSegment(records, SEGMENT_NAME);
+ _indexSegment = immutableSegment;
+ _indexSegments = Arrays.asList(immutableSegment, immutableSegment);
+
+ // divide testSegment into 4 distinct segments for distinct inter segment tests
+ // by doing so, we can avoid calculating global covariance again
+ _instances = new ArrayList<>();
+ int segmentSize = NUM_RECORDS / 4;
+ ImmutableSegment immutableSegment1 = setUpSingleSegment(records.subList(0, segmentSize), SEGMENT_NAME_1);
+ ImmutableSegment immutableSegment2 =
+ setUpSingleSegment(records.subList(segmentSize, segmentSize * 2), SEGMENT_NAME_2);
+ ImmutableSegment immutableSegment3 =
+ setUpSingleSegment(records.subList(segmentSize * 2, segmentSize * 3), SEGMENT_NAME_3);
+ ImmutableSegment immutableSegment4 =
+ setUpSingleSegment(records.subList(segmentSize * 3, NUM_RECORDS), SEGMENT_NAME_4);
+ // generate 2 instances each with 2 distinct segments
+ _instances.add(Arrays.asList(immutableSegment1, immutableSegment2));
+ _instances.add(Arrays.asList(immutableSegment3, immutableSegment4));
+ }
+
+ private ImmutableSegment setUpSingleSegment(List<GenericRow> recordSet, String segmentName)
+ throws Exception {
+ SegmentGeneratorConfig segmentGeneratorConfig = new SegmentGeneratorConfig(TABLE_CONFIG, SCHEMA);
+ segmentGeneratorConfig.setTableName(RAW_TABLE_NAME);
+ segmentGeneratorConfig.setSegmentName(segmentName);
+ segmentGeneratorConfig.setOutDir(INDEX_DIR.getPath());
+
+ SegmentIndexCreationDriverImpl driver = new SegmentIndexCreationDriverImpl();
+ driver.init(segmentGeneratorConfig, new GenericRowRecordReader(recordSet));
+ driver.build();
+
+ ImmutableSegment immutableSegment = ImmutableSegmentLoader.load(new File(INDEX_DIR, segmentName), ReadMode.mmap);
+ return immutableSegment;
+ }
+
+ @Test
+ public void testAggregationOnly() {
+ // Inner Segment
+ String query = "SELECT COV_POP(intColumnX, intColumnY), COV_POP(doubleColumnX, doubleColumnY), COV_POP(intColumnX, "
+ + "doubleColumnX), " + "COV_POP(intColumnX, longColumn), COV_POP(intColumnX, floatColumn), "
+ + "COV_POP(doubleColumnX, longColumn), COV_POP(doubleColumnX, floatColumn), COV_POP(longColumn, "
+ + "floatColumn) FROM testTable";
Review Comment:
standard syntax for covariance is `COVAR_POP` see
* https://calcite.apache.org/docs/reference.html#aggregate-functions
* https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions033.htm
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] amrishlal commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
amrishlal commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954095008
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
Review Comment:
Can we add the following comment (or something similar) here?
```
// Covariance between two random variables X and Y is defined as either:
// cov(X,Y) = E[(X - E[X]) * (Y - EY)] or
// cov(X,Y) = E[X*Y] - E[X]*E[Y],
// where E[X] represents mean or expected value of X (https://en.wikipedia.org/wiki/Covariance). The
// calculations here are based on the second definition shown above.
```
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] amrishlal commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
amrishlal commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954095008
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
Review Comment:
Can we add the following comment (or something similar) here?
```
// Covariance between two random variables X and Y is defined as either:
// cov(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
// cov(X,Y) = E[X*Y] - E[X]*E[Y],
// where E[X] represents mean or expected value of X (https://en.wikipedia.org/wiki/Covariance). The
// calculations here are based on the second definition shown above.
```
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954356571
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
This part should actually be `(sumXY / divBase) - (sumX / count) * (sumY / divBase)` as the the `sample_covariance = popCov * besselCorrection = popCov * count/(count-1)`. We did the multiplication then divide to provide better readability and avoid this confusion. IMO it should be fine as we only pay a little price once in final reduction?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r953207193
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -199,6 +201,9 @@ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
double sumX = covarianceTuple.getSumX();
double sumY = covarianceTuple.getSumY();
double sumXY = covarianceTuple.getSumXY();
+ if (_isSample) {
+ return (sumXY / (count - 1)) - (sumX / (count - 1)) * (sumY / (count - 1));
Review Comment:
Explicitly use Bessel's correction
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954370975
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
yes. try to avoid divide if possible since this is on the critical path
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] siddharthteotia commented on pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on PR #9236:
URL: https://github.com/apache/pinot/pull/9236#issuecomment-1224309660
Please update the PR description with a brief note on what the new function is about, what it does, how to use etc.
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954320248
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
```suggestion
if (_isSample) {
return (sumXY / (count - 1)) - (sumX / count) * (sumY / (count - 1));
} else {
return (sumXY / count) - (sumX / count) * (sumY / count);
}
}
```
Let's avoid the multiplication then divide
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] siddharthteotia commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954414122
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
Can we also take care of divide by zero error ? May be add a test where denominator is 0.0 and result is NaN
https://stackoverflow.com/questions/14137989/java-division-by-zero-doesnt-throw-an-arithmeticexception-why
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] siddharthteotia commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r953361062
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
Review Comment:
This should be changed if we are planning to use the same function/classs for both population based and sample based
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] SabrinaZhaozyf commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] siddharthteotia commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r952850506
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/customobject/CovarianceTuple.java:
##########
@@ -0,0 +1,117 @@
+/**
+ * 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;
+
+
+public class CovarianceTuple implements Comparable<CovarianceTuple> {
Review Comment:
(nit) javadoc please
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954131730
##########
pinot-core/src/test/java/org/apache/pinot/queries/BaseQueriesTest.java:
##########
@@ -146,14 +160,21 @@ private BrokerResponseNative getBrokerResponse(String query, PlanMaker planMaker
/**
* Run query on multiple index segments with custom plan maker.
* <p>Use this to test the whole flow from server to broker.
- * <p>The result should be equivalent to querying 4 identical index segments.
+ * <p>Unless explicitly override getDistinctInstances or initialize 2 distinct index segments in test, the result
+ * should be equivalent to querying 4 identical index segments. Otherwise, the result will be equivalent to
+ * querying 2 distinct instances each having 2 segments.
*/
private BrokerResponseNative getBrokerResponse(PinotQuery pinotQuery, PlanMaker planMaker) {
PinotQuery serverPinotQuery = GapfillUtils.stripGapfill(pinotQuery);
QueryContext queryContext = QueryContextConverterUtils.getQueryContext(pinotQuery);
QueryContext serverQueryContext =
serverPinotQuery == pinotQuery ? queryContext : QueryContextConverterUtils.getQueryContext(serverPinotQuery);
+ List<List<IndexSegment>> instances = getDistinctInstances();
+ if (instances.size() == 2) {
+ return getBrokerResponseDistinctInstances(pinotQuery, planMaker);
+ }
+
Review Comment:
@amrishlal 1) causes problem for all non-additive statistical values, e.g. average, standard deviation, covariance, median, percentile ... for these values the result querying 4 identical segments is no different from querying one single segment, as can be potential problematic for full functional testing. For 2) can you elaborate more? I think the code currently can run on multiple segments, but people are just setting two identical segments?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r952958168
##########
pinot-core/src/test/java/org/apache/pinot/queries/CovarianceQueriesTest.java:
##########
@@ -0,0 +1,444 @@
+/**
+ * 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.queries;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.math3.stat.correlation.Covariance;
+import org.apache.commons.math3.util.Precision;
+import org.apache.pinot.common.response.broker.BrokerResponseNative;
+import org.apache.pinot.common.response.broker.ResultTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.operator.blocks.IntermediateResultsBlock;
+import org.apache.pinot.core.operator.query.AggregationGroupByOrderByOperator;
+import org.apache.pinot.core.operator.query.AggregationOperator;
+import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader;
+import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.spi.ImmutableSegment;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.utils.ReadMode;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+
+/**
+ * Queries test for covariance queries.
+ */
+public class CovarianceQueriesTest extends BaseQueriesTest {
+ private static final File INDEX_DIR = new File(FileUtils.getTempDirectory(), "CovarianceQueriesTest");
+ private static final String RAW_TABLE_NAME = "testTable";
+ private static final String SEGMENT_NAME = "testSegment";
+
+ // test segments 1-4 evenly divide testSegment into 4 distinct segments
+ private static final String SEGMENT_NAME_1 = "testSegment1";
+ private static final String SEGMENT_NAME_2 = "testSegment2";
+ private static final String SEGMENT_NAME_3 = "testSegment3";
+ private static final String SEGMENT_NAME_4 = "testSegment4";
+
+ private static final int NUM_RECORDS = 2000;
+ private static final int NUM_GROUPS = 10;
+ private static final int MAX_VALUE = 500;
+ private static final double RELATIVE_EPSILON = 0.0001;
+ private static final double DELTA = 0.0001;
+
+ private static final String INT_COLUMN_X = "intColumnX";
+ private static final String INT_COLUMN_Y = "intColumnY";
+ private static final String DOUBLE_COLUMN_X = "doubleColumnX";
+ private static final String DOUBLE_COLUMN_Y = "doubleColumnY";
+ private static final String LONG_COLUMN = "longColumn";
+ private static final String FLOAT_COLUMN = "floatColumn";
+ private static final String GROUP_BY_COLUMN = "groupByColumn";
+
+ private static final Schema SCHEMA =
+ new Schema.SchemaBuilder().addSingleValueDimension(INT_COLUMN_X, FieldSpec.DataType.INT)
+ .addSingleValueDimension(INT_COLUMN_Y, FieldSpec.DataType.INT)
+ .addSingleValueDimension(DOUBLE_COLUMN_X, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(DOUBLE_COLUMN_Y, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(LONG_COLUMN, FieldSpec.DataType.LONG)
+ .addSingleValueDimension(FLOAT_COLUMN, FieldSpec.DataType.FLOAT)
+ .addSingleValueDimension(GROUP_BY_COLUMN, FieldSpec.DataType.DOUBLE).build();
+ private static final TableConfig TABLE_CONFIG =
+ new TableConfigBuilder(TableType.OFFLINE).setTableName(RAW_TABLE_NAME).build();
+
+ private IndexSegment _indexSegment;
+ private List<IndexSegment> _indexSegments;
+ private List<List<IndexSegment>> _instances;
+ private int _sumIntX = 0;
+ private int _sumIntY = 0;
+ private int _sumIntXY = 0;
+
+ private double _sumDoubleX = 0;
+ private double _sumDoubleY = 0;
+ private double _sumDoubleXY = 0;
+
+ private long _sumLong = 0L;
+ private double _sumFloat = 0;
+
+ private double _sumIntDouble = 0;
+ private long _sumIntLong = 0L;
+ private double _sumIntFloat = 0;
+ private double _sumDoubleLong = 0;
+ private double _sumDoubleFloat = 0;
+ private double _sumLongFloat = 0;
+
+ private double _expectedCovIntXY;
+ private double _expectedCovDoubleXY;
+ private double _expectedCovIntDouble;
+ private double _expectedCovIntLong;
+ private double _expectedCovIntFloat;
+ private double _expectedCovDoubleLong;
+ private double _expectedCovDoubleFloat;
+ private double _expectedCovLongFloat;
+
+ private double _expectedCovWithFilter;
+
+ private CovarianceTuple[] _expectedGroupByResultVer1 = new CovarianceTuple[NUM_GROUPS];
+ private CovarianceTuple[] _expectedGroupByResultVer2 = new CovarianceTuple[NUM_GROUPS];
+ private double[] _expectedFinalResultVer1 = new double[NUM_GROUPS];
+ private double[] _expectedFinalResultVer2 = new double[NUM_GROUPS];
+
+ @Override
+ protected String getFilter() {
+ // filter out half of the rows based on group id
+ return " WHERE groupByColumn < " + (NUM_GROUPS / 2);
+ }
+
+ @Override
+ protected IndexSegment getIndexSegment() {
+ return _indexSegment;
+ }
+
+ @Override
+ protected List<IndexSegment> getIndexSegments() {
+ return _indexSegments;
+ }
+
+ @BeforeClass
+ public void setUp()
+ throws Exception {
+ FileUtils.deleteDirectory(INDEX_DIR);
+
+ List<GenericRow> records = new ArrayList<>(NUM_RECORDS);
+
+ Random rand = new Random();
+ int[] intColX = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ int[] intColY = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColX = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColY = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ long[] longCol = rand.longs(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] floatCol = new double[NUM_RECORDS];
+ double[] groupByCol = new double[NUM_RECORDS];
+
+ int groupSize = NUM_RECORDS / NUM_GROUPS;
+ double sumX = 0;
+ double sumY = 0;
+ double sumGroupBy = 0;
+ double sumXY = 0;
+ double sumXGroupBy = 0;
+ int groupByVal = 0;
+
+ for (int i = 0; i < NUM_RECORDS; i++) {
+ GenericRow record = new GenericRow();
+ int intX = intColX[i];
+ int intY = intColY[i];
+ double doubleX = doubleColX[i];
+ double doubleY = doubleColY[i];
+ long longVal = longCol[i];
+ float floatVal = -MAX_VALUE + rand.nextFloat() * 2 * MAX_VALUE;
+
+ // set up inner segment group by results
+ groupByVal = (int) Math.floor(i / groupSize);
+ if (i % groupSize == 0 && groupByVal > 0) {
+ _expectedGroupByResultVer1[groupByVal - 1] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal - 1] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+ sumX = 0;
+ sumY = 0;
+ sumGroupBy = 0;
+ sumXY = 0;
+ sumXGroupBy = 0;
+ }
+
+ sumX += doubleX;
+ sumY += doubleY;
+ sumGroupBy += groupByVal;
+ sumXY += doubleX * doubleY;
+ sumXGroupBy += doubleX * groupByVal;
+
+ floatCol[i] = floatVal;
+ groupByCol[i] = groupByVal;
+
+ // calculate inner segment results
+ _sumIntX += intX;
+ _sumIntY += intY;
+ _sumDoubleX += doubleX;
+ _sumDoubleY += doubleY;
+ _sumLong += longVal;
+ _sumFloat += floatVal;
+ _sumIntXY += intX * intY;
+ _sumDoubleXY += doubleX * doubleY;
+ _sumIntDouble += intX * doubleX;
+ _sumIntLong += intX * longVal;
+ _sumIntFloat += intX * floatCol[i];
+ _sumDoubleLong += doubleX * longVal;
+ _sumDoubleFloat += doubleX * floatCol[i];
+ _sumLongFloat += longVal * floatCol[i];
+
+ record.putValue(INT_COLUMN_X, intX);
+ record.putValue(INT_COLUMN_Y, intY);
+ record.putValue(DOUBLE_COLUMN_X, doubleX);
+ record.putValue(DOUBLE_COLUMN_Y, doubleY);
+ record.putValue(LONG_COLUMN, longVal);
+ record.putValue(FLOAT_COLUMN, floatVal);
+ record.putValue(GROUP_BY_COLUMN, groupByVal);
+ records.add(record);
+ }
+ _expectedGroupByResultVer1[groupByVal] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+
+ // calculate inter segment result
+ Covariance cov = new Covariance();
+ double[] newIntColX = Arrays.stream(intColX).asDoubleStream().toArray();
+ double[] newIntColY = Arrays.stream(intColY).asDoubleStream().toArray();
+ double[] newLongCol = Arrays.stream(longCol).asDoubleStream().toArray();
+ _expectedCovIntXY = cov.covariance(newIntColX, newIntColY, false);
+ _expectedCovDoubleXY = cov.covariance(doubleColX, doubleColY, false);
+ _expectedCovIntDouble = cov.covariance(newIntColX, doubleColX, false);
+ _expectedCovIntLong = cov.covariance(newIntColX, newLongCol, false);
+ _expectedCovIntFloat = cov.covariance(newIntColX, floatCol, false);
+ _expectedCovDoubleLong = cov.covariance(doubleColX, newLongCol, false);
+ _expectedCovDoubleFloat = cov.covariance(doubleColX, floatCol, false);
+ _expectedCovLongFloat = cov.covariance(newLongCol, floatCol, false);
+
+ double[] filteredX = Arrays.copyOfRange(doubleColX, 0, NUM_RECORDS / 2);
+ double[] filteredY = Arrays.copyOfRange(doubleColY, 0, NUM_RECORDS / 2);
+ _expectedCovWithFilter = cov.covariance(filteredX, filteredY, false);
+
+ // calculate inter segment group by results
+ for (int i = 0; i < NUM_GROUPS; i++) {
+ double[] colX = Arrays.copyOfRange(doubleColX, i * groupSize, (i + 1) * groupSize);
+ double[] colGroupBy = Arrays.copyOfRange(groupByCol, i * groupSize, (i + 1) * groupSize);
+ double[] colY = Arrays.copyOfRange(doubleColY, i * groupSize, (i + 1) * groupSize);
+ _expectedFinalResultVer1[i] = cov.covariance(colX, colGroupBy, false);
+ _expectedFinalResultVer2[i] = cov.covariance(colX, colY, false);
+ }
+
+ // generate testSegment
+ ImmutableSegment immutableSegment = setUpSingleSegment(records, SEGMENT_NAME);
+ _indexSegment = immutableSegment;
+ _indexSegments = Arrays.asList(immutableSegment, immutableSegment);
+
+ // divide testSegment into 4 distinct segments for distinct inter segment tests
+ // by doing so, we can avoid calculating global covariance again
+ _instances = new ArrayList<>();
Review Comment:
We can probably create an
1. add function `getDistinctInstances` in baseQueriesTest and pass this in
2. or add a new code path in `getBrokerResponse` if we pass 4 segments in `getIndexSegments` (combining `getBrokerResponseDistinctInstance` with `getBrokerResponse`)
discussed offline
##########
pinot-core/src/test/java/org/apache/pinot/queries/BaseQueriesTest.java:
##########
@@ -197,4 +197,70 @@ protected BrokerResponseNative getBrokerResponseForOptimizedQuery(String query,
OPTIMIZER.optimize(pinotQuery, config, schema);
return getBrokerResponse(pinotQuery, PLAN_MAKER);
}
+
Review Comment:
Can you add java doc here calling out that future aggregation functions on non-additive(?) stats should use this function to test? Also mention the result will be on how many distinct segments?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r953207193
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -199,6 +201,9 @@ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
double sumX = covarianceTuple.getSumX();
double sumY = covarianceTuple.getSumY();
double sumXY = covarianceTuple.getSumXY();
+ if (_isSample) {
+ return (sumXY / (count - 1)) - (sumX / (count - 1)) * (sumY / (count - 1));
Review Comment:
Explicitly use Bessel correction
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] amrishlal commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
amrishlal commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954253485
##########
pinot-core/src/test/java/org/apache/pinot/queries/BaseQueriesTest.java:
##########
@@ -146,14 +160,21 @@ private BrokerResponseNative getBrokerResponse(String query, PlanMaker planMaker
/**
* Run query on multiple index segments with custom plan maker.
* <p>Use this to test the whole flow from server to broker.
- * <p>The result should be equivalent to querying 4 identical index segments.
+ * <p>Unless explicitly override getDistinctInstances or initialize 2 distinct index segments in test, the result
+ * should be equivalent to querying 4 identical index segments. Otherwise, the result will be equivalent to
+ * querying 2 distinct instances each having 2 segments.
*/
private BrokerResponseNative getBrokerResponse(PinotQuery pinotQuery, PlanMaker planMaker) {
PinotQuery serverPinotQuery = GapfillUtils.stripGapfill(pinotQuery);
QueryContext queryContext = QueryContextConverterUtils.getQueryContext(pinotQuery);
QueryContext serverQueryContext =
serverPinotQuery == pinotQuery ? queryContext : QueryContextConverterUtils.getQueryContext(serverPinotQuery);
+ List<List<IndexSegment>> instances = getDistinctInstances();
+ if (instances.size() == 2) {
+ return getBrokerResponseDistinctInstances(pinotQuery, planMaker);
+ }
+
Review Comment:
@jasperjiaguo Yes, the code can run on multiple segments, but we are duplicating segments in many test cases and we also duplicate server in BaseQueriesTest.java to mimic realtime server. I think @SabrinaZhaozyf is adding the case for running queries with two servers on two segments (?). At some point, I guess, this file could be cleaned up a bit to setup all the common cases more cleanly: 1) single server single segment, 2) single server multiple segments, 3) two servers, one segement each, etc... But that is probably another PR.
I am ok with the if check ( `if (instances.size() == 2)`). I confused this a bit with other cases.
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954320248
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
```suggestion
int divBase = _isSample ? count - 1 : count;
return (sumXY / divBase) - (sumX / divBase) * (sumY / divBase);
}
```
Let's avoid the multiplication then divide
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954356571
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
This part should actually be `(sumXY / divBase) - (sumX / count) * (sumY / divBase)` as the the `sample_covariance = popCov * besselCorrection = popCov * count/(count-1)`. We did the multiplication then divide to provide better readability and avoid this confusion. IMO it should be fine as we only pay a little price once in the final yield of results?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] SabrinaZhaozyf commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
SabrinaZhaozyf commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r953186422
##########
pinot-core/src/test/java/org/apache/pinot/queries/BaseQueriesTest.java:
##########
@@ -197,4 +197,70 @@ protected BrokerResponseNative getBrokerResponseForOptimizedQuery(String query,
OPTIMIZER.optimize(pinotQuery, config, schema);
return getBrokerResponse(pinotQuery, PLAN_MAKER);
}
+
Review Comment:
Done
##########
pinot-core/src/test/java/org/apache/pinot/queries/CovarianceQueriesTest.java:
##########
@@ -0,0 +1,444 @@
+/**
+ * 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.queries;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.math3.stat.correlation.Covariance;
+import org.apache.commons.math3.util.Precision;
+import org.apache.pinot.common.response.broker.BrokerResponseNative;
+import org.apache.pinot.common.response.broker.ResultTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.operator.blocks.IntermediateResultsBlock;
+import org.apache.pinot.core.operator.query.AggregationGroupByOrderByOperator;
+import org.apache.pinot.core.operator.query.AggregationOperator;
+import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader;
+import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.spi.ImmutableSegment;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.utils.ReadMode;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+
+/**
+ * Queries test for covariance queries.
+ */
+public class CovarianceQueriesTest extends BaseQueriesTest {
+ private static final File INDEX_DIR = new File(FileUtils.getTempDirectory(), "CovarianceQueriesTest");
+ private static final String RAW_TABLE_NAME = "testTable";
+ private static final String SEGMENT_NAME = "testSegment";
+
+ // test segments 1-4 evenly divide testSegment into 4 distinct segments
+ private static final String SEGMENT_NAME_1 = "testSegment1";
+ private static final String SEGMENT_NAME_2 = "testSegment2";
+ private static final String SEGMENT_NAME_3 = "testSegment3";
+ private static final String SEGMENT_NAME_4 = "testSegment4";
+
+ private static final int NUM_RECORDS = 2000;
+ private static final int NUM_GROUPS = 10;
+ private static final int MAX_VALUE = 500;
+ private static final double RELATIVE_EPSILON = 0.0001;
+ private static final double DELTA = 0.0001;
+
+ private static final String INT_COLUMN_X = "intColumnX";
+ private static final String INT_COLUMN_Y = "intColumnY";
+ private static final String DOUBLE_COLUMN_X = "doubleColumnX";
+ private static final String DOUBLE_COLUMN_Y = "doubleColumnY";
+ private static final String LONG_COLUMN = "longColumn";
+ private static final String FLOAT_COLUMN = "floatColumn";
+ private static final String GROUP_BY_COLUMN = "groupByColumn";
+
+ private static final Schema SCHEMA =
+ new Schema.SchemaBuilder().addSingleValueDimension(INT_COLUMN_X, FieldSpec.DataType.INT)
+ .addSingleValueDimension(INT_COLUMN_Y, FieldSpec.DataType.INT)
+ .addSingleValueDimension(DOUBLE_COLUMN_X, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(DOUBLE_COLUMN_Y, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(LONG_COLUMN, FieldSpec.DataType.LONG)
+ .addSingleValueDimension(FLOAT_COLUMN, FieldSpec.DataType.FLOAT)
+ .addSingleValueDimension(GROUP_BY_COLUMN, FieldSpec.DataType.DOUBLE).build();
+ private static final TableConfig TABLE_CONFIG =
+ new TableConfigBuilder(TableType.OFFLINE).setTableName(RAW_TABLE_NAME).build();
+
+ private IndexSegment _indexSegment;
+ private List<IndexSegment> _indexSegments;
+ private List<List<IndexSegment>> _instances;
+ private int _sumIntX = 0;
+ private int _sumIntY = 0;
+ private int _sumIntXY = 0;
+
+ private double _sumDoubleX = 0;
+ private double _sumDoubleY = 0;
+ private double _sumDoubleXY = 0;
+
+ private long _sumLong = 0L;
+ private double _sumFloat = 0;
+
+ private double _sumIntDouble = 0;
+ private long _sumIntLong = 0L;
+ private double _sumIntFloat = 0;
+ private double _sumDoubleLong = 0;
+ private double _sumDoubleFloat = 0;
+ private double _sumLongFloat = 0;
+
+ private double _expectedCovIntXY;
+ private double _expectedCovDoubleXY;
+ private double _expectedCovIntDouble;
+ private double _expectedCovIntLong;
+ private double _expectedCovIntFloat;
+ private double _expectedCovDoubleLong;
+ private double _expectedCovDoubleFloat;
+ private double _expectedCovLongFloat;
+
+ private double _expectedCovWithFilter;
+
+ private CovarianceTuple[] _expectedGroupByResultVer1 = new CovarianceTuple[NUM_GROUPS];
+ private CovarianceTuple[] _expectedGroupByResultVer2 = new CovarianceTuple[NUM_GROUPS];
+ private double[] _expectedFinalResultVer1 = new double[NUM_GROUPS];
+ private double[] _expectedFinalResultVer2 = new double[NUM_GROUPS];
+
+ @Override
+ protected String getFilter() {
+ // filter out half of the rows based on group id
+ return " WHERE groupByColumn < " + (NUM_GROUPS / 2);
+ }
+
+ @Override
+ protected IndexSegment getIndexSegment() {
+ return _indexSegment;
+ }
+
+ @Override
+ protected List<IndexSegment> getIndexSegments() {
+ return _indexSegments;
+ }
+
+ @BeforeClass
+ public void setUp()
+ throws Exception {
+ FileUtils.deleteDirectory(INDEX_DIR);
+
+ List<GenericRow> records = new ArrayList<>(NUM_RECORDS);
+
+ Random rand = new Random();
+ int[] intColX = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ int[] intColY = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColX = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColY = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ long[] longCol = rand.longs(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] floatCol = new double[NUM_RECORDS];
+ double[] groupByCol = new double[NUM_RECORDS];
+
+ int groupSize = NUM_RECORDS / NUM_GROUPS;
+ double sumX = 0;
+ double sumY = 0;
+ double sumGroupBy = 0;
+ double sumXY = 0;
+ double sumXGroupBy = 0;
+ int groupByVal = 0;
+
+ for (int i = 0; i < NUM_RECORDS; i++) {
+ GenericRow record = new GenericRow();
+ int intX = intColX[i];
+ int intY = intColY[i];
+ double doubleX = doubleColX[i];
+ double doubleY = doubleColY[i];
+ long longVal = longCol[i];
+ float floatVal = -MAX_VALUE + rand.nextFloat() * 2 * MAX_VALUE;
+
+ // set up inner segment group by results
+ groupByVal = (int) Math.floor(i / groupSize);
+ if (i % groupSize == 0 && groupByVal > 0) {
+ _expectedGroupByResultVer1[groupByVal - 1] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal - 1] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+ sumX = 0;
+ sumY = 0;
+ sumGroupBy = 0;
+ sumXY = 0;
+ sumXGroupBy = 0;
+ }
+
+ sumX += doubleX;
+ sumY += doubleY;
+ sumGroupBy += groupByVal;
+ sumXY += doubleX * doubleY;
+ sumXGroupBy += doubleX * groupByVal;
+
+ floatCol[i] = floatVal;
+ groupByCol[i] = groupByVal;
+
+ // calculate inner segment results
+ _sumIntX += intX;
+ _sumIntY += intY;
+ _sumDoubleX += doubleX;
+ _sumDoubleY += doubleY;
+ _sumLong += longVal;
+ _sumFloat += floatVal;
+ _sumIntXY += intX * intY;
+ _sumDoubleXY += doubleX * doubleY;
+ _sumIntDouble += intX * doubleX;
+ _sumIntLong += intX * longVal;
+ _sumIntFloat += intX * floatCol[i];
+ _sumDoubleLong += doubleX * longVal;
+ _sumDoubleFloat += doubleX * floatCol[i];
+ _sumLongFloat += longVal * floatCol[i];
+
+ record.putValue(INT_COLUMN_X, intX);
+ record.putValue(INT_COLUMN_Y, intY);
+ record.putValue(DOUBLE_COLUMN_X, doubleX);
+ record.putValue(DOUBLE_COLUMN_Y, doubleY);
+ record.putValue(LONG_COLUMN, longVal);
+ record.putValue(FLOAT_COLUMN, floatVal);
+ record.putValue(GROUP_BY_COLUMN, groupByVal);
+ records.add(record);
+ }
+ _expectedGroupByResultVer1[groupByVal] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+
+ // calculate inter segment result
+ Covariance cov = new Covariance();
+ double[] newIntColX = Arrays.stream(intColX).asDoubleStream().toArray();
+ double[] newIntColY = Arrays.stream(intColY).asDoubleStream().toArray();
+ double[] newLongCol = Arrays.stream(longCol).asDoubleStream().toArray();
+ _expectedCovIntXY = cov.covariance(newIntColX, newIntColY, false);
+ _expectedCovDoubleXY = cov.covariance(doubleColX, doubleColY, false);
+ _expectedCovIntDouble = cov.covariance(newIntColX, doubleColX, false);
+ _expectedCovIntLong = cov.covariance(newIntColX, newLongCol, false);
+ _expectedCovIntFloat = cov.covariance(newIntColX, floatCol, false);
+ _expectedCovDoubleLong = cov.covariance(doubleColX, newLongCol, false);
+ _expectedCovDoubleFloat = cov.covariance(doubleColX, floatCol, false);
+ _expectedCovLongFloat = cov.covariance(newLongCol, floatCol, false);
+
+ double[] filteredX = Arrays.copyOfRange(doubleColX, 0, NUM_RECORDS / 2);
+ double[] filteredY = Arrays.copyOfRange(doubleColY, 0, NUM_RECORDS / 2);
+ _expectedCovWithFilter = cov.covariance(filteredX, filteredY, false);
+
+ // calculate inter segment group by results
+ for (int i = 0; i < NUM_GROUPS; i++) {
+ double[] colX = Arrays.copyOfRange(doubleColX, i * groupSize, (i + 1) * groupSize);
+ double[] colGroupBy = Arrays.copyOfRange(groupByCol, i * groupSize, (i + 1) * groupSize);
+ double[] colY = Arrays.copyOfRange(doubleColY, i * groupSize, (i + 1) * groupSize);
+ _expectedFinalResultVer1[i] = cov.covariance(colX, colGroupBy, false);
+ _expectedFinalResultVer2[i] = cov.covariance(colX, colY, false);
+ }
+
+ // generate testSegment
+ ImmutableSegment immutableSegment = setUpSingleSegment(records, SEGMENT_NAME);
+ _indexSegment = immutableSegment;
+ _indexSegments = Arrays.asList(immutableSegment, immutableSegment);
+
+ // divide testSegment into 4 distinct segments for distinct inter segment tests
+ // by doing so, we can avoid calculating global covariance again
+ _instances = new ArrayList<>();
+ int segmentSize = NUM_RECORDS / 4;
+ ImmutableSegment immutableSegment1 = setUpSingleSegment(records.subList(0, segmentSize), SEGMENT_NAME_1);
+ ImmutableSegment immutableSegment2 =
+ setUpSingleSegment(records.subList(segmentSize, segmentSize * 2), SEGMENT_NAME_2);
+ ImmutableSegment immutableSegment3 =
+ setUpSingleSegment(records.subList(segmentSize * 2, segmentSize * 3), SEGMENT_NAME_3);
+ ImmutableSegment immutableSegment4 =
+ setUpSingleSegment(records.subList(segmentSize * 3, NUM_RECORDS), SEGMENT_NAME_4);
+ // generate 2 instances each with 2 distinct segments
+ _instances.add(Arrays.asList(immutableSegment1, immutableSegment2));
+ _instances.add(Arrays.asList(immutableSegment3, immutableSegment4));
+ }
+
+ private ImmutableSegment setUpSingleSegment(List<GenericRow> recordSet, String segmentName)
+ throws Exception {
+ SegmentGeneratorConfig segmentGeneratorConfig = new SegmentGeneratorConfig(TABLE_CONFIG, SCHEMA);
+ segmentGeneratorConfig.setTableName(RAW_TABLE_NAME);
+ segmentGeneratorConfig.setSegmentName(segmentName);
+ segmentGeneratorConfig.setOutDir(INDEX_DIR.getPath());
+
+ SegmentIndexCreationDriverImpl driver = new SegmentIndexCreationDriverImpl();
+ driver.init(segmentGeneratorConfig, new GenericRowRecordReader(recordSet));
+ driver.build();
+
+ ImmutableSegment immutableSegment = ImmutableSegmentLoader.load(new File(INDEX_DIR, segmentName), ReadMode.mmap);
+ return immutableSegment;
+ }
+
+ @Test
+ public void testAggregationOnly() {
+ // Inner Segment
+ String query = "SELECT COV_POP(intColumnX, intColumnY), COV_POP(doubleColumnX, doubleColumnY), COV_POP(intColumnX, "
+ + "doubleColumnX), " + "COV_POP(intColumnX, longColumn), COV_POP(intColumnX, floatColumn), "
+ + "COV_POP(doubleColumnX, longColumn), COV_POP(doubleColumnX, floatColumn), COV_POP(longColumn, "
+ + "floatColumn) FROM testTable";
Review Comment:
Done
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/customobject/CovarianceTuple.java:
##########
@@ -0,0 +1,117 @@
+/**
+ * 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;
+
+
+public class CovarianceTuple implements Comparable<CovarianceTuple> {
Review Comment:
Done
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -199,6 +201,9 @@ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
double sumX = covarianceTuple.getSumX();
double sumY = covarianceTuple.getSumY();
double sumXY = covarianceTuple.getSumXY();
+ if (_isSample) {
+ return (sumXY / (count - 1)) - (sumX / (count - 1)) * (sumY / (count - 1));
Review Comment:
Done
##########
pinot-core/src/test/java/org/apache/pinot/queries/CovarianceQueriesTest.java:
##########
@@ -0,0 +1,444 @@
+/**
+ * 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.queries;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.math3.stat.correlation.Covariance;
+import org.apache.commons.math3.util.Precision;
+import org.apache.pinot.common.response.broker.BrokerResponseNative;
+import org.apache.pinot.common.response.broker.ResultTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.operator.blocks.IntermediateResultsBlock;
+import org.apache.pinot.core.operator.query.AggregationGroupByOrderByOperator;
+import org.apache.pinot.core.operator.query.AggregationOperator;
+import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader;
+import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.spi.ImmutableSegment;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.utils.ReadMode;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+
+/**
+ * Queries test for covariance queries.
+ */
+public class CovarianceQueriesTest extends BaseQueriesTest {
+ private static final File INDEX_DIR = new File(FileUtils.getTempDirectory(), "CovarianceQueriesTest");
+ private static final String RAW_TABLE_NAME = "testTable";
+ private static final String SEGMENT_NAME = "testSegment";
+
+ // test segments 1-4 evenly divide testSegment into 4 distinct segments
+ private static final String SEGMENT_NAME_1 = "testSegment1";
+ private static final String SEGMENT_NAME_2 = "testSegment2";
+ private static final String SEGMENT_NAME_3 = "testSegment3";
+ private static final String SEGMENT_NAME_4 = "testSegment4";
+
+ private static final int NUM_RECORDS = 2000;
+ private static final int NUM_GROUPS = 10;
+ private static final int MAX_VALUE = 500;
+ private static final double RELATIVE_EPSILON = 0.0001;
+ private static final double DELTA = 0.0001;
+
+ private static final String INT_COLUMN_X = "intColumnX";
+ private static final String INT_COLUMN_Y = "intColumnY";
+ private static final String DOUBLE_COLUMN_X = "doubleColumnX";
+ private static final String DOUBLE_COLUMN_Y = "doubleColumnY";
+ private static final String LONG_COLUMN = "longColumn";
+ private static final String FLOAT_COLUMN = "floatColumn";
+ private static final String GROUP_BY_COLUMN = "groupByColumn";
+
+ private static final Schema SCHEMA =
+ new Schema.SchemaBuilder().addSingleValueDimension(INT_COLUMN_X, FieldSpec.DataType.INT)
+ .addSingleValueDimension(INT_COLUMN_Y, FieldSpec.DataType.INT)
+ .addSingleValueDimension(DOUBLE_COLUMN_X, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(DOUBLE_COLUMN_Y, FieldSpec.DataType.DOUBLE)
+ .addSingleValueDimension(LONG_COLUMN, FieldSpec.DataType.LONG)
+ .addSingleValueDimension(FLOAT_COLUMN, FieldSpec.DataType.FLOAT)
+ .addSingleValueDimension(GROUP_BY_COLUMN, FieldSpec.DataType.DOUBLE).build();
+ private static final TableConfig TABLE_CONFIG =
+ new TableConfigBuilder(TableType.OFFLINE).setTableName(RAW_TABLE_NAME).build();
+
+ private IndexSegment _indexSegment;
+ private List<IndexSegment> _indexSegments;
+ private List<List<IndexSegment>> _instances;
+ private int _sumIntX = 0;
+ private int _sumIntY = 0;
+ private int _sumIntXY = 0;
+
+ private double _sumDoubleX = 0;
+ private double _sumDoubleY = 0;
+ private double _sumDoubleXY = 0;
+
+ private long _sumLong = 0L;
+ private double _sumFloat = 0;
+
+ private double _sumIntDouble = 0;
+ private long _sumIntLong = 0L;
+ private double _sumIntFloat = 0;
+ private double _sumDoubleLong = 0;
+ private double _sumDoubleFloat = 0;
+ private double _sumLongFloat = 0;
+
+ private double _expectedCovIntXY;
+ private double _expectedCovDoubleXY;
+ private double _expectedCovIntDouble;
+ private double _expectedCovIntLong;
+ private double _expectedCovIntFloat;
+ private double _expectedCovDoubleLong;
+ private double _expectedCovDoubleFloat;
+ private double _expectedCovLongFloat;
+
+ private double _expectedCovWithFilter;
+
+ private CovarianceTuple[] _expectedGroupByResultVer1 = new CovarianceTuple[NUM_GROUPS];
+ private CovarianceTuple[] _expectedGroupByResultVer2 = new CovarianceTuple[NUM_GROUPS];
+ private double[] _expectedFinalResultVer1 = new double[NUM_GROUPS];
+ private double[] _expectedFinalResultVer2 = new double[NUM_GROUPS];
+
+ @Override
+ protected String getFilter() {
+ // filter out half of the rows based on group id
+ return " WHERE groupByColumn < " + (NUM_GROUPS / 2);
+ }
+
+ @Override
+ protected IndexSegment getIndexSegment() {
+ return _indexSegment;
+ }
+
+ @Override
+ protected List<IndexSegment> getIndexSegments() {
+ return _indexSegments;
+ }
+
+ @BeforeClass
+ public void setUp()
+ throws Exception {
+ FileUtils.deleteDirectory(INDEX_DIR);
+
+ List<GenericRow> records = new ArrayList<>(NUM_RECORDS);
+
+ Random rand = new Random();
+ int[] intColX = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ int[] intColY = rand.ints(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColX = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] doubleColY = rand.doubles(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ long[] longCol = rand.longs(NUM_RECORDS, -MAX_VALUE, MAX_VALUE).toArray();
+ double[] floatCol = new double[NUM_RECORDS];
+ double[] groupByCol = new double[NUM_RECORDS];
+
+ int groupSize = NUM_RECORDS / NUM_GROUPS;
+ double sumX = 0;
+ double sumY = 0;
+ double sumGroupBy = 0;
+ double sumXY = 0;
+ double sumXGroupBy = 0;
+ int groupByVal = 0;
+
+ for (int i = 0; i < NUM_RECORDS; i++) {
+ GenericRow record = new GenericRow();
+ int intX = intColX[i];
+ int intY = intColY[i];
+ double doubleX = doubleColX[i];
+ double doubleY = doubleColY[i];
+ long longVal = longCol[i];
+ float floatVal = -MAX_VALUE + rand.nextFloat() * 2 * MAX_VALUE;
+
+ // set up inner segment group by results
+ groupByVal = (int) Math.floor(i / groupSize);
+ if (i % groupSize == 0 && groupByVal > 0) {
+ _expectedGroupByResultVer1[groupByVal - 1] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal - 1] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+ sumX = 0;
+ sumY = 0;
+ sumGroupBy = 0;
+ sumXY = 0;
+ sumXGroupBy = 0;
+ }
+
+ sumX += doubleX;
+ sumY += doubleY;
+ sumGroupBy += groupByVal;
+ sumXY += doubleX * doubleY;
+ sumXGroupBy += doubleX * groupByVal;
+
+ floatCol[i] = floatVal;
+ groupByCol[i] = groupByVal;
+
+ // calculate inner segment results
+ _sumIntX += intX;
+ _sumIntY += intY;
+ _sumDoubleX += doubleX;
+ _sumDoubleY += doubleY;
+ _sumLong += longVal;
+ _sumFloat += floatVal;
+ _sumIntXY += intX * intY;
+ _sumDoubleXY += doubleX * doubleY;
+ _sumIntDouble += intX * doubleX;
+ _sumIntLong += intX * longVal;
+ _sumIntFloat += intX * floatCol[i];
+ _sumDoubleLong += doubleX * longVal;
+ _sumDoubleFloat += doubleX * floatCol[i];
+ _sumLongFloat += longVal * floatCol[i];
+
+ record.putValue(INT_COLUMN_X, intX);
+ record.putValue(INT_COLUMN_Y, intY);
+ record.putValue(DOUBLE_COLUMN_X, doubleX);
+ record.putValue(DOUBLE_COLUMN_Y, doubleY);
+ record.putValue(LONG_COLUMN, longVal);
+ record.putValue(FLOAT_COLUMN, floatVal);
+ record.putValue(GROUP_BY_COLUMN, groupByVal);
+ records.add(record);
+ }
+ _expectedGroupByResultVer1[groupByVal] = new CovarianceTuple(sumX, sumGroupBy, sumXGroupBy, groupSize);
+ _expectedGroupByResultVer2[groupByVal] = new CovarianceTuple(sumX, sumY, sumXY, groupSize);
+
+ // calculate inter segment result
+ Covariance cov = new Covariance();
+ double[] newIntColX = Arrays.stream(intColX).asDoubleStream().toArray();
+ double[] newIntColY = Arrays.stream(intColY).asDoubleStream().toArray();
+ double[] newLongCol = Arrays.stream(longCol).asDoubleStream().toArray();
+ _expectedCovIntXY = cov.covariance(newIntColX, newIntColY, false);
+ _expectedCovDoubleXY = cov.covariance(doubleColX, doubleColY, false);
+ _expectedCovIntDouble = cov.covariance(newIntColX, doubleColX, false);
+ _expectedCovIntLong = cov.covariance(newIntColX, newLongCol, false);
+ _expectedCovIntFloat = cov.covariance(newIntColX, floatCol, false);
+ _expectedCovDoubleLong = cov.covariance(doubleColX, newLongCol, false);
+ _expectedCovDoubleFloat = cov.covariance(doubleColX, floatCol, false);
+ _expectedCovLongFloat = cov.covariance(newLongCol, floatCol, false);
+
+ double[] filteredX = Arrays.copyOfRange(doubleColX, 0, NUM_RECORDS / 2);
+ double[] filteredY = Arrays.copyOfRange(doubleColY, 0, NUM_RECORDS / 2);
+ _expectedCovWithFilter = cov.covariance(filteredX, filteredY, false);
+
+ // calculate inter segment group by results
+ for (int i = 0; i < NUM_GROUPS; i++) {
+ double[] colX = Arrays.copyOfRange(doubleColX, i * groupSize, (i + 1) * groupSize);
+ double[] colGroupBy = Arrays.copyOfRange(groupByCol, i * groupSize, (i + 1) * groupSize);
+ double[] colY = Arrays.copyOfRange(doubleColY, i * groupSize, (i + 1) * groupSize);
+ _expectedFinalResultVer1[i] = cov.covariance(colX, colGroupBy, false);
+ _expectedFinalResultVer2[i] = cov.covariance(colX, colY, false);
+ }
+
+ // generate testSegment
+ ImmutableSegment immutableSegment = setUpSingleSegment(records, SEGMENT_NAME);
+ _indexSegment = immutableSegment;
+ _indexSegments = Arrays.asList(immutableSegment, immutableSegment);
+
+ // divide testSegment into 4 distinct segments for distinct inter segment tests
+ // by doing so, we can avoid calculating global covariance again
+ _instances = new ArrayList<>();
Review Comment:
Done - added getDistinctInstances and changed logic path to tigger getBrokerResponseDistinctInstances if there's more than 1 instances found.
##########
pinot-core/src/test/java/org/apache/pinot/queries/BaseQueriesTest.java:
##########
@@ -91,7 +95,9 @@ protected <T extends Operator> T getOperatorWithFilter(String query) {
/**
* Run query on multiple index segments.
* <p>Use this to test the whole flow from server to broker.
- * <p>The result should be equivalent to querying 4 identical index segments.
+ * <p>Unless explicitly override getDistinctInstances or initialize 2 distinct index segments in test, the result
Review Comment:
Done.
##########
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:
Thank you for pointing it out. For the context, I was trying to keep the return value (-1/1/0) consistent with other customobjects such as [AvgPair](https://github.com/apache/pinot/blob/master/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/customobject/AvgPair.java#L71).
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
Review Comment:
Done
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
I see where you are coming from but
`[(sumXY / count) - (sumX / count) * (sumY / count)] * [count / (count - 1)]` actually does not equal to `(sumXY / (count - 1)) - (sumX / (count - 1)) * (sumY / (count - 1))` - a subtle math mistake that I also missed the first time. Using multiplication with besselCorrection is clearer/correct in this case.
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954356571
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
This part should actually be `(sumXY / divBase) - (sumX / count) * (sumY / divBase)` as the the `sample_covariance = popCov * besselCorrection = popCov * n/(n-1)`. We did the multiplication then divide to provide better readability and avoid this confusion. IMO it should be fine as we only pay a little price once in final reduction?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954370975
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
yes. try to avoid divide if possible since this is on the critical path, computed once per agg group
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] siddharthteotia merged pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
siddharthteotia merged PR #9236:
URL: https://github.com/apache/pinot/pull/9236
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954319642
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
Review Comment:
actually ignore. see my other comment
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
```suggestion
double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
double besselCorrection = count / (count - 1);
if (_isSample) {
return popCov * besselCorrection;
}
return popCov;
}
```
```suggestion
if (_isSample) {
return (sumXY / count) - (sumX / count) * (sumY / (count - 1));
} else {
return (sumXY / count) - (sumX / count) * (sumY / count);
}
}
```
Let's avoid the multiplication then divide
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954314178
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
Review Comment:
```suggestion
if (_isSample) {
double besselCorrection = count / (count - 1);
return popCov * besselCorrection;
}
```
##########
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:
compareTo should return the diff value, not just 0 and 1/-1. although I am not sure how to convert the double into int, is there any typical comparison written in other SQL system?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] codecov-commenter commented on pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #9236:
URL: https://github.com/apache/pinot/pull/9236#issuecomment-1223392965
# [Codecov](https://codecov.io/gh/apache/pinot/pull/9236?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
> Merging [#9236](https://codecov.io/gh/apache/pinot/pull/9236?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (f6ddf41) into [master](https://codecov.io/gh/apache/pinot/commit/e8a8684e2de9a3536203f6c448408552be2ded64?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (e8a8684) will **decrease** coverage by `43.81%`.
> The diff coverage is `1.60%`.
```diff
@@ Coverage Diff @@
## master #9236 +/- ##
=============================================
- Coverage 69.99% 26.18% -43.82%
+ Complexity 4682 44 -4638
=============================================
Files 1848 1850 +2
Lines 98572 98935 +363
Branches 14967 15058 +91
=============================================
- Hits 68999 25906 -43093
- Misses 24723 70468 +45745
+ Partials 4850 2561 -2289
```
| Flag | Coverage Δ | |
|---|---|---|
| integration1 | `26.18% <1.60%> (-0.17%)` | :arrow_down: |
| integration2 | `?` | |
| unittests1 | `?` | |
| unittests2 | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Impacted Files](https://codecov.io/gh/apache/pinot/pull/9236?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
|---|---|---|
| [...gregation/function/AggregationFunctionFactory.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3QtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvY29yZS9xdWVyeS9hZ2dyZWdhdGlvbi9mdW5jdGlvbi9BZ2dyZWdhdGlvbkZ1bmN0aW9uRmFjdG9yeS5qYXZh) | `11.94% <0.00%> (-69.27%)` | :arrow_down: |
| [...gation/function/CovarianceAggregationFunction.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3QtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvY29yZS9xdWVyeS9hZ2dyZWdhdGlvbi9mdW5jdGlvbi9Db3ZhcmlhbmNlQWdncmVnYXRpb25GdW5jdGlvbi5qYXZh) | `0.00% <0.00%> (ø)` | |
| [...ot/segment/local/customobject/CovarianceTuple.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc2VnbWVudC1sb2NhbC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3Qvc2VnbWVudC9sb2NhbC9jdXN0b21vYmplY3QvQ292YXJpYW5jZVR1cGxlLmphdmE=) | `0.00% <0.00%> (ø)` | |
| [...che/pinot/segment/spi/AggregationFunctionType.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc2VnbWVudC1zcGkvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3Bpbm90L3NlZ21lbnQvc3BpL0FnZ3JlZ2F0aW9uRnVuY3Rpb25UeXBlLmphdmE=) | `0.00% <0.00%> (-89.54%)` | :arrow_down: |
| [...org/apache/pinot/core/common/ObjectSerDeUtils.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3QtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvY29yZS9jb21tb24vT2JqZWN0U2VyRGVVdGlscy5qYXZh) | `36.27% <28.57%> (-54.18%)` | :arrow_down: |
| [...in/java/org/apache/pinot/spi/utils/BytesUtils.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdXRpbHMvQnl0ZXNVdGlscy5qYXZh) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [.../java/org/apache/pinot/spi/utils/BooleanUtils.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdXRpbHMvQm9vbGVhblV0aWxzLmphdmE=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [...java/org/apache/pinot/spi/trace/BaseRecording.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdHJhY2UvQmFzZVJlY29yZGluZy5qYXZh) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [...java/org/apache/pinot/spi/trace/NoOpRecording.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdHJhY2UvTm9PcFJlY29yZGluZy5qYXZh) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [...ava/org/apache/pinot/spi/config/table/FSTType.java](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvY29uZmlnL3RhYmxlL0ZTVFR5cGUuamF2YQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| ... and [1396 more](https://codecov.io/gh/apache/pinot/pull/9236/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] jasperjiaguo commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
jasperjiaguo commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954297069
##########
pinot-core/src/test/java/org/apache/pinot/queries/BaseQueriesTest.java:
##########
@@ -91,7 +95,9 @@ protected <T extends Operator> T getOperatorWithFilter(String query) {
/**
* Run query on multiple index segments.
* <p>Use this to test the whole flow from server to broker.
- * <p>The result should be equivalent to querying 4 identical index segments.
+ * <p>Unless explicitly override getDistinctInstances or initialize 2 distinct index segments in test, the result
Review Comment:
From your code it seems overriding `getDistinctInstances` and initializing 2 instances is the way to trigger the new `getBrokerResponseDistinctInstances ` behavior? Also the user should take care of assigning the distinct segments correctly to the two instances. Maybe update this comment a little bit and also add a pointer to the usage `CovarianceQueriesTest`?
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] walterddr commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r954440149
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
also I am no expert in JIT compiler but `(sumX / count) * (sumY / count)` can be rewritten as `sumX * sumY / (count * count)` which also avoid another divide.
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[GitHub] [pinot] SabrinaZhaozyf commented on a diff in pull request #9236: Add Support for Covariance Function
Posted by GitBox <gi...@apache.org>.
SabrinaZhaozyf commented on code in PR #9236:
URL: https://github.com/apache/pinot/pull/9236#discussion_r955294845
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/CovarianceAggregationFunction.java:
##########
@@ -0,0 +1,237 @@
+/**
+ * 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.core.query.aggregation.function;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.local.customobject.CovarianceTuple;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+/**
+ * Aggregation function which returns the population covariance of 2 expressions.
+ * COVAR_POP(exp1, exp2) = mean(exp1 * exp2) - mean(exp1) * mean(exp2)
+ * COVAR_SAMP(exp1, exp2) = (sum(exp1 * exp2) - sum(exp1) * sum(exp2)) / (count - 1)
+ *
+ * Population covariance between two random variables X and Y is defined as either
+ * covarPop(X,Y) = E[(X - E[X]) * (Y - E[Y])] or
+ * covarPop(X,Y) = E[X*Y] - E[X] * E[Y],
+ * here E[X] represents mean of X
+ * @see <a href="https://en.wikipedia.org/wiki/Covariance">Covariance</a>
+ * The calculations here are based on the second definition shown above.
+ * Sample covariance = covarPop(X, Y) * besselCorrection
+ * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel's correction</a>
+ */
+public class CovarianceAggregationFunction implements AggregationFunction<CovarianceTuple, Double> {
+ private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY;
+ protected final ExpressionContext _expression1;
+ protected final ExpressionContext _expression2;
+ protected final boolean _isSample;
+
+ public CovarianceAggregationFunction(List<ExpressionContext> arguments, boolean isSample) {
+ _expression1 = arguments.get(0);
+ _expression2 = arguments.get(1);
+ _isSample = isSample;
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ if (_isSample) {
+ return AggregationFunctionType.COVARSAMP;
+ }
+ return AggregationFunctionType.COVARPOP;
+ }
+
+ @Override
+ public String getColumnName() {
+ return getType().getName() + "_" + _expression1 + "_" + _expression2;
+ }
+
+ @Override
+ public String getResultColumnName() {
+ return getType().getName().toLowerCase() + "(" + _expression1 + "," + _expression2 + ")";
+ }
+
+ @Override
+ public List<ExpressionContext> getInputExpressions() {
+ ArrayList<ExpressionContext> inputExpressions = new ArrayList<>();
+ inputExpressions.add(_expression1);
+ inputExpressions.add(_expression2);
+ return inputExpressions;
+ }
+
+ @Override
+ public AggregationResultHolder createAggregationResultHolder() {
+ return new ObjectAggregationResultHolder();
+ }
+
+ @Override
+ public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) {
+ return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+ }
+
+ @Override
+ public void aggregate(int length, AggregationResultHolder aggregationResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+
+ double sumX = 0.0;
+ double sumY = 0.0;
+ double sumXY = 0.0;
+
+ for (int i = 0; i < length; i++) {
+ sumX += values1[i];
+ sumY += values2[i];
+ sumXY += values1[i] * values2[i];
+ }
+ setAggregationResult(aggregationResultHolder, sumX, sumY, sumXY, length);
+ }
+
+ protected void setAggregationResult(AggregationResultHolder aggregationResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ aggregationResultHolder.setValue(new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ protected void setGroupByResult(int groupKey, GroupByResultHolder groupByResultHolder, double sumX, double sumY,
+ double sumXY, long count) {
+ CovarianceTuple covarianceTuple = groupByResultHolder.getResult(groupKey);
+ if (covarianceTuple == null) {
+ groupByResultHolder.setValueForKey(groupKey, new CovarianceTuple(sumX, sumY, sumXY, count));
+ } else {
+ covarianceTuple.apply(sumX, sumY, sumXY, count);
+ }
+ }
+
+ private double[] getValSet(Map<ExpressionContext, BlockValSet> blockValSetMap, ExpressionContext expression) {
+ BlockValSet blockValSet = blockValSetMap.get(expression);
+ //TODO: Add MV support for covariance
+ Preconditions.checkState(blockValSet.isSingleValue(),
+ "Covariance function currently only supports single-valued column");
+ switch (blockValSet.getValueType().getStoredType()) {
+ case INT:
+ case LONG:
+ case FLOAT:
+ case DOUBLE:
+ return blockValSet.getDoubleValuesSV();
+ default:
+ throw new IllegalStateException(
+ "Cannot compute covariance for non-numeric type: " + blockValSet.getValueType());
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ setGroupByResult(groupKeyArray[i], groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray, GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ double[] values1 = getValSet(blockValSetMap, _expression1);
+ double[] values2 = getValSet(blockValSetMap, _expression2);
+ for (int i = 0; i < length; i++) {
+ for (int groupKey : groupKeysArray[i]) {
+ setGroupByResult(groupKey, groupByResultHolder, values1[i], values2[i], values1[i] * values2[i], 1L);
+ }
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractAggregationResult(AggregationResultHolder aggregationResultHolder) {
+ CovarianceTuple covarianceTuple = aggregationResultHolder.getResult();
+ if (covarianceTuple == null) {
+ return new CovarianceTuple(0.0, 0.0, 0.0, 0L);
+ } else {
+ return covarianceTuple;
+ }
+ }
+
+ @Override
+ public CovarianceTuple extractGroupByResult(GroupByResultHolder groupByResultHolder, int groupKey) {
+ return groupByResultHolder.getResult(groupKey);
+ }
+
+ @Override
+ public CovarianceTuple merge(CovarianceTuple intermediateResult1, CovarianceTuple intermediateResult2) {
+ intermediateResult1.apply(intermediateResult2);
+ return intermediateResult1;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+ return DataSchema.ColumnDataType.OBJECT;
+ }
+
+ @Override
+ public DataSchema.ColumnDataType getFinalResultColumnType() {
+ return DataSchema.ColumnDataType.DOUBLE;
+ }
+
+ @Override
+ public Double extractFinalResult(CovarianceTuple covarianceTuple) {
+ long count = covarianceTuple.getCount();
+ if (count == 0L) {
+ return DEFAULT_FINAL_RESULT;
+ } else {
+ double sumX = covarianceTuple.getSumX();
+ double sumY = covarianceTuple.getSumY();
+ double sumXY = covarianceTuple.getSumXY();
+ double popCov = (sumXY / count) - (sumX / count) * (sumY / count);
+ double besselCorrection = count / (count - 1);
+ if (_isSample) {
+ return popCov * besselCorrection;
+ }
+ return popCov;
+ }
Review Comment:
Done. Ty for the suggestions!
--
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-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org