|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Functions that are query-able and searchable via the `\h` command |
| 19 | +use arrow::array::StringArray; |
| 20 | +use arrow::datatypes::{DataType, Field, Schema}; |
| 21 | +use arrow::record_batch::RecordBatch; |
| 22 | +use arrow::util::pretty::pretty_format_batches; |
| 23 | +use datafusion::error::{DataFusionError, Result}; |
| 24 | +use std::fmt; |
| 25 | +use std::str::FromStr; |
| 26 | +use std::sync::Arc; |
| 27 | + |
| 28 | +#[derive(Debug)] |
| 29 | +pub enum Function { |
| 30 | + Select, |
| 31 | + Explain, |
| 32 | + Show, |
| 33 | + CreateTable, |
| 34 | + CreateTableAs, |
| 35 | + Insert, |
| 36 | + DropTable, |
| 37 | +} |
| 38 | + |
| 39 | +const ALL_FUNCTIONS: [Function; 7] = [ |
| 40 | + Function::CreateTable, |
| 41 | + Function::CreateTableAs, |
| 42 | + Function::DropTable, |
| 43 | + Function::Explain, |
| 44 | + Function::Insert, |
| 45 | + Function::Select, |
| 46 | + Function::Show, |
| 47 | +]; |
| 48 | + |
| 49 | +impl Function { |
| 50 | + pub fn function_details(&self) -> Result<&str> { |
| 51 | + let details = match self { |
| 52 | + Function::Select => { |
| 53 | + r#" |
| 54 | +Command: SELECT |
| 55 | +Description: retrieve rows from a table or view |
| 56 | +Syntax: |
| 57 | +SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] |
| 58 | + [ * | expression [ [ AS ] output_name ] [, ...] ] |
| 59 | + [ FROM from_item [, ...] ] |
| 60 | + [ WHERE condition ] |
| 61 | + [ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ] |
| 62 | + [ HAVING condition ] |
| 63 | + [ WINDOW window_name AS ( window_definition ) [, ...] ] |
| 64 | + [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ] |
| 65 | + [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] |
| 66 | + [ LIMIT { count | ALL } ] |
| 67 | + [ OFFSET start [ ROW | ROWS ] ] |
| 68 | +
|
| 69 | +where from_item can be one of: |
| 70 | +
|
| 71 | + [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ] |
| 72 | + [ TABLESAMPLE sampling_method ( argument [, ...] ) [ REPEATABLE ( seed ) ] ] |
| 73 | + [ LATERAL ] ( select ) [ AS ] alias [ ( column_alias [, ...] ) ] |
| 74 | + with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ] |
| 75 | + [ LATERAL ] function_name ( [ argument [, ...] ] ) |
| 76 | + [ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ] |
| 77 | + [ LATERAL ] function_name ( [ argument [, ...] ] ) [ AS ] alias ( column_definition [, ...] ) |
| 78 | + [ LATERAL ] function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] ) |
| 79 | + [ LATERAL ] ROWS FROM( function_name ( [ argument [, ...] ] ) [ AS ( column_definition [, ...] ) ] [, ...] ) |
| 80 | + [ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ] |
| 81 | + from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) [ AS join_using_alias ] ] |
| 82 | +
|
| 83 | +and grouping_element can be one of: |
| 84 | +
|
| 85 | + ( ) |
| 86 | + expression |
| 87 | + ( expression [, ...] ) |
| 88 | +
|
| 89 | +and with_query is: |
| 90 | +
|
| 91 | + with_query_name [ ( column_name [, ...] ) ] AS [ [ NOT ] MATERIALIZED ] ( select | values | insert | update | delete ) |
| 92 | +
|
| 93 | +TABLE [ ONLY ] table_name [ * ]"# |
| 94 | + } |
| 95 | + Function::Explain => { |
| 96 | + r#" |
| 97 | +Command: EXPLAIN |
| 98 | +Description: show the execution plan of a statement |
| 99 | +Syntax: |
| 100 | +EXPLAIN [ ANALYZE ] statement |
| 101 | +"# |
| 102 | + } |
| 103 | + Function::Show => { |
| 104 | + r#" |
| 105 | +Command: SHOW |
| 106 | +Description: show the value of a run-time parameter |
| 107 | +Syntax: |
| 108 | +SHOW name |
| 109 | +"# |
| 110 | + } |
| 111 | + Function::CreateTable => { |
| 112 | + r#" |
| 113 | +Command: CREATE TABLE |
| 114 | +Description: define a new table |
| 115 | +Syntax: |
| 116 | +CREATE [ EXTERNAL ] TABLE table_name ( [ |
| 117 | + { column_name data_type } |
| 118 | + [, ... ] |
| 119 | +] ) |
| 120 | +"# |
| 121 | + } |
| 122 | + Function::CreateTableAs => { |
| 123 | + r#" |
| 124 | +Command: CREATE TABLE AS |
| 125 | +Description: define a new table from the results of a query |
| 126 | +Syntax: |
| 127 | +CREATE TABLE table_name |
| 128 | + [ (column_name [, ...] ) ] |
| 129 | + AS query |
| 130 | + [ WITH [ NO ] DATA ] |
| 131 | +"# |
| 132 | + } |
| 133 | + Function::Insert => { |
| 134 | + r#" |
| 135 | +Command: INSERT |
| 136 | +Description: create new rows in a table |
| 137 | +Syntax: |
| 138 | +INSERT INTO table_name [ ( column_name [, ...] ) ] |
| 139 | + { VALUES ( { expression } [, ...] ) [, ...] } |
| 140 | +"# |
| 141 | + } |
| 142 | + Function::DropTable => { |
| 143 | + r#" |
| 144 | +Command: DROP TABLE |
| 145 | +Description: remove a table |
| 146 | +Syntax: |
| 147 | +DROP TABLE [ IF EXISTS ] name [, ...] |
| 148 | +"# |
| 149 | + } |
| 150 | + }; |
| 151 | + Ok(details) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl FromStr for Function { |
| 156 | + type Err = (); |
| 157 | + |
| 158 | + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { |
| 159 | + Ok(match s.trim().to_uppercase().as_str() { |
| 160 | + "SELECT" => Self::Select, |
| 161 | + "EXPLAIN" => Self::Explain, |
| 162 | + "SHOW" => Self::Show, |
| 163 | + "CREATE TABLE" => Self::CreateTable, |
| 164 | + "CREATE TABLE AS" => Self::CreateTableAs, |
| 165 | + "INSERT" => Self::Insert, |
| 166 | + "DROP TABLE" => Self::DropTable, |
| 167 | + _ => return Err(()), |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +impl fmt::Display for Function { |
| 173 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 174 | + match *self { |
| 175 | + Function::Select => write!(f, "SELECT"), |
| 176 | + Function::Explain => write!(f, "EXPLAIN"), |
| 177 | + Function::Show => write!(f, "SHOW"), |
| 178 | + Function::CreateTable => write!(f, "CREATE TABLE"), |
| 179 | + Function::CreateTableAs => write!(f, "CREATE TABLE AS"), |
| 180 | + Function::Insert => write!(f, "INSERT"), |
| 181 | + Function::DropTable => write!(f, "DROP TABLE"), |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +pub fn display_all_functions() -> Result<()> { |
| 187 | + println!("Available help:"); |
| 188 | + let array = StringArray::from( |
| 189 | + ALL_FUNCTIONS |
| 190 | + .iter() |
| 191 | + .map(|f| format!("{}", f)) |
| 192 | + .collect::<Vec<String>>(), |
| 193 | + ); |
| 194 | + let schema = Schema::new(vec![Field::new("Function", DataType::Utf8, false)]); |
| 195 | + let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(array)])?; |
| 196 | + println!("{}", pretty_format_batches(&[batch]).unwrap()); |
| 197 | + Ok(()) |
| 198 | +} |
0 commit comments