Skip to content

Commit 78c0c93

Browse files
committed
Merge remote-tracking branch 'upstream/main' into internal_column
2 parents c489a6a + 59c0007 commit 78c0c93

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/query/sql/src/planner/binder/bind_context.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use std::collections::BTreeMap;
1616
use std::hash::Hash;
1717

18+
use common_ast::ast::Query;
1819
use common_ast::ast::TableAlias;
1920
use common_catalog::plan::InternalColumn;
2021
use common_exception::ErrorCode;
@@ -30,7 +31,6 @@ use dashmap::DashMap;
3031
use super::AggregateInfo;
3132
use super::INTERNAL_COLUMN_FACTORY;
3233
use crate::normalize_identifier;
33-
use crate::optimizer::SExpr;
3434
use crate::plans::ScalarExpr;
3535
use crate::ColumnSet;
3636
use crate::IndexType;
@@ -137,8 +137,7 @@ pub struct BindContext {
137137
#[derive(Clone, Debug)]
138138
pub struct CteInfo {
139139
pub columns_alias: Vec<String>,
140-
pub s_expr: SExpr,
141-
pub bind_context: BindContext,
140+
pub query: Query,
142141
}
143142

144143
impl BindContext {

src/query/sql/src/planner/binder/select.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,9 @@ impl Binder {
206206
"duplicate cte {table_name}"
207207
)));
208208
}
209-
let (s_expr, cte_bind_context) = self.bind_query(bind_context, &cte.query).await?;
210209
let cte_info = CteInfo {
211210
columns_alias: cte.alias.columns.iter().map(|c| c.name.clone()).collect(),
212-
s_expr,
213-
bind_context: cte_bind_context.clone(),
211+
query: cte.query.clone(),
214212
};
215213
bind_context.ctes_map.insert(table_name, cte_info);
216214
}

src/query/sql/src/planner/binder/table.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use common_storages_result_cache::ResultCacheReader;
5656
use common_storages_result_cache::ResultScan;
5757
use common_storages_view::view_table::QUERY;
5858
use common_users::UserApiProvider;
59+
use dashmap::DashMap;
5960

6061
use crate::binder::copy::parse_stage_location_v2;
6162
use crate::binder::location::parse_uri_location;
@@ -139,7 +140,9 @@ impl Binder {
139140
};
140141
// Check and bind common table expression
141142
if let Some(cte_info) = bind_context.ctes_map.get(&table_name) {
142-
return self.bind_cte(bind_context, &table_name, alias, &cte_info);
143+
return self
144+
.bind_cte(bind_context, &table_name, alias, &cte_info)
145+
.await;
143146
}
144147

145148
if database == "system" {
@@ -154,15 +157,36 @@ impl Binder {
154157
};
155158

156159
// Resolve table with catalog
157-
let table_meta: Arc<dyn Table> = self
160+
let table_meta = match self
158161
.resolve_data_source(
159162
tenant.as_str(),
160163
catalog.as_str(),
161164
database.as_str(),
162165
table_name.as_str(),
163166
&navigation_point,
164167
)
165-
.await?;
168+
.await
169+
{
170+
Ok(table) => table,
171+
Err(_) => {
172+
let mut parent = bind_context.parent.as_ref();
173+
loop {
174+
if parent.is_none() {
175+
break;
176+
}
177+
if let Some(cte_info) = parent.unwrap().ctes_map.get(&table_name) {
178+
return self
179+
.bind_cte(bind_context, &table_name, alias, &cte_info)
180+
.await;
181+
}
182+
parent = parent.unwrap().parent.as_ref();
183+
}
184+
return Err(ErrorCode::UnknownTable(format!(
185+
"Unknown table '{table_name}'"
186+
)));
187+
}
188+
};
189+
166190
match table_meta.engine() {
167191
"VIEW" => {
168192
let query = table_meta
@@ -418,15 +442,23 @@ impl Binder {
418442
}
419443
}
420444

421-
fn bind_cte(
445+
async fn bind_cte(
422446
&mut self,
423447
bind_context: &BindContext,
424448
table_name: &str,
425449
alias: &Option<TableAlias>,
426450
cte_info: &CteInfo,
427451
) -> Result<(SExpr, BindContext)> {
428-
let mut new_bind_context = bind_context.clone();
429-
new_bind_context.columns = cte_info.bind_context.columns.clone();
452+
let new_bind_context = BindContext {
453+
parent: Some(Box::new(bind_context.clone())),
454+
columns: vec![],
455+
aggregate_info: Default::default(),
456+
in_grouping: false,
457+
ctes_map: Box::new(DashMap::new()),
458+
is_view: false,
459+
};
460+
let (s_expr, mut new_bind_context) =
461+
self.bind_query(&new_bind_context, &cte_info.query).await?;
430462
let mut cols_alias = cte_info.columns_alias.clone();
431463
if let Some(alias) = alias {
432464
for (idx, col_alias) in alias.columns.iter().enumerate() {
@@ -456,7 +488,7 @@ impl Binder {
456488
for (index, column_name) in cols_alias.iter().enumerate() {
457489
new_bind_context.columns[index].column_name = column_name.clone();
458490
}
459-
Ok((cte_info.s_expr.clone(), new_bind_context))
491+
Ok((s_expr, new_bind_context))
460492
}
461493

462494
async fn bind_base_table(

tests/sqllogictests/suites/query/cte.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,9 @@ query II
365365
select * from (WITH source AS (select 1 as e) SELECT * FROM source) A, (WITH source AS (select 2 as e) SELECT * FROM source) B
366366
----
367367
1 2
368+
369+
query II
370+
with v as (select * from numbers(2)) select * from v t1, (select * from v where number = 0) t2 where t1.number = 1 and t2.number = 1
371+
----
372+
373+

0 commit comments

Comments
 (0)