diff --git a/velox/functions/prestosql/aggregates/CovarianceAggregates.cpp b/velox/functions/prestosql/aggregates/CovarianceAggregates.cpp index 1275d39444fb..3cdbdbfae5a6 100644 --- a/velox/functions/prestosql/aggregates/CovarianceAggregates.cpp +++ b/velox/functions/prestosql/aggregates/CovarianceAggregates.cpp @@ -236,9 +236,8 @@ struct CorrResultAccessor { } static double result(const CorrAccumulator& accumulator) { - double stddevX = std::sqrt(accumulator.m2X()); - double stddevY = std::sqrt(accumulator.m2Y()); - return accumulator.c2() / stddevX / stddevY; + // Modify the calculation order to maintain the same accuracy with spark. + return accumulator.c2() / std::sqrt(accumulator.m2X() * accumulator.m2Y()); } };