Apache DataFusion SQL Query Engine
add list table command (#1229)
Jiayu Liudd9350607a9b
diff --git a/datafusion-cli/src/command.rs b/datafusion-cli/src/command.rsindex f99130532c..ea9bea3fb6 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.rsindex 979a222601..702d288545 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)}}