YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_select.cc
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//--------------------------------------------------------------------------------------------------
15
16
#include "yb/yql/pggate/pg_select.h"
17
18
#include "yb/client/yb_op.h"
19
20
#include "yb/yql/pggate/pg_select_index.h"
21
#include "yb/yql/pggate/util/pg_doc_data.h"
22
23
namespace yb {
24
namespace pggate {
25
26
using std::make_shared;
27
28
//--------------------------------------------------------------------------------------------------
29
// PgSelect
30
//--------------------------------------------------------------------------------------------------
31
32
PgSelect::PgSelect(PgSession::ScopedRefPtr pg_session, const PgObjectId& table_id,
33
                   const PgObjectId& index_id, const PgPrepareParameters *prepare_params)
34
2.05M
    : PgDmlRead(pg_session, table_id, index_id, prepare_params) {}
35
36
2.05M
PgSelect::~PgSelect() {
37
2.05M
}
38
39
1.49M
Result<PgTableDescPtr> PgSelect::LoadTable() {
40
1.49M
  return pg_session_->LoadTable(table_id_);
41
1.49M
}
42
43
1.49M
bool PgSelect::UseSecondaryIndex() const {
44
1.49M
  return prepare_params_.use_secondary_index;
45
1.49M
}
46
47
1.59M
Status PgSelect::Prepare() {
48
  // Prepare target and bind descriptors.
49
1.59M
  target_ = PgTable(VERIFY_RESULT(LoadTable()));
50
51
1.59M
  if (!UseSecondaryIndex()) {
52
1.13M
    bind_ = target_;
53
1.13M
  } else {
54
460k
    bind_ = PgTable(nullptr);
55
56
    // Create secondary index query.
57
460k
    secondary_index_query_ =
58
460k
      std::make_unique<PgSelectIndex>(pg_session_, table_id_, index_id_, &prepare_params_);
59
460k
  }
60
61
  // Allocate READ requests to send to DocDB.
62
1.59M
  auto read_op = std::make_shared<PgsqlReadOp>(*target_);
63
1.59M
  read_req_ = std::shared_ptr<PgsqlReadRequestPB>(read_op, &read_op->read_request());
64
65
1.59M
  auto doc_op = std::make_shared<PgDocReadOp>(pg_session_, &target_, std::move(read_op));
66
67
  // Prepare the index selection if this operation is using the index.
68
1.59M
  RETURN_NOT_OK(PrepareSecondaryIndex());
69
70
  // Prepare binds for the request.
71
1.59M
  PrepareBinds();
72
73
1.59M
  doc_op_ = doc_op;
74
1.59M
  return Status::OK();
75
1.59M
}
76
77
1.59M
Status PgSelect::PrepareSecondaryIndex() {
78
1.59M
  if (!secondary_index_query_) {
79
    // This DML statement is not using secondary index.
80
1.12M
    return Status::OK();
81
1.12M
  }
82
83
  // Prepare the index operation to read ybctids from the index table. There are two different
84
  // scenarios on how ybctids are requested.
85
  // - Due to an optimization in DocDB, for colocated tables (both system and user colocated), index
86
  //   request is sent as a part of the actual read request using protobuf field
87
  //   "PgsqlReadRequestPB::index_request"
88
  //
89
  //   For this case, "mutable_index_request" is allocated here and passed to PgSelectIndex node to
90
  //   fill in with bind-values when necessary.
91
  //
92
  // - For regular tables, the index subquery will send separate request to tablet servers collect
93
  //   batches of ybctids which is then used by 'this' outer select to query actual data.
94
461k
  std::shared_ptr<PgsqlReadRequestPB> index_req = nullptr;
95
461k
  if (prepare_params_.querying_colocated_table) {
96
    // Allocate "index_request" and pass to PgSelectIndex.
97
458k
    index_req = std::shared_ptr<PgsqlReadRequestPB>(read_req_, read_req_->mutable_index_request());
98
458k
  }
99
100
  // Prepare subquery. When index_req is not null, it is part of 'this' SELECT request. When it
101
  // is nullptr, the subquery will create its own doc_op to run a separate read request.
102
461k
  return secondary_index_query_->PrepareSubquery(index_req);
103
1.59M
}
104
105
}  // namespace pggate
106
}  // namespace yb