Skip to content

Commit c71a00c

Browse files
committed
feat: Show View
1 parent 6c72eef commit c71a00c

12 files changed

Lines changed: 140 additions & 12 deletions

File tree

src/binder/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ mod explain;
1414
pub mod expr;
1515
mod insert;
1616
mod select;
17-
mod show;
17+
mod show_table;
1818
mod truncate;
1919
mod update;
20+
mod show_view;
2021

2122
use sqlparser::ast::{Ident, ObjectName, ObjectType, SetExpr, Statement};
2223
use std::collections::{BTreeMap, HashMap, HashSet};
@@ -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,13 @@ 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 } => {
415+
let value = variable[0].value.clone();
416+
match &value[..] {
417+
"views" => self.bind_show_views()?,
418+
_ => return Err(DatabaseError::UnsupportedStmt(stmt.to_string()))
419+
}
420+
},
412421
Statement::Copy {
413422
source,
414423
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
@@ -13,6 +13,7 @@ pub(crate) mod show_table;
1313
pub(crate) mod sort;
1414
pub(crate) mod union;
1515
pub(crate) mod values;
16+
pub(crate) mod show_view;
1617

1718
#[cfg(test)]
1819
pub(crate) mod test {

src/execution/dql/show_view.rs

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

src/execution/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache};
4242
use crate::types::index::IndexInfo;
4343
use crate::types::tuple::Tuple;
4444
use std::ops::Coroutine;
45+
use crate::execution::dql::show_view::ShowViews;
4546

4647
pub type Executor<'a> =
4748
Box<dyn Coroutine<Yield = Result<Tuple, DatabaseError>, Return = ()> + 'a + Unpin>;
@@ -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)