Skip to content
Merged
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
3 changes: 2 additions & 1 deletion datafusion-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 14 additions & 12 deletions datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ pub async fn exec_from_commands(
ctx: &mut SessionContext,
commands: Vec<String>,
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
pub async fn exec_from_lines(
ctx: &mut SessionContext,
reader: &mut BufReader<File>,
print_options: &PrintOptions,
) {
) -> Result<()> {
let mut query = "".to_owned();

for line in reader.lines() {
Expand Down Expand Up @@ -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<String>,
print_options: &PrintOptions,
) {
) -> Result<()> {
let files = files
.into_iter()
.map(|file_path| File::open(file_path).unwrap())
.collect::<Vec<_>>();

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
Expand Down Expand Up @@ -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?;
Expand Down
20 changes: 16 additions & 4 deletions datafusion-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

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();

Expand Down Expand Up @@ -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)
Expand All @@ -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(())
Expand Down