Skip to content

Introduce \i command to execute from a file #3136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Changes from 1 commit
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
26 changes: 25 additions & 1 deletion datafusion-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

//! Command within CLI

use crate::exec::exec_from_lines;
use crate::functions::{display_all_functions, Function};
use crate::print_format::PrintFormat;
use crate::print_options::PrintOptions;
Expand All @@ -26,6 +27,8 @@ use datafusion::arrow::datatypes::{DataType, Field, Schema};
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::error::{DataFusionError, Result};
use datafusion::prelude::SessionContext;
use std::fs::File;
use std::io::BufReader;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Instant;
Expand All @@ -38,6 +41,7 @@ pub enum Command {
ListTables,
DescribeTable(String),
ListFunctions,
Include(Option<String>),
SearchFunctions(String),
QuietMode(Option<bool>),
OutputFormat(Option<String>),
Expand Down Expand Up @@ -72,6 +76,22 @@ impl Command {
.print_batches(&batches, now)
.map_err(|e| DataFusionError::Execution(e.to_string()))
}
Self::Include(filename) => {
if let Some(filename) = filename {
let file = match File::open(filename) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we either use ? or map_err here rather than this match?

Ok(file) => file,
Err(error) => {
return Err(DataFusionError::Execution(error.to_string()))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, something like this perhaps?

Suggested change
let file = match File::open(filename) {
Ok(file) => file,
Err(error) => {
return Err(DataFusionError::Execution(error.to_string()))
}
let file = File::open(filename)
.map_err(|e| {
DataFusionError::Execution(format!("Error opening {:?} {}", filename, e))
})?;

As the e doesn't contain the filename

This gives something like:

❯ \i /tmp/foo.dd
Execution error: Error opening "/tmp/foo.dd" No such file or directory (os error 2)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! yup of course, happy to add the documentation change as well :)

};
exec_from_lines(ctx, &mut BufReader::new(file), print_options).await;
Ok(())
} else {
Err(DataFusionError::Execution(
"Required filename argument is missing".into(),
))
}
}
Self::QuietMode(quiet) => {
if let Some(quiet) = quiet {
print_options.quiet = *quiet;
Expand Down Expand Up @@ -113,6 +133,7 @@ impl Command {
Self::ListTables => ("\\d", "list tables"),
Self::DescribeTable(_) => ("\\d name", "describe table"),
Self::Help => ("\\?", "help"),
Self::Include(_) => ("\\i filename", "reads input from the specified filename"),
Self::ListFunctions => ("\\h", "function list"),
Self::SearchFunctions(_) => ("\\h function", "search function"),
Self::QuietMode(_) => ("\\quiet (true|false)?", "print or set quiet mode"),
Expand All @@ -123,11 +144,12 @@ impl Command {
}
}

const ALL_COMMANDS: [Command; 8] = [
const ALL_COMMANDS: [Command; 9] = [
Command::ListTables,
Command::DescribeTable(String::new()),
Command::Quit,
Command::Help,
Command::Include(Some(String::new())),
Command::ListFunctions,
Command::SearchFunctions(String::new()),
Command::QuietMode(None),
Expand Down Expand Up @@ -169,6 +191,8 @@ impl FromStr for Command {
("?", None) => Self::Help,
("h", None) => Self::ListFunctions,
("h", Some(function)) => Self::SearchFunctions(function.into()),
("i", None) => Self::Include(None),
("i", Some(filename)) => Self::Include(Some(filename.to_owned())),
("quiet", Some("true" | "t" | "yes" | "y" | "on")) => {
Self::QuietMode(Some(true))
}
Expand Down