/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_sample.h
Line | Count | Source |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. 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 distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | //-------------------------------------------------------------------------------------------------- |
14 | | |
15 | | #ifndef YB_YQL_PGGATE_PG_SAMPLE_H_ |
16 | | #define YB_YQL_PGGATE_PG_SAMPLE_H_ |
17 | | |
18 | | #include "yb/yql/pggate/pg_select_index.h" |
19 | | |
20 | | |
21 | | namespace yb { |
22 | | namespace pggate { |
23 | | |
24 | | //-------------------------------------------------------------------------------------------------- |
25 | | // SAMPLE collect table statistics and take random rows sample |
26 | | //-------------------------------------------------------------------------------------------------- |
27 | | class PgSample : public PgDmlRead { |
28 | | public: |
29 | | PgSample(PgSession::ScopedRefPtr pg_session, |
30 | | const int targrows, |
31 | | const PgObjectId& table_id); |
32 | | virtual ~PgSample(); |
33 | | |
34 | 1.47k | StmtOp stmt_op() const override { return StmtOp::STMT_SAMPLE; } |
35 | | |
36 | | // Prepare query |
37 | | CHECKED_STATUS Prepare() override; |
38 | | |
39 | | // Prepare PgSamplePicker's random state |
40 | | CHECKED_STATUS InitRandomState(double rstate_w, uint64 rand_state); |
41 | | |
42 | | // Make PgSamplePicker to process next block of rows in the table. |
43 | | // The has_more parameter is set to true if table has and needs more blocks. |
44 | | // PgSampler is not ready to be executed until this function returns false |
45 | | CHECKED_STATUS SampleNextBlock(bool *has_more); |
46 | | |
47 | | // Retrieve estimated number of live and dead rows. Available after execution. |
48 | | CHECKED_STATUS GetEstimatedRowCount(double *liverows, double *deadrows); |
49 | | |
50 | | private: |
51 | | // How many sample rows are needed |
52 | | const int targrows_; |
53 | | }; |
54 | | |
55 | | // Internal class to work as the secondary_index_query_ to select sample tuples. |
56 | | // Like index, it produces ybctids of random records and outer PgSample fetches them. |
57 | | // Unlike index, it does not use a secondary index, but scans main table instead. |
58 | | class PgSamplePicker : public PgSelectIndex { |
59 | | public: |
60 | | PgSamplePicker(PgSession::ScopedRefPtr pg_session, |
61 | | const PgObjectId& table_id); |
62 | | virtual ~PgSamplePicker(); |
63 | | |
64 | | // Prepare picker |
65 | | CHECKED_STATUS Prepare() override; |
66 | | |
67 | | // Seed random numbers generator before execution |
68 | | CHECKED_STATUS PrepareSamplingState(int targrows, double rstate_w, uint64 rand_state); |
69 | | |
70 | | // Process next block of table rows and update the reservoir with ybctids of randomly selected |
71 | | // rows from the block. Returns true if there is another block to process. |
72 | | // Reservoir is not finalized until this function returns false. |
73 | | Result<bool> ProcessNextBlock(); |
74 | | |
75 | | // Overrides inherited function returning ybctids of records to fetch. |
76 | | // PgSamplePicker::FetchYbctidBatch returns entire reservoir in one batch. |
77 | | virtual Result<bool> FetchYbctidBatch(const vector<Slice> **ybctids) override; |
78 | | |
79 | | // Retrieve estimated number of live and dead rows. Available after execution. |
80 | | CHECKED_STATUS GetEstimatedRowCount(double *liverows, double *deadrows); |
81 | | |
82 | | private: |
83 | | // The reservoir to keep ybctids of selected sample rows |
84 | | std::unique_ptr<std::string[]> reservoir_; |
85 | | // If true sampling is completed and ybctids can be collected from the reservoir |
86 | | bool reservoir_ready_ = false; |
87 | | // Vector of Slices pointing to the values in the reservoir |
88 | | vector<Slice> ybctids_; |
89 | | }; |
90 | | |
91 | | } // namespace pggate |
92 | | } // namespace yb |
93 | | |
94 | | #endif // YB_YQL_PGGATE_PG_SAMPLE_H_ |