Skip to content

Commit a390e53

Browse files
authored
feat: Show View (#261)
* feat: Show View * feat: Show View * feat: Show View
1 parent 6c72eef commit a390e53

File tree

12 files changed

+136
-12
lines changed

12 files changed

+136
-12
lines changed

src/binder/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ mod explain;
1414
pub mod expr;
1515
mod insert;
1616
mod select;
17-
mod show;
17+
mod show_table;
18+
mod show_view;
1819
mod truncate;
1920
mod update;
2021

@@ -55,7 +56,8 @@ pub fn command_type(stmt: &Statement) -> Result<CommandType, DatabaseError> {
5556
Statement::Query(_)
5657
| Statement::Explain { .. }
5758
| Statement::ExplainTable { .. }
58-
| Statement::ShowTables { .. } => Ok(CommandType::DQL),
59+
| Statement::ShowTables { .. }
60+
| Statement::ShowVariable { .. } => Ok(CommandType::DQL),
5961
Statement::Analyze { .. }
6062
| Statement::Truncate { .. }
6163
| Statement::Update { .. }
@@ -409,6 +411,10 @@ impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '
409411
Statement::Analyze { table_name, .. } => self.bind_analyze(table_name)?,
410412
Statement::Truncate { table_name, .. } => self.bind_truncate(table_name)?,
411413
Statement::ShowTables { .. } => self.bind_show_tables()?,
414+
Statement::ShowVariable { variable } => match &variable[0].value.to_lowercase()[..] {
415+
"views" => self.bind_show_views()?,
416+
_ => return Err(DatabaseError::UnsupportedStmt(stmt.to_string())),
417+
},
412418
Statement::Copy {
413419
source,
414420
to,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ use crate::types::value::DataValue;
77

88
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
99
pub(crate) fn bind_show_tables(&mut self) -> Result<LogicalPlan, DatabaseError> {
10-
Ok(LogicalPlan::new(Operator::Show, Childrens::None))
10+
Ok(LogicalPlan::new(Operator::ShowTable, Childrens::None))
1111
}
1212
}

src/binder/show_view.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::binder::Binder;
2+
use crate::errors::DatabaseError;
3+
use crate::planner::operator::Operator;
4+
use crate::planner::{Childrens, LogicalPlan};
5+
use crate::storage::Transaction;
6+
use crate::types::value::DataValue;
7+
8+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
9+
pub(crate) fn bind_show_views(&mut self) -> Result<LogicalPlan, DatabaseError> {
10+
Ok(LogicalPlan::new(Operator::ShowView, Childrens::None))
11+
}
12+
}

src/execution/dql/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) mod limit;
1010
pub(crate) mod projection;
1111
pub(crate) mod seq_scan;
1212
pub(crate) mod show_table;
13+
pub(crate) mod show_view;
1314
pub(crate) mod sort;
1415
pub(crate) mod union;
1516
pub(crate) mod values;

src/execution/dql/show_view.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::catalog::view::View;
2+
use crate::execution::{Executor, ReadExecutor};
3+
use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache};
4+
use crate::throw;
5+
use crate::types::tuple::Tuple;
6+
use crate::types::value::{DataValue, Utf8Type};
7+
use sqlparser::ast::CharLengthUnits;
8+
9+
pub struct ShowViews;
10+
11+
impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for ShowViews {
12+
fn execute(
13+
self,
14+
(TableCache, _, _): (&'a TableCache, &'a ViewCache, &'a StatisticsMetaCache),
15+
transaction: *mut T,
16+
) -> Executor<'a> {
17+
Box::new(
18+
#[coroutine]
19+
move || {
20+
let metas = throw!(unsafe { &mut (*transaction) }.views(TableCache));
21+
22+
for View { name, .. } in metas {
23+
let values = vec![DataValue::Utf8 {
24+
value: name.to_string(),
25+
ty: Utf8Type::Variable(None),
26+
unit: CharLengthUnits::Characters,
27+
}];
28+
29+
yield Ok(Tuple::new(None, values));
30+
}
31+
},
32+
)
33+
}
34+
}

