forked from splitgraph/seafowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_tables.rs
287 lines (254 loc) · 9.05 KB
/
system_tables.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
//! Mechanism for creating virtual Seafowl system tables, inspired by influxdb_iox system tables
//! and datafusion's information_schema.
use crate::catalog::TableStore;
use crate::repository::interface::DroppedTablesResult;
use arrow::array::{Int64Builder, StringBuilder, StructBuilder, TimestampSecondBuilder};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef, TimeUnit};
use arrow::record_batch::RecordBatch;
use async_trait::async_trait;
use datafusion::catalog::{SchemaProvider, Session};
use datafusion::common::DataFusionError;
use datafusion::datasource::TableProvider;
use datafusion::error::Result;
use datafusion::physical_plan::memory::MemoryExec;
use datafusion::physical_plan::ExecutionPlan;
use datafusion_expr::{Expr, TableType};
use std::any::Any;
use std::sync::Arc;
pub const SYSTEM_SCHEMA: &str = "system";
const TABLE_VERSIONS: &str = "table_versions";
const DROPPED_TABLES: &str = "dropped_tables";
pub struct SystemSchemaProvider {
database: Arc<str>,
table_catalog: Arc<dyn TableStore>,
}
impl SystemSchemaProvider {
pub fn new(database: Arc<str>, table_catalog: Arc<dyn TableStore>) -> Self {
Self {
database,
table_catalog,
}
}
}
#[async_trait]
impl SchemaProvider for SystemSchemaProvider {
fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
fn table_names(&self) -> Vec<String> {
vec![TABLE_VERSIONS.to_string(), DROPPED_TABLES.to_string()]
}
async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> {
Ok(match name {
// Lazy instantiate the tables, but defer loading the rows until the actual scan is invoked.
TABLE_VERSIONS => {
let table = TableVersionsTable::new(
self.database.clone(),
self.table_catalog.clone(),
);
Some(Arc::new(SystemTableProvider {
table: Arc::new(table),
}))
}
DROPPED_TABLES => {
let table = DroppedTablesTable::new(
self.database.clone(),
self.table_catalog.clone(),
);
Some(Arc::new(SystemTableProvider {
table: Arc::new(table),
}))
}
_ => None,
})
}
fn table_exist(&self, name: &str) -> bool {
matches!(
name.to_ascii_lowercase().as_str(),
TABLE_VERSIONS | DROPPED_TABLES
)
}
}
// Base trait for Seafowl system tables, as a way to imitate OOP
#[async_trait]
trait SeafowlSystemTable: Send + Sync {
/// The schema for this system table
fn schema(&self) -> SchemaRef;
/// Get the rows of the system table
async fn load_record_batch(&self) -> Result<RecordBatch>;
}
/// Adapter that makes any `SeafowlSystemTable` a DataFusion `TableProvider`
struct SystemTableProvider<T: SeafowlSystemTable> {
table: Arc<T>,
}
#[async_trait]
impl<T> TableProvider for SystemTableProvider<T>
where
T: SeafowlSystemTable + 'static,
{
fn as_any(&self) -> &dyn Any {
self
}
fn schema(&self) -> SchemaRef {
self.table.schema()
}
fn table_type(&self) -> TableType {
TableType::View
}
// TODO: Investigate streaming from sqlx instead of loading all the results in memory
async fn scan(
&self,
_ctx: &dyn Session,
projection: Option<&Vec<usize>>,
_filters: &[Expr],
_limit: Option<usize>,
) -> Result<Arc<dyn ExecutionPlan>> {
Ok(Arc::new(MemoryExec::try_new(
&[vec![self.table.load_record_batch().await?]],
self.table.schema(),
projection.cloned(),
)?))
}
}
// Table listing all available version for the given database
struct TableVersionsTable {
database: Arc<str>,
schema: SchemaRef,
table_catalog: Arc<dyn TableStore>,
}
impl TableVersionsTable {
fn new(database: Arc<str>, table_catalog: Arc<dyn TableStore>) -> Self {
Self {
// This is dictated by the output of `get_all_versions`, except that we omit the
// database_name field, since we scope down to the database at hand.
database,
schema: Arc::new(Schema::new(vec![
Field::new("table_schema", DataType::Utf8, false),
Field::new("table_name", DataType::Utf8, false),
Field::new("table_version_id", DataType::Int64, false),
Field::new("version", DataType::Int64, false),
Field::new(
"creation_time",
DataType::Timestamp(TimeUnit::Second, None),
false,
),
])),
table_catalog,
}
}
}
#[async_trait]
impl SeafowlSystemTable for TableVersionsTable {
fn schema(&self) -> SchemaRef {
self.schema.clone()
}
async fn load_record_batch(&self) -> Result<RecordBatch> {
let table_versions = self
.table_catalog
.get_all_versions(&self.database, None)
.await?;
let mut builder = StructBuilder::from_fields(
self.schema.fields().clone(),
table_versions.len(),
);
// Construct the table columns from the returned rows
for table_version in &table_versions {
builder
.field_builder::<StringBuilder>(0)
.unwrap()
.append_value(table_version.collection_name.clone());
builder
.field_builder::<StringBuilder>(1)
.unwrap()
.append_value(table_version.table_name.clone());
builder
.field_builder::<Int64Builder>(2)
.unwrap()
.append_value(table_version.table_version_id);
builder
.field_builder::<Int64Builder>(3)
.unwrap()
.append_value(table_version.version);
builder
.field_builder::<TimestampSecondBuilder>(4)
.unwrap()
.append_value(table_version.creation_time);
builder.append(true);
}
let struct_array = builder.finish();
RecordBatch::try_new(self.schema.clone(), struct_array.columns().to_vec())
.map_err(DataFusionError::from)
}
}
// Table listing all dropped tables that are pending lazy deletion on subsequent `VACUUM`s
struct DroppedTablesTable {
database: Arc<str>,
schema: SchemaRef,
table_catalog: Arc<dyn TableStore>,
}
impl DroppedTablesTable {
fn new(database: Arc<str>, table_catalog: Arc<dyn TableStore>) -> Self {
Self {
// This is dictated by the output of `get_dropped_tables`, except that we omit the
// database_name field, since we scope down to the database at hand.
database,
schema: Arc::new(Schema::new(vec![
Field::new("table_schema", DataType::Utf8, false),
Field::new("table_name", DataType::Utf8, false),
Field::new("uuid", DataType::Utf8, false),
Field::new("deletion_status", DataType::Utf8, false),
Field::new(
"drop_time",
DataType::Timestamp(TimeUnit::Second, None),
false,
),
])),
table_catalog,
}
}
}
#[async_trait]
impl SeafowlSystemTable for DroppedTablesTable {
fn schema(&self) -> SchemaRef {
self.schema.clone()
}
async fn load_record_batch(&self) -> Result<RecordBatch> {
let dropped_tables = self
.table_catalog
.get_dropped_tables(Some(self.database.to_string()))
.await?
.into_iter()
.collect::<Vec<DroppedTablesResult>>();
let mut builder = StructBuilder::from_fields(
self.schema.fields().clone(),
dropped_tables.len(),
);
// Construct the table columns from the returned rows
for dropped_table in &dropped_tables {
builder
.field_builder::<StringBuilder>(0)
.unwrap()
.append_value(dropped_table.collection_name.clone());
builder
.field_builder::<StringBuilder>(1)
.unwrap()
.append_value(dropped_table.table_name.clone());
builder
.field_builder::<StringBuilder>(2)
.unwrap()
.append_value(dropped_table.uuid.to_string().clone());
builder
.field_builder::<StringBuilder>(3)
.unwrap()
.append_value(dropped_table.deletion_status.to_string().clone());
builder
.field_builder::<TimestampSecondBuilder>(4)
.unwrap()
.append_value(dropped_table.drop_time);
builder.append(true);
}
let struct_array = builder.finish();
RecordBatch::try_new(self.schema.clone(), struct_array.columns().to_vec())
.map_err(DataFusionError::from)
}
}