Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion datafusion-cli/examples/cli-session-context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,7 @@ pub async fn main() {
instrumented_registry: Arc::new(InstrumentedObjectStoreRegistry::new()),
};

exec_from_repl(&my_ctx, &mut print_options).await.unwrap();
exec_from_repl(&my_ctx, &mut print_options, None)
.await
.unwrap();
}
7 changes: 5 additions & 2 deletions datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::Path;
use tokio::signal;

/// run and execute SQL statements and commands, against a context with the given print options
Expand Down Expand Up @@ -129,13 +130,15 @@ pub async fn exec_from_files(
pub async fn exec_from_repl(
ctx: &dyn CliSessionContext,
print_options: &mut PrintOptions,
history_file: Option<&Path>,
) -> rustyline::Result<()> {
let history_file = history_file.unwrap_or_else(|| Path::new(".history"));
let mut rl = Editor::new()?;
rl.set_helper(Some(CliHelper::new(
&ctx.task_ctx().session_config().options().sql_parser.dialect,
print_options.color,
)));
rl.load_history(".history").ok();
rl.load_history(history_file).ok();

loop {
match rl.readline("> ") {
Expand Down Expand Up @@ -210,7 +213,7 @@ pub async fn exec_from_repl(
}
}

rl.save_history(".history")
rl.save_history(history_file)
}

pub(super) async fn exec_and_print(
Expand Down
35 changes: 33 additions & 2 deletions datafusion-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use std::collections::HashMap;
use std::env;
use std::num::NonZeroUsize;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::{Arc, LazyLock};

Expand Down Expand Up @@ -109,6 +109,13 @@ struct Args {
)]
rc: Option<Vec<String>>,

#[clap(
long,
help = "Path to the file used to persist interactive shell history. Overrides DATAFUSION_HISTORY_FILE if set, default to .history",
value_parser(parse_valid_history_file)
)]
history_file: Option<String>,

#[clap(long, value_enum, default_value_t = PrintFormat::Automatic)]
format: PrintFormat,

Expand Down Expand Up @@ -304,12 +311,17 @@ async fn main_inner() -> Result<()> {
}
};

let history_file = args
.history_file
.map(PathBuf::from)
.or_else(|| env::var_os("DATAFUSION_HISTORY_FILE").map(PathBuf::from));

if repl_mode {
if !rc.is_empty() {
exec::exec_from_files(&ctx, rc, &print_options).await?;
}
// TODO maybe we can have thiserror for cli but for now let's keep it simple
return exec::exec_from_repl(&ctx, &mut print_options)
return exec::exec_from_repl(&ctx, &mut print_options, history_file.as_deref())
.await
.map_err(|e| DataFusionError::External(Box::new(e)));
}
Expand Down Expand Up @@ -379,6 +391,25 @@ fn parse_valid_data_dir(dir: &str) -> Result<String, String> {
}
}

fn parse_valid_history_file(path: &str) -> Result<String, String> {
let path = Path::new(path);
if path.is_dir() {
return Err(format!(
"Invalid history file '{}': is a directory",
path.display()
));
}
match path.parent() {
Some(parent) if !parent.as_os_str().is_empty() && !parent.is_dir() => {
Err(format!(
"Invalid history file '{}': parent directory does not exist",
path.display()
))
}
_ => Ok(path.to_string_lossy().into_owned()),
}
}

fn parse_batch_size(size: &str) -> Result<usize, String> {
match size.parse::<usize>() {
Ok(size) if size > 0 => Ok(size),
Expand Down
2 changes: 2 additions & 0 deletions docs/source/user-guide/cli/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Options:
Execute commands from file(s), then exit
-r, --rc [<RC>...]
Run the provided files on startup instead of ~/.datafusionrc
--history-file <HISTORY_FILE>
Path to the file used to persist interactive shell history. Overrides DATAFUSION_HISTORY_FILE if set, default to .history
--format <FORMAT>
[default: automatic] [possible values: csv, tsv, table, json, nd-json, automatic]
-q, --quiet
Expand Down
Loading