common_meta/ddl/
physical_table_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 std::collections::HashSet;
16
17use api::v1::SemanticType;
18use store_api::metadata::ColumnMetadata;
19use table::metadata::RawTableInfo;
20
21/// Generate the new physical table info.
22pub(crate) fn build_new_physical_table_info(
23    mut raw_table_info: RawTableInfo,
24    physical_columns: &[ColumnMetadata],
25) -> RawTableInfo {
26    let existing_columns = raw_table_info
27        .meta
28        .schema
29        .column_schemas
30        .iter()
31        .map(|col| col.name.clone())
32        .collect::<HashSet<_>>();
33    let primary_key_indices = &mut raw_table_info.meta.primary_key_indices;
34    let value_indices = &mut raw_table_info.meta.value_indices;
35    value_indices.clear();
36    let time_index = &mut raw_table_info.meta.schema.timestamp_index;
37    let columns = &mut raw_table_info.meta.schema.column_schemas;
38    columns.clear();
39
40    for (idx, col) in physical_columns.iter().enumerate() {
41        match col.semantic_type {
42            SemanticType::Tag => {
43                // push new primary key to the end.
44                if !existing_columns.contains(&col.column_schema.name) {
45                    primary_key_indices.push(idx);
46                }
47            }
48            SemanticType::Field => value_indices.push(idx),
49            SemanticType::Timestamp => *time_index = Some(idx),
50        }
51
52        columns.push(col.column_schema.clone());
53    }
54
55    if let Some(time_index) = *time_index {
56        raw_table_info.meta.schema.column_schemas[time_index].set_time_index();
57    }
58
59    raw_table_info
60}