Skip to content

Commit ab7507f

Browse files
alambcomphead
andauthored
[branch-53] cli: Fix datafusion-cli hint edge cases (apache#20609) (apache#20887)
- Part of apache#19692 This PR: - Backports apache#20609 from @comphead to the branch-53 line Co-authored-by: Oleks V <comphead@users.noreply.github.com>
1 parent 7698fdc commit ab7507f

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

datafusion-cli/src/exec.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub async fn exec_from_repl(
196196
}
197197
Err(ReadlineError::Interrupted) => {
198198
println!("^C");
199+
rl.helper().unwrap().reset_hint();
199200
continue;
200201
}
201202
Err(ReadlineError::Eof) => {

datafusion-cli/src/helper.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! and auto-completion for file name during creating external table.
2020
2121
use std::borrow::Cow;
22+
use std::cell::Cell;
2223

2324
use crate::highlighter::{Color, NoSyntaxHighlighter, SyntaxHighlighter};
2425

@@ -40,6 +41,10 @@ pub struct CliHelper {
4041
completer: FilenameCompleter,
4142
dialect: Dialect,
4243
highlighter: Box<dyn Highlighter>,
44+
/// Tracks whether to show the default hint. Set to `false` once the user
45+
/// types anything, so the hint doesn't reappear after deleting back to
46+
/// an empty line. Reset to `true` when the line is submitted.
47+
show_hint: Cell<bool>,
4348
}
4449

4550
impl CliHelper {
@@ -53,6 +58,7 @@ impl CliHelper {
5358
completer: FilenameCompleter::new(),
5459
dialect: *dialect,
5560
highlighter,
61+
show_hint: Cell::new(true),
5662
}
5763
}
5864

@@ -62,6 +68,11 @@ impl CliHelper {
6268
}
6369
}
6470

71+
/// Re-enable the default hint for the next prompt.
72+
pub fn reset_hint(&self) {
73+
self.show_hint.set(true);
74+
}
75+
6576
fn validate_input(&self, input: &str) -> Result<ValidationResult> {
6677
if let Some(sql) = input.strip_suffix(';') {
6778
let dialect = match dialect_from_str(self.dialect) {
@@ -119,12 +130,11 @@ impl Hinter for CliHelper {
119130
type Hint = String;
120131

121132
fn hint(&self, line: &str, _pos: usize, _ctx: &Context<'_>) -> Option<String> {
122-
if line.trim().is_empty() {
123-
let suggestion = Color::gray(DEFAULT_HINT_SUGGESTION);
124-
Some(suggestion)
125-
} else {
126-
None
133+
if !line.is_empty() {
134+
self.show_hint.set(false);
127135
}
136+
(self.show_hint.get() && line.trim().is_empty())
137+
.then(|| Color::gray(DEFAULT_HINT_SUGGESTION))
128138
}
129139
}
130140

@@ -133,12 +143,9 @@ impl Hinter for CliHelper {
133143
fn is_open_quote_for_location(line: &str, pos: usize) -> bool {
134144
let mut sql = line[..pos].to_string();
135145
sql.push('\'');
136-
if let Ok(stmts) = DFParser::parse_sql(&sql)
137-
&& let Some(Statement::CreateExternalTable(_)) = stmts.back()
138-
{
139-
return true;
140-
}
141-
false
146+
DFParser::parse_sql(&sql).is_ok_and(|stmts| {
147+
matches!(stmts.back(), Some(Statement::CreateExternalTable(_)))
148+
})
142149
}
143150

144151
impl Completer for CliHelper {
@@ -161,7 +168,9 @@ impl Completer for CliHelper {
161168
impl Validator for CliHelper {
162169
fn validate(&self, ctx: &mut ValidationContext<'_>) -> Result<ValidationResult> {
163170
let input = ctx.input().trim_end();
164-
self.validate_input(input)
171+
let result = self.validate_input(input);
172+
self.reset_hint();
173+
result
165174
}
166175
}
167176

0 commit comments

Comments
 (0)