forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.rs
More file actions
199 lines (181 loc) · 8.06 KB
/
Copy pathhandler.rs
File metadata and controls
199 lines (181 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2023 Databend Cloud
//
// Licensed under the Elastic License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.elastic.co/licensing/elastic-license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::sync::Arc;
use databend_common_base::base::GlobalInstance;
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use databend_common_expression::TableSchema;
use databend_common_meta_app::schema::CreateTableReq;
use databend_common_meta_app::schema::TableMeta;
use databend_common_meta_app::schema::TableNameIdent;
use databend_common_meta_app::schema::TableStatistics;
use databend_common_sql::plans::CreateTablePlan;
use databend_common_storage::EndpointPolicyScope;
use databend_common_storage::check_operator;
use databend_common_storage::init_operator_with_policy_scope;
use databend_common_storages_fuse::FUSE_OPT_KEY_ATTACH_COLUMN_IDS;
use databend_common_storages_fuse::io::MetaReaders;
use databend_common_storages_fuse::operations::load_last_snapshot_hint;
use databend_enterprise_attach_table::AttachTableHandler;
use databend_enterprise_attach_table::AttachTableHandlerWrapper;
use databend_storages_common_cache::LoadParams;
use databend_storages_common_table_meta::meta::TableSnapshot;
use databend_storages_common_table_meta::meta::Versioned;
use databend_storages_common_table_meta::table::OPT_KEY_SNAPSHOT_LOCATION;
pub struct RealAttachTableHandler {}
#[async_trait::async_trait]
impl AttachTableHandler for RealAttachTableHandler {
#[async_backtrace::framed]
async fn build_attach_table_request(
&self,
storage_prefix: &str,
plan: &CreateTablePlan,
) -> databend_common_exception::Result<CreateTableReq> {
// Safe to unwrap here, as attach table must have storage params.
let sp = plan.storage_params.as_ref().unwrap();
let operator = init_operator_with_policy_scope(sp, EndpointPolicyScope::External)?;
check_operator(&operator, sp).await?;
let snapshot_hint = load_last_snapshot_hint(storage_prefix, &operator)
.await?
.ok_or_else(|| {
ErrorCode::StorageOther(format!(
"hint file of table {}.{} does not exist",
&plan.database, &plan.table
))
})?;
let reader = MetaReaders::table_snapshot_reader(operator.clone());
// TODO duplicated code
let snapshot_full_path = snapshot_hint.snapshot_full_path;
let info = operator.info();
let root = info.root();
let snapshot_loc = snapshot_full_path[root.len()..].to_string();
let mut options = plan.options.clone();
options.insert(OPT_KEY_SNAPSHOT_LOCATION.to_string(), snapshot_loc.clone());
let params = LoadParams {
location: snapshot_loc.clone(),
len_hint: None,
ver: TableSnapshot::VERSION,
put_cache: true,
};
let snapshot = reader.read(¶ms).await?;
let stat = TableStatistics {
number_of_rows: snapshot.summary.row_count,
data_bytes: snapshot.summary.uncompressed_byte_size,
compressed_data_bytes: snapshot.summary.compressed_byte_size,
index_data_bytes: snapshot.summary.index_size,
bloom_index_size: snapshot.summary.bloom_index_size,
ngram_index_size: snapshot.summary.ngram_index_size,
inverted_index_size: snapshot.summary.inverted_index_size,
vector_index_size: snapshot.summary.vector_index_size,
virtual_column_size: snapshot.summary.virtual_column_size,
number_of_segments: Some(snapshot.segments.len() as u64),
number_of_blocks: Some(snapshot.summary.block_count),
};
let attach_table_schema = Self::gen_schema(&plan, &snapshot)?;
// let field_comments = vec!["".to_string(); snapshot.schema.num_fields()];
let field_comments = snapshot_hint.entity_comments.field_comments;
let comment = snapshot_hint.entity_comments.table_comment;
let table_meta = TableMeta {
schema: Arc::new(attach_table_schema),
engine: plan.engine.to_string(),
storage_params: plan.storage_params.clone(),
options,
comment,
field_comments,
drop_on: None,
statistics: stat,
indexes: snapshot_hint.indexes,
..Default::default()
};
let req = CreateTableReq {
create_option: plan.create_option,
catalog_name: if plan.create_option.is_overriding() {
Some(plan.catalog.to_string())
} else {
None
},
name_ident: TableNameIdent {
tenant: plan.tenant.clone(),
db_name: plan.database.to_string(),
table_name: plan.table.to_string(),
},
table_meta,
as_dropped: false,
table_properties: None,
table_partition: None,
};
Ok(req)
}
}
impl RealAttachTableHandler {
pub fn init() -> databend_common_exception::Result<()> {
let rm = RealAttachTableHandler {};
let wrapper = AttachTableHandlerWrapper::new(Box::new(rm));
GlobalInstance::set(Arc::new(wrapper));
Ok(())
}
fn gen_schema(
plan: &&CreateTablePlan,
base_table_snapshot: &Arc<TableSnapshot>,
) -> Result<TableSchema> {
let schema = if let Some(attached_columns) = &plan.attached_columns {
// Columns to include are specified, let's check them
let base_table_schema = &base_table_snapshot.schema;
let mut fields_to_attach = Vec::with_capacity(attached_columns.len());
// The ids of columns being included
let mut field_ids_to_include = Vec::with_capacity(attached_columns.len());
// Columns that do not exist in the table being attached to, if any
let mut invalid_cols = vec![];
for field in attached_columns {
match base_table_schema.field_with_name(&field.name) {
Ok(f) => {
field_ids_to_include.push(f.column_id);
fields_to_attach.push(f.clone())
}
Err(_) => invalid_cols.push(field.name.as_str()),
}
}
if !invalid_cols.is_empty() {
return Err(ErrorCode::InvalidArgument(format!(
"Columns [{}] do not exist in the table being attached to",
invalid_cols.join(",")
)));
}
let new_table_schema_metadata = if !field_ids_to_include.is_empty() {
// If columns to include are specified explicitly, their ids should
// be kept in the metadata of TableSchema.
let ids = field_ids_to_include
.iter()
.map(|id| format!("{id}"))
.collect::<Vec<_>>()
.join(",");
let mut v = base_table_schema.metadata.clone();
v.insert(FUSE_OPT_KEY_ATTACH_COLUMN_IDS.to_owned(), ids);
v
} else {
base_table_schema.metadata.clone()
};
TableSchema {
fields: fields_to_attach,
metadata: new_table_schema_metadata,
next_column_id: base_table_schema.next_column_id,
}
} else {
// If columns are not specified, use all the fields of table being attached to,
// in this case, no schema meta of key FUSE_OPT_KEY_ATTACH_COLUMN_IDS will be kept.
base_table_snapshot.schema.clone()
};
Ok(schema)
}
}