-
Notifications
You must be signed in to change notification settings - Fork 213
/
main.rs
358 lines (316 loc) · 10.7 KB
/
main.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
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.
//! A simple interactive shell of the database.
#![feature(div_duration)]
use std::fs::File;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use clap::Parser;
use humantime::format_duration;
use itertools::Itertools;
use risinglight::array::{datachunk_to_sqllogictest_string, Chunk};
use risinglight::server::run_server;
use risinglight::storage::SecondaryStorageOptions;
use risinglight::utils::time::RoundingDuration;
use risinglight::Database;
use rustyline::error::ReadlineError;
use rustyline::DefaultEditor;
use sqllogictest::DefaultColumnType;
use tokio::{select, signal};
use tracing::{info, warn, Level};
use tracing_subscriber::prelude::*;
/// RisingLight: an OLAP database system.
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Where to store the database files
#[clap()]
storage_path: Option<String>,
/// File to execute. Can be either a SQL `sql` file or sqllogictest `slt` file.
#[clap(short, long)]
file: Option<String>,
/// Whether to use in-memory engine
#[clap(long)]
memory: bool,
/// Control the output format
/// - `text`: plain text
/// - `human`: human readable format
#[clap(long)]
output_format: Option<String>,
/// Whether to use minitrace
#[clap(long)]
enable_tracing: bool,
/// Whether to use tokio console.
#[clap(long)]
tokio_console: bool,
/// Start the postgres server instead of the interactive shell.
#[clap(long)]
server: bool,
/// The host to bind to.
/// Defaults to localhost.
/// Ignored if --server is not set.
#[clap(long)]
host: Option<String>,
/// The port to listen on.
/// Default to 5432.
/// Ignored if `--server` is not specified.
#[clap(long)]
port: Option<u16>,
}
// human-readable message
fn print_chunk(chunk: &Chunk, output_format: &Option<String>) {
let output_format = output_format.as_ref().map(|x| x.as_str());
match output_format {
Some("human") | None => match chunk.header() {
Some(header) => match header[0].as_str() {
"$insert.row_counts" => {
println!(
"{} rows inserted",
chunk.get_first_data_chunk().array_at(0).get_to_string(0)
)
}
"$delete.row_counts" => {
println!(
"{} rows deleted",
chunk.get_first_data_chunk().array_at(0).get_to_string(0)
)
}
"$create" => println!("created"),
"$drop" => println!("dropped"),
"$explain" => println!(
"{}",
chunk.get_first_data_chunk().array_at(0).get_to_string(0)
),
_ => println!("{}", chunk),
},
None => println!("{}", chunk),
},
Some("text") => println!(
"{}",
datachunk_to_sqllogictest_string(chunk)
.iter()
.format_with("\n", |row, f| f(&row.iter().format(","))),
),
Some(format) => panic!("unsupported output format: {}", format),
}
}
fn print_execution_time(start_time: Instant) {
let duration = start_time.elapsed();
let duration_in_seconds = duration.div_duration_f64(Duration::new(1, 0));
if duration_in_seconds > 1.0 {
println!(
"in {:.3}s ({})",
duration_in_seconds,
format_duration(duration.round_to_seconds())
);
} else {
println!("in {:.3}s", duration_in_seconds);
}
}
async fn run_query_in_background(db: Arc<Database>, sql: String, output_format: Option<String>) {
let start_time = Instant::now();
select! {
_ = signal::ctrl_c() => {
// we simply drop the future `task` to cancel the query.
println!("Interrupted");
}
ret = db.run(&sql) => {
match ret {
Ok(chunks) => {
for chunk in chunks {
print_chunk(&chunk, &output_format);
}
print_execution_time(start_time);
}
Err(err) => println!("{}", err),
}
}
}
}
/// Read line by line from STDIN until a line ending with `;`.
///
/// Note that `;` in string literals will also be treated as a terminator
/// as long as it is at the end of a line.
fn read_sql(rl: &mut DefaultEditor) -> Result<String, ReadlineError> {
let mut sql = String::new();
loop {
let prompt = if sql.is_empty() { "> " } else { "? " };
let line = rl.readline(prompt)?;
if line.is_empty() {
continue;
}
// internal commands starts with "\"
if line.starts_with('\\') && sql.is_empty() {
return Ok(line);
}
sql.push_str(line.as_str());
if line.ends_with(';') {
return Ok(sql);
} else {
sql.push('\n');
}
}
}
/// Run RisingLight interactive mode
async fn interactive(db: Database, output_format: Option<String>) -> Result<()> {
let mut rl = DefaultEditor::new()?;
let history_path = dirs::cache_dir().map(|p| {
let cache_dir = p.join("risinglight");
std::fs::create_dir_all(cache_dir.as_path()).ok();
let history_path = cache_dir.join("history.txt");
if !history_path.as_path().exists() {
File::create(history_path.as_path()).ok();
}
history_path.into_boxed_path()
});
if let Some(ref history_path) = history_path {
if let Err(err) = rl.load_history(&history_path) {
println!("No previous history. {err}");
}
}
let db = Arc::new(db);
loop {
let read_sql = read_sql(&mut rl);
match read_sql {
Ok(sql) => {
if !sql.trim().is_empty() {
rl.add_history_entry(sql.as_str())?;
run_query_in_background(db.clone(), sql, output_format.clone()).await;
}
}
Err(ReadlineError::Interrupted) => {
println!("Interrupted");
}
Err(ReadlineError::Eof) => {
println!("Exited");
break;
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
if let Some(ref history_path) = history_path {
if let Err(err) = rl.save_history(&history_path) {
println!("Save history failed, {err}");
}
}
Ok(())
}
/// Run a SQL file in RisingLight
async fn run_sql(db: Database, path: &str, output_format: Option<String>) -> Result<()> {
let lines = std::fs::read_to_string(path)?;
info!("{}", lines);
let chunks = db.run(&lines).await?;
for chunk in chunks {
print_chunk(&chunk, &output_format);
}
Ok(())
}
/// Wrapper for sqllogictest
struct DatabaseWrapper {
db: Database,
output_format: Option<String>,
}
#[async_trait]
impl sqllogictest::AsyncDB for &DatabaseWrapper {
type ColumnType = DefaultColumnType;
type Error = risinglight::Error;
async fn run(
&mut self,
sql: &str,
) -> Result<sqllogictest::DBOutput<DefaultColumnType>, Self::Error> {
use sqllogictest::DBOutput;
let is_query_sql = {
let lower_sql = sql.trim_start().to_ascii_lowercase();
lower_sql.starts_with("select")
|| lower_sql.starts_with("values")
|| lower_sql.starts_with("show")
|| lower_sql.starts_with("with")
|| lower_sql.starts_with("describe")
};
info!("{}", sql);
let chunks = self.db.run(sql).await?;
for chunk in &chunks {
print_chunk(chunk, &self.output_format);
}
if chunks.is_empty() || chunks.iter().all(|c| c.data_chunks().is_empty()) {
if is_query_sql {
return Ok(DBOutput::Rows {
types: vec![],
rows: vec![],
});
} else {
return Ok(DBOutput::StatementComplete(0));
}
}
let types = vec![DefaultColumnType::Any; chunks[0].get_first_data_chunk().column_count()];
let rows = chunks
.iter()
.flat_map(datachunk_to_sqllogictest_string)
.collect();
Ok(DBOutput::Rows { types, rows })
}
}
/// Run a sqllogictest file in RisingLight
async fn run_sqllogictest(db: Database, path: &str, output_format: Option<String>) -> Result<()> {
let db = DatabaseWrapper { db, output_format };
let mut tester = sqllogictest::Runner::new(|| async { Ok(&db) });
let path = path.to_string();
tester
.run_file_async(path)
.await
// `ParseError` isn't Send, so we cannot directly use it as anyhow Error.
.map_err(|err| anyhow!("{:?}", err))?;
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
if args.tokio_console {
console_subscriber::init();
} else {
let fmt_layer = tracing_subscriber::fmt::layer().compact();
let filter_layer = tracing_subscriber::EnvFilter::from_default_env()
.add_directive(Level::INFO.into())
.add_directive("egg=warn".parse()?);
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();
}
if args.enable_tracing {
use minitrace::collector::{Config, ConsoleReporter};
minitrace::set_reporter(ConsoleReporter, Config::default());
}
let db = if args.memory {
info!("using memory engine");
Database::new_in_memory()
} else {
info!("using Secondary engine");
let mut options = SecondaryStorageOptions::default_for_cli();
if let Some(path) = args.storage_path {
options.path = PathBuf::new().join(path);
}
Database::new_on_disk(options).await
};
if let Some(file) = args.file {
if file.ends_with(".sql") {
run_sql(db, &file, args.output_format).await?;
} else if file.ends_with(".slt") {
run_sqllogictest(db, &file, args.output_format).await?;
} else {
warn!("No suffix detected, assume sql file");
run_sql(db, &file, args.output_format).await?;
}
} else if args.server {
run_server(args.host, args.port, db).await;
} else {
interactive(db, args.output_format).await?;
}
minitrace::flush();
Ok(())
}