Apache DataFusion SQL Query Engine
add filename completer for create table statement (#1278)
Jiayu Liu1a523ff07851
diff --git a/datafusion-cli/src/helper.rs b/datafusion-cli/src/helper.rsindex ac417d2d54..7961868b6e 100644--- a/datafusion-cli/src/helper.rs+++ b/datafusion-cli/src/helper.rs@@ -16,10 +16,12 @@// under the License.//! Helper that helps with interactive editing, including multi-line parsing and validation,-//! hinting and auto-completion, etc.+//! and auto-completion for file name during creating external table.-use datafusion::sql::parser::DFParser;+use datafusion::sql::parser::{DFParser, Statement};use rustyline::completion::Completer;+use rustyline::completion::FilenameCompleter;+use rustyline::completion::Pair;use rustyline::error::ReadlineError;use rustyline::highlight::Highlighter;use rustyline::hint::Hinter;@@ -31,7 +33,9 @@ use rustyline::Helper;use rustyline::Result;#[derive(Default)]-pub(crate) struct CliHelper {}+pub(crate) struct CliHelper {+ completer: FilenameCompleter,+}impl Highlighter for CliHelper {}@@ -39,8 +43,34 @@ impl Hinter for CliHelper {type Hint = String;}+/// returns true if the current position is after the open quote for+/// creating an external table.+fn is_open_quote_for_location(line: &str, pos: usize) -> bool {+ let mut sql = line[..pos].to_string();+ sql.push('\'');+ if let Ok(stmts) = DFParser::parse_sql(&sql) {+ if let Some(Statement::CreateExternalTable(_)) = stmts.last() {+ return true;+ }+ }+ false+}+impl Completer for CliHelper {- type Candidate = String;+ type Candidate = Pair;++ fn complete(+ &self,+ line: &str,+ pos: usize,+ ctx: &Context<'_>,+ ) -> std::result::Result<(usize, Vec<Pair>), ReadlineError> {+ if is_open_quote_for_location(line, pos) {+ self.completer.complete(line, pos, ctx)+ } else {+ Ok((0, Vec::with_capacity(0)))+ }+ }}impl Validator for CliHelper {