Skip to content

Commit

Permalink
fix: materialized cte is defined but not be used (#13046)
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 authored Sep 27, 2023
1 parent 7691b38 commit d53524f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/query/sql/src/planner/binder/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'a> Binder {
let (mut s_expr, bind_context) = self.bind_query(bind_context, query).await?;
// Wrap `LogicalMaterializedCte` to `s_expr`
for (_, cte_info) in self.ctes_map.iter().rev() {
if !cte_info.materialized {
if !cte_info.materialized || cte_info.used_count == 0{
continue;
}
let cte_s_expr = self.m_cte_bound_s_expr.get(&cte_info.cte_idx).unwrap();
Expand Down
16 changes: 16 additions & 0 deletions tests/sqllogictests/suites/query/materialized_cte.test
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,19 @@ with t1 as materialized (select number as a from numbers(10)), t2 as materialize
0
0
0

statement ok
create table test (a int, b string);

statement ok
insert into test values (1, 'a'), (2, 'b');

# main query doesn't use materialized cte, but materialized cte is defined
query TT rowsort
WITH tq9v2(cHAx0) AS materialized (SELECT a AS cHAx0 FROM test) SELECT b from test;
----
a
b

statement ok
drop table test

1 comment on commit d53524f

@vercel
Copy link

@vercel vercel bot commented on d53524f Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.