-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathlib.rs
More file actions
278 lines (260 loc) · 9.44 KB
/
Copy pathlib.rs
File metadata and controls
278 lines (260 loc) · 9.44 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
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
use std::{any::Any, collections::HashMap, ops::Deref, sync::Arc};
use datafusion::{
catalog::SchemaProvider,
datasource::TableProvider,
error::DataFusionError,
logical_expr::{BinaryExpr, Expr, Operator, col},
scalar::ScalarValue,
};
use generations::GenerationDurationsTable;
use influxdb3_catalog::catalog::{Catalog, DatabaseSchema, INTERNAL_DB_NAME};
use influxdb3_processing_engine::ProcessingEngineManagerImpl;
use influxdb3_sys_events::SysEventStore;
use influxdb3_write::WriteBuffer;
use iox_query::query_log::QueryLog;
use iox_system_tables::SystemTableProvider;
use tonic::async_trait;
mod databases;
use databases::DatabasesTable;
mod distinct_caches;
use distinct_caches::DistinctCachesTable;
mod generations;
mod influxdb_schema;
use influxdb_schema::InfluxdbSchemaTable;
mod last_caches;
use last_caches::LastCachesTable;
mod nodes;
use nodes::NodeSystemTable;
mod parquet_files;
use parquet_files::ParquetFilesTable;
mod plugins;
use plugins::PluginsTable;
mod python_call;
use python_call::{
ProcessingEngineLogsTable, ProcessingEngineTriggerArgumentsTable, ProcessingEngineTriggerTable,
};
mod queries;
use queries::QueriesTable;
mod tables;
use tables::TablesTable;
mod tokens;
use tokens::TokenSystemTable;
/// The default timezone used in the system schema.
pub const DEFAULT_TIMEZONE: &str = "UTC";
/// Global system schema name used in queries
///
/// # Example
/// ```sql
/// SELECT * FROM system.queries;
/// ```
pub const SYSTEM_SCHEMA_NAME: &str = "system";
pub const TABLE_NAME_PREDICATE: &str = "table_name";
pub const QUERIES_TABLE_NAME: &str = "queries";
pub const LAST_CACHES_TABLE_NAME: &str = "last_caches";
pub const DISTINCT_CACHES_TABLE_NAME: &str = "distinct_caches";
pub const PARQUET_FILES_TABLE_NAME: &str = "parquet_files";
pub const TOKENS_TABLE_NAME: &str = "tokens";
pub const DATABASES_TABLE_NAME: &str = "databases";
pub const TABLES_TABLE_NAME: &str = "tables";
pub const NODES_TABLE_NAME: &str = "nodes";
pub const GENERATION_DURATIONS_TABLE_NAME: &str = "generation_durations";
pub const INFLUXDB_SCHEMA_TABLE_NAME: &str = "influxdb_schema";
pub const PLUGIN_FILES_TABLE_NAME: &str = "plugin_files";
pub const PROCESSING_ENGINE_TRIGGERS_TABLE_NAME: &str = "processing_engine_triggers";
pub const PROCESSING_ENGINE_TRIGGER_ARGUMENTS_TABLE_NAME: &str =
"processing_engine_trigger_arguments";
pub const PROCESSING_ENGINE_LOGS_TABLE_NAME: &str = "processing_engine_logs";
#[derive(Debug)]
pub enum SystemSchemaProvider {
AllSystemSchemaTables(AllSystemSchemaTablesProvider),
}
#[async_trait]
impl SchemaProvider for SystemSchemaProvider {
fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
fn table_names(&self) -> Vec<String> {
match self {
Self::AllSystemSchemaTables(all_system_tables) => all_system_tables.table_names(),
}
}
async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>, DataFusionError> {
match self {
Self::AllSystemSchemaTables(all_system_tables) => all_system_tables.table(name).await,
}
}
fn table_exist(&self, name: &str) -> bool {
match self {
Self::AllSystemSchemaTables(all_system_tables) => all_system_tables.table_exist(name),
}
}
}
pub struct AllSystemSchemaTablesProvider {
tables: HashMap<&'static str, Arc<dyn TableProvider>>,
}
impl std::fmt::Debug for AllSystemSchemaTablesProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut keys = self.tables.keys().copied().collect::<Vec<_>>();
keys.sort_unstable();
f.debug_struct("AllSystemSchemaTablesProvider")
.field("tables", &keys.join(", "))
.finish()
}
}
impl AllSystemSchemaTablesProvider {
pub fn new(
db_schema: Arc<DatabaseSchema>,
query_log: Arc<QueryLog>,
buffer: Arc<dyn WriteBuffer>,
sys_events_store: Arc<SysEventStore>,
catalog: Arc<Catalog>,
started_with_auth: bool,
processing_engine: Option<Arc<ProcessingEngineManagerImpl>>,
) -> Self {
let mut tables = HashMap::<&'static str, Arc<dyn TableProvider>>::new();
let queries = Arc::new(SystemTableProvider::new(Arc::new(QueriesTable::new(
query_log,
))));
tables.insert(QUERIES_TABLE_NAME, queries);
let last_caches = Arc::new(SystemTableProvider::new(Arc::new(LastCachesTable::new(
Arc::clone(&db_schema),
))));
tables.insert(LAST_CACHES_TABLE_NAME, last_caches);
let distinct_caches = Arc::new(SystemTableProvider::new(Arc::new(
DistinctCachesTable::new(Arc::clone(&db_schema)),
)));
tables.insert(DISTINCT_CACHES_TABLE_NAME, distinct_caches);
let parquet_files = Arc::new(SystemTableProvider::new(Arc::new(ParquetFilesTable::new(
db_schema.id,
Arc::clone(&buffer),
))));
tables.insert(
PROCESSING_ENGINE_TRIGGERS_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(
ProcessingEngineTriggerTable::new(
db_schema
.processing_engine_triggers
.resource_iter()
.cloned()
.collect(),
),
))),
);
tables.insert(
PROCESSING_ENGINE_TRIGGER_ARGUMENTS_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(
ProcessingEngineTriggerArgumentsTable::new(
db_schema
.processing_engine_triggers
.resource_iter()
.cloned()
.collect(),
),
))),
);
tables.insert(PARQUET_FILES_TABLE_NAME, parquet_files);
let logs_table = Arc::new(SystemTableProvider::new(Arc::new(
ProcessingEngineLogsTable::new(sys_events_store),
)));
tables.insert(PROCESSING_ENGINE_LOGS_TABLE_NAME, logs_table);
tables.insert(
INFLUXDB_SCHEMA_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(
InfluxdbSchemaTable::new(Arc::clone(&db_schema)),
))),
);
let is_internal_database = db_schema.name.as_ref() == INTERNAL_DB_NAME;
// `system.tables` is available for all databases but restricts tables
// to that contained in the mentioned database only.
tables.insert(
TABLES_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(TablesTable::new(
Arc::clone(&catalog),
(!is_internal_database).then_some(Arc::clone(&db_schema.name)),
)))),
);
if is_internal_database {
tables.insert(
TOKENS_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(TokenSystemTable::new(
Arc::clone(&catalog),
started_with_auth,
)))),
);
tables.insert(
PLUGIN_FILES_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(PluginsTable::new(
processing_engine,
)))),
);
tables.insert(
NODES_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(NodeSystemTable::new(
Arc::clone(&catalog),
)))),
);
tables.insert(
DATABASES_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(DatabasesTable::new(
Arc::clone(&catalog),
)))),
);
tables.insert(
GENERATION_DURATIONS_TABLE_NAME,
Arc::new(SystemTableProvider::new(Arc::new(
GenerationDurationsTable::new(Arc::clone(&catalog)),
))),
);
}
Self { tables }
}
}
#[async_trait]
impl SchemaProvider for AllSystemSchemaTablesProvider {
fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
fn table_names(&self) -> Vec<String> {
let mut names = self
.tables
.keys()
.map(|s| (*s).to_owned())
.collect::<Vec<_>>();
names.sort();
names
}
async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>, DataFusionError> {
Ok(self.tables.get(name).cloned())
}
fn table_exist(&self, name: &str) -> bool {
self.tables.contains_key(name)
}
}
/// Used in queries to the system.{table_name} table
///
/// # Example
/// ```sql
/// SELECT * FROM system.parquet_files WHERE table_name = 'foo'
/// ```
pub fn find_table_name_in_filter(filters: Option<Vec<Expr>>) -> Option<Arc<str>> {
filters.map(|all_filters| {
all_filters.iter().find_map(|f| match f {
Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
if left.deref() == &col(TABLE_NAME_PREDICATE) && op == &Operator::Eq {
match right.deref() {
Expr::Literal(
ScalarValue::Utf8(Some(s))
| ScalarValue::LargeUtf8(Some(s))
| ScalarValue::Utf8View(Some(s)),
_,
) => Some(s.as_str().into()),
_ => None,
}
} else {
None
}
}
_ => None,
})
})?
}