cli/metadata/repair/
alter_table.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// 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
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use client::api::v1::alter_table_expr::Kind;
16use client::api::v1::region::{AlterRequests, RegionRequest, RegionRequestHeader, region_request};
17use client::api::v1::{AddColumn, AddColumns, AlterTableExpr};
18use common_meta::ddl::alter_logical_tables::make_alter_region_request;
19use common_meta::peer::Peer;
20use common_meta::rpc::router::{RegionRoute, find_leader_regions};
21use operator::expr_helper::column_schemas_to_defs;
22use snafu::ResultExt;
23use store_api::storage::{RegionId, TableId};
24use table::metadata::TableInfo;
25
26use crate::error::{CovertColumnSchemasToDefsSnafu, Result};
27
28/// Generates alter table expression for all columns.
29pub fn generate_alter_table_expr_for_all_columns(table_info: &TableInfo) -> Result<AlterTableExpr> {
30    let schema = &table_info.meta.schema;
31
32    let mut alter_table_expr = AlterTableExpr {
33        catalog_name: table_info.catalog_name.clone(),
34        schema_name: table_info.schema_name.clone(),
35        table_name: table_info.name.clone(),
36        ..Default::default()
37    };
38
39    let primary_keys = table_info
40        .meta
41        .primary_key_indices
42        .iter()
43        .map(|i| schema.column_schemas()[*i].name.clone())
44        .collect::<Vec<_>>();
45
46    let add_columns = column_schemas_to_defs(schema.column_schemas().to_vec(), &primary_keys)
47        .context(CovertColumnSchemasToDefsSnafu)?;
48
49    alter_table_expr.kind = Some(Kind::AddColumns(AddColumns {
50        add_columns: add_columns
51            .into_iter()
52            .map(|col| AddColumn {
53                column_def: Some(col),
54                location: None,
55                add_if_not_exists: true,
56            })
57            .collect(),
58    }));
59
60    Ok(alter_table_expr)
61}
62
63/// Makes an alter region request for a peer.
64pub fn make_alter_region_request_for_peer(
65    logical_table_id: TableId,
66    alter_table_expr: &AlterTableExpr,
67    peer: &Peer,
68    region_routes: &[RegionRoute],
69) -> Result<RegionRequest> {
70    let regions_on_this_peer = find_leader_regions(region_routes, peer);
71    let mut requests = Vec::with_capacity(regions_on_this_peer.len());
72    for region_number in &regions_on_this_peer {
73        let region_id = RegionId::new(logical_table_id, *region_number);
74        let request = make_alter_region_request(region_id, alter_table_expr);
75        requests.push(request);
76    }
77
78    Ok(RegionRequest {
79        header: Some(RegionRequestHeader::default()),
80        body: Some(region_request::Body::Alters(AlterRequests { requests })),
81    })
82}