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_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
22.7M
    : schema_(schema), index_(index) {
38
22.7M
}
39
40
//--------------------------------------------------------------------------------------------------
41
42
11.1M
PgsqlExpressionPB *PgColumn::AllocPrimaryBindPB(PgsqlWriteRequestPB *write_req) {
43
11.1M
  if (is_partition()) {
44
1.59M
    bind_pb_ = write_req->add_partition_column_values();
45
9.57M
  } else if (is_primary()) {
46
2.04M
    bind_pb_ = write_req->add_range_column_values();
47
2.04M
  }
48
11.1M
  return bind_pb_;
49
11.1M
}
50
51
6.24M
PgsqlExpressionPB *PgColumn::AllocBindPB(PgsqlWriteRequestPB *write_req) {
52
6.24M
  if (bind_pb_ == nullptr) {
53
27
    DCHECK(!is_partition() && !is_primary())
54
27
      << "Binds for primary columns should have already been allocated by AllocPrimaryBindPB()";
55
56
6.24M
    if (is_virtual_column()) {
57
1.77M
      bind_pb_ = write_req->mutable_ybctid_column_value();
58
4.47M
    } else {
59
4.47M
      PgsqlColumnValuePB* col_pb = write_req->add_column_values();
60
4.47M
      col_pb->set_column_id(id());
61
4.47M
      bind_pb_ = col_pb->mutable_expr();
62
4.47M
    }
63
6.24M
  }
64
6.24M
  return bind_pb_;
65
6.24M
}
66
67
259k
PgsqlExpressionPB *PgColumn::AllocAssignPB(PgsqlWriteRequestPB *write_req) {
68
259k
  if (assign_pb_ == nullptr) {
69
259k
    PgsqlColumnValuePB* col_pb = write_req->add_column_new_values();
70
259k
    col_pb->set_column_id(id());
71
259k
    assign_pb_ = col_pb->mutable_expr();
72
259k
  }
73
259k
  return assign_pb_;
74
259k
}
75
76
26.9M
bool PgColumn::is_partition() const {
77
26.9M
  return index_ < schema_.num_hash_key_columns();
78
26.9M
}
79
80
25.1M
bool PgColumn::is_primary() const {
81
25.1M
  return index_ < schema_.num_key_columns();
82
25.1M
}
83
84
69.2M
bool PgColumn::is_virtual_column() const {
85
69.2M
  return index_ == schema_.num_columns();
86
69.2M
}
87
88
27.9M
const ColumnSchema& PgColumn::desc() const {
89
26.1M
  return is_virtual_column() ? kColumnYBctid : schema_.column(index_);
90
27.9M
}
91
92
//--------------------------------------------------------------------------------------------------
93
94
6.61M
PgsqlExpressionPB *PgColumn::AllocPrimaryBindPB(PgsqlReadRequestPB *read_req) {
95
6.61M
  if (is_partition()) {
96
197k
    bind_pb_ = read_req->add_partition_column_values();
97
6.41M
  } else if (is_primary()) {
98
908k
    bind_pb_ = read_req->add_range_column_values();
99
908k
  }
100
6.61M
  return bind_pb_;
101
6.61M
}
102
103
9.48k
PgsqlExpressionPB *PgColumn::AllocBindPB(PgsqlReadRequestPB *read_req) {
104
9.48k
  if (bind_pb_ == nullptr) {
105
0
    DCHECK(!is_partition() && !is_primary())
106
0
      << "Binds for primary columns should have already been allocated by AllocPrimaryBindPB()";
107
108
9.48k
    if (is_virtual_column()) {
109
9.48k
      bind_pb_ = read_req->mutable_ybctid_column_value();
110
0
    } else {
111
0
      DLOG(FATAL) << "Binds for other columns are not allowed";
112
0
    }
113
9.48k
  }
114
9.48k
  return bind_pb_;
115
9.48k
}
116
117
//--------------------------------------------------------------------------------------------------
118
119
2.89k
PgsqlExpressionPB *PgColumn::AllocBindConditionExprPB(PgsqlReadRequestPB *read_req) {
120
2.89k
  if (bind_condition_expr_pb_ == nullptr) {
121
2.89k
    bind_condition_expr_pb_ = read_req->mutable_condition_expr();
122
2.89k
    bind_condition_expr_pb_->mutable_condition()->set_op(QL_OP_AND);
123
2.89k
  }
124
2.89k
  return bind_condition_expr_pb_->mutable_condition()->add_operands();
125
2.89k
}
126
127
5.52M
void PgColumn::ResetBindPB() {
128
5.52M
  bind_pb_ = nullptr;
129
5.52M
}
130
131
27.2M
int PgColumn::id() const {
132
493k
  return is_virtual_column() ? to_underlying(PgSystemAttrNum::kYBTupleId)
133
26.7M
                             : schema_.column_id(index_);
134
27.2M
}
135
136
13.6M
InternalType PgColumn::internal_type() const {
137
13.6M
  return client::YBColumnSchema::ToInternalDataType(desc().type());
138
13.6M
}
139
140
0
const std::string& PgColumn::attr_name() const {
141
0
  return desc().name();
142
0
}
143
144
11.4M
int PgColumn::attr_num() const {
145
11.4M
  return desc().order();
146
11.4M
}
147
148
}  // namespace pggate
149
}  // namespace yb