|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "function_test_util.h" |
| 22 | +#include "util/quantile_state.h" |
| 23 | +#include "util/url_coding.h" |
| 24 | +#include "vec/core/types.h" |
| 25 | +#include "vec/data_types/data_type_quantilestate.h" |
| 26 | +#include "vec/data_types/data_type_string.h" |
| 27 | + |
| 28 | +namespace doris::vectorized { |
| 29 | + |
| 30 | +TEST(function_quantile_state_test, function_quantile_state_to_base64) { |
| 31 | + std::string func_name = "quantile_state_to_base64"; |
| 32 | + InputTypeSet input_types = {TypeIndex::QuantileState}; |
| 33 | + |
| 34 | + QuantileState empty_quantile_state; |
| 35 | + |
| 36 | + QuantileState single_quantile_state; |
| 37 | + single_quantile_state.add_value(1.0); |
| 38 | + |
| 39 | + QuantileState multi_quantile_state; |
| 40 | + multi_quantile_state.add_value(1.0); |
| 41 | + multi_quantile_state.add_value(2.0); |
| 42 | + multi_quantile_state.add_value(3.0); |
| 43 | + multi_quantile_state.add_value(4.0); |
| 44 | + multi_quantile_state.add_value(5.0); |
| 45 | + |
| 46 | + QuantileState explicit_quantile_state; |
| 47 | + for (int i = 0; i < 100; i++) { |
| 48 | + explicit_quantile_state.add_value(static_cast<double>(i)); |
| 49 | + } |
| 50 | + |
| 51 | + QuantileState tdigest_quantile_state; |
| 52 | + for (int i = 0; i < 3000; i++) { |
| 53 | + tdigest_quantile_state.add_value(static_cast<double>(i)); |
| 54 | + } |
| 55 | + |
| 56 | + uint8_t buf[65536]; |
| 57 | + unsigned char encoded_buf[131072]; |
| 58 | + |
| 59 | + std::string empty_base64; |
| 60 | + { |
| 61 | + size_t len = empty_quantile_state.serialize(buf); |
| 62 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 63 | + empty_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 64 | + } |
| 65 | + |
| 66 | + std::string single_base64; |
| 67 | + { |
| 68 | + size_t len = single_quantile_state.serialize(buf); |
| 69 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 70 | + single_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 71 | + } |
| 72 | + |
| 73 | + std::string multi_base64; |
| 74 | + { |
| 75 | + size_t len = multi_quantile_state.serialize(buf); |
| 76 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 77 | + multi_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 78 | + } |
| 79 | + |
| 80 | + std::string explicit_base64; |
| 81 | + { |
| 82 | + size_t len = explicit_quantile_state.serialize(buf); |
| 83 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 84 | + explicit_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 85 | + } |
| 86 | + |
| 87 | + std::string tdigest_base64; |
| 88 | + { |
| 89 | + size_t len = tdigest_quantile_state.serialize(buf); |
| 90 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 91 | + tdigest_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 92 | + } |
| 93 | + |
| 94 | + { |
| 95 | + DataSet data_set = {{{&empty_quantile_state}, empty_base64}, |
| 96 | + {{&single_quantile_state}, single_base64}, |
| 97 | + {{&multi_quantile_state}, multi_base64}, |
| 98 | + {{&explicit_quantile_state}, explicit_base64}, |
| 99 | + {{&tdigest_quantile_state}, tdigest_base64}}; |
| 100 | + |
| 101 | + static_cast<void>(check_function<DataTypeString, true>(func_name, input_types, data_set)); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +TEST(function_quantile_state_test, function_quantile_state_from_base64) { |
| 106 | + std::string func_name = "quantile_state_from_base64"; |
| 107 | + InputTypeSet input_types = {TypeIndex::String}; |
| 108 | + |
| 109 | + // Create quantile states for comparison |
| 110 | + QuantileState empty_quantile_state; |
| 111 | + |
| 112 | + QuantileState single_quantile_state; |
| 113 | + single_quantile_state.add_value(1.0); |
| 114 | + |
| 115 | + QuantileState multi_quantile_state; |
| 116 | + multi_quantile_state.add_value(1.0); |
| 117 | + multi_quantile_state.add_value(2.0); |
| 118 | + multi_quantile_state.add_value(3.0); |
| 119 | + multi_quantile_state.add_value(4.0); |
| 120 | + multi_quantile_state.add_value(5.0); |
| 121 | + |
| 122 | + uint8_t buf[65536]; |
| 123 | + unsigned char encoded_buf[131072]; |
| 124 | + std::string empty_base64; |
| 125 | + std::string single_base64; |
| 126 | + std::string multi_base64; |
| 127 | + |
| 128 | + { |
| 129 | + size_t len = empty_quantile_state.serialize(buf); |
| 130 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 131 | + empty_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 132 | + } |
| 133 | + |
| 134 | + { |
| 135 | + size_t len = single_quantile_state.serialize(buf); |
| 136 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 137 | + single_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 138 | + } |
| 139 | + |
| 140 | + { |
| 141 | + size_t len = multi_quantile_state.serialize(buf); |
| 142 | + size_t encoded_len = base64_encode(buf, len, encoded_buf); |
| 143 | + multi_base64 = std::string(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 144 | + } |
| 145 | + |
| 146 | + { |
| 147 | + char decoded_buf[65536]; |
| 148 | + int decoded_len = base64_decode(empty_base64.c_str(), empty_base64.length(), decoded_buf); |
| 149 | + EXPECT_GT(decoded_len, 0); |
| 150 | + |
| 151 | + QuantileState decoded_empty; |
| 152 | + doris::Slice slice(decoded_buf, decoded_len); |
| 153 | + EXPECT_TRUE(decoded_empty.deserialize(slice)); |
| 154 | + |
| 155 | + EXPECT_TRUE(std::isnan(empty_quantile_state.get_value_by_percentile(0.5))); |
| 156 | + EXPECT_TRUE(std::isnan(decoded_empty.get_value_by_percentile(0.5))); |
| 157 | + } |
| 158 | + |
| 159 | + { |
| 160 | + char decoded_buf[65536]; |
| 161 | + int decoded_len = base64_decode(single_base64.c_str(), single_base64.length(), decoded_buf); |
| 162 | + EXPECT_GT(decoded_len, 0); |
| 163 | + |
| 164 | + QuantileState decoded_single; |
| 165 | + doris::Slice slice(decoded_buf, decoded_len); |
| 166 | + EXPECT_TRUE(decoded_single.deserialize(slice)); |
| 167 | + |
| 168 | + EXPECT_NEAR(single_quantile_state.get_value_by_percentile(0.5), |
| 169 | + decoded_single.get_value_by_percentile(0.5), 0.01); |
| 170 | + } |
| 171 | + |
| 172 | + { |
| 173 | + char decoded_buf[65536]; |
| 174 | + int decoded_len = base64_decode(multi_base64.c_str(), multi_base64.length(), decoded_buf); |
| 175 | + EXPECT_GT(decoded_len, 0); |
| 176 | + |
| 177 | + QuantileState decoded_multi; |
| 178 | + doris::Slice slice(decoded_buf, decoded_len); |
| 179 | + EXPECT_TRUE(decoded_multi.deserialize(slice)); |
| 180 | + |
| 181 | + EXPECT_NEAR(multi_quantile_state.get_value_by_percentile(0.5), |
| 182 | + decoded_multi.get_value_by_percentile(0.5), 0.01); |
| 183 | + EXPECT_NEAR(multi_quantile_state.get_value_by_percentile(0.9), |
| 184 | + decoded_multi.get_value_by_percentile(0.9), 0.01); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +TEST(function_quantile_state_test, function_quantile_state_roundtrip) { |
| 189 | + QuantileState original; |
| 190 | + for (int i = 0; i < 50; i++) { |
| 191 | + original.add_value(static_cast<double>(i * 2)); |
| 192 | + } |
| 193 | + |
| 194 | + uint8_t ser_buf[65536]; |
| 195 | + size_t ser_len = original.serialize(ser_buf); |
| 196 | + |
| 197 | + unsigned char encoded_buf[131072]; |
| 198 | + size_t encoded_len = base64_encode(ser_buf, ser_len, encoded_buf); |
| 199 | + std::string base64_str(reinterpret_cast<char*>(encoded_buf), encoded_len); |
| 200 | + |
| 201 | + char decoded_buf[65536]; |
| 202 | + int decoded_len = base64_decode(base64_str.c_str(), base64_str.length(), decoded_buf); |
| 203 | + EXPECT_GT(decoded_len, 0); |
| 204 | + |
| 205 | + QuantileState recovered; |
| 206 | + doris::Slice slice(decoded_buf, decoded_len); |
| 207 | + EXPECT_TRUE(recovered.deserialize(slice)); |
| 208 | + |
| 209 | + EXPECT_NEAR(original.get_value_by_percentile(0.5), recovered.get_value_by_percentile(0.5), |
| 210 | + 0.01); |
| 211 | + EXPECT_NEAR(original.get_value_by_percentile(0.9), recovered.get_value_by_percentile(0.9), |
| 212 | + 0.01); |
| 213 | +} |
| 214 | + |
| 215 | +} // namespace doris::vectorized |
0 commit comments