-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(optimizer): support intersect (#9573)
- Loading branch information
Showing
14 changed files
with
679 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO true; | ||
|
||
statement ok | ||
create table t1 (v1 int, v2 int); | ||
|
||
statement ok | ||
create table t2 (v1 int, v3 int); | ||
|
||
statement ok | ||
insert into t1 values(1, 2),(1, 2); | ||
|
||
statement ok | ||
insert into t2 values(1, 2),(1, 2); | ||
|
||
query II | ||
select * from t1 intersect select * from t2 | ||
---- | ||
1 2 | ||
|
||
query I | ||
select 1 intersect select 1 | ||
---- | ||
1 | ||
|
||
query I | ||
select 1 intersect select 2 | ||
---- | ||
|
||
query I | ||
select null intersect select null | ||
---- | ||
NULL | ||
|
||
query I | ||
select 1 as a intersect select 1 intersect select 1 | ||
---- | ||
1 | ||
|
||
statement ok | ||
drop table t1; | ||
|
||
statement ok | ||
drop table t2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO true; | ||
|
||
statement ok | ||
create table t1 (v1 int, v2 int); | ||
|
||
statement ok | ||
create table t2 (v1 int, v3 int); | ||
|
||
statement ok | ||
create materialized view v as select * from t1 intersect select * from t2; | ||
|
||
query II | ||
select * from v; | ||
---- | ||
|
||
statement ok | ||
insert into t1 values(1, 2),(1, 2); | ||
|
||
query II | ||
select * from v; | ||
---- | ||
|
||
|
||
statement ok | ||
insert into t2 values(1, 2),(1, 2); | ||
|
||
|
||
query II | ||
select * from v; | ||
---- | ||
1 2 | ||
|
||
|
||
statement ok | ||
delete from t1 where v1 = 1; | ||
|
||
query II | ||
select * from v; | ||
---- | ||
|
||
statement ok | ||
drop materialized view v; | ||
|
||
statement ok | ||
drop table t1; | ||
|
||
statement ok | ||
drop table t2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
253 changes: 253 additions & 0 deletions
253
src/frontend/planner_test/tests/testdata/intersect.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2023 RisingWave 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 std::fmt; | ||
|
||
use risingwave_common::catalog::Schema; | ||
|
||
use super::{GenericPlanNode, GenericPlanRef}; | ||
use crate::optimizer::optimizer_context::OptimizerContextRef; | ||
use crate::optimizer::property::FunctionalDependencySet; | ||
|
||
/// `Intersect` returns the intersect of the rows of its inputs. | ||
/// If `all` is false, it needs to eliminate duplicates. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct Intersect<PlanRef> { | ||
pub all: bool, | ||
pub inputs: Vec<PlanRef>, | ||
} | ||
|
||
impl<PlanRef: GenericPlanRef> GenericPlanNode for Intersect<PlanRef> { | ||
fn schema(&self) -> Schema { | ||
self.inputs[0].schema().clone() | ||
} | ||
|
||
fn logical_pk(&self) -> Option<Vec<usize>> { | ||
Some(self.inputs[0].logical_pk().to_vec()) | ||
} | ||
|
||
fn ctx(&self) -> OptimizerContextRef { | ||
self.inputs[0].ctx() | ||
} | ||
|
||
fn functional_dependency(&self) -> FunctionalDependencySet { | ||
FunctionalDependencySet::new(self.inputs[0].schema().len()) | ||
} | ||
} | ||
|
||
impl<PlanRef: GenericPlanRef> Intersect<PlanRef> { | ||
pub fn fmt_with_name(&self, f: &mut fmt::Formatter<'_>, name: &str) -> fmt::Result { | ||
let mut builder = f.debug_struct(name); | ||
self.fmt_fields_with_builder(&mut builder); | ||
builder.finish() | ||
} | ||
|
||
pub fn fmt_fields_with_builder(&self, builder: &mut fmt::DebugStruct<'_, '_>) { | ||
builder.field("all", &self.all); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.