metric_engine/engine/alter/
extract_new_columns.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 std::collections::{HashMap, HashSet};
16
17use store_api::metadata::ColumnMetadata;
18use store_api::region_request::{AlterKind, RegionAlterRequest};
19use store_api::storage::{ColumnId, RegionId};
20
21use crate::error::Result;
22
23/// Extract new columns from the create requests.
24///
25/// # Panics
26///
27/// This function will panic if the alter kind is not `AddColumns`.
28pub fn extract_new_columns<'a>(
29    requests: &'a [(RegionId, RegionAlterRequest)],
30    physical_columns: &HashMap<String, ColumnId>,
31    new_column_names: &mut HashSet<&'a str>,
32    new_columns: &mut Vec<ColumnMetadata>,
33) -> Result<()> {
34    for (_, request) in requests {
35        let AlterKind::AddColumns { columns } = &request.kind else {
36            unreachable!()
37        };
38        for col in columns {
39            let column_name = col.column_metadata.column_schema.name.as_str();
40            if !physical_columns.contains_key(column_name)
41                && !new_column_names.contains(column_name)
42            {
43                new_column_names.insert(column_name);
44                // TODO(weny): avoid clone
45                new_columns.push(col.column_metadata.clone());
46            }
47        }
48    }
49
50    Ok(())
51}