/Users/deen/code/yugabyte-db/src/yb/tablet/abstract_tablet.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/tablet/abstract_tablet.h" |
15 | | |
16 | | #include "yb/common/ql_resultset.h" |
17 | | #include "yb/common/ql_value.h" |
18 | | #include "yb/common/schema.h" |
19 | | |
20 | | #include "yb/docdb/cql_operation.h" |
21 | | #include "yb/docdb/pgsql_operation.h" |
22 | | |
23 | | #include "yb/tablet/read_result.h" |
24 | | |
25 | | #include "yb/util/trace.h" |
26 | | |
27 | | namespace yb { |
28 | | namespace tablet { |
29 | | |
30 | | Result<HybridTime> AbstractTablet::SafeTime(RequireLease require_lease, |
31 | | HybridTime min_allowed, |
32 | 10.5M | CoarseTimePoint deadline) const { |
33 | 10.5M | return DoGetSafeTime(require_lease, min_allowed, deadline); |
34 | 10.5M | } |
35 | | |
36 | | Status AbstractTablet::HandleQLReadRequest(CoarseTimePoint deadline, |
37 | | const ReadHybridTime& read_time, |
38 | | const QLReadRequestPB& ql_read_request, |
39 | | const TransactionOperationContext& txn_op_context, |
40 | 7.52M | QLReadRequestResult* result) { |
41 | | |
42 | | // TODO(Robert): verify that all key column values are provided |
43 | 7.52M | docdb::QLReadOperation doc_op(ql_read_request, txn_op_context); |
44 | | |
45 | | // Form a schema of columns that are referenced by this query. |
46 | 7.52M | const SchemaPtr schema = GetSchema(); |
47 | 7.52M | Schema projection; |
48 | 7.52M | const QLReferencedColumnsPB& column_pbs = ql_read_request.column_refs(); |
49 | 7.52M | vector<ColumnId> column_refs; |
50 | 7.52M | for (int32_t id : column_pbs.static_ids()) { |
51 | 430 | column_refs.emplace_back(id); |
52 | 430 | } |
53 | 24.0M | for (int32_t id : column_pbs.ids()) { |
54 | 24.0M | column_refs.emplace_back(id); |
55 | 24.0M | } |
56 | 7.52M | RETURN_NOT_OK(schema->CreateProjectionByIdsIgnoreMissing(column_refs, &projection)); |
57 | | |
58 | 7.52M | const QLRSRowDesc rsrow_desc(ql_read_request.rsrow_desc()); |
59 | 7.52M | QLResultSet resultset(&rsrow_desc, &result->rows_data); |
60 | 7.52M | TRACE("Start Execute"); |
61 | 7.52M | const Status s = doc_op.Execute( |
62 | 7.52M | QLStorage(), deadline, read_time, *schema, projection, &resultset, &result->restart_read_ht); |
63 | 7.52M | TRACE("Done Execute"); |
64 | 7.52M | if (!s.ok()) { |
65 | 2 | if (s.IsQLError()) { |
66 | 2 | result->response.set_status(QLResponsePB::YQL_STATUS_USAGE_ERROR); |
67 | 2 | } else { |
68 | 0 | result->response.set_status(QLResponsePB::YQL_STATUS_RUNTIME_ERROR); |
69 | 0 | } |
70 | 2 | result->response.set_error_message(s.message().cdata(), s.message().size()); |
71 | 2 | return Status::OK(); |
72 | 2 | } |
73 | 7.52M | result->response.Swap(&doc_op.response()); |
74 | | |
75 | 7.52M | RETURN_NOT_OK(CreatePagingStateForRead( |
76 | 7.52M | ql_read_request, resultset.rsrow_count(), &result->response)); |
77 | | |
78 | 7.52M | result->response.set_status(QLResponsePB::YQL_STATUS_OK); |
79 | 7.52M | return Status::OK(); |
80 | 7.52M | } |
81 | | |
82 | | Status AbstractTablet::HandlePgsqlReadRequest(CoarseTimePoint deadline, |
83 | | const ReadHybridTime& read_time, |
84 | | bool is_explicit_request_read_time, |
85 | | const PgsqlReadRequestPB& pgsql_read_request, |
86 | | const TransactionOperationContext& txn_op_context, |
87 | | PgsqlReadRequestResult* result, |
88 | 3.47M | size_t* num_rows_read) { |
89 | | |
90 | 3.47M | docdb::PgsqlReadOperation doc_op(pgsql_read_request, txn_op_context); |
91 | | |
92 | | // Form a schema of columns that are referenced by this query. |
93 | 3.47M | const SchemaPtr schema = GetSchema(pgsql_read_request.table_id()); |
94 | 3.47M | const SchemaPtr index_schema = pgsql_read_request.has_index_request() |
95 | 3.47M | ? GetSchema(pgsql_read_request.index_request().table_id())458k : nullptr3.01M ; |
96 | | |
97 | 3.47M | TRACE("Start Execute"); |
98 | 3.47M | auto fetched_rows = doc_op.Execute( |
99 | 3.47M | QLStorage(), deadline, read_time, is_explicit_request_read_time, *schema, index_schema.get(), |
100 | 3.47M | &result->rows_data, &result->restart_read_ht); |
101 | 3.47M | TRACE("Done Execute"); |
102 | 3.47M | if (!fetched_rows.ok()) { |
103 | 0 | result->response.set_status(PgsqlResponsePB::PGSQL_STATUS_RUNTIME_ERROR); |
104 | 0 | const auto& s = fetched_rows.status(); |
105 | 0 | result->response.set_error_message(s.message().cdata(), s.message().size()); |
106 | 0 | return Status::OK(); |
107 | 0 | } |
108 | 3.47M | result->response.Swap(&doc_op.response()); |
109 | | |
110 | 3.47M | if (num_rows_read) { |
111 | 3.46M | *num_rows_read = *fetched_rows; |
112 | 3.46M | } |
113 | | |
114 | 3.47M | RETURN_NOT_OK(CreatePagingStateForRead( |
115 | 3.47M | pgsql_read_request, *fetched_rows, &result->response)); |
116 | | |
117 | | // TODO(neil) The clients' request should indicate what encoding method should be used. When |
118 | | // multi-shard is used to process more complicated queries, proxy-server might prefer a different |
119 | | // encoding. For now, we'll call PgsqlSerialize() without checking encoding method. |
120 | 3.47M | result->response.set_status(PgsqlResponsePB::PGSQL_STATUS_OK); |
121 | | |
122 | | // Serializing data for PgGate API. |
123 | 3.47M | CHECK(!pgsql_read_request.has_rsrow_desc()) << "Row description is not needed"5.09k ; |
124 | 3.47M | TRACE("Done Handle"); |
125 | | |
126 | 3.47M | return Status::OK(); |
127 | 3.47M | } |
128 | | |
129 | | } // namespace tablet |
130 | | } // namespace yb |