You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ji...@apache.org on 2021/11/13 10:16:45 UTC
[arrow-datafusion] branch master updated: add filename completer for create table statement (#1278)
This is an automated email from the ASF dual-hosted git repository.
jiayuliu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/master by this push:
new 1a523ff add filename completer for create table statement (#1278)
1a523ff is described below
commit 1a523ff078512d4a62fa08e12488da0986423abc
Author: Jiayu Liu <Ji...@users.noreply.github.com>
AuthorDate: Sat Nov 13 18:16:42 2021 +0800
add filename completer for create table statement (#1278)
---
datafusion-cli/src/helper.rs | 38 ++++++++++++++++++++++++++++++++++----
1 file changed, 34 insertions(+), 4 deletions(-)
diff --git a/datafusion-cli/src/helper.rs b/datafusion-cli/src/helper.rs
index ac417d2..7961868 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 {