forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.rs
433 lines (382 loc) · 10.9 KB
/
schema.rs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
//! Schema contains one or more tables
use std::{collections::HashMap, sync::Arc};
use async_trait::async_trait;
use common_types::{column_schema::ColumnSchema, table::ShardId};
use common_util::error::GenericError;
use snafu::{Backtrace, Snafu};
use table_engine::{
engine::{self, TableEngineRef, TableState},
partition::PartitionInfo,
table::{SchemaId, TableId, TableRef},
};
#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum Error {
#[snafu(display("Unsupported method, msg:{}.\nBacktrace:\n{}", msg, backtrace))]
UnSupported { msg: String, backtrace: Backtrace },
#[snafu(display(
"Failed to allocate table id, schema:{}, table:{}, err:{}",
schema,
table,
source
))]
AllocateTableId {
schema: String,
table: String,
source: GenericError,
},
#[snafu(display(
"Failed to invalidate table id, schema:{}, table:{}, table_id:{}, err:{}",
schema,
table_name,
table_id,
source
))]
InvalidateTableId {
schema: String,
table_name: String,
table_id: TableId,
source: GenericError,
},
#[snafu(display(
"Failed to create table, request:{:?}, msg:{}.\nBacktrace:\n{}",
request,
msg,
backtrace
))]
CreateTable {
request: CreateTableRequest,
msg: String,
backtrace: Backtrace,
},
#[snafu(display("Failed to create table, err:{}", source))]
CreateTableWithCause { source: GenericError },
#[snafu(display(
"Failed to drop table, request:{:?}, msg:{}.\nBacktrace:\n{}",
request,
msg,
backtrace
))]
DropTable {
request: DropTableRequest,
msg: String,
backtrace: Backtrace,
},
#[snafu(display("Failed to drop table, err:{}", source))]
DropTableWithCause { source: GenericError },
#[snafu(display(
"Failed to open table, request:{:?}, msg:{}.\nBacktrace:\n{}",
request,
msg,
backtrace
))]
OpenTable {
request: OpenTableRequest,
msg: String,
backtrace: Backtrace,
},
#[snafu(display("Failed to open table, source:{}", source))]
OpenTableWithCause { source: GenericError },
#[snafu(display(
"Failed to close table, request:{:?}, msg:{}.\nBacktrace:\n{}",
request,
msg,
backtrace
))]
CloseTable {
request: CloseTableRequest,
msg: String,
backtrace: Backtrace,
},
#[snafu(display("Failed to close table, source:{}", source))]
CloseTableWithCause { source: GenericError },
#[snafu(display(
"Failed to create table, table already exists, table:{}.\nBacktrace:\n{}",
table,
backtrace
))]
CreateExistTable { table: String, backtrace: Backtrace },
#[snafu(display(
"Failed to create table, cannot persist meta, table:{}, err:{}",
table,
source
))]
WriteTableMeta { table: String, source: GenericError },
#[snafu(display(
"Catalog mismatch, expect:{}, given:{}.\nBacktrace:\n{}",
expect,
given,
backtrace
))]
CatalogMismatch {
expect: String,
given: String,
backtrace: Backtrace,
},
#[snafu(display(
"Schema mismatch, expect:{}, given:{}.\nBacktrace:\n{}",
expect,
given,
backtrace
))]
SchemaMismatch {
expect: String,
given: String,
backtrace: Backtrace,
},
#[snafu(display(
"Invalid table id, msg:{}, table_id:{}.\nBacktrace:\n{}",
msg,
table_id,
backtrace
))]
InvalidTableId {
msg: &'static str,
table_id: TableId,
backtrace: Backtrace,
},
#[snafu(display("Failed to find table, table:{}.\nBacktrace:\n{}", table, backtrace))]
TableNotFound { table: String, backtrace: Backtrace },
#[snafu(display("Failed to alter table, err:{}", source))]
AlterTable { source: GenericError },
#[snafu(display(
"Too many table, cannot create table, schema:{}, table:{}.\nBacktrace:\n{}",
schema,
table,
backtrace
))]
TooManyTable {
schema: String,
table: String,
backtrace: Backtrace,
},
}
define_result!(Error);
/// A name reference.
pub type NameRef<'a> = &'a str;
// TODO: This name is conflict with [table_engine::schema::SchemaRef].
pub type SchemaRef = Arc<dyn Schema + Send + Sync>;
/// Request of creating table.
#[derive(Debug, Clone)]
pub struct CreateTableRequest {
/// Catalog name
pub catalog_name: String,
/// Schema name
pub schema_name: String,
/// Table name
pub table_name: String,
/// Table id
// TODO: remove this field
pub table_id: Option<TableId>,
/// Table schema
pub table_schema: common_types::schema::Schema,
/// Table engine type
pub engine: String,
/// Table options used by each engine
pub options: HashMap<String, String>,
/// Tells state of the table
pub state: TableState,
/// Shard id of the table
pub shard_id: ShardId,
/// Partition info if this is a partitioned table
pub partition_info: Option<PartitionInfo>,
}
impl CreateTableRequest {
pub fn into_engine_create_request(
self,
table_id: Option<TableId>,
schema_id: SchemaId,
) -> engine::CreateTableRequest {
let table_id = self.table_id.unwrap_or(table_id.unwrap_or(TableId::MIN));
engine::CreateTableRequest {
catalog_name: self.catalog_name,
schema_name: self.schema_name,
schema_id,
table_name: self.table_name,
table_id,
table_schema: self.table_schema,
engine: self.engine,
options: self.options,
state: self.state,
shard_id: self.shard_id,
partition_info: self.partition_info,
}
}
}
/// Create table options.
#[derive(Clone)]
pub struct CreateOptions {
/// Table engine
// FIXME(yingwen): We have engine type in create request, remove this
pub table_engine: TableEngineRef,
/// Create if not exists, if table already exists, wont return error
// TODO(yingwen): Maybe remove this?
pub create_if_not_exists: bool,
}
/// Drop table request
#[derive(Debug, Clone)]
pub struct DropTableRequest {
/// Catalog name
pub catalog_name: String,
/// Schema name
pub schema_name: String,
/// Table name
pub table_name: String,
/// Table engine type
pub engine: String,
}
impl DropTableRequest {
pub fn into_engine_drop_request(self, schema_id: SchemaId) -> engine::DropTableRequest {
engine::DropTableRequest {
catalog_name: self.catalog_name,
schema_name: self.schema_name,
schema_id,
table_name: self.table_name,
engine: self.engine,
}
}
}
/// Drop table options
#[derive(Clone)]
pub struct DropOptions {
/// Table engine
pub table_engine: TableEngineRef,
}
/// Open table request
#[derive(Debug, Clone)]
pub struct OpenTableRequest {
/// Catalog name
pub catalog_name: String,
/// Schema name
pub schema_name: String,
/// Table name
pub table_name: String,
/// Table id
pub table_id: TableId,
/// Table engine type
pub engine: String,
/// Shard id, shard is the table set about scheduling from nodes
pub shard_id: ShardId,
}
impl OpenTableRequest {
pub fn into_engine_open_request(self, schema_id: SchemaId) -> engine::OpenTableRequest {
engine::OpenTableRequest {
catalog_name: self.catalog_name,
schema_name: self.schema_name,
schema_id,
table_name: self.table_name,
table_id: self.table_id,
engine: self.engine,
shard_id: self.shard_id,
}
}
}
/// Open table options.
#[derive(Clone)]
pub struct OpenOptions {
/// Table engine
pub table_engine: TableEngineRef,
}
/// Close table request
#[derive(Clone, Debug)]
pub struct CloseTableRequest {
/// Catalog name
pub catalog_name: String,
/// Schema name
pub schema_name: String,
/// Table name
pub table_name: String,
/// Table id
pub table_id: TableId,
/// Table engine type
pub engine: String,
}
impl CloseTableRequest {
pub fn into_engine_close_request(self, schema_id: SchemaId) -> engine::CloseTableRequest {
engine::CloseTableRequest {
catalog_name: self.catalog_name,
schema_name: self.schema_name,
schema_id,
table_name: self.table_name,
table_id: self.table_id,
engine: self.engine,
}
}
}
/// Close table options.
#[derive(Clone)]
pub struct CloseOptions {
/// Table engine
pub table_engine: TableEngineRef,
}
/// Alter table operations.
#[derive(Debug)]
pub enum AlterTableOperation {
/// Add column operation, the column id in [ColumnSchema] will be ignored.
/// Primary key column is not allowed to be added, so all columns will
/// be added as normal columns.
AddColumn(ColumnSchema),
}
/// Alter table request.
#[derive(Debug)]
pub struct AlterTableRequest {
pub table_name: String,
pub operations: Vec<AlterTableOperation>,
}
#[derive(Debug, Clone)]
pub struct OpenShardRequest {
/// Shard id
pub shard_id: ShardId,
/// Table infos
pub table_defs: Vec<TableDef>,
/// Table engine type
pub engine: String,
}
#[derive(Clone, Debug)]
pub struct TableDef {
pub catalog_name: String,
pub schema_name: String,
pub id: TableId,
pub name: String,
}
impl TableDef {
pub fn into_engine_table_def(self, schema_id: SchemaId) -> engine::TableDef {
engine::TableDef {
catalog_name: self.catalog_name,
schema_name: self.schema_name,
schema_id,
id: self.id,
name: self.name,
}
}
}
pub type CloseShardRequest = OpenShardRequest;
/// Schema manage tables.
#[async_trait]
pub trait Schema {
/// Get schema name.
fn name(&self) -> NameRef;
/// Get schema id
fn id(&self) -> SchemaId;
/// Find table by name.
fn table_by_name(&self, name: NameRef) -> Result<Option<TableRef>>;
/// TODO: remove this method afterwards.
/// Create table according to `request`.
async fn create_table(
&self,
request: CreateTableRequest,
opts: CreateOptions,
) -> Result<TableRef>;
/// TODO: remove this method afterwards.
/// Drop table according to `request`.
///
/// Returns true if the table is really dropped.
async fn drop_table(&self, request: DropTableRequest, opts: DropOptions) -> Result<bool>;
/// All tables
fn all_tables(&self) -> Result<Vec<TableRef>>;
/// Register the opened table into schema.
fn register_table(&self, table: TableRef);
/// Unregister table
fn unregister_table(&self, table_name: &str);
}