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
5 changes: 4 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,8 @@ pub async fn main() {
instrumented_registry: Arc::new(InstrumentedObjectStoreRegistry::new()),
};

exec_from_repl(&my_ctx, &mut print_options).await.unwrap();
let repl_options = Default::default();
exec_from_repl(&my_ctx, &mut print_options, &repl_options)
.await
.unwrap();
}
6 changes: 4 additions & 2 deletions datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use crate::cli_context::CliSessionContext;
use crate::helper::split_from_semicolon;
use crate::print_format::PrintFormat;
use crate::repl_options::ReplOptions;
use crate::{
command::{Command, OutputFormat},
helper::CliHelper,
Expand Down Expand Up @@ -129,13 +130,14 @@ pub async fn exec_from_files(
pub async fn exec_from_repl(
ctx: &dyn CliSessionContext,
print_options: &mut PrintOptions,
repl_options: &ReplOptions,
) -> rustyline::Result<()> {
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(&repl_options.history_file).ok();

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

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

pub(super) async fn exec_and_print(
Expand Down
1 change: 1 addition & 0 deletions datafusion-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ pub mod object_storage;
pub mod pool_type;
pub mod print_format;
pub mod print_options;
pub mod repl_options;
43 changes: 41 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 All @@ -43,6 +43,7 @@ use datafusion_cli::{
pool_type::PoolType,
print_format::PrintFormat,
print_options::{MaxRows, PrintOptions},
repl_options::ReplOptions,
};

use clap::Parser;
Expand Down Expand Up @@ -109,6 +110,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 @@ -308,8 +316,10 @@ async fn main_inner() -> Result<()> {
if !rc.is_empty() {
exec::exec_from_files(&ctx, rc, &print_options).await?;
}
let repl_options = get_repl_options(&args.history_file);

// 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, &repl_options)
.await
.map_err(|e| DataFusionError::External(Box::new(e)));
}
Expand Down Expand Up @@ -379,6 +389,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 All @@ -394,6 +423,16 @@ fn parse_command(command: &str) -> Result<String, String> {
}
}

fn get_repl_options(history_file: &Option<String>) -> ReplOptions {
if let Some(history_file) = history_file {
ReplOptions {
history_file: PathBuf::from(history_file),
}
} else {
Default::default()
}
}

#[derive(Debug, Clone, Copy)]
enum ByteUnit {
Byte,
Expand Down
33 changes: 33 additions & 0 deletions datafusion-cli/src/repl_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use std::env;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct ReplOptions {
pub history_file: PathBuf,
}

impl Default for ReplOptions {
fn default() -> Self {
let history_file = env::var_os("DATAFUSION_HISTORY_FILE")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(".history"));
Self { history_file }
}
}
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