|
| 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 | +#include <folly/base64.h> |
| 17 | +#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" |
| 18 | +#include "velox/functions/prestosql/types/TDigestRegistration.h" |
| 19 | +#include "velox/functions/prestosql/types/TDigestType.h" |
| 20 | + |
| 21 | +using namespace facebook::velox; |
| 22 | +using namespace facebook::velox::exec; |
| 23 | +using namespace facebook::velox::functions::test; |
| 24 | + |
| 25 | +class TDigestFunctionsTest : public FunctionBaseTest { |
| 26 | + protected: |
| 27 | + void SetUp() override { |
| 28 | + FunctionBaseTest::SetUp(); |
| 29 | + registerTDigestType(); |
| 30 | + } |
| 31 | + |
| 32 | + protected: |
| 33 | + std::string decodeBase64(std::string_view input) { |
| 34 | + std::string decoded(folly::base64DecodedSize(input), '\0'); |
| 35 | + folly::base64Decode(input, decoded.data()); |
| 36 | + return decoded; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +TEST_F(TDigestFunctionsTest, valueAtQuantile) { |
| 41 | + const TypePtr type = TDIGEST(DOUBLE()); |
| 42 | + const auto valueAtQuantile = [&](const std::optional<std::string>& input, |
| 43 | + const std::optional<double>& quantile) { |
| 44 | + return evaluateOnce<double>( |
| 45 | + "value_at_quantile(c0, c1)", type, input, quantile); |
| 46 | + }; |
| 47 | + const std::string input = decodeBase64( |
| 48 | + "AQAAAAAAAADwPwAAAAAAABRAAAAAAAAALkAAAAAAAABZQAAAAAAAABRABQAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABRA"); |
| 49 | + ASSERT_EQ(1.0, valueAtQuantile(input, 0.1)); |
| 50 | + ASSERT_EQ(3.0, valueAtQuantile(input, 0.5)); |
| 51 | + ASSERT_EQ(5.0, valueAtQuantile(input, 0.9)); |
| 52 | + ASSERT_EQ(5.0, valueAtQuantile(input, 0.99)); |
| 53 | +}; |
| 54 | + |
| 55 | +TEST_F(TDigestFunctionsTest, valuesAtQuantiles) { |
| 56 | + const TypePtr type = TDIGEST(DOUBLE()); |
| 57 | + const std::string input = decodeBase64( |
| 58 | + "AQAAAAAAAADwPwAAAAAAABRAAAAAAAAALkAAAAAAAABZQAAAAAAAABRABQAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABRA"); |
| 59 | + VectorPtr arg0 = makeFlatVector<std::string>({input}, type); |
| 60 | + VectorPtr arg1 = makeNullableArrayVector<double>({{0.1, 0.5, 0.9, 0.99}}); |
| 61 | + auto expected = makeNullableArrayVector<double>({{1.0, 3.0, 5.0, 5.0}}); |
| 62 | + auto result = |
| 63 | + evaluate("values_at_quantiles(c0, c1)", makeRowVector({arg0, arg1})); |
| 64 | + test::assertEqualVectors(expected, result); |
| 65 | +} |
0 commit comments