YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/common/ql_scanspec.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
//
14
// This file contains QLScanSpec that implements a QL scan (SELECT) specification.
15
16
#ifndef YB_COMMON_QL_SCANSPEC_H
17
#define YB_COMMON_QL_SCANSPEC_H
18
19
#include <map>
20
21
#include <boost/functional/hash.hpp>
22
#include <boost/optional/optional.hpp>
23
24
#include "yb/common/common_fwd.h"
25
#include "yb/common/column_id.h"
26
#include "yb/common/common_types.pb.h"
27
#include "yb/common/value.pb.h"
28
29
namespace yb {
30
31
//--------------------------------------------------------------------------------------------------
32
// YQL Scanspec.
33
// This class represents all scan specifications.
34
//--------------------------------------------------------------------------------------------------
35
class YQLScanSpec {
36
 public:
37
19.2M
  explicit YQLScanSpec(QLClient client_type) : client_type_(client_type) {
38
19.2M
  }
39
40
19.2M
  virtual ~YQLScanSpec() {
41
19.2M
  }
42
43
0
  QLClient client_type() const {
44
0
    return client_type_;
45
0
  }
46
47
 private:
48
  const QLClient client_type_;
49
};
50
51
//--------------------------------------------------------------------------------------------------
52
// CQL Support.
53
//--------------------------------------------------------------------------------------------------
54
55
// A class to determine the lower/upper-bound range components of a QL scan from its WHERE
56
// condition.
57
class QLScanRange {
58
 public:
59
60
  // Value range of a column
61
  struct QLRange {
62
    boost::optional<QLValuePB> min_value;
63
    boost::optional<QLValuePB> max_value;
64
  };
65
66
  QLScanRange(const Schema& schema, const QLConditionPB& condition);
67
  QLScanRange(const Schema& schema, const PgsqlConditionPB& condition);
68
69
477k
  QLRange RangeFor(ColumnId col_id) const {
70
477k
    const auto& iter = ranges_.find(col_id);
71
477k
    return (iter == ranges_.end() ? 
QLRange()0
: iter->second);
72
477k
  }
73
74
75.4k
  std::vector<ColumnId> GetColIds() const {
75
75.4k
    std::vector<ColumnId> col_id_list;
76
128k
    for (auto &it : ranges_) {
77
128k
      col_id_list.push_back(it.first);
78
128k
    }
79
75.4k
    return col_id_list;
80
75.4k
  }
81
82
28.4k
  bool has_in_range_options() const {
83
28.4k
    return has_in_range_options_;
84
28.4k
  }
85
86
  // Intersect / union / complement operators.
87
  QLScanRange& operator&=(const QLScanRange& other);
88
  QLScanRange& operator|=(const QLScanRange& other);
89
  QLScanRange& operator~();
90
91
  QLScanRange& operator=(QLScanRange&& other);
92
93
 private:
94
95
  // Table schema being scanned.
96
  const Schema& schema_;
97
98
  // Mapping of column id to the column value ranges (inclusive lower/upper bounds) to scan.
99
  std::unordered_map<ColumnId, QLRange, boost::hash<ColumnId>> ranges_;
100
101
  // Whether the condition has an IN condition on a range (clustering) column.
102
  // Used in doc_ql_scanspec to try to construct the set of options for a multi-point scan.
103
  bool has_in_range_options_ = false;
104
};
105
106
// A scan specification for a QL scan. It may be used to scan either a specified doc key
107
// or a hash key + optional WHERE condition clause.
108
class QLScanSpec : public YQLScanSpec {
109
 public:
110
  explicit QLScanSpec(QLExprExecutorPtr executor = nullptr);
111
112
  // Scan for the given hash key and a condition.
113
  QLScanSpec(const QLConditionPB* condition,
114
             const QLConditionPB* if_condition,
115
             const bool is_forward_scan,
116
             QLExprExecutorPtr executor = nullptr);
117
118
7.61M
  virtual ~QLScanSpec() {}
119
120
  // Evaluate the WHERE condition for the given row to decide if it is selected or not.
121
  // virtual to make the class polymorphic.
122
  virtual CHECKED_STATUS Match(const QLTableRow& table_row, bool* match) const;
123
124
7.39M
  bool is_forward_scan() const {
125
7.39M
    return is_forward_scan_;
126
7.39M
  }
127
128
  // Get Schema if available.
129
19.5M
  virtual const Schema* schema() const { return nullptr; }
130
131
 protected:
132
  const QLConditionPB* condition_;
133
  const QLConditionPB* if_condition_;
134
  const bool is_forward_scan_;
135
  QLExprExecutorPtr executor_;
136
};
137
138
//--------------------------------------------------------------------------------------------------
139
// PostgreSQL Support.
140
//--------------------------------------------------------------------------------------------------
141
class PgsqlScanSpec : public YQLScanSpec {
142
 public:
143
  typedef std::unique_ptr<PgsqlScanSpec> UniPtr;
144
145
  explicit PgsqlScanSpec(const PgsqlExpressionPB *where_expr,
146
                         QLExprExecutorPtr executor = nullptr);
147
148
  virtual ~PgsqlScanSpec();
149
150
0
  const PgsqlExpressionPB *where_expr() {
151
0
    return where_expr_;
152
0
  }
153
154
 protected:
155
  const PgsqlExpressionPB *where_expr_;
156
  QLExprExecutorPtr executor_;
157
};
158
159
} // namespace yb
160
161
#endif // YB_COMMON_QL_SCANSPEC_H