diff --git a/datafusion-cli/src/command.rs b/datafusion-cli/src/command.rs index f7f36b6f9d512..feef137e6195a 100644 --- a/datafusion-cli/src/command.rs +++ b/datafusion-cli/src/command.rs @@ -79,7 +79,8 @@ impl Command { filename, e )) })?; - exec_from_lines(ctx, &mut BufReader::new(file), print_options).await; + exec_from_lines(ctx, &mut BufReader::new(file), print_options) + .await?; Ok(()) } else { exec_err!("Required filename argument is missing") diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rs index 659843783016c..9c3c20bf794a1 100644 --- a/datafusion-cli/src/exec.rs +++ b/datafusion-cli/src/exec.rs @@ -53,13 +53,12 @@ pub async fn exec_from_commands( ctx: &mut SessionContext, commands: Vec, print_options: &PrintOptions, -) { +) -> Result<()> { for sql in commands { - match exec_and_print(ctx, print_options, sql).await { - Ok(_) => {} - Err(err) => println!("{err}"), - } + exec_and_print(ctx, print_options, sql).await?; } + + Ok(()) } /// run and execute SQL statements and commands from a file, against a context with the given print options @@ -67,7 +66,7 @@ pub async fn exec_from_lines( ctx: &mut SessionContext, reader: &mut BufReader, print_options: &PrintOptions, -) { +) -> Result<()> { let mut query = "".to_owned(); for line in reader.lines() { @@ -97,26 +96,28 @@ pub async fn exec_from_lines( // run the left over query if the last statement doesn't contain ‘;’ // ignore if it only consists of '\n' if query.contains(|c| c != '\n') { - match exec_and_print(ctx, print_options, query).await { - Ok(_) => {} - Err(err) => println!("{err}"), - } + exec_and_print(ctx, print_options, query).await?; } + + Ok(()) } pub async fn exec_from_files( ctx: &mut SessionContext, files: Vec, print_options: &PrintOptions, -) { +) -> Result<()> { let files = files .into_iter() .map(|file_path| File::open(file_path).unwrap()) .collect::>(); + for file in files { let mut reader = BufReader::new(file); - exec_from_lines(ctx, &mut reader, print_options).await; + exec_from_lines(ctx, &mut reader, print_options).await?; } + + Ok(()) } /// run and execute SQL statements and commands against a context with the given print options @@ -215,6 +216,7 @@ async fn exec_and_print( MsSQL, ClickHouse, BigQuery, Ansi." ) })?; + let statements = DFParser::parse_sql_with_dialect(&sql, dialect.as_ref())?; for statement in statements { let mut plan = ctx.state().statement_to_plan(statement).await?; diff --git a/datafusion-cli/src/main.rs b/datafusion-cli/src/main.rs index dcfd28df1cb03..a9082f2e5351f 100644 --- a/datafusion-cli/src/main.rs +++ b/datafusion-cli/src/main.rs @@ -18,6 +18,7 @@ use std::collections::HashMap; use std::env; use std::path::Path; +use std::process::ExitCode; use std::str::FromStr; use std::sync::{Arc, OnceLock}; @@ -138,7 +139,18 @@ struct Args { } #[tokio::main] -pub async fn main() -> Result<()> { +/// Calls [`main_inner`], then handles printing errors and returning the correct exit code +pub async fn main() -> ExitCode { + if let Err(e) = main_inner().await { + println!("Error: {e}"); + return ExitCode::FAILURE; + } + + ExitCode::SUCCESS +} + +/// Main CLI entrypoint +async fn main_inner() -> Result<()> { env_logger::init(); let args = Args::parse(); @@ -216,7 +228,7 @@ pub async fn main() -> Result<()> { if commands.is_empty() && files.is_empty() { if !rc.is_empty() { - exec::exec_from_files(&mut ctx, rc, &print_options).await + exec::exec_from_files(&mut 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(&mut ctx, &mut print_options) @@ -225,11 +237,11 @@ pub async fn main() -> Result<()> { } if !files.is_empty() { - exec::exec_from_files(&mut ctx, files, &print_options).await; + exec::exec_from_files(&mut ctx, files, &print_options).await?; } if !commands.is_empty() { - exec::exec_from_commands(&mut ctx, commands, &print_options).await; + exec::exec_from_commands(&mut ctx, commands, &print_options).await?; } Ok(())