-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||||||||||
|
@@ -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; | ||||||||||||||||||||
|
@@ -38,6 +41,7 @@ pub enum Command { | |||||||||||||||||||
ListTables, | ||||||||||||||||||||
DescribeTable(String), | ||||||||||||||||||||
ListFunctions, | ||||||||||||||||||||
Include(Option<String>), | ||||||||||||||||||||
SearchFunctions(String), | ||||||||||||||||||||
QuietMode(Option<bool>), | ||||||||||||||||||||
OutputFormat(Option<String>), | ||||||||||||||||||||
|
@@ -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) { | ||||||||||||||||||||
Ok(file) => file, | ||||||||||||||||||||
Err(error) => { | ||||||||||||||||||||
return Err(DataFusionError::Execution(error.to_string())) | ||||||||||||||||||||
} | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, something like this perhaps?
Suggested change
As the This gives something like: ❯ \i /tmp/foo.dd
Execution error: Error opening "/tmp/foo.dd" No such file or directory (os error 2) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||||||||||||
|
@@ -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"), | ||||||||||||||||||||
|
@@ -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), | ||||||||||||||||||||
|
@@ -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)) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we either use
?
ormap_err
here rather than this match?