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_column.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_column.h"
17
18
#include "yb/client/schema.h"
19
20
#include "yb/common/ql_type.h"
21
#include "yb/common/pg_system_attr.h"
22
#include "yb/common/pgsql_protocol.pb.h"
23
#include "yb/common/schema.h"
24
25
namespace yb {
26
namespace pggate {
27
28
namespace {
29
30
ColumnSchema kColumnYBctid(
31
    "ybctid", QLType::CreatePrimitiveType<DataType::BINARY>(),
32
    false, false, false, false, to_underlying(PgSystemAttrNum::kYBTupleId));
33
34
}
35
36
PgColumn::PgColumn(std::reference_wrapper<const Schema> schema, size_t index)
37
70.7M
    : schema_(schema), index_(index) {
38
70.7M
}
39
40
//--------------------------------------------------------------------------------------------------
41
42
37.9M
PgsqlExpressionPB *PgColumn::AllocPrimaryBindPB(PgsqlWriteRequestPB *write_req) {
43
37.9M
  if (is_partition()) {
44
5.97M
    bind_pb_ = write_req->add_partition_column_values();
45
32.0M
  } else if (is_primary()) {
46
5.34M
    bind_pb_ = write_req->add_range_column_values();
47
5.34M
  }
48
37.9M
  return bind_pb_;
49
37.9M
}
50
51
20.5M
PgsqlExpressionPB *PgColumn::AllocBindPB(PgsqlWriteRequestPB *write_req) {
52
20.5M
  if (
bind_pb_ == nullptr20.5M
) {
53
20.5M
    DCHECK(!is_partition() && !is_primary())
54
33
      << "Binds for primary columns should have already been allocated by AllocPrimaryBindPB()";
55
56
20.5M
    if (is_virtual_column()) {
57
5.86M
      bind_pb_ = write_req->mutable_ybctid_column_value();
58
14.7M
    } else {
59
14.7M
      PgsqlColumnValuePB* col_pb = write_req->add_column_values();
60
14.7M
      col_pb->set_column_id(id());
61
14.7M
      bind_pb_ = col_pb->mutable_expr();
62
14.7M
    }
63
20.5M
  }
64
20.5M
  return bind_pb_;
65
20.5M
}
66
67
983k
PgsqlExpressionPB *PgColumn::AllocAssignPB(PgsqlWriteRequestPB *write_req) {
68
983k
  if (assign_pb_ == nullptr) {
69
983k
    PgsqlColumnValuePB* col_pb = write_req->add_column_new_values();
70
983k
    col_pb->set_column_id(id());
71
983k
    assign_pb_ = col_pb->mutable_expr();
72
983k
  }
73
983k
  return assign_pb_;
74
983k
}
75
76
85.0M
bool PgColumn::is_partition() const {
77
85.0M
  return index_ < schema_.num_hash_key_columns();
78
85.0M
}
79
80
78.7M
bool PgColumn::is_primary() const {
81
78.7M
  return index_ < schema_.num_key_columns();
82
78.7M
}
83
84
204M
bool PgColumn::is_virtual_column() const {
85
204M
  return index_ == schema_.num_columns();
86
204M
}
87
88
77.1M
const ColumnSchema& PgColumn::desc() const {
89
77.1M
  return is_virtual_column() ? 
kColumnYBctid5.87M
:
schema_.column(index_)71.2M
;
90
77.1M
}
91
92
//--------------------------------------------------------------------------------------------------
93
94
19.9M
PgsqlExpressionPB *PgColumn::AllocPrimaryBindPB(PgsqlReadRequestPB *read_req) {
95
19.9M
  if (is_partition()) {
96
368k
    bind_pb_ = read_req->add_partition_column_values();
97
19.6M
  } else if (is_primary()) {
98
2.86M
    bind_pb_ = read_req->add_range_column_values();
99
2.86M
  }
100
19.9M
  return bind_pb_;
101
19.9M
}
102
103
12.8k
PgsqlExpressionPB *PgColumn::AllocBindPB(PgsqlReadRequestPB *read_req) {
104
12.8k
  if (bind_pb_ == nullptr) {
105
12.8k
    DCHECK(!is_partition() && !is_primary())
106
0
      << "Binds for primary columns should have already been allocated by AllocPrimaryBindPB()";
107
108
12.8k
    if (is_virtual_column()) {
109
12.8k
      bind_pb_ = read_req->mutable_ybctid_column_value();
110
12.8k
    } else {
111
0
      DLOG(FATAL) << "Binds for other columns are not allowed";
112
0
    }
113
12.8k
  }
114
12.8k
  return bind_pb_;
115
12.8k
}
116
117
//--------------------------------------------------------------------------------------------------
118
119
6.57k
PgsqlExpressionPB *PgColumn::AllocBindConditionExprPB(PgsqlReadRequestPB *read_req) {
120
6.57k
  if (bind_condition_expr_pb_ == nullptr) {
121
6.57k
    bind_condition_expr_pb_ = read_req->mutable_condition_expr();
122
6.57k
    bind_condition_expr_pb_->mutable_condition()->set_op(QL_OP_AND);
123
6.57k
  }
124
6.57k
  return bind_condition_expr_pb_->mutable_condition()->add_operands();
125
6.57k
}
126
127
16.6M
void PgColumn::ResetBindPB() {
128
16.6M
  bind_pb_ = nullptr;
129
16.6M
}
130
131
83.0M
int PgColumn::id() const {
132
83.0M
  return is_virtual_column() ? 
to_underlying(PgSystemAttrNum::kYBTupleId)1.38M
133
83.0M
                             : 
schema_.column_id(index_)81.6M
;
134
83.0M
}
135
136
40.0M
InternalType PgColumn::internal_type() const {
137
40.0M
  return client::YBColumnSchema::ToInternalDataType(desc().type());
138
40.0M
}
139
140
0
const std::string& PgColumn::attr_name() const {
141
0
  return desc().name();
142
0
}
143
144
30.7M
int PgColumn::attr_num() const {
145
30.7M
  return desc().order();
146
30.7M
}
147
148
}  // namespace pggate
149
}  // namespace yb