Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(planner): Fix correlated subquery with joins #5947

Merged
merged 1 commit into from
Jun 13, 2022
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
7 changes: 7 additions & 0 deletions query/src/sql/planner/binder/bind_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ impl BindContext {
}
}

/// Create a new BindContext with self's parent as its parent
pub fn replace(&self) -> Self {
leiysky marked this conversation as resolved.
Show resolved Hide resolved
let mut bind_context = BindContext::new();
bind_context.parent = self.parent.clone();
bind_context
}

/// Generate a new BindContext and take current BindContext as its parent.
pub fn push(self) -> Self {
Self::with_parent(Box::new(self))
Expand Down
11 changes: 5 additions & 6 deletions query/src/sql/planner/binder/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ impl<'a> Binder {
) -> Result<(SExpr, BindContext)> {
let (left_child, left_context) =
self.bind_table_reference(bind_context, &join.left).await?;
let (right_child, right_context) = self
.bind_table_reference(&left_context, &join.right)
.await?;
let (right_child, right_context) =
self.bind_table_reference(bind_context, &join.right).await?;

check_duplicate_join_tables(&left_context, &right_context)?;

let mut bind_context = BindContext::new();
let mut bind_context = bind_context.replace();
leiysky marked this conversation as resolved.
Show resolved Hide resolved
for column in left_context.all_column_bindings() {
bind_context.add_column_binding(column.clone());
}
Expand All @@ -69,12 +68,12 @@ impl<'a> Binder {
{
return Err(ErrorCode::SemanticError(
"outer join should contain join conditions".to_string(),
))
));
}
JoinOperator::CrossJoin if join.condition != JoinCondition::None => {
return Err(ErrorCode::SemanticError(
"cross join should not contain join conditions".to_string(),
))
));
}
_ => (),
};
Expand Down
1 change: 1 addition & 0 deletions query/tests/it/sql/optimizer/heuristic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

mod join;
mod select;
mod subquery;

use std::io::Write;
use std::sync::Arc;
Expand Down
40 changes: 40 additions & 0 deletions query/tests/it/sql/optimizer/heuristic/subquery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use common_base::base::tokio;
use common_exception::Result;
use databend_query::sql::optimizer::DEFAULT_REWRITE_RULES;
use goldenfile::Mint;

use super::run_suites;
use super::Suite;
use crate::tests::create_query_context;

#[tokio::test]
pub async fn test_optimizer_subquery() -> Result<()> {
let mut mint = Mint::new("tests/it/sql/optimizer/heuristic/testdata/");
let mut file = mint.new_goldenfile("subquery.test")?;

let ctx = create_query_context().await?;

let suites = vec![
Suite {
comment: "# Correlated subquery with joins".to_string(),
query: "select t.number from numbers(1) as t, numbers(1) as t1 where t.number = (select count(*) from numbers(1) as t2, numbers(1) as t3 where t.number = t2.number)"
.to_string(),
rules: DEFAULT_REWRITE_RULES.clone(),
}
];

run_suites(ctx, &mut file, &suites).await
}
19 changes: 19 additions & 0 deletions query/tests/it/sql/optimizer/heuristic/testdata/subquery.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Correlated subquery with joins
select t.number from numbers(1) as t, numbers(1) as t1 where t.number = (select count(*) from numbers(1) as t2, numbers(1) as t3 where t.number = t2.number)
----
Project: [number]
Filter: [number = subquery_4]
CrossApply
PhysicalHashJoin: build keys: [], probe keys: []
PhysicalScan: default.system.numbers
PhysicalScan: default.system.numbers
Max1Row
Project: [COUNT(*)]
EvalScalar: [COUNT(*)]
Aggregate: group items: [], aggregate functions: [COUNT(*)]
Filter: [number = number]
PhysicalHashJoin: build keys: [], probe keys: []
PhysicalScan: default.system.numbers
PhysicalScan: default.system.numbers