Skip to content

Commit a754969

Browse files
committed
feat: impl View as data source
1 parent a828a57 commit a754969

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+659
-408
lines changed

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2024-04-27
1+
nightly-2024-10-10

src/binder/aggregate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414

1515
use super::{Binder, QueryBindStep};
1616

17-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
17+
impl<T: Transaction> Binder<'_, '_, T> {
1818
pub fn bind_aggregate(
1919
&mut self,
2020
children: LogicalPlan,

src/binder/alter_table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::planner::operator::Operator;
1212
use crate::planner::LogicalPlan;
1313
use crate::storage::Transaction;
1414

15-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
15+
impl<T: Transaction> Binder<'_, '_, T> {
1616
pub(crate) fn bind_alter_table(
1717
&mut self,
1818
name: &ObjectName,
@@ -21,7 +21,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
2121
let table_name: Arc<String> = Arc::new(lower_case_name(name)?);
2222
let table = self
2323
.context
24-
.table(table_name.clone())
24+
.table(table_name.clone())?
2525
.ok_or(DatabaseError::TableNotFound)?;
2626
let plan = match operation {
2727
AlterTableOperation::AddColumn {

src/binder/analyze.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::binder::{lower_case_name, Binder};
1+
use crate::binder::{lower_case_name, Binder, Source};
22
use crate::errors::DatabaseError;
33
use crate::planner::operator::analyze::AnalyzeOperator;
44
use crate::planner::operator::table_scan::TableScanOperator;
@@ -8,16 +8,24 @@ use crate::storage::Transaction;
88
use sqlparser::ast::ObjectName;
99
use std::sync::Arc;
1010

11-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
11+
impl<T: Transaction> Binder<'_, '_, T> {
1212
pub(crate) fn bind_analyze(&mut self, name: &ObjectName) -> Result<LogicalPlan, DatabaseError> {
1313
let table_name = Arc::new(lower_case_name(name)?);
1414

15-
let table_catalog = self
15+
let table = self
1616
.context
17-
.table_and_bind(table_name.clone(), None, None)?;
18-
let index_metas = table_catalog.indexes.clone();
17+
.source_and_bind(table_name.clone(), None, None, true)?
18+
.and_then(|source| {
19+
if let Source::Table(table) = source {
20+
Some(table)
21+
} else {
22+
None
23+
}
24+
})
25+
.ok_or(DatabaseError::TableNotFound)?;
26+
let index_metas = table.indexes.clone();
1927

20-
let scan_op = TableScanOperator::build(table_name.clone(), table_catalog);
28+
let scan_op = TableScanOperator::build(table_name.clone(), table);
2129
Ok(LogicalPlan::new(
2230
Operator::Analyze(AnalyzeOperator {
2331
table_name,

src/binder/copy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl FromStr for ExtSource {
6262
}
6363
}
6464

65-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
65+
impl<T: Transaction> Binder<'_, '_, T> {
6666
pub(super) fn bind_copy(
6767
&mut self,
6868
source: CopySource,
@@ -80,7 +80,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
8080
}
8181
};
8282

83-
if let Some(table) = self.context.table(Arc::new(table_name.to_string())) {
83+
if let Some(table) = self.context.table(Arc::new(table_name.to_string()))? {
8484
let schema_ref = table.schema_ref().clone();
8585
let ext_source = ExtSource {
8686
path: match target {

src/binder/create_index.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::binder::{lower_case_name, Binder};
1+
use crate::binder::{lower_case_name, Binder, Source};
22
use crate::errors::DatabaseError;
33
use crate::expression::ScalarExpression;
44
use crate::planner::operator::create_index::CreateIndexOperator;
@@ -10,7 +10,7 @@ use crate::types::index::IndexType;
1010
use sqlparser::ast::{ObjectName, OrderByExpr};
1111
use std::sync::Arc;
1212

13-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
13+
impl<T: Transaction> Binder<'_, '_, T> {
1414
pub(crate) fn bind_create_index(
1515
&mut self,
1616
table_name: &ObjectName,
@@ -29,10 +29,14 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
2929
IndexType::Composite
3030
};
3131

32-
let table = self
32+
let source = self
3333
.context
34-
.table_and_bind(table_name.clone(), None, None)?;
35-
let plan = TableScanOperator::build(table_name.clone(), table);
34+
.source_and_bind(table_name.clone(), None, None, false)?
35+
.ok_or(DatabaseError::SourceNotFound)?;
36+
let plan = match source {
37+
Source::Table(table) => TableScanOperator::build(table_name.clone(), table),
38+
Source::View(view) => LogicalPlan::clone(&view.plan),
39+
};
3640
let mut columns = Vec::with_capacity(exprs.len());
3741

3842
for expr in exprs {

src/binder/create_table.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::planner::LogicalPlan;
1414
use crate::storage::Transaction;
1515
use crate::types::LogicalType;
1616

17-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
17+
impl<T: Transaction> Binder<'_, '_, T> {
1818
// TODO: TableConstraint
1919
pub(crate) fn bind_create_table(
2020
&mut self,
@@ -158,13 +158,15 @@ mod tests {
158158
let storage = RocksStorage::new(temp_dir.path())?;
159159
let transaction = storage.transaction()?;
160160
let table_cache = Arc::new(ShardingLruCache::new(4, 1, RandomState::new())?);
161+
let view_cache = Arc::new(ShardingLruCache::new(4, 1, RandomState::new())?);
161162
let scala_functions = Default::default();
162163
let table_functions = Default::default();
163164

164165
let sql = "create table t1 (id int primary key, name varchar(10) null)";
165166
let mut binder = Binder::new(
166167
BinderContext::new(
167168
&table_cache,
169+
&view_cache,
168170
&transaction,
169171
&scala_functions,
170172
&table_functions,

src/binder/create_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use sqlparser::ast::{Ident, ObjectName, Query};
1212
use std::sync::Arc;
1313
use ulid::Ulid;
1414

15-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
15+
impl<T: Transaction> Binder<'_, '_, T> {
1616
pub(crate) fn bind_create_view(
1717
&mut self,
1818
or_replace: &bool,

src/binder/delete.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::binder::{lower_case_name, Binder};
1+
use crate::binder::{lower_case_name, Binder, Source};
22
use crate::errors::DatabaseError;
33
use crate::planner::operator::delete::DeleteOperator;
44
use crate::planner::operator::table_scan::TableScanOperator;
@@ -8,7 +8,7 @@ use crate::storage::Transaction;
88
use sqlparser::ast::{Expr, TableAlias, TableFactor, TableWithJoins};
99
use std::sync::Arc;
1010

11-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
11+
impl<T: Transaction> Binder<'_, '_, T> {
1212
pub(crate) fn bind_delete(
1313
&mut self,
1414
from: &TableWithJoins,
@@ -23,15 +23,20 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
2323
table_alias = Some(Arc::new(name.value.to_lowercase()));
2424
alias_idents = Some(columns);
2525
}
26-
let table_catalog =
27-
self.context
28-
.table_and_bind(table_name.clone(), table_alias.clone(), None)?;
29-
let primary_key_column = table_catalog
30-
.columns()
26+
let source = self
27+
.context
28+
.source_and_bind(table_name.clone(), table_alias.as_ref(), None, false)?
29+
.ok_or(DatabaseError::SourceNotFound)?;
30+
let schema_buf = self.table_schema_buf.entry(table_name.clone()).or_default();
31+
let primary_key_column = source
32+
.columns(schema_buf)
3133
.find(|column| column.desc().is_primary)
3234
.cloned()
3335
.unwrap();
34-
let mut plan = TableScanOperator::build(table_name.clone(), table_catalog);
36+
let mut plan = match source {
37+
Source::Table(table) => TableScanOperator::build(table_name.clone(), table),
38+
Source::View(view) => LogicalPlan::clone(&view.plan),
39+
};
3540

3641
if let Some(alias_idents) = alias_idents {
3742
plan =

src/binder/describe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::storage::Transaction;
77
use sqlparser::ast::ObjectName;
88
use std::sync::Arc;
99

10-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
10+
impl<T: Transaction> Binder<'_, '_, T> {
1111
pub(crate) fn bind_describe(
1212
&mut self,
1313
name: &ObjectName,

0 commit comments

Comments
 (0)