YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_select.cc
Line
Count
Source
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
713k
    : PgDmlRead(pg_session, table_id, index_id, prepare_params) {}
35
36
713k
PgSelect::~PgSelect() {
37
713k
}
38
39
517k
Result<PgTableDescPtr> PgSelect::LoadTable() {
40
517k
  return pg_session_->LoadTable(table_id_);
41
517k
}
42
43
517k
bool PgSelect::UseSecondaryIndex() const {
44
517k
  return prepare_params_.use_secondary_index;
45
517k
}
46
47
562k
Status PgSelect::Prepare() {
48
  // Prepare target and bind descriptors.
49
562k
  target_ = PgTable(VERIFY_RESULT(LoadTable()));
50
51
562k
  if (!UseSecondaryIndex()) {
52
410k
    bind_ = target_;
53
151k
  } else {
54
151k
    bind_ = PgTable(nullptr);
55
56
    // Create secondary index query.
57
151k
    secondary_index_query_ =
58
151k
      std::make_unique<PgSelectIndex>(pg_session_, table_id_, index_id_, &prepare_params_);
59
151k
  }
60
61
  // Allocate READ requests to send to DocDB.
62
562k
  auto read_op = std::make_shared<PgsqlReadOp>(*target_);
63
562k
  read_req_ = std::shared_ptr<PgsqlReadRequestPB>(read_op, &read_op->read_request());
64
65
562k
  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
562k
  RETURN_NOT_OK(PrepareSecondaryIndex());
69
70
  // Prepare binds for the request.
71
562k
  PrepareBinds();
72
73
562k
  doc_op_ = doc_op;
74
562k
  return Status::OK();
75
562k
}
76
77
562k
Status PgSelect::PrepareSecondaryIndex() {
78
562k
  if (!secondary_index_query_) {
79
    // This DML statement is not using secondary index.
80
410k
    return Status::OK();
81
410k
  }
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
151k
  std::shared_ptr<PgsqlReadRequestPB> index_req = nullptr;
95
151k
  if (prepare_params_.querying_colocated_table) {
96
    // Allocate "index_request" and pass to PgSelectIndex.
97
151k
    index_req = std::shared_ptr<PgsqlReadRequestPB>(read_req_, read_req_->mutable_index_request());
98
151k
  }
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
151k
  return secondary_index_query_->PrepareSubquery(index_req);
103
151k
}
104
105
}  // namespace pggate
106
}  // namespace yb