Skip to content

Commit 7a6e21d

Browse files
committed
refactor(cli): sync REPL parser options via CliHelper::set_parser_options
1 parent 4648c72 commit 7a6e21d

2 files changed

Lines changed: 22 additions & 20 deletions

File tree

‎datafusion-cli/src/exec.rs‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,18 @@ pub async fn exec_from_repl(
126126
print_options: &mut PrintOptions,
127127
) -> rustyline::Result<()> {
128128
let mut rl = Editor::new()?;
129-
{
130-
let task_ctx = ctx.task_ctx();
131-
let sql_parser = &task_ctx.session_config().options().sql_parser;
132-
let mut helper = CliHelper::new(&sql_parser.dialect, print_options.color);
133-
helper.set_recursion_limit(sql_parser.recursion_limit.get());
134-
rl.set_helper(Some(helper));
135-
}
129+
rl.set_helper(Some(CliHelper::new(
130+
&ctx.task_ctx().session_config().options().sql_parser.dialect,
131+
print_options.color,
132+
)));
136133
rl.load_history(".history").ok();
137134

138135
loop {
139-
// The dialect and recursion limit can change through any input,
140-
// including `\i` files, so sync the validator before every read.
141-
let task_ctx = ctx.task_ctx();
142-
let sql_parser = &task_ctx.session_config().options().sql_parser;
143-
let helper = rl.helper_mut().unwrap();
144-
helper.set_dialect(&sql_parser.dialect);
145-
helper.set_recursion_limit(sql_parser.recursion_limit.get());
136+
// Parser options can change through any input, including `\i` files,
137+
// so sync the validator before every read.
138+
rl.helper_mut()
139+
.unwrap()
140+
.set_parser_options(&ctx.task_ctx().session_config().options().sql_parser);
146141

147142
match rl.readline("> ") {
148143
Ok(line) if line.starts_with('\\') => {

‎datafusion-cli/src/helper.rs‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ impl CliHelper {
7070
}
7171
}
7272

73-
/// Sets the recursion limit used when validating input, see
74-
/// `datafusion.sql_parser.recursion_limit`.
75-
pub fn set_recursion_limit(&mut self, recursion_limit: usize) {
76-
self.recursion_limit = recursion_limit;
73+
/// Sets the parser options (dialect and recursion limit) used when
74+
/// validating input, see `datafusion.sql_parser.*`.
75+
pub fn set_parser_options(&mut self, options: &SqlParserOptions) {
76+
self.set_dialect(&options.dialect);
77+
self.recursion_limit = options.recursion_limit.get();
7778
}
7879

7980
/// Re-enable the default hint for the next prompt.
@@ -227,6 +228,8 @@ mod tests {
227228

228229
use super::*;
229230

231+
use datafusion_common::config::ConfigNonZeroUsize;
232+
230233
fn readline_direct(
231234
mut reader: impl BufRead,
232235
validator: &CliHelper,
@@ -337,18 +340,22 @@ mod tests {
337340
let nested = |depth: usize| {
338341
format!("select {}1{};", "abs(".repeat(depth), ")".repeat(depth))
339342
};
343+
let with_limit = |limit: usize| SqlParserOptions {
344+
recursion_limit: ConfigNonZeroUsize::try_new(limit).unwrap(),
345+
..Default::default()
346+
};
340347

341348
// exceeds the default recursion limit
342349
let result = readline_direct(Cursor::new(nested(60)), &validator)?;
343350
assert!(
344351
matches!(result, ValidationResult::Invalid(Some(e)) if e.contains("RecursionLimitExceeded"))
345352
);
346353

347-
validator.set_recursion_limit(100);
354+
validator.set_parser_options(&with_limit(100));
348355
let result = readline_direct(Cursor::new(nested(60)), &validator)?;
349356
assert!(matches!(result, ValidationResult::Valid(None)));
350357

351-
validator.set_recursion_limit(5);
358+
validator.set_parser_options(&with_limit(5));
352359
let result = readline_direct(Cursor::new(nested(10)), &validator)?;
353360
assert!(
354361
matches!(result, ValidationResult::Invalid(Some(e)) if e.contains("RecursionLimitExceeded"))

0 commit comments

Comments
 (0)