|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include "velox/core/PlanNode.h" |
| 19 | +#include "velox/exec/fuzzer/ResultVerifier.h" |
| 20 | +#include "velox/exec/tests/utils/AssertQueryBuilder.h" |
| 21 | +#include "velox/exec/tests/utils/PlanBuilder.h" |
| 22 | +#include "velox/functions/lib/TDigest.h" |
| 23 | +#include "velox/vector/ComplexVector.h" |
| 24 | + |
| 25 | +namespace facebook::velox::exec::test { |
| 26 | + |
| 27 | +class TDigestAggregateResultVerifier : public ResultVerifier { |
| 28 | + public: |
| 29 | + bool supportsCompare() override { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + bool supportsVerify() override { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + void initialize( |
| 38 | + const std::vector<RowVectorPtr>& /*input*/, |
| 39 | + const std::vector<core::ExprPtr>& /*projections*/, |
| 40 | + const std::vector<std::string>& groupingKeys, |
| 41 | + const core::AggregationNode::Aggregate& aggregate, |
| 42 | + const std::string& aggregateName) override { |
| 43 | + keys_ = groupingKeys; |
| 44 | + resultName_ = aggregateName; |
| 45 | + |
| 46 | + // Check TDigest types |
| 47 | + validateTDigestTypes(aggregate.call); |
| 48 | + } |
| 49 | + |
| 50 | + void initializeWindow( |
| 51 | + const std::vector<RowVectorPtr>& /*input*/, |
| 52 | + const std::vector<core::ExprPtr>& /*projections*/, |
| 53 | + const std::vector<std::string>& /*partitionByKeys*/, |
| 54 | + const std::vector<SortingKeyAndOrder>& /*sortingKeysAndOrders*/, |
| 55 | + const core::WindowNode::Function& function, |
| 56 | + const std::string& /*frame*/, |
| 57 | + const std::string& windowName) override { |
| 58 | + keys_ = {"row_number"}; |
| 59 | + resultName_ = windowName; |
| 60 | + |
| 61 | + // Check TDigest types |
| 62 | + validateTDigestTypes(function.functionCall); |
| 63 | + } |
| 64 | + |
| 65 | + bool compare(const RowVectorPtr& result, const RowVectorPtr& altResult) |
| 66 | + override { |
| 67 | + VELOX_CHECK_EQ(result->size(), altResult->size()); |
| 68 | + |
| 69 | + auto projection = keys_; |
| 70 | + projection.push_back(resultName_); |
| 71 | + |
| 72 | + auto planNodeIdGenerator = std::make_shared<core::PlanNodeIdGenerator>(); |
| 73 | + auto builder = PlanBuilder(planNodeIdGenerator).values({result}); |
| 74 | + if (!keys_.empty()) { |
| 75 | + builder = builder.orderBy(keys_, false); |
| 76 | + } |
| 77 | + auto sortByKeys = builder.project(projection).planNode(); |
| 78 | + auto sortedResult = |
| 79 | + AssertQueryBuilder(sortByKeys).copyResults(result->pool()); |
| 80 | + |
| 81 | + builder = PlanBuilder(planNodeIdGenerator).values({altResult}); |
| 82 | + if (!keys_.empty()) { |
| 83 | + builder = builder.orderBy(keys_, false); |
| 84 | + } |
| 85 | + sortByKeys = builder.project(projection).planNode(); |
| 86 | + auto sortedAltResult = |
| 87 | + AssertQueryBuilder(sortByKeys).copyResults(altResult->pool()); |
| 88 | + |
| 89 | + VELOX_CHECK_EQ(sortedResult->size(), sortedAltResult->size()); |
| 90 | + auto size = sortedResult->size(); |
| 91 | + for (auto i = 0; i < size; i++) { |
| 92 | + auto resultIsNull = sortedResult->childAt(resultName_)->isNullAt(i); |
| 93 | + auto altResultIsNull = sortedAltResult->childAt(resultName_)->isNullAt(i); |
| 94 | + if (resultIsNull || altResultIsNull) { |
| 95 | + VELOX_CHECK(resultIsNull && altResultIsNull); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + auto resultValue = sortedResult->childAt(resultName_) |
| 100 | + ->as<SimpleVector<StringView>>() |
| 101 | + ->valueAt(i); |
| 102 | + auto altResultValue = sortedAltResult->childAt(resultName_) |
| 103 | + ->as<SimpleVector<StringView>>() |
| 104 | + ->valueAt(i); |
| 105 | + if (resultValue == altResultValue) { |
| 106 | + continue; |
| 107 | + } else { |
| 108 | + checkEquivalentTDigest(resultValue, altResultValue); |
| 109 | + } |
| 110 | + } |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + bool verify(const RowVectorPtr& /*result*/) override { |
| 115 | + VELOX_UNSUPPORTED(); |
| 116 | + } |
| 117 | + |
| 118 | + void reset() override { |
| 119 | + keys_.clear(); |
| 120 | + resultName_.clear(); |
| 121 | + } |
| 122 | + |
| 123 | + private: |
| 124 | + // Helper method to check TDigest input and return types |
| 125 | + void validateTDigestTypes(const core::CallTypedExprPtr& call) const { |
| 126 | + // Check input type is double |
| 127 | + auto inputType = call->inputs()[0]->type(); |
| 128 | + if (inputType->kind() != TypeKind::DOUBLE) { |
| 129 | + VELOX_FAIL( |
| 130 | + "TDigest only supports DOUBLE input type, got {}", |
| 131 | + inputType->toString()); |
| 132 | + } |
| 133 | + auto returnType = call->type(); |
| 134 | + if (returnType->kind() != TypeKind::VARBINARY) { |
| 135 | + VELOX_FAIL( |
| 136 | + "TDigest return type must be VARBINARY, got {}", |
| 137 | + returnType->toString()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + void checkEquivalentTDigest( |
| 142 | + const StringView& result, |
| 143 | + const StringView& altResult) { |
| 144 | + // Create TDigests from serialized data |
| 145 | + facebook::velox::functions::TDigest<> resultTdigest; |
| 146 | + facebook::velox::functions::TDigest<> altResultTdigest; |
| 147 | + std::vector<int16_t> positions; |
| 148 | + |
| 149 | + try { |
| 150 | + resultTdigest.mergeDeserialized(positions, result.data()); |
| 151 | + resultTdigest.compress(positions); |
| 152 | + |
| 153 | + positions.clear(); |
| 154 | + altResultTdigest.mergeDeserialized(positions, altResult.data()); |
| 155 | + altResultTdigest.compress(positions); |
| 156 | + } catch (const std::exception& e) { |
| 157 | + VELOX_FAIL("Failed to deserialize TDigest: {}", e.what()); |
| 158 | + } |
| 159 | + |
| 160 | + // Compare TDigest values at specific quantiles |
| 161 | + for (auto quantile : kQuantiles) { |
| 162 | + double resultQuantile = resultTdigest.estimateQuantile(quantile); |
| 163 | + double altResultQuantile = altResultTdigest.estimateQuantile(quantile); |
| 164 | + |
| 165 | + variant resultVariant(resultQuantile); |
| 166 | + variant altResultVariant(altResultQuantile); |
| 167 | + VELOX_CHECK( |
| 168 | + resultVariant.equalsWithEpsilon(altResultVariant), |
| 169 | + "TDigest quantile values differ at {}: {} vs {}", |
| 170 | + quantile, |
| 171 | + resultQuantile, |
| 172 | + altResultQuantile); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + static constexpr double kQuantiles[] = { |
| 177 | + 0.01, |
| 178 | + 0.05, |
| 179 | + 0.1, |
| 180 | + 0.25, |
| 181 | + 0.50, |
| 182 | + 0.75, |
| 183 | + 0.9, |
| 184 | + 0.95, |
| 185 | + 0.99, |
| 186 | + }; |
| 187 | + |
| 188 | + std::vector<std::string> keys_; |
| 189 | + std::string resultName_; |
| 190 | +}; |
| 191 | + |
| 192 | +} // namespace facebook::velox::exec::test |
0 commit comments