/Users/deen/code/yugabyte-db/src/yb/docdb/doc_scanspec_util.cc
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 | | #include "yb/docdb/doc_scanspec_util.h" |
15 | | |
16 | | #include "yb/common/ql_scanspec.h" |
17 | | #include "yb/common/ql_value.h" |
18 | | #include "yb/common/schema.h" |
19 | | #include "yb/docdb/primitive_value.h" |
20 | | #include "yb/docdb/value_type.h" |
21 | | |
22 | | namespace yb { |
23 | | namespace docdb { |
24 | | |
25 | | std::vector<PrimitiveValue> GetRangeKeyScanSpec( |
26 | | const Schema& schema, |
27 | | const std::vector<PrimitiveValue>* prefixed_hash_components, |
28 | | const QLScanRange* scan_range, |
29 | | bool lower_bound, |
30 | 25.9M | bool include_static_columns) { |
31 | 25.9M | std::vector<PrimitiveValue> range_components; |
32 | 25.9M | range_components.reserve(schema.num_range_key_columns()); |
33 | 25.9M | if (prefixed_hash_components) { |
34 | 5.29M | range_components.insert(range_components.begin(), |
35 | 5.29M | prefixed_hash_components->begin(), |
36 | 5.29M | prefixed_hash_components->end()); |
37 | 5.29M | } |
38 | 25.9M | if (scan_range != nullptr) { |
39 | | // Return the lower/upper range components for the scan. |
40 | | |
41 | 114k | for (size_t i = schema.num_hash_key_columns() + range_components.size(); |
42 | 305k | i < schema.num_key_columns(); |
43 | 190k | i++) { |
44 | 190k | const auto& column = schema.column(i); |
45 | 190k | const auto& range = scan_range->RangeFor(schema.column_id(i)); |
46 | 190k | range_components.emplace_back(GetQLRangeBoundAsPVal(range, |
47 | 190k | column.sorting_type(), |
48 | 190k | lower_bound)); |
49 | 190k | } |
50 | 114k | } |
51 | | |
52 | 25.9M | if (!lower_bound) { |
53 | | // We add +inf as an extra component to make sure this is greater than all keys in range. |
54 | | // For lower bound, this is true already, because dockey + suffix is > dockey |
55 | 13.0M | range_components.emplace_back(PrimitiveValue(ValueType::kHighest)); |
56 | 12.9M | } else if (schema.has_statics() && !include_static_columns && range_components.empty()) { |
57 | | // If we want to skip static columns, make sure the range components are non-empty. |
58 | | // We use kMinPrimitiveValueType instead of kLowest because it compares as higher than |
59 | | // kHybridTime in RocksDB. |
60 | 48 | range_components.emplace_back(kMinPrimitiveValueType); |
61 | 48 | } |
62 | 25.9M | return range_components; |
63 | 25.9M | } |
64 | | |
65 | | PrimitiveValue GetQLRangeBoundAsPVal(const QLScanRange::QLRange& ql_range, |
66 | | SortingType sorting_type, |
67 | 342k | bool lower_bound) { |
68 | 342k | const auto sort_order = PrimitiveValue::SortOrderFromColumnSchemaSortingType(sorting_type); |
69 | | |
70 | | // lower bound for ASC column and upper bound for DESC column -> min value |
71 | | // otherwise -> max value |
72 | | // for ASC col: lower -> min_value; upper -> max_value |
73 | | // for DESC : -> max_value; -> min_value |
74 | 342k | bool min_bound = lower_bound ^ (sort_order == SortOrder::kDescending); |
75 | 171k | const auto& ql_bound = min_bound ? ql_range.min_value : ql_range.max_value; |
76 | 342k | if (ql_bound) { |
77 | | // Special case for nulls because FromQLValuePB will turn them into kTombstone instead. |
78 | 188k | if (IsNull(*ql_bound)) { |
79 | 0 | return PrimitiveValue::NullValue(sorting_type); |
80 | 0 | } |
81 | 188k | return PrimitiveValue::FromQLValuePB(*ql_bound, sorting_type); |
82 | 188k | } |
83 | | |
84 | | // For unset use kLowest/kHighest to ensure we cover entire scanned range. |
85 | 153k | return lower_bound ? PrimitiveValue(ValueType::kLowest) |
86 | 76.8k | : PrimitiveValue(ValueType::kHighest); |
87 | 153k | } |
88 | | |
89 | | } // namespace docdb |
90 | | } // namespace yb |