Skip to content

Commit 87790e3

Browse files
committed
Remove all cli unescaping
1 parent e3c56d4 commit 87790e3

File tree

2 files changed

+10
-71
lines changed

2 files changed

+10
-71
lines changed

datafusion-cli/src/exec.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::helper::split_from_semicolon;
2222
use crate::print_format::PrintFormat;
2323
use crate::{
2424
command::{Command, OutputFormat},
25-
helper::{unescape_input, CliHelper},
25+
helper::CliHelper,
2626
object_storage::get_object_store,
2727
print_options::{MaxRows, PrintOptions},
2828
};
@@ -172,7 +172,7 @@ pub async fn exec_from_repl(
172172
}
173173
}
174174
Ok(line) => {
175-
let lines = split_from_semicolon(line);
175+
let lines = split_from_semicolon(&line);
176176
for line in lines {
177177
rl.add_history_entry(line.trim_end())?;
178178
tokio::select! {
@@ -215,7 +215,6 @@ pub(super) async fn exec_and_print(
215215
sql: String,
216216
) -> Result<()> {
217217
let now = Instant::now();
218-
let sql = unescape_input(&sql)?;
219218
let task_ctx = ctx.task_ctx();
220219
let dialect = &task_ctx.session_config().options().sql_parser.dialect;
221220
let dialect = dialect_from_str(dialect).ok_or_else(|| {

datafusion-cli/src/helper.rs

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ use std::borrow::Cow;
2222

2323
use crate::highlighter::{NoSyntaxHighlighter, SyntaxHighlighter};
2424

25-
use datafusion::common::sql_datafusion_err;
26-
use datafusion::error::DataFusionError;
2725
use datafusion::sql::parser::{DFParser, Statement};
2826
use datafusion::sql::sqlparser::dialect::dialect_from_str;
29-
use datafusion::sql::sqlparser::parser::ParserError;
3027

3128
use rustyline::completion::{Completer, FilenameCompleter, Pair};
3229
use rustyline::error::ReadlineError;
@@ -63,15 +60,6 @@ impl CliHelper {
6360

6461
fn validate_input(&self, input: &str) -> Result<ValidationResult> {
6562
if let Some(sql) = input.strip_suffix(';') {
66-
let sql = match unescape_input(sql) {
67-
Ok(sql) => sql,
68-
Err(err) => {
69-
return Ok(ValidationResult::Invalid(Some(format!(
70-
" 🤔 Invalid statement: {err}",
71-
))))
72-
}
73-
};
74-
7563
let dialect = match dialect_from_str(&self.dialect) {
7664
Some(dialect) => dialect,
7765
None => {
@@ -166,56 +154,8 @@ impl Validator for CliHelper {
166154

167155
impl Helper for CliHelper {}
168156

169-
/// Unescape input string from readline.
170-
///
171-
/// The data read from stdio will be escaped, so we need to unescape the input before executing the input
172-
pub fn unescape_input(input: &str) -> datafusion::error::Result<String> {
173-
let mut chars = input.chars().peekable();
174-
let mut result = String::with_capacity(input.len());
175-
176-
while let Some(ch) = chars.next() {
177-
if ch == '\\' {
178-
if let Some(&next) = chars.peek() {
179-
match next {
180-
'0' => {
181-
chars.next();
182-
result.push('\0');
183-
}
184-
'n' => {
185-
chars.next();
186-
result.push('\n');
187-
}
188-
'r' => {
189-
chars.next();
190-
result.push('\r');
191-
}
192-
't' => {
193-
chars.next();
194-
result.push('\t');
195-
}
196-
'\\' | '\'' => result.push('\\'),
197-
_ => {
198-
return Err(DataFusionError::Execution(format!(
199-
"Invalid escape sequence: \\{}",
200-
next
201-
)))
202-
}
203-
}
204-
} else {
205-
return Err(sql_datafusion_err!(ParserError::TokenizerError(
206-
"incomplete escape sequence: trailing backslash".to_string()
207-
)));
208-
}
209-
} else {
210-
result.push(ch);
211-
}
212-
}
213-
214-
Ok(result)
215-
}
216-
217157
/// Splits a string which consists of multiple queries.
218-
pub(crate) fn split_from_semicolon(sql: String) -> Vec<String> {
158+
pub(crate) fn split_from_semicolon(sql: &str) -> Vec<String> {
219159
let mut commands = Vec::new();
220160
let mut current_command = String::new();
221161
let mut in_single_quote = false;
@@ -370,34 +310,34 @@ mod tests {
370310
fn test_split_from_semicolon() {
371311
let sql = "SELECT 1; SELECT 2;";
372312
let expected = vec!["SELECT 1;", "SELECT 2;"];
373-
assert_eq!(split_from_semicolon(sql.to_string()), expected);
313+
assert_eq!(split_from_semicolon(sql), expected);
374314

375315
let sql = r#"SELECT ";";"#;
376316
let expected = vec![r#"SELECT ";";"#];
377-
assert_eq!(split_from_semicolon(sql.to_string()), expected);
317+
assert_eq!(split_from_semicolon(sql), expected);
378318

379319
let sql = "SELECT ';';";
380320
let expected = vec!["SELECT ';';"];
381-
assert_eq!(split_from_semicolon(sql.to_string()), expected);
321+
assert_eq!(split_from_semicolon(sql), expected);
382322

383323
let sql = r#"SELECT 1; SELECT 'value;value'; SELECT 1 as "text;text";"#;
384324
let expected = vec![
385325
"SELECT 1;",
386326
"SELECT 'value;value';",
387327
r#"SELECT 1 as "text;text";"#,
388328
];
389-
assert_eq!(split_from_semicolon(sql.to_string()), expected);
329+
assert_eq!(split_from_semicolon(sql), expected);
390330

391331
let sql = "";
392332
let expected: Vec<String> = Vec::new();
393-
assert_eq!(split_from_semicolon(sql.to_string()), expected);
333+
assert_eq!(split_from_semicolon(sql), expected);
394334

395335
let sql = "SELECT 1";
396336
let expected = vec!["SELECT 1;"];
397-
assert_eq!(split_from_semicolon(sql.to_string()), expected);
337+
assert_eq!(split_from_semicolon(sql), expected);
398338

399339
let sql = "SELECT 1; ";
400340
let expected = vec!["SELECT 1;"];
401-
assert_eq!(split_from_semicolon(sql.to_string()), expected);
341+
assert_eq!(split_from_semicolon(sql), expected);
402342
}
403343
}

0 commit comments

Comments
 (0)