/Users/deen/code/yugabyte-db/src/yb/docdb/doc_scanspec_util.cc
Line | Count | Source |
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 | 58.4M | bool include_static_columns) { |
31 | 58.4M | std::vector<PrimitiveValue> range_components; |
32 | 58.4M | range_components.reserve(schema.num_range_key_columns()); |
33 | 58.4M | if (prefixed_hash_components) { |
34 | 12.1M | range_components.insert(range_components.begin(), |
35 | 12.1M | prefixed_hash_components->begin(), |
36 | 12.1M | prefixed_hash_components->end()); |
37 | 12.1M | } |
38 | 58.4M | if (scan_range != nullptr) { |
39 | | // Return the lower/upper range components for the scan. |
40 | | |
41 | 218k | for (size_t i = schema.num_hash_key_columns() + range_components.size(); |
42 | 568k | i < schema.num_key_columns(); |
43 | 349k | i++) { |
44 | 349k | const auto& column = schema.column(i); |
45 | 349k | const auto& range = scan_range->RangeFor(schema.column_id(i)); |
46 | 349k | range_components.emplace_back(GetQLRangeBoundAsPVal(range, |
47 | 349k | column.sorting_type(), |
48 | 349k | lower_bound)); |
49 | 349k | } |
50 | 218k | } |
51 | | |
52 | 58.4M | 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 | 29.3M | range_components.emplace_back(PrimitiveValue(ValueType::kHighest)); |
56 | 29.3M | } else if (29.1M schema.has_statics()29.1M && !include_static_columns424 && range_components.empty()76 ) { |
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 | 58.4M | return range_components; |
63 | 58.4M | } |
64 | | |
65 | | PrimitiveValue GetQLRangeBoundAsPVal(const QLScanRange::QLRange& ql_range, |
66 | | SortingType sorting_type, |
67 | 604k | bool lower_bound) { |
68 | 604k | const auto sort_order = 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 | 604k | bool min_bound = lower_bound ^ (sort_order == SortOrder::kDescending); |
75 | 604k | const auto& ql_bound = min_bound ? ql_range.min_value302k : ql_range.max_value302k ; |
76 | 604k | if (ql_bound) { |
77 | | // Special case for nulls because FromQLValuePB will turn them into kTombstone instead. |
78 | 330k | if (IsNull(*ql_bound)) { |
79 | 20 | return PrimitiveValue::NullValue(sorting_type); |
80 | 20 | } |
81 | 330k | return PrimitiveValue::FromQLValuePB(*ql_bound, sorting_type); |
82 | 330k | } |
83 | | |
84 | | // For unset use kLowest/kHighest to ensure we cover entire scanned range. |
85 | 274k | return lower_bound ? PrimitiveValue(ValueType::kLowest)135k |
86 | 274k | : PrimitiveValue(ValueType::kHighest)139k ; |
87 | 604k | } |
88 | | |
89 | | } // namespace docdb |
90 | | } // namespace yb |