/Users/deen/code/yugabyte-db/src/yb/docdb/primitive_value_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 | | #include "yb/docdb/primitive_value_util.h" |
14 | | |
15 | | #include "yb/common/ql_expr.h" |
16 | | #include "yb/common/ql_value.h" |
17 | | #include "yb/common/pgsql_protocol.pb.h" |
18 | | #include "yb/common/schema.h" |
19 | | |
20 | | #include "yb/docdb/primitive_value.h" |
21 | | |
22 | | #include "yb/util/result.h" |
23 | | #include "yb/util/status_format.h" |
24 | | |
25 | | using std::vector; |
26 | | |
27 | | namespace yb { |
28 | | namespace docdb { |
29 | | |
30 | | // Add primary key column values to the component group. Verify that they are in the same order |
31 | | // as in the table schema. |
32 | | Status QLKeyColumnValuesToPrimitiveValues( |
33 | | const google::protobuf::RepeatedPtrField<QLExpressionPB> &column_values, |
34 | | const Schema &schema, size_t column_idx, const size_t column_count, |
35 | 16.6M | vector<PrimitiveValue> *components) { |
36 | 16.6M | for (const auto& column_value : column_values) { |
37 | 14.5M | if (!schema.is_key_column(column_idx)) { |
38 | 0 | auto status = STATUS_FORMAT( |
39 | 0 | Corruption, "Column at $0 is not key column in $1", column_idx, schema.ToString()); |
40 | 0 | LOG(DFATAL) << status; |
41 | 0 | return status; |
42 | 0 | } |
43 | | |
44 | 14.5M | if (!column_value.has_value() || IsNull(column_value.value())14.5M ) { |
45 | 4.30k | components->push_back(PrimitiveValue(ValueType::kNullLow)); |
46 | 14.5M | } else { |
47 | 14.5M | components->push_back(PrimitiveValue::FromQLValuePB( |
48 | 14.5M | column_value.value(), schema.column(column_idx).sorting_type())); |
49 | 14.5M | } |
50 | 14.5M | column_idx++; |
51 | 14.5M | } |
52 | 16.6M | return Status::OK(); |
53 | 16.6M | } |
54 | | |
55 | | // ------------------------------------------------------------------------------------------------ |
56 | | Result<vector<PrimitiveValue>> InitKeyColumnPrimitiveValues( |
57 | | const google::protobuf::RepeatedPtrField<PgsqlExpressionPB> &column_values, |
58 | | const Schema &schema, |
59 | 27.3M | size_t start_idx) { |
60 | 27.3M | vector<PrimitiveValue> values; |
61 | 27.3M | values.reserve(column_values.size()); |
62 | 27.3M | size_t column_idx = start_idx; |
63 | 27.3M | for (const auto& column_value : column_values) { |
64 | 26.5M | if (!schema.is_key_column(column_idx)) { |
65 | 0 | auto status = STATUS_FORMAT( |
66 | 0 | Corruption, "Column at $0 is not key column in $1", column_idx, schema.ToString()); |
67 | 0 | LOG(DFATAL) << status; |
68 | 0 | return status; |
69 | 0 | } |
70 | 26.5M | const auto sorting_type = schema.column(column_idx).sorting_type(); |
71 | 26.5M | if (column_value.has_value()26.5M ) { |
72 | 26.5M | const auto& value = column_value.value(); |
73 | 26.5M | values.push_back(IsNull(value) ? PrimitiveValue::NullValue(sorting_type)1.52M |
74 | 26.5M | : PrimitiveValue::FromQLValuePB(value, sorting_type)25.0M ); |
75 | 18.4E | } else { |
76 | | // TODO(neil) The current setup only works for CQL as it assumes primary key value must not |
77 | | // be dependent on any column values. This needs to be fixed as PostgreSQL expression might |
78 | | // require a read from a table. |
79 | | // |
80 | | // Use regular executor for now. |
81 | 18.4E | QLExprExecutor executor; |
82 | 18.4E | QLExprResult result; |
83 | 18.4E | RETURN_NOT_OK(executor.EvalExpr(column_value, nullptr, result.Writer(), &schema)); |
84 | | |
85 | 18.4E | values.push_back(PrimitiveValue::FromQLValuePB(result.Value(), sorting_type)); |
86 | 18.4E | } |
87 | 26.5M | column_idx++; |
88 | 26.5M | } |
89 | 27.3M | return std::move(values); |
90 | 27.3M | } |
91 | | |
92 | | } // namespace docdb |
93 | | } // namespace yb |