YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/client/table_alterer.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/client/table_alterer.h"
15
16
#include "yb/client/client-internal.h"
17
#include "yb/client/schema-internal.h"
18
19
#include "yb/common/schema.h"
20
#include "yb/common/transaction.h"
21
22
#include "yb/master/master_ddl.pb.h"
23
24
namespace yb {
25
namespace client {
26
27
struct YBTableAlterer::Step {
28
  master::AlterTableRequestPB::StepType step_type;
29
  std::unique_ptr<YBColumnSpec> spec;
30
};
31
32
YBTableAlterer::YBTableAlterer(YBClient* client, const YBTableName& name)
33
167
  : client_(client), table_name_(name) {
34
167
}
35
36
YBTableAlterer::YBTableAlterer(YBClient* client, const string id)
37
155
  : client_(client), table_id_(std::move(id)) {
38
155
}
39
40
322
YBTableAlterer::~YBTableAlterer() {
41
322
}
42
43
42
YBTableAlterer* YBTableAlterer::RenameTo(const YBTableName& new_name) {
44
42
  rename_to_ = std::make_unique<YBTableName>(new_name);
45
42
  return this;
46
42
}
47
48
173
YBColumnSpec* YBTableAlterer::AddColumn(const string& name) {
49
173
  steps_.push_back({master::AlterTableRequestPB::ADD_COLUMN, std::make_unique<YBColumnSpec>(name)});
50
173
  return steps_.back().spec.get();
51
173
}
52
53
15
YBColumnSpec* YBTableAlterer::AlterColumn(const string& name) {
54
15
  steps_.push_back(
55
15
      {master::AlterTableRequestPB::ALTER_COLUMN, std::make_unique<YBColumnSpec>(name)});
56
15
  return steps_.back().spec.get();
57
15
}
58
59
96
YBTableAlterer* YBTableAlterer::DropColumn(const string& name) {
60
96
  steps_.push_back(
61
96
      {master::AlterTableRequestPB::DROP_COLUMN, std::make_unique<YBColumnSpec>(name)});
62
96
  return this;
63
96
}
64
65
7
YBTableAlterer* YBTableAlterer::SetTableProperties(const TableProperties& table_properties) {
66
7
  table_properties_ = std::make_unique<TableProperties>(table_properties);
67
7
  return this;
68
7
}
69
70
4
YBTableAlterer* YBTableAlterer::replication_info(const master::ReplicationInfoPB& ri) {
71
4
  replication_info_ = std::make_unique<master::ReplicationInfoPB>(ri);
72
4
  return this;
73
4
}
74
75
0
YBTableAlterer* YBTableAlterer::SetWalRetentionSecs(const uint32_t wal_retention_secs) {
76
0
  wal_retention_secs_ = wal_retention_secs;
77
0
  return this;
78
0
}
79
80
155
YBTableAlterer* YBTableAlterer::timeout(const MonoDelta& timeout) {
81
155
  timeout_ = timeout;
82
155
  return this;
83
155
}
84
85
0
YBTableAlterer* YBTableAlterer::wait(bool wait) {
86
0
  wait_ = wait;
87
0
  return this;
88
0
}
89
90
155
YBTableAlterer* YBTableAlterer::part_of_transaction(const TransactionMetadata* txn) {
91
155
  txn_ = txn;
92
155
  return this;
93
155
}
94
95
322
Status YBTableAlterer::Alter() {
96
322
  master::AlterTableRequestPB req;
97
322
  RETURN_NOT_OK(ToRequest(&req));
98
99
322
  MonoDelta timeout = timeout_.Initialized() ?
100
155
    timeout_ :
101
167
    client_->default_admin_operation_timeout();
102
322
  auto deadline = CoarseMonoClock::Now() + timeout;
103
322
  RETURN_NOT_OK(client_->data_->AlterTable(client_, req, deadline));
104
321
  if (wait_) {
105
279
    YBTableName alter_name = rename_to_ ? *rename_to_ : table_name_;
106
321
    RETURN_NOT_OK(client_->data_->WaitForAlterTableToFinish(
107
321
        client_, alter_name, table_id_, deadline));
108
321
  }
109
110
321
  return Status::OK();
111
321
}
112
113
322
Status YBTableAlterer::ToRequest(master::AlterTableRequestPB* req) {
114
322
  if (!status_.ok()) {
115
0
    return status_;
116
0
  }
117
118
322
  if (!rename_to_ && steps_.empty() && !table_properties_ && !wal_retention_secs_ &&
119
4
      !replication_info_) {
120
0
    return STATUS(InvalidArgument, "No alter steps provided");
121
0
  }
122
123
322
  req->Clear();
124
125
322
  if (table_name_.has_table()) {
126
167
    table_name_.SetIntoTableIdentifierPB(req->mutable_table());
127
167
  }
128
129
322
  if (!table_id_.empty()) {
130
155
    (req->mutable_table())->set_table_id(table_id_);
131
155
  }
132
133
322
  if (rename_to_) {
134
42
    req->set_new_table_name(rename_to_->table_name());
135
136
42
    if (rename_to_->has_namespace()) {
137
42
      req->mutable_new_namespace()->set_name(rename_to_->namespace_name());
138
42
    }
139
42
  }
140
141
284
  for (const Step& s : steps_) {
142
284
    auto* pb_step = req->add_alter_schema_steps();
143
284
    pb_step->set_type(s.step_type);
144
145
284
    switch (s.step_type) {
146
173
      case master::AlterTableRequestPB::ADD_COLUMN:
147
173
      {
148
173
        YBColumnSchema col;
149
173
        RETURN_NOT_OK(s.spec->ToColumnSchema(&col));
150
173
        ColumnSchemaToPB(*col.col_,
151
173
                         pb_step->mutable_add_column()->mutable_schema());
152
173
        break;
153
173
      }
154
96
      case master::AlterTableRequestPB::DROP_COLUMN:
155
96
      {
156
96
        pb_step->mutable_drop_column()->set_name(s.spec->data_->name);
157
96
        break;
158
173
      }
159
15
      case master::AlterTableRequestPB::ALTER_COLUMN:
160
        // TODO(KUDU-861): support altering a column in the wire protocol.
161
        // For now, we just give an error if the caller tries to do
162
        // any operation other than rename.
163
15
        if (s.spec->data_->has_type ||
164
15
            s.spec->data_->has_nullable ||
165
15
            s.spec->data_->primary_key) {
166
0
          return STATUS(NotSupported, "cannot support AlterColumn of this type",
167
0
                                      s.spec->data_->name);
168
0
        }
169
        // We only support rename column
170
15
        if (!s.spec->data_->has_rename_to) {
171
0
          return STATUS(InvalidArgument, "no alter operation specified",
172
0
                                         s.spec->data_->name);
173
0
        }
174
15
        pb_step->mutable_rename_column()->set_old_name(s.spec->data_->name);
175
15
        pb_step->mutable_rename_column()->set_new_name(s.spec->data_->rename_to);
176
15
        pb_step->set_type(master::AlterTableRequestPB::RENAME_COLUMN);
177
15
        break;
178
0
      default:
179
0
        LOG(FATAL) << "unknown step type " << s.step_type;
180
284
    }
181
284
  }
182
183
322
  if (table_properties_) {
184
7
    table_properties_->ToTablePropertiesPB(req->mutable_alter_properties());
185
7
  }
186
187
322
  if (wal_retention_secs_) {
188
0
    req->set_wal_retention_secs(*wal_retention_secs_);
189
0
  }
190
191
322
  if (replication_info_) {
192
    // TODO: Maybe add checks for the sanity of the replication_info.
193
4
    req->mutable_replication_info()->CopyFrom(*replication_info_);
194
4
  }
195
196
322
  if (txn_) {
197
155
    txn_->ToPB(req->mutable_transaction());
198
155
  }
199
200
322
  return Status::OK();
201
322
}
202
203
} // namespace client
204
} // namespace yb