common_meta/ddl/alter_table/
update_metadata.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 common_grpc_expr::alter_expr_to_request;
16use snafu::ResultExt;
17use table::metadata::{RawTableInfo, TableInfo};
18use table::requests::AlterKind;
19
20use crate::ddl::alter_table::AlterTableProcedure;
21use crate::error::{self, Result};
22use crate::key::table_info::TableInfoValue;
23use crate::key::{DeserializedValueWithBytes, RegionDistribution};
24
25impl AlterTableProcedure {
26    /// Builds new table info after alteration.
27    /// It bumps the column id of the table by the number of the add column requests.
28    /// So there may be holes in the column id sequence.
29    pub(crate) fn build_new_table_info(&self, table_info: &RawTableInfo) -> Result<TableInfo> {
30        let table_info =
31            TableInfo::try_from(table_info.clone()).context(error::ConvertRawTableInfoSnafu)?;
32        let table_ref = self.data.table_ref();
33        let alter_expr = self.data.task.alter_table.clone();
34        let request = alter_expr_to_request(self.data.table_id(), alter_expr)
35            .context(error::ConvertAlterTableRequestSnafu)?;
36
37        let new_meta = table_info
38            .meta
39            .builder_with_alter_kind(table_ref.table, &request.alter_kind)
40            .context(error::TableSnafu)?
41            .build()
42            .with_context(|_| error::BuildTableMetaSnafu {
43                table_name: table_ref.table,
44            })?;
45
46        let mut new_info = table_info.clone();
47        new_info.meta = new_meta;
48        new_info.ident.version = table_info.ident.version + 1;
49        match request.alter_kind {
50            AlterKind::AddColumns { columns } => {
51                // Bumps the column id for the new columns.
52                // It may bump more than the actual number of columns added if there are
53                // existing columns, but it's fine.
54                new_info.meta.next_column_id += columns.len() as u32;
55            }
56            AlterKind::RenameTable { new_table_name } => {
57                new_info.name = new_table_name.to_string();
58            }
59            AlterKind::DropColumns { .. }
60            | AlterKind::ModifyColumnTypes { .. }
61            | AlterKind::SetTableOptions { .. }
62            | AlterKind::UnsetTableOptions { .. }
63            | AlterKind::SetIndex { .. }
64            | AlterKind::UnsetIndex { .. } => {}
65        }
66
67        Ok(new_info)
68    }
69
70    /// Updates table metadata for rename table operation.
71    pub(crate) async fn on_update_metadata_for_rename(
72        &self,
73        new_table_name: String,
74        current_table_info_value: &DeserializedValueWithBytes<TableInfoValue>,
75    ) -> Result<()> {
76        let table_metadata_manager = &self.context.table_metadata_manager;
77        table_metadata_manager
78            .rename_table(current_table_info_value, new_table_name)
79            .await?;
80
81        Ok(())
82    }
83
84    /// Updates table metadata for alter table operation.
85    pub(crate) async fn on_update_metadata_for_alter(
86        &self,
87        new_table_info: RawTableInfo,
88        region_distribution: RegionDistribution,
89        current_table_info_value: &DeserializedValueWithBytes<TableInfoValue>,
90    ) -> Result<()> {
91        let table_metadata_manager = &self.context.table_metadata_manager;
92        table_metadata_manager
93            .update_table_info(
94                current_table_info_value,
95                Some(region_distribution),
96                new_table_info,
97            )
98            .await?;
99
100        Ok(())
101    }
102}