Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ mod explain;
pub mod expr;
mod insert;
mod select;
mod show;
mod show_table;
mod show_view;
mod truncate;
mod update;

Expand Down Expand Up @@ -55,7 +56,8 @@ pub fn command_type(stmt: &Statement) -> Result<CommandType, DatabaseError> {
Statement::Query(_)
| Statement::Explain { .. }
| Statement::ExplainTable { .. }
| Statement::ShowTables { .. } => Ok(CommandType::DQL),
| Statement::ShowTables { .. }
| Statement::ShowVariable { .. } => Ok(CommandType::DQL),
Statement::Analyze { .. }
| Statement::Truncate { .. }
| Statement::Update { .. }
Expand Down Expand Up @@ -409,6 +411,10 @@ impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '
Statement::Analyze { table_name, .. } => self.bind_analyze(table_name)?,
Statement::Truncate { table_name, .. } => self.bind_truncate(table_name)?,
Statement::ShowTables { .. } => self.bind_show_tables()?,
Statement::ShowVariable { variable } => match &variable[0].value.to_lowercase()[..] {
"views" => self.bind_show_views()?,
_ => return Err(DatabaseError::UnsupportedStmt(stmt.to_string())),
},
Statement::Copy {
source,
to,
Expand Down
2 changes: 1 addition & 1 deletion src/binder/show.rs → src/binder/show_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use crate::types::value::DataValue;

impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_show_tables(&mut self) -> Result<LogicalPlan, DatabaseError> {
Ok(LogicalPlan::new(Operator::Show, Childrens::None))
Ok(LogicalPlan::new(Operator::ShowTable, Childrens::None))
}
}
12 changes: 12 additions & 0 deletions src/binder/show_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::binder::Binder;
use crate::errors::DatabaseError;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_show_views(&mut self) -> Result<LogicalPlan, DatabaseError> {
Ok(LogicalPlan::new(Operator::ShowView, Childrens::None))
}
}
1 change: 1 addition & 0 deletions src/execution/dql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) mod limit;
pub(crate) mod projection;
pub(crate) mod seq_scan;
pub(crate) mod show_table;
pub(crate) mod show_view;
pub(crate) mod sort;
pub(crate) mod union;
pub(crate) mod values;
Expand Down
34 changes: 34 additions & 0 deletions src/execution/dql/show_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::catalog::view::View;
use crate::execution::{Executor, ReadExecutor};
use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache};
use crate::throw;
use crate::types::tuple::Tuple;
use crate::types::value::{DataValue, Utf8Type};
use sqlparser::ast::CharLengthUnits;

pub struct ShowViews;

impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for ShowViews {
fn execute(
self,
(TableCache, _, _): (&'a TableCache, &'a ViewCache, &'a StatisticsMetaCache),
transaction: *mut T,
) -> Executor<'a> {
Box::new(
#[coroutine]
move || {
let metas = throw!(unsafe { &mut (*transaction) }.views(TableCache));

for View { name, .. } in metas {
let values = vec![DataValue::Utf8 {
value: name.to_string(),
ty: Utf8Type::Variable(None),
unit: CharLengthUnits::Characters,
}];

yield Ok(Tuple::new(None, values));
}
},
)
}
}
4 changes: 3 additions & 1 deletion src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::execution::dql::limit::Limit;
use crate::execution::dql::projection::Projection;
use crate::execution::dql::seq_scan::SeqScan;
use crate::execution::dql::show_table::ShowTables;
use crate::execution::dql::show_view::ShowViews;
use crate::execution::dql::sort::Sort;
use crate::execution::dql::union::Union;
use crate::execution::dql::values::Values;
Expand Down Expand Up @@ -131,7 +132,8 @@ pub fn build_read<'a, T: Transaction + 'a>(
Limit::from((op, input)).execute(cache, transaction)
}
Operator::Values(op) => Values::from(op).execute(cache, transaction),
Operator::Show => ShowTables.execute(cache, transaction),
Operator::ShowTable => ShowTables.execute(cache, transaction),
Operator::ShowView => ShowViews.execute(cache, transaction),
Operator::Explain => {
let input = childrens.pop_only();

Expand Down
1 change: 1 addition & 0 deletions src/optimizer/core/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ impl Histogram {
| LogicalType::Bigint
| LogicalType::UBigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _) => value.clone().cast(&LogicalType::Double)?.double(),
LogicalType::Tuple(_) => match value {
Expand Down
3 changes: 2 additions & 1 deletion src/optimizer/rule/normalization/column_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ impl ColumnPruning {
| Operator::DropTable(_)
| Operator::DropView(_)
| Operator::Truncate(_)
| Operator::Show
| Operator::ShowTable
| Operator::ShowView
| Operator::CopyFromFile(_)
| Operator::CopyToFile(_)
| Operator::AddColumn(_)
Expand Down
6 changes: 4 additions & 2 deletions src/optimizer/rule/normalization/compilation_in_advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ impl ExpressionRemapper {
| Operator::TableScan(_)
| Operator::Limit(_)
| Operator::Values(_)
| Operator::Show
| Operator::ShowTable
| Operator::ShowView
| Operator::Explain
| Operator::Describe(_)
| Operator::Insert(_)
Expand Down Expand Up @@ -197,7 +198,8 @@ impl EvaluatorBind {
| Operator::TableScan(_)
| Operator::Limit(_)
| Operator::Values(_)
| Operator::Show
| Operator::ShowTable
| Operator::ShowView
| Operator::Explain
| Operator::Describe(_)
| Operator::Insert(_)
Expand Down
5 changes: 4 additions & 1 deletion src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ impl LogicalPlan {
..
}) => SchemaOutput::SchemaRef(schema_ref.clone()),
Operator::Dummy => SchemaOutput::Schema(vec![]),
Operator::Show => SchemaOutput::Schema(vec![ColumnRef::from(
Operator::ShowTable => SchemaOutput::Schema(vec![ColumnRef::from(
ColumnCatalog::new_dummy("TABLE".to_string()),
)]),
Operator::ShowView => SchemaOutput::Schema(vec![ColumnRef::from(
ColumnCatalog::new_dummy("VIEW".to_string()),
)]),
Operator::Explain => SchemaOutput::Schema(vec![ColumnRef::from(
ColumnCatalog::new_dummy("PLAN".to_string()),
)]),
Expand Down
12 changes: 8 additions & 4 deletions src/planner/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ pub enum Operator {
Sort(SortOperator),
Limit(LimitOperator),
Values(ValuesOperator),
Show,
ShowTable,
ShowView,
Explain,
Describe(DescribeOperator),
Union(UnionOperator),
Expand Down Expand Up @@ -159,7 +160,8 @@ impl Operator {
.map(|column| ScalarExpression::ColumnRef(column.clone()))
.collect_vec(),
),
Operator::Show
Operator::ShowTable
| Operator::ShowView
| Operator::Explain
| Operator::Describe(_)
| Operator::Insert(_)
Expand Down Expand Up @@ -239,7 +241,8 @@ impl Operator {
Operator::Delete(op) => op.primary_keys.clone(),
Operator::Dummy
| Operator::Limit(_)
| Operator::Show
| Operator::ShowTable
| Operator::ShowView
| Operator::Explain
| Operator::Describe(_)
| Operator::Insert(_)
Expand Down Expand Up @@ -271,7 +274,8 @@ impl fmt::Display for Operator {
Operator::Sort(op) => write!(f, "{}", op),
Operator::Limit(op) => write!(f, "{}", op),
Operator::Values(op) => write!(f, "{}", op),
Operator::Show => write!(f, "Show Tables"),
Operator::ShowTable => write!(f, "Show Tables"),
Operator::ShowView => write!(f, "Show Views"),
Operator::Explain => unreachable!(),
Operator::Describe(op) => write!(f, "{}", op),
Operator::Insert(op) => write!(f, "{}", op),
Expand Down
15 changes: 15 additions & 0 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::types::value::DataValue;
use crate::types::{ColumnId, LogicalType};
use crate::utils::lru::SharedLruCache;
use itertools::Itertools;
use sqlparser::keywords::NULL;
use std::collections::Bound;
use std::io::Cursor;
use std::mem;
Expand Down Expand Up @@ -466,6 +467,20 @@ pub trait Transaction: Sized {
})?))
}

fn views(&self, table_cache: &TableCache) -> Result<Vec<View>, DatabaseError> {
let mut metas = vec![];
let (min, max) = unsafe { &*self.table_codec() }.view_bound();
let mut iter = self.range(Bound::Included(min), Bound::Included(max))?;

while let Some((_, value)) = iter.try_next().ok().flatten() {
let meta = TableCodec::decode_view(&value, (self, table_cache))?;

metas.push(meta);
}

Ok(metas)
}

fn table<'a>(
&'a self,
table_cache: &'a TableCache,
Expand Down
1 change: 1 addition & 0 deletions src/types/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl EvaluatorFactory {
LogicalType::UInteger => numeric_binary_evaluator!(UInt32, op, LogicalType::UInteger),
LogicalType::UBigint => numeric_binary_evaluator!(UInt64, op, LogicalType::UBigint),
LogicalType::Float => numeric_binary_evaluator!(Float32, op, LogicalType::Float),
LogicalType::Real => numeric_binary_evaluator!(Float32, op, LogicalType::Real),
LogicalType::Double => numeric_binary_evaluator!(Float64, op, LogicalType::Double),
LogicalType::Date => numeric_binary_evaluator!(Date, op, LogicalType::Date),
LogicalType::DateTime => numeric_binary_evaluator!(DateTime, op, LogicalType::DateTime),
Expand Down
31 changes: 28 additions & 3 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum LogicalType {
Bigint,
UBigint,
Float,
Real,
Double,
Char(u32, CharLengthUnits),
Varchar(Option<u32>, CharLengthUnits),
Expand Down Expand Up @@ -76,6 +77,8 @@ impl LogicalType {
Some(LogicalType::UBigint)
} else if type_id == TypeId::of::<f32>() {
Some(LogicalType::Float)
} else if type_id == TypeId::of::<f32>() {
Some(LogicalType::Real)
} else if type_id == TypeId::of::<f64>() {
Some(LogicalType::Double)
} else if type_id == TypeId::of::<NaiveDate>() {
Expand Down Expand Up @@ -106,6 +109,7 @@ impl LogicalType {
LogicalType::Bigint => Some(8),
LogicalType::UBigint => Some(8),
LogicalType::Float => Some(4),
LogicalType::Real => Some(4),
LogicalType::Double => Some(8),
/// Note: The non-fixed length type's raw_len is None e.g. Varchar
LogicalType::Varchar(_, _) => None,
Expand All @@ -132,6 +136,7 @@ impl LogicalType {
LogicalType::Bigint,
LogicalType::UBigint,
LogicalType::Float,
LogicalType::Real,
LogicalType::Double,
]
}
Expand All @@ -148,6 +153,7 @@ impl LogicalType {
| LogicalType::Bigint
| LogicalType::UBigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
)
Expand All @@ -174,7 +180,10 @@ impl LogicalType {
}

pub fn is_floating_point_numeric(&self) -> bool {
matches!(self, LogicalType::Float | LogicalType::Double)
matches!(
self,
LogicalType::Float | LogicalType::Real | LogicalType::Double
)
}

pub fn max_logical_type(
Expand Down Expand Up @@ -290,6 +299,7 @@ impl LogicalType {
| LogicalType::Integer
| LogicalType::Bigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
Expand All @@ -302,6 +312,7 @@ impl LogicalType {
| LogicalType::Integer
| LogicalType::Bigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
Expand All @@ -310,6 +321,7 @@ impl LogicalType {
LogicalType::Integer
| LogicalType::Bigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
Expand All @@ -320,13 +332,15 @@ impl LogicalType {
| LogicalType::Integer
| LogicalType::Bigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
LogicalType::Integer => matches!(
to,
LogicalType::Bigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
Expand All @@ -335,18 +349,26 @@ impl LogicalType {
LogicalType::UBigint
| LogicalType::Bigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
LogicalType::Bigint => matches!(
to,
LogicalType::Float | LogicalType::Double | LogicalType::Decimal(_, _)
LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
LogicalType::UBigint => matches!(
to,
LogicalType::Float | LogicalType::Double | LogicalType::Decimal(_, _)
LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _)
),
LogicalType::Float => matches!(to, LogicalType::Double | LogicalType::Decimal(_, _)),
LogicalType::Real => matches!(to, LogicalType::Double | LogicalType::Decimal(_, _)),
LogicalType::Double => matches!(to, LogicalType::Decimal(_, _)),
LogicalType::Char(..) => false,
LogicalType::Varchar(..) => false,
Expand Down Expand Up @@ -406,6 +428,7 @@ impl TryFrom<sqlparser::ast::DataType> for LogicalType {
Ok(LogicalType::Varchar(None, CharLengthUnits::Characters))
}
sqlparser::ast::DataType::Float(_) => Ok(LogicalType::Float),
sqlparser::ast::DataType::Real => Ok(LogicalType::Real),
sqlparser::ast::DataType::Double | sqlparser::ast::DataType::DoublePrecision => {
Ok(LogicalType::Double)
}
Expand Down Expand Up @@ -473,6 +496,7 @@ impl std::fmt::Display for LogicalType {
LogicalType::Bigint => write!(f, "Bigint")?,
LogicalType::UBigint => write!(f, "UBigint")?,
LogicalType::Float => write!(f, "Float")?,
LogicalType::Real => write!(f, "Real")?,
LogicalType::Double => write!(f, "Double")?,
LogicalType::Char(len, units) => write!(f, "Char({}, {})", len, units)?,
LogicalType::Varchar(len, units) => write!(f, "Varchar({:?}, {})", len, units)?,
Expand Down Expand Up @@ -542,6 +566,7 @@ pub(crate) mod test {
fn_assert(&mut cursor, &mut reference_tables, LogicalType::Bigint)?;
fn_assert(&mut cursor, &mut reference_tables, LogicalType::UBigint)?;
fn_assert(&mut cursor, &mut reference_tables, LogicalType::Float)?;
fn_assert(&mut cursor, &mut reference_tables, LogicalType::Real)?;
fn_assert(&mut cursor, &mut reference_tables, LogicalType::Double)?;
fn_assert(
&mut cursor,
Expand Down
Loading
Loading