Skip to content

Commit 00204fe

Browse files
authored
Merge pull request #9046 from xudong963/fix_cte
fix: try to fix cte with duplicate name
2 parents 796dec2 + 8cbb324 commit 00204fe

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
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
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use std::hash::Hash;
16-
use std::sync::Arc;
1716

1817
use common_ast::ast::TableAlias;
1918
use common_ast::parser::token::Token;
@@ -95,7 +94,7 @@ pub struct BindContext {
9594
/// functions, otherwise a grouping error will be raised.
9695
pub in_grouping: bool,
9796

98-
pub ctes_map: Arc<DashMap<String, CteInfo>>,
97+
pub ctes_map: Box<DashMap<String, CteInfo>>,
9998
}
10099

101100
#[derive(Clone, Debug)]
@@ -112,7 +111,7 @@ impl BindContext {
112111
columns: Vec::new(),
113112
aggregate_info: AggregateInfo::default(),
114113
in_grouping: false,
115-
ctes_map: Arc::new(DashMap::new()),
114+
ctes_map: Box::new(DashMap::new()),
116115
}
117116
}
118117

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,26 @@ impl<'a> Binder {
153153
let tokens = tokenize_sql(query.as_str())?;
154154
let backtrace = Backtrace::new();
155155
let (stmt, _) = parse_sql(&tokens, Dialect::PostgreSQL, &backtrace)?;
156-
156+
// For view, we need use a new context to bind it.
157+
let new_bind_context =
158+
BindContext::with_parent(Box::new(bind_context.clone()));
157159
if let Statement::Query(query) = &stmt {
158-
let (s_expr, mut bind_context) =
159-
self.bind_query(bind_context, query).await?;
160+
let (s_expr, mut new_bind_context) =
161+
self.bind_query(&new_bind_context, query).await?;
160162
if let Some(alias) = alias {
161163
// view maybe has alias, e.g. select v1.col1 from v as v1;
162-
bind_context.apply_table_alias(alias, &self.name_resolution_ctx)?;
164+
new_bind_context
165+
.apply_table_alias(alias, &self.name_resolution_ctx)?;
163166
} else {
164167
// e.g. select v0.c0 from v0;
165-
for column in bind_context.columns.iter_mut() {
168+
for column in new_bind_context.columns.iter_mut() {
166169
column.database_name = None;
167170
column.table_name = Some(
168171
normalize_identifier(table, &self.name_resolution_ctx).name,
169172
);
170173
}
171174
}
172-
Ok((s_expr, bind_context))
175+
Ok((s_expr, new_bind_context))
173176
} else {
174177
Err(ErrorCode::Internal(format!(
175178
"Invalid VIEW object: {}",
@@ -261,11 +264,14 @@ impl<'a> Binder {
261264
subquery,
262265
alias,
263266
} => {
264-
let (s_expr, mut bind_context) = self.bind_query(bind_context, subquery).await?;
267+
// For subquery, we need use a new context to bind it.
268+
let new_bind_context = BindContext::with_parent(Box::new(bind_context.clone()));
269+
let (s_expr, mut new_bind_context) =
270+
self.bind_query(&new_bind_context, subquery).await?;
265271
if let Some(alias) = alias {
266-
bind_context.apply_table_alias(alias, &self.name_resolution_ctx)?;
272+
new_bind_context.apply_table_alias(alias, &self.name_resolution_ctx)?;
267273
}
268-
Ok((s_expr, bind_context))
274+
Ok((s_expr, new_bind_context))
269275
}
270276
}
271277
}

tests/logictest/suites/query/cte.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,9 @@ with it as ( select * from numbers(1) ) select i.number from it as i;
382382

383383
----
384384
0
385+
386+
statement query II
387+
select * from (WITH source AS (select 1 as e) SELECT * FROM source) A, (WITH source AS (select 2 as e) SELECT * FROM source) B;
388+
389+
----
390+
1 2

0 commit comments

Comments
 (0)