|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <IO/Compression/ALP/Compression.h> |
| 18 | + |
| 19 | +#include <random> |
| 20 | +#include <span> |
| 21 | + |
| 22 | +namespace DB::ALP |
| 23 | +{ |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +class Analyze |
| 27 | +{ |
| 28 | +public: |
| 29 | + using State = CompressionState<T>; |
| 30 | + |
| 31 | + static void run(const std::span<const T> & vectors, size_t n_samples, State & state) |
| 32 | + { |
| 33 | + std::vector<std::vector<T>> vectors_sampled; |
| 34 | + vectors_sampled.reserve(n_samples); |
| 35 | + |
| 36 | + std::random_device rd; |
| 37 | + std::mt19937 gen(rd()); |
| 38 | + std::uniform_int_distribution<> dis(0, vectors.size() - 1); |
| 39 | + |
| 40 | + // Sample vectors. |
| 41 | + // Each vector is a sample of SAMPLES_PER_VECTOR randomly selected values. |
| 42 | + for (size_t i = 0; i < n_samples; ++i) |
| 43 | + { |
| 44 | + std::vector<T> sample; |
| 45 | + sample.reserve(Constants::SAMPLES_PER_VECTOR); |
| 46 | + for (size_t j = 0; j < Constants::SAMPLES_PER_VECTOR; ++j) |
| 47 | + { |
| 48 | + auto random_idx = dis(gen); |
| 49 | + sample.push_back(vectors[random_idx]); |
| 50 | + } |
| 51 | + vectors_sampled.emplace_back(std::move(sample)); |
| 52 | + } |
| 53 | + Compression<T, true>::findTopKCombinations(vectors_sampled, state); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +}; // namespace DB::ALP |
0 commit comments