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/03 06:39:34 UTC

[arrow-datafusion] branch master updated: add list table command (#1229)

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 dd93506  add list table command (#1229)
dd93506 is described below

commit dd9350607a9b4c8aaf6d7204fda8f1eab31a7ac2
Author: Jiayu Liu <Ji...@users.noreply.github.com>
AuthorDate: Wed Nov 3 14:39:31 2021 +0800

    add list table command (#1229)
---
 datafusion-cli/src/command.rs | 24 +++++++++++++++++-------
 datafusion-cli/src/exec.rs    |  2 +-
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/datafusion-cli/src/command.rs b/datafusion-cli/src/command.rs
index f991305..ea9bea3 100644
--- a/datafusion-cli/src/command.rs
+++ b/datafusion-cli/src/command.rs
@@ -17,6 +17,7 @@
 
 //! Command within CLI
 
+use crate::context::Context;
 use datafusion::arrow::array::{ArrayRef, StringArray};
 use datafusion::arrow::datatypes::{DataType, Field, Schema};
 use datafusion::arrow::record_batch::RecordBatch;
@@ -30,13 +31,20 @@ use std::sync::Arc;
 pub enum Command {
     Quit,
     Help,
+    ListTables,
 }
 
 impl Command {
-    pub fn execute(&self) -> Result<()> {
+    pub async fn execute(&self, ctx: &mut Context) -> Result<()> {
         match self {
             Self::Help => pretty::print_batches(&[all_commands_info()])
                 .map_err(|e| DataFusionError::Execution(e.to_string())),
+            Self::ListTables => {
+                let df = ctx.sql("SHOW TABLES").await?;
+                let batches = df.collect().await?;
+                pretty::print_batches(&batches)
+                    .map_err(|e| DataFusionError::Execution(e.to_string()))
+            }
             Self::Quit => Err(DataFusionError::Execution(
                 "Unexpected quit, this should be handled outside".into(),
             )),
@@ -46,12 +54,13 @@ impl Command {
     fn get_name_and_description(&self) -> (&str, &str) {
         match self {
             Self::Quit => ("\\q", "quit datafusion-cli"),
+            Self::ListTables => ("\\d", "list tables"),
             Self::Help => ("\\?", "help"),
         }
     }
 }
 
-const ALL_COMMANDS: [Command; 2] = [Command::Quit, Command::Help];
+const ALL_COMMANDS: [Command; 3] = [Command::ListTables, Command::Quit, Command::Help];
 
 fn all_commands_info() -> RecordBatch {
     let schema = Arc::new(Schema::new(vec![
@@ -76,10 +85,11 @@ impl FromStr for Command {
     type Err = ();
 
     fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
-        match s {
-            "q" => Ok(Self::Quit),
-            "?" => Ok(Self::Help),
-            _ => Err(()),
-        }
+        Ok(match s {
+            "q" => Self::Quit,
+            "d" => Self::ListTables,
+            "?" => Self::Help,
+            _ => return Err(()),
+        })
     }
 }
diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rs
index 979a222..702d288 100644
--- a/datafusion-cli/src/exec.rs
+++ b/datafusion-cli/src/exec.rs
@@ -88,7 +88,7 @@ pub async fn exec_from_repl(ctx: &mut Context, print_options: PrintOptions) {
                     match cmd {
                         Command::Quit => break,
                         others => {
-                            if let Err(e) = others.execute() {
+                            if let Err(e) = others.execute(ctx).await {
                                 eprintln!("{}", e)
                             }
                         }