Skip to content
Merged
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
17 changes: 15 additions & 2 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ use crate::catalog::{RootCatalogRef, TableCatalog};
pub struct Binder {
catalog: RootCatalogRef,
context: BinderContext,
subquery_base_index: usize,
}

#[derive(Default)]
// FIXME: remove dead_code after we used parent context
#[derive(Default, Clone, Debug)]
#[allow(dead_code)]
struct BinderContext {
/// table_name == table_id
/// table_id -> table_catalog
tables: HashMap<String, TableCatalog>,
aliases: HashMap<String, BoundExpr>,
subquery_base_index: usize,
parent: Option<Box<BinderContext>>,
}

impl Binder {
pub fn new(catalog: RootCatalogRef) -> Self {
Self {
catalog,
context: BinderContext::default(),
subquery_base_index: 0,
}
}

Expand All @@ -44,6 +48,15 @@ impl Binder {
}
}

impl BinderContext {
pub fn with_parent(parent: Box<BinderContext>) -> Self {
Self {
parent: Some(parent),
..Default::default()
}
}
}

#[derive(thiserror::Error, Debug)]
pub enum BindError {
#[error("unsupported statement {0}")]
Expand Down
3 changes: 2 additions & 1 deletion src/binder/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use join::*;
use sqlparser::ast::{TableFactor, TableWithJoins};
pub use subquery::*;

use super::{BindError, Binder};
use super::{BindError, Binder, BinderContext};
use crate::catalog::{ColumnCatalog, ColumnId, TableCatalog, TableId};

pub static DEFAULT_DATABASE_NAME: &str = "postgres";
Expand Down Expand Up @@ -186,6 +186,7 @@ impl Binder {
} => {
// handle subquery as source
let query = self.bind_select(subquery)?;
self.context = BinderContext::with_parent(Box::new(self.context.clone()));
let alias = alias
.clone()
.map(|a| a.to_string().to_lowercase())
Expand Down
4 changes: 2 additions & 2 deletions src/binder/table/subquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ impl BoundSubquery {

impl Binder {
pub fn gen_subquery_table_id(&mut self) -> String {
let id = format!("subquery_{}", self.context.subquery_base_index);
self.context.subquery_base_index += 1;
let id = format!("subquery_{}", self.subquery_base_index);
self.subquery_base_index += 1;
id
}
}
7 changes: 5 additions & 2 deletions tests/slt/subquery.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
statement error
select * from (select * from t1 where a > 1) where b > 7;

# TODO: handle multi-layer binder context to resolve current context all columns
# select * from (select * from t1 where a > 1) t where t.b > 7;
query III
select * from (select * from (select * from t1 where c < 2) t_1 where t_1.a > 1) t_2 where t_2.b > 7;
----
2 8 1


query III
select t.* from (select * from t1 where a > 1) t where t.b > 7;
Expand Down