/Users/deen/code/yugabyte-db/src/yb/common/ql_protocol_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/common/ql_protocol_util.h" |
15 | | |
16 | | #include "yb/common/ql_protocol.pb.h" |
17 | | #include "yb/common/ql_rowblock.h" |
18 | | #include "yb/common/ql_type.h" |
19 | | #include "yb/common/schema.h" |
20 | | |
21 | | #include "yb/gutil/casts.h" |
22 | | |
23 | | #include "yb/util/result.h" |
24 | | #include "yb/util/status_log.h" |
25 | | |
26 | | namespace yb { |
27 | | |
28 | 3.38M | QLValuePB* QLPrepareColumn(QLWriteRequestPB* req, int column_id) { |
29 | 3.38M | auto column_value = req->add_column_values(); |
30 | 3.38M | column_value->set_column_id(column_id); |
31 | 3.38M | return column_value->mutable_expr()->mutable_value(); |
32 | 3.38M | } |
33 | | |
34 | 27.5k | QLValuePB* QLPrepareCondition(QLConditionPB* condition, int column_id, QLOperator op) { |
35 | 27.5k | condition->add_operands()->set_column_id(column_id); |
36 | 27.5k | condition->set_op(op); |
37 | 27.5k | return condition->add_operands()->mutable_value(); |
38 | 27.5k | } |
39 | | |
40 | | #define QL_PROTOCOL_TYPE_DEFINITIONS_IMPL(name, lname, type) \ |
41 | | void PP_CAT3(QLAdd, name, ColumnValue)( \ |
42 | 132k | QLWriteRequestPB* req, int column_id, type value) { \ |
43 | 132k | QLPrepareColumn(req, column_id)->PP_CAT3(set_, lname, _value)(value); \ |
44 | 132k | } \ Unexecuted instantiation: _ZN2yb20QLAddInt8ColumnValueEPNS_16QLWriteRequestPBEia Unexecuted instantiation: _ZN2yb21QLAddInt16ColumnValueEPNS_16QLWriteRequestPBEis _ZN2yb21QLAddInt32ColumnValueEPNS_16QLWriteRequestPBEii Line | Count | Source | 42 | 51.8k | QLWriteRequestPB* req, int column_id, type value) { \ | 43 | 51.8k | QLPrepareColumn(req, column_id)->PP_CAT3(set_, lname, _value)(value); \ | 44 | 51.8k | } \ |
Unexecuted instantiation: _ZN2yb21QLAddInt64ColumnValueEPNS_16QLWriteRequestPBEix _ZN2yb22QLAddStringColumnValueEPNS_16QLWriteRequestPBEiRKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE Line | Count | Source | 42 | 80.7k | QLWriteRequestPB* req, int column_id, type value) { \ | 43 | 80.7k | QLPrepareColumn(req, column_id)->PP_CAT3(set_, lname, _value)(value); \ | 44 | 80.7k | } \ |
Unexecuted instantiation: _ZN2yb22QLAddBinaryColumnValueEPNS_16QLWriteRequestPBEiRKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb20QLAddBoolColumnValueEPNS_16QLWriteRequestPBEib Unexecuted instantiation: _ZN2yb21QLAddFloatColumnValueEPNS_16QLWriteRequestPBEif Unexecuted instantiation: _ZN2yb22QLAddDoubleColumnValueEPNS_16QLWriteRequestPBEid Unexecuted instantiation: _ZN2yb21QLAddJsonbColumnValueEPNS_16QLWriteRequestPBEiRKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb25QLAddTimestampColumnValueEPNS_16QLWriteRequestPBEix |
45 | | \ |
46 | 1.99M | void PP_CAT3(QLSet, name, Expression)(QLExpressionPB* expr, type value) { \ |
47 | 1.99M | expr->mutable_value()->PP_CAT3(set_, lname, _value)(value); \ |
48 | 1.99M | } \ _ZN2yb19QLSetInt8ExpressionEPNS_14QLExpressionPBEa Line | Count | Source | 46 | 135k | void PP_CAT3(QLSet, name, Expression)(QLExpressionPB* expr, type value) { \ | 47 | 135k | expr->mutable_value()->PP_CAT3(set_, lname, _value)(value); \ | 48 | 135k | } \ |
_ZN2yb20QLSetInt16ExpressionEPNS_14QLExpressionPBEs Line | Count | Source | 46 | 71.3k | void PP_CAT3(QLSet, name, Expression)(QLExpressionPB* expr, type value) { \ | 47 | 71.3k | expr->mutable_value()->PP_CAT3(set_, lname, _value)(value); \ | 48 | 71.3k | } \ |
_ZN2yb20QLSetInt32ExpressionEPNS_14QLExpressionPBEi Line | Count | Source | 46 | 1.53M | void PP_CAT3(QLSet, name, Expression)(QLExpressionPB* expr, type value) { \ | 47 | 1.53M | expr->mutable_value()->PP_CAT3(set_, lname, _value)(value); \ | 48 | 1.53M | } \ |
_ZN2yb20QLSetInt64ExpressionEPNS_14QLExpressionPBEx Line | Count | Source | 46 | 173k | void PP_CAT3(QLSet, name, Expression)(QLExpressionPB* expr, type value) { \ | 47 | 173k | expr->mutable_value()->PP_CAT3(set_, lname, _value)(value); \ | 48 | 173k | } \ |
_ZN2yb21QLSetStringExpressionEPNS_14QLExpressionPBERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE Line | Count | Source | 46 | 76.7k | void PP_CAT3(QLSet, name, Expression)(QLExpressionPB* expr, type value) { \ | 47 | 76.7k | expr->mutable_value()->PP_CAT3(set_, lname, _value)(value); \ | 48 | 76.7k | } \ |
_ZN2yb21QLSetBinaryExpressionEPNS_14QLExpressionPBERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE Line | Count | Source | 46 | 300 | void PP_CAT3(QLSet, name, Expression)(QLExpressionPB* expr, type value) { \ | 47 | 300 | expr->mutable_value()->PP_CAT3(set_, lname, _value)(value); \ | 48 | 300 | } \ |
Unexecuted instantiation: _ZN2yb19QLSetBoolExpressionEPNS_14QLExpressionPBEb Unexecuted instantiation: _ZN2yb20QLSetFloatExpressionEPNS_14QLExpressionPBEf Unexecuted instantiation: _ZN2yb21QLSetDoubleExpressionEPNS_14QLExpressionPBEd Unexecuted instantiation: _ZN2yb20QLSetJsonbExpressionEPNS_14QLExpressionPBERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb24QLSetTimestampExpressionEPNS_14QLExpressionPBEx |
49 | | \ |
50 | | void PP_CAT3(QLSet, name, Condition)( \ |
51 | 27.5k | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ |
52 | 27.5k | QLPrepareCondition(condition, column_id, op)->PP_CAT3(set_, lname, _value)(value); \ |
53 | 27.5k | } \ _ZN2yb18QLSetInt8ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEa Line | Count | Source | 51 | 26.2k | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 52 | 26.2k | QLPrepareCondition(condition, column_id, op)->PP_CAT3(set_, lname, _value)(value); \ | 53 | 26.2k | } \ |
_ZN2yb19QLSetInt16ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEs Line | Count | Source | 51 | 14 | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 52 | 14 | QLPrepareCondition(condition, column_id, op)->PP_CAT3(set_, lname, _value)(value); \ | 53 | 14 | } \ |
_ZN2yb19QLSetInt32ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEi Line | Count | Source | 51 | 994 | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 52 | 994 | QLPrepareCondition(condition, column_id, op)->PP_CAT3(set_, lname, _value)(value); \ | 53 | 994 | } \ |
Unexecuted instantiation: _ZN2yb19QLSetInt64ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEx _ZN2yb20QLSetStringConditionEPNS_13QLConditionPBEiNS_10QLOperatorERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE Line | Count | Source | 51 | 308 | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 52 | 308 | QLPrepareCondition(condition, column_id, op)->PP_CAT3(set_, lname, _value)(value); \ | 53 | 308 | } \ |
Unexecuted instantiation: _ZN2yb20QLSetBinaryConditionEPNS_13QLConditionPBEiNS_10QLOperatorERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb18QLSetBoolConditionEPNS_13QLConditionPBEiNS_10QLOperatorEb Unexecuted instantiation: _ZN2yb19QLSetFloatConditionEPNS_13QLConditionPBEiNS_10QLOperatorEf Unexecuted instantiation: _ZN2yb20QLSetDoubleConditionEPNS_13QLConditionPBEiNS_10QLOperatorEd Unexecuted instantiation: _ZN2yb19QLSetJsonbConditionEPNS_13QLConditionPBEiNS_10QLOperatorERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb23QLSetTimestampConditionEPNS_13QLConditionPBEiNS_10QLOperatorEx |
54 | | \ |
55 | | void PP_CAT3(QLAdd, name, Condition)( \ |
56 | 26.5k | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ |
57 | 26.5k | PP_CAT3(QLSet, name, Condition)( \ |
58 | 26.5k | condition->add_operands()->mutable_condition(), column_id, op, value); \ |
59 | 26.5k | } \ _ZN2yb18QLAddInt8ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEa Line | Count | Source | 56 | 26.2k | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 57 | 26.2k | PP_CAT3(QLSet, name, Condition)( \ | 58 | 26.2k | condition->add_operands()->mutable_condition(), column_id, op, value); \ | 59 | 26.2k | } \ |
_ZN2yb19QLAddInt16ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEs Line | Count | Source | 56 | 11 | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 57 | 11 | PP_CAT3(QLSet, name, Condition)( \ | 58 | 11 | condition->add_operands()->mutable_condition(), column_id, op, value); \ | 59 | 11 | } \ |
_ZN2yb19QLAddInt32ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEi Line | Count | Source | 56 | 4 | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 57 | 4 | PP_CAT3(QLSet, name, Condition)( \ | 58 | 4 | condition->add_operands()->mutable_condition(), column_id, op, value); \ | 59 | 4 | } \ |
Unexecuted instantiation: _ZN2yb19QLAddInt64ConditionEPNS_13QLConditionPBEiNS_10QLOperatorEx _ZN2yb20QLAddStringConditionEPNS_13QLConditionPBEiNS_10QLOperatorERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE Line | Count | Source | 56 | 308 | QLConditionPB* condition, int column_id, QLOperator op, type value) { \ | 57 | 308 | PP_CAT3(QLSet, name, Condition)( \ | 58 | 308 | condition->add_operands()->mutable_condition(), column_id, op, value); \ | 59 | 308 | } \ |
Unexecuted instantiation: _ZN2yb20QLAddBinaryConditionEPNS_13QLConditionPBEiNS_10QLOperatorERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb18QLAddBoolConditionEPNS_13QLConditionPBEiNS_10QLOperatorEb Unexecuted instantiation: _ZN2yb19QLAddFloatConditionEPNS_13QLConditionPBEiNS_10QLOperatorEf Unexecuted instantiation: _ZN2yb20QLAddDoubleConditionEPNS_13QLConditionPBEiNS_10QLOperatorEd Unexecuted instantiation: _ZN2yb19QLAddJsonbConditionEPNS_13QLConditionPBEiNS_10QLOperatorERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb23QLAddTimestampConditionEPNS_13QLConditionPBEiNS_10QLOperatorEx |
60 | | |
61 | | #define QL_PROTOCOL_TYPE_DEFINITIONS(i, data, entry) QL_PROTOCOL_TYPE_DEFINITIONS_IMPL entry |
62 | | |
63 | | BOOST_PP_SEQ_FOR_EACH(QL_PROTOCOL_TYPE_DEFINITIONS, ~, QL_PROTOCOL_TYPES); |
64 | | |
65 | 5.72k | void QLAddNullColumnValue(QLWriteRequestPB* req, int column_id) { |
66 | 5.72k | QLPrepareColumn(req, column_id); |
67 | 5.72k | } |
68 | | |
69 | | void QLAddColumns(const Schema& schema, const std::vector<ColumnId>& columns, |
70 | 2.03k | QLReadRequestPB* req) { |
71 | 2.03k | if (columns.empty()) { |
72 | 1.01k | QLAddColumns(schema, schema.column_ids(), req); |
73 | 1.01k | return; |
74 | 1.01k | } |
75 | 1.01k | req->clear_selected_exprs(); |
76 | 1.01k | req->mutable_column_refs()->Clear(); |
77 | 1.01k | QLRSRowDescPB* rsrow_desc = req->mutable_rsrow_desc(); |
78 | 1.01k | rsrow_desc->Clear(); |
79 | 2.07k | for (const auto& id : columns) { |
80 | 2.07k | auto column = schema.column_by_id(id); |
81 | 2.07k | CHECK_OK(column); |
82 | 2.07k | req->add_selected_exprs()->set_column_id(id); |
83 | 2.07k | req->mutable_column_refs()->add_ids(id); |
84 | | |
85 | 2.07k | QLRSColDescPB* rscol_desc = rsrow_desc->add_rscol_descs(); |
86 | 2.07k | rscol_desc->set_name(column->name()); |
87 | 2.07k | column->type()->ToQLTypePB(rscol_desc->mutable_ql_type()); |
88 | 2.07k | } |
89 | 1.01k | } |
90 | | |
91 | 188k | std::unique_ptr<QLRowBlock> CreateRowBlock(QLClient client, const Schema& schema, Slice data) { |
92 | 188k | auto rowblock = std::make_unique<QLRowBlock>(schema); |
93 | 188k | if (!data.empty()) { |
94 | | // TODO: a better way to handle errors here? |
95 | 188k | CHECK_OK(rowblock->Deserialize(client, &data)); |
96 | 188k | } |
97 | 188k | return rowblock; |
98 | 188k | } |
99 | | |
100 | 6.71M | bool RequireReadForExpressions(const QLWriteRequestPB& request) { |
101 | | // A QLWriteOperation requires a read if it contains an IF clause or an UPDATE assignment that |
102 | | // involves an expresion with a column reference. If the IF clause contains a condition that |
103 | | // involves a column reference, the column will be included in "column_refs". However, we cannot |
104 | | // rely on non-empty "column_ref" alone to decide if a read is required because "IF EXISTS" and |
105 | | // "IF NOT EXISTS" do not involve a column reference explicitly. |
106 | 6.71M | return request.has_if_expr() || |
107 | 6.71M | (request.has_column_refs() && |
108 | 2.53M | (!request.column_refs().ids().empty() || !request.column_refs().static_ids().empty())); |
109 | 6.71M | } |
110 | | |
111 | | // If range key portion is missing and there are no targeted columns this is a range operation |
112 | | // (e.g. range delete) -- it affects all rows within a hash key that match the where clause. |
113 | | // Note: If target columns are given this could just be e.g. a delete targeting a static column |
114 | | // which can also omit the range portion -- Analyzer will check these restrictions. |
115 | 6.76M | bool IsRangeOperation(const QLWriteRequestPB& request, const Schema& schema) { |
116 | 6.76M | return implicit_cast<size_t>(request.range_column_values().size()) < |
117 | 6.76M | schema.num_range_key_columns() && |
118 | 167 | request.column_values().empty(); |
119 | 6.76M | } |
120 | | |
121 | 3.38M | bool RequireRead(const QLWriteRequestPB& request, const Schema& schema) { |
122 | | // In case of a user supplied timestamp, we need a read (and hence appropriate locks for read |
123 | | // modify write) but it is at the docdb level on a per key basis instead of a QL read of the |
124 | | // latest row. |
125 | 3.38M | bool has_user_timestamp = request.has_user_timestamp_usec(); |
126 | | |
127 | | // We need to read the rows in the given range to find out which rows to write to. |
128 | 3.38M | bool is_range_operation = IsRangeOperation(request, schema); |
129 | | |
130 | 3.38M | return RequireReadForExpressions(request) || has_user_timestamp || is_range_operation; |
131 | 3.38M | } |
132 | | |
133 | 3.97M | Result<int32_t> CQLDecodeLength(Slice* data) { |
134 | 3.97M | RETURN_NOT_ENOUGH(data, sizeof(int32_t)); |
135 | 3.97M | const auto len = static_cast<int32_t>(NetworkByteOrder::Load32(data->data())); |
136 | 3.97M | data->remove_prefix(sizeof(int32_t)); |
137 | 3.97M | return len; |
138 | 3.97M | } |
139 | | |
140 | 42.3M | void CQLEncodeLength(const ssize_t length, faststring* buffer) { |
141 | 42.3M | uint32_t byte_value; |
142 | 42.3M | NetworkByteOrder::Store32(&byte_value, narrow_cast<int32_t>(length)); |
143 | 42.3M | buffer->append(&byte_value, sizeof(byte_value)); |
144 | 42.3M | } |
145 | | |
146 | | // Encode a 32-bit length into the buffer without extending the buffer. Caller should ensure the |
147 | | // buffer size is at least 4 bytes. |
148 | 7.40M | void CQLEncodeLength(const ssize_t length, void* buffer) { |
149 | 7.40M | NetworkByteOrder::Store32(buffer, narrow_cast<int32_t>(length)); |
150 | 7.40M | } |
151 | | |
152 | | } // namespace yb |