forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow_create.rs
243 lines (212 loc) · 7.43 KB
/
show_create.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
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
use std::{collections::HashMap, convert::TryInto, sync::Arc};
use arrow::{
array::StringArray,
datatypes::{DataType, Field, Schema},
record_batch::RecordBatch,
};
use datafusion::logical_expr::Expr;
use datafusion_proto::bytes::Serializeable;
use log::error;
use query_engine::executor::RecordBatchVec;
use query_frontend::{ast::ShowCreateObject, plan::ShowCreatePlan};
use snafu::ensure;
use table_engine::{partition::PartitionInfo, table::TableRef};
use crate::{
interpreter::Output,
show::{Result, UnsupportedType},
};
pub struct ShowCreateInterpreter {
plan: ShowCreatePlan,
}
impl ShowCreateInterpreter {
pub fn create(plan: ShowCreatePlan) -> ShowCreateInterpreter {
Self { plan }
}
pub fn execute_show_create(self) -> Result<Output> {
let ShowCreatePlan { table, obj_type } = self.plan;
ensure!(
obj_type == ShowCreateObject::Table,
UnsupportedType { obj_type }
);
Self::table_ref_to_record_batch(table).map(Output::Records)
}
fn table_ref_to_record_batch(table_ref: TableRef) -> Result<RecordBatchVec> {
let tables = vec![table_ref.name().to_string()];
let sqls = vec![Self::render_table_sql(table_ref)];
let schema = Schema::new(vec![
Field::new("Table", DataType::Utf8, false),
Field::new("Create Table", DataType::Utf8, false),
]);
let arrow_record_batch = RecordBatch::try_new(
Arc::new(schema),
vec![
Arc::new(StringArray::from(tables)),
Arc::new(StringArray::from(sqls)),
],
)
.unwrap();
let record_batch = arrow_record_batch.try_into().unwrap();
Ok(vec![record_batch])
}
fn render_table_sql(table_ref: TableRef) -> String {
//TODO(boyan) pretty output
format!(
"CREATE TABLE `{}` ({}){} ENGINE={}{}",
table_ref.name(),
Self::render_columns_and_constrains(&table_ref),
Self::render_partition_info(table_ref.partition_info()),
table_ref.engine_type(),
Self::render_options(table_ref.options())
)
}
fn render_columns_and_constrains(table_ref: &TableRef) -> String {
let table_schema = table_ref.schema();
let key_columns = table_schema.key_columns();
let timestamp_key = table_schema.timestamp_name();
let mut res = String::new();
for col in table_schema.columns() {
res += format!("`{}` {}", col.name, col.data_type).as_str();
if col.is_tag {
res += " TAG";
}
if !col.is_nullable {
res += " NOT NULL";
}
if let Some(expr) = &col.default_value {
res += format!(" DEFAULT {expr}").as_str();
}
if !col.comment.is_empty() {
res += format!(" COMMENT '{}'", col.comment).as_str();
}
res += ", ";
}
let keys: Vec<String> = key_columns.iter().map(|col| col.name.to_string()).collect();
res += format!("PRIMARY KEY({}), ", keys.join(",")).as_str();
res += format!("TIMESTAMP KEY({timestamp_key})").as_str();
res
}
fn render_partition_info(partition_info: Option<PartitionInfo>) -> String {
let mut res = String::new();
if partition_info.is_none() {
return res;
}
let partition_info = partition_info.unwrap();
match partition_info {
PartitionInfo::Hash(v) => {
let expr = match Expr::from_bytes(&v.expr) {
Ok(expr) => expr,
Err(e) => {
error!("show create table parse partition info failed, err:{}", e);
return res;
}
};
if v.linear {
res += format!(
" PARTITION BY LINEAR HASH({}) PARTITIONS {}",
expr,
v.definitions.len()
)
.as_str()
} else {
res += format!(
" PARTITION BY HASH({}) PARTITIONS {}",
expr,
v.definitions.len()
)
.as_str()
}
}
PartitionInfo::Key(v) => {
let rendered_partition_key = v.partition_key.join(",");
if v.linear {
res += format!(
" PARTITION BY LINEAR KEY({}) PARTITIONS {}",
rendered_partition_key,
v.definitions.len()
)
.as_str()
} else {
res += format!(
" PARTITION BY KEY({}) PARTITIONS {}",
rendered_partition_key,
v.definitions.len()
)
.as_str()
}
}
}
res
}
fn render_options(opts: HashMap<String, String>) -> String {
if !opts.is_empty() {
let mut v: Vec<String> = opts
.into_iter()
.map(|(k, v)| format!("{k}='{v}'"))
.collect();
// sorted by option name
v.sort();
format!(" WITH({})", v.join(", "))
} else {
"".to_string()
}
}
}
#[cfg(test)]
mod test {
use std::ops::Add;
use datafusion::logical_expr::col;
use datafusion_proto::bytes::Serializeable;
use table_engine::partition::{
HashPartitionInfo, KeyPartitionInfo, PartitionDefinition, PartitionInfo,
};
use super::*;
#[test]
fn test_render_hash_partition_info() {
let expr = col("col1").add(col("col2"));
let partition_info = PartitionInfo::Hash(HashPartitionInfo {
version: 0,
definitions: vec![
PartitionDefinition {
name: "p0".to_string(),
origin_name: None,
},
PartitionDefinition {
name: "p1".to_string(),
origin_name: None,
},
],
expr: expr.to_bytes().unwrap(),
linear: false,
});
let expected = " PARTITION BY HASH(col1 + col2) PARTITIONS 2".to_string();
assert_eq!(
expected,
ShowCreateInterpreter::render_partition_info(Some(partition_info))
);
}
#[test]
fn test_render_key_partition_info() {
let partition_key_col_name = "col1";
let partition_info = PartitionInfo::Key(KeyPartitionInfo {
version: 0,
definitions: vec![
PartitionDefinition {
name: "p0".to_string(),
origin_name: None,
},
PartitionDefinition {
name: "p1".to_string(),
origin_name: None,
},
],
partition_key: vec![partition_key_col_name.to_string()],
linear: false,
});
let expected = " PARTITION BY KEY(col1) PARTITIONS 2".to_string();
assert_eq!(
expected,
ShowCreateInterpreter::render_partition_info(Some(partition_info))
);
}
}