src/execution/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::execution::dql::limit::Limit;
3232
use crate::execution::dql::projection::Projection;
3333
use crate::execution::dql::seq_scan::SeqScan;
3434
use crate::execution::dql::show_table::ShowTables;
35+
use crate::execution::dql::show_view::ShowViews;
3536
use crate::execution::dql::sort::Sort;
3637
use crate::execution::dql::union::Union;
3738
use crate::execution::dql::values::Values;
@@ -131,7 +132,8 @@ pub fn build_read<'a, T: Transaction + 'a>(
131132
Limit::from((op, input)).execute(cache, transaction)
132133
}
133134
Operator::Values(op) => Values::from(op).execute(cache, transaction),
134-
Operator::Show => ShowTables.execute(cache, transaction),
135+
Operator::ShowTable => ShowTables.execute(cache, transaction),
136+
Operator::ShowView => ShowViews.execute(cache, transaction),
135137
Operator::Explain => {
136138
let input = childrens.pop_only();
137139

src/optimizer/rule/normalization/column_pruning.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ impl ColumnPruning {
149149
| Operator::DropTable(_)
150150
| Operator::DropView(_)
151151
| Operator::Truncate(_)
152-
| Operator::Show
152+
| Operator::ShowTable
153+
| Operator::ShowView
153154
| Operator::CopyFromFile(_)
154155
| Operator::CopyToFile(_)
155156
| Operator::AddColumn(_)

src/optimizer/rule/normalization/compilation_in_advance.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ impl ExpressionRemapper {
9090
| Operator::TableScan(_)
9191
| Operator::Limit(_)
9292
| Operator::Values(_)
93-
| Operator::Show
93+
| Operator::ShowTable
94+
| Operator::ShowView
9495
| Operator::Explain
9596
| Operator::Describe(_)
9697
| Operator::Insert(_)
@@ -197,7 +198,8 @@ impl EvaluatorBind {
197198
| Operator::TableScan(_)
198199
| Operator::Limit(_)
199200
| Operator::Values(_)
200-
| Operator::Show
201+
| Operator::ShowTable
202+
| Operator::ShowView
201203
| Operator::Explain
202204
| Operator::Describe(_)
203205
| Operator::Insert(_)

src/planner/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,12 @@ impl LogicalPlan {
174174
..
175175
}) => SchemaOutput::SchemaRef(schema_ref.clone()),
176176
Operator::Dummy => SchemaOutput::Schema(vec![]),
177-
Operator::Show => SchemaOutput::Schema(vec![ColumnRef::from(
177+
Operator::ShowTable => SchemaOutput::Schema(vec![ColumnRef::from(
178178
ColumnCatalog::new_dummy("TABLE".to_string()),
179179
)]),
180+
Operator::ShowView => SchemaOutput::Schema(vec![ColumnRef::from(
181+
ColumnCatalog::new_dummy("VIEW".to_string()),
182+
)]),
180183
Operator::Explain => SchemaOutput::Schema(vec![ColumnRef::from(
181184
ColumnCatalog::new_dummy("PLAN".to_string()),
182185
)]),

src/planner/operator/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ pub enum Operator {
6767
Sort(SortOperator),
6868
Limit(LimitOperator),
6969
Values(ValuesOperator),
70-
Show,
70+
ShowTable,
71+
ShowView,
7172
Explain,
7273
Describe(DescribeOperator),
7374
Union(UnionOperator),
@@ -159,7 +160,8 @@ impl Operator {
159160
.map(|column| ScalarExpression::ColumnRef(column.clone()))
160161
.collect_vec(),
161162
),
162-
Operator::Show
163+
Operator::ShowTable
164+
| Operator::ShowView
163165
| Operator::Explain
164166
| Operator::Describe(_)
165167
| Operator::Insert(_)
@@ -239,7 +241,8 @@ impl Operator {
239241
Operator::Delete(op) => op.primary_keys.clone(),
240242
Operator::Dummy
241243
| Operator::Limit(_)
242-
| Operator::Show
244+
| Operator::ShowTable
245+
| Operator::ShowView
243246
| Operator::Explain
244247
| Operator::Describe(_)
245248
| Operator::Insert(_)
@@ -271,7 +274,8 @@ impl fmt::Display for Operator {
271274
Operator::Sort(op) => write!(f, "{}", op),
272275
Operator::Limit(op) => write!(f, "{}", op),
273276
Operator::Values(op) => write!(f, "{}", op),
274-
Operator::Show => write!(f, "Show Tables"),
277+
Operator::ShowTable => write!(f, "Show Tables"),
278+
Operator::ShowView => write!(f, "Show Views"),
275279
Operator::Explain => unreachable!(),
276280
Operator::Describe(op) => write!(f, "{}", op),
277281
Operator::Insert(op) => write!(f, "{}", op),

0 commit comments

Comments
 (0)