Apache DataFusion SQL Query Engine
datafusion-cli support for multiple commands in a single line (#9831)
Berkay Şahind7957636327f
diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rsindex 114e3cefa3..53375ab410 100644--- a/datafusion-cli/src/exec.rs+++ b/datafusion-cli/src/exec.rs@@ -22,6 +22,7 @@ use std::fs::File;use std::io::prelude::*;use std::io::BufReader;+use crate::helper::split_from_semicolon;use crate::print_format::PrintFormat;use crate::{command::{Command, OutputFormat},@@ -164,21 +165,24 @@ pub async fn exec_from_repl(}}Ok(line) => {- rl.add_history_entry(line.trim_end())?;- tokio::select! {- res = exec_and_print(ctx, print_options, line) => match res {- Ok(_) => {}- Err(err) => eprintln!("{err}"),- },- _ = signal::ctrl_c() => {- println!("^C");- continue- },+ let lines = split_from_semicolon(line);+ for line in lines {+ rl.add_history_entry(line.trim_end())?;+ tokio::select! {+ res = exec_and_print(ctx, print_options, line) => match res {+ Ok(_) => {}+ Err(err) => eprintln!("{err}"),+ },+ _ = signal::ctrl_c() => {+ println!("^C");+ continue+ },+ }+ // dialect might have changed+ rl.helper_mut().unwrap().set_dialect(+ &ctx.task_ctx().session_config().options().sql_parser.dialect,+ );}- // dialect might have changed- rl.helper_mut().unwrap().set_dialect(- &ctx.task_ctx().session_config().options().sql_parser.dialect,- );}Err(ReadlineError::Interrupted) => {println!("^C");diff --git a/datafusion-cli/src/helper.rs b/datafusion-cli/src/helper.rsindex a8e149b4c5..8b196484ee 100644--- a/datafusion-cli/src/helper.rs+++ b/datafusion-cli/src/helper.rs@@ -86,16 +86,23 @@ impl CliHelper {))))}};-- match DFParser::parse_sql_with_dialect(&sql, dialect.as_ref()) {- Ok(statements) if statements.is_empty() => Ok(ValidationResult::Invalid(- Some(" 🤔 You entered an empty statement".to_string()),- )),- Ok(_statements) => Ok(ValidationResult::Valid(None)),- Err(err) => Ok(ValidationResult::Invalid(Some(format!(- " 🤔 Invalid statement: {err}",- )))),+ let lines = split_from_semicolon(sql);+ for line in lines {+ match DFParser::parse_sql_with_dialect(&line, dialect.as_ref()) {+ Ok(statements) if statements.is_empty() => {+ return Ok(ValidationResult::Invalid(Some(+ " 🤔 You entered an empty statement".to_string(),+ )));+ }+ Ok(_statements) => {}+ Err(err) => {+ return Ok(ValidationResult::Invalid(Some(format!(+ " 🤔 Invalid statement: {err}",+ ))));+ }+ }}+ Ok(ValidationResult::Valid(None))} else if input.starts_with('\\') {// commandOk(ValidationResult::Valid(None))@@ -197,6 +204,37 @@ pub fn unescape_input(input: &str) -> datafusion::error::Result<String> {Ok(result)}+/// Splits a string which consists of multiple queries.+pub(crate) fn split_from_semicolon(sql: String) -> Vec<String> {+ let mut commands = Vec::new();+ let mut current_command = String::new();+ let mut in_single_quote = false;+ let mut in_double_quote = false;++ for c in sql.chars() {+ if c == '\'' && !in_double_quote {+ in_single_quote = !in_single_quote;+ } else if c == '"' && !in_single_quote {+ in_double_quote = !in_double_quote;+ }++ if c == ';' && !in_single_quote && !in_double_quote {+ if !current_command.trim().is_empty() {+ commands.push(format!("{};", current_command.trim()));+ current_command.clear();+ }+ } else {+ current_command.push(c);+ }+ }++ if !current_command.trim().is_empty() {+ commands.push(format!("{};", current_command.trim()));+ }++ commands+}+#[cfg(test)]mod tests {use std::io::{BufRead, Cursor};@@ -292,4 +330,39 @@ mod tests {Ok(())}++ #[test]+ fn test_split_from_semicolon() {+ let sql = "SELECT 1; SELECT 2;";+ let expected = vec!["SELECT 1;", "SELECT 2;"];+ assert_eq!(split_from_semicolon(sql.to_string()), expected);++ let sql = r#"SELECT ";";"#;+ let expected = vec![r#"SELECT ";";"#];+ assert_eq!(split_from_semicolon(sql.to_string()), expected);++ let sql = "SELECT ';';";+ let expected = vec!["SELECT ';';"];+ assert_eq!(split_from_semicolon(sql.to_string()), expected);++ let sql = r#"SELECT 1; SELECT 'value;value'; SELECT 1 as "text;text";"#;+ let expected = vec![+ "SELECT 1;",+ "SELECT 'value;value';",+ r#"SELECT 1 as "text;text";"#,+ ];+ assert_eq!(split_from_semicolon(sql.to_string()), expected);++ let sql = "";+ let expected: Vec<String> = Vec::new();+ assert_eq!(split_from_semicolon(sql.to_string()), expected);++ let sql = "SELECT 1";+ let expected = vec!["SELECT 1;"];+ assert_eq!(split_from_semicolon(sql.to_string()), expected);++ let sql = "SELECT 1; ";+ let expected = vec!["SELECT 1;"];+ assert_eq!(split_from_semicolon(sql.to_string()), expected);+ }}