Skip to content

Commit

Permalink
fix(query): expr children with projectset not fold count (#10827)
Browse files Browse the repository at this point in the history
* fix(query): expr children with projectset not fold count

* if expr contains ProjectSet, not prune unused column

* ProjectSet precise_cardinality set None

* add count() from (unnest()) explain result

* fix ci: unnest explain result read bytes result diff

* modify explain test result diff
  • Loading branch information
TCeason authored Apr 2, 2023
1 parent 61f46fa commit 720fa46
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use common_exception::ErrorCode;
use common_exception::Result;

use crate::optimizer::util::contaions_project_set;
use crate::optimizer::ColumnSet;
use crate::optimizer::SExpr;
use crate::plans::Aggregate;
Expand Down Expand Up @@ -99,6 +100,14 @@ impl UnusedColumnPruner {

RelOperator::EvalScalar(p) => {
let mut used = vec![];
if contaions_project_set(expr) {
return Ok(SExpr::create_unary(
RelOperator::EvalScalar(EvalScalar {
items: p.items.clone(),
}),
expr.child(0)?.clone(),
));
}
// Only keep columns needed by parent plan.
for s in p.items.iter() {
if !required.contains(&s.index) {
Expand Down
15 changes: 15 additions & 0 deletions src/query/sql/src/planner/optimizer/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

use super::SExpr;
use crate::plans::Operator;
use crate::plans::RelOp;
use crate::plans::RelOperator;
use crate::MetadataRef;

Expand All @@ -28,3 +30,16 @@ pub fn contains_local_table_scan(s_expr: &SExpr, metadata: &MetadataRef) -> bool
false
}
}

/// Check the expr contains ProjectSet op.
pub fn contaions_project_set(s_expr: &SExpr) -> bool {
if let Some(child) = s_expr.children().iter().next() {
// Check children
return match child.plan.rel_op() {
RelOp::ProjectSet => true,
_ => contaions_project_set(child),
};
}

false
}
2 changes: 2 additions & 0 deletions src/query/sql/src/planner/plans/project_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl Operator for ProjectSet {
child_prop.output_columns.insert(srf.index);
}

// ProjectSet is set-returning functions, precise_cardinality set None
child_prop.statistics.precise_cardinality = None;
Ok(child_prop)
}

Expand Down
91 changes: 71 additions & 20 deletions tests/sqllogictests/suites/mode/standalone/explain/project_set.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
statement ok
use default;

statement ok
drop table if exists fold_count;

statement ok
create table fold_count(id array(int), c1 string);

statement ok
insert into fold_count values([1,2,3,4], 'x');

query T
explain select count() from (select unnest(id), c1 from fold_count)
----
EvalScalar
├── expressions: [count() (#4)]
├── estimated rows: 1.00
└── AggregateFinal
├── group by: []
├── aggregate functions: [count()]
├── estimated rows: 1.00
└── AggregatePartial
├── group by: []
├── aggregate functions: [count()]
├── estimated rows: 1.00
└── EvalScalar
├── expressions: [get(unnest (#2))]
├── estimated rows: 1.00
└── ProjectSet
├── estimated rows: 1.00
├── set returning functions: unnest(CAST(fold_count.id (#0) AS Array(Int32 NULL)))
└── TableScan
├── table: default.default.fold_count
├── read rows: 1
├── read bytes: 87
├── partitions total: 1
├── partitions scanned: 1
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1, bloom pruning: 0 to 0>]
├── push downs: [filters: [], limit: NONE]
└── estimated rows: 1.00

statement ok
drop table fold_count;

statement ok
drop database if exists project_set

Expand All @@ -10,32 +55,38 @@ use project_set
query T
explain select number from (select unnest([1,2,3]), number from numbers(10)) t
----
ProjectSet
EvalScalar
├── expressions: [get(unnest (#1))]
├── estimated rows: 10.00
├── set returning functions: unnest([1, 2, 3])
└── TableScan
├── table: default.system.numbers
├── read rows: 10
├── read bytes: 80
├── partitions total: 1
├── partitions scanned: 1
├── push downs: [filters: [], limit: NONE]
└── estimated rows: 10.00
└── ProjectSet
├── estimated rows: 10.00
├── set returning functions: unnest([1, 2, 3])
└── TableScan
├── table: default.system.numbers
├── read rows: 10
├── read bytes: 80
├── partitions total: 1
├── partitions scanned: 1
├── push downs: [filters: [], limit: NONE]
└── estimated rows: 10.00

query T
explain select number from (select unnest([1,2,3,number]), number from numbers(10)) t
----
ProjectSet
EvalScalar
├── expressions: [get(unnest (#1))]
├── estimated rows: 10.00
├── set returning functions: unnest(CAST(array(1, 2, 3, numbers.number (#0)) AS Array(UInt64 NULL)))
└── TableScan
├── table: default.system.numbers
├── read rows: 10
├── read bytes: 80
├── partitions total: 1
├── partitions scanned: 1
├── push downs: [filters: [], limit: NONE]
└── estimated rows: 10.00
└── ProjectSet
├── estimated rows: 10.00
├── set returning functions: unnest(CAST(array(1, 2, 3, numbers.number (#0)) AS Array(UInt64 NULL)))
└── TableScan
├── table: default.system.numbers
├── read rows: 10
├── read bytes: 80
├── partitions total: 1
├── partitions scanned: 1
├── push downs: [filters: [], limit: NONE]
└── estimated rows: 10.00

statement ok
drop database project_set
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,33 @@ drop table t;
statement ok
drop table t2;

statement ok
drop table if exists fold_count;

statement ok
create table fold_count(id array(int), c1 string);

statement ok
insert into fold_count values([1,2,3,4], 'x')

query T
select concat(c,'y') as b from (select concat(c::String, c1) as c from (select unnest(id) as c, c1 from fold_count));
----
1xy
2xy
3xy
4xy

query I
select count(b) from (select concat(c,'y') as b from (select concat(c::String, c1) as c from (select unnest(id) as c, c1 from fold_count)));
----
4

query I
select count() from (select unnest(id), c1 from fold_count)
----
4

statement ok
DROP DATABASE db_02_0062;

Expand Down

1 comment on commit 720fa46

@vercel
Copy link

@vercel vercel bot commented on 720fa46 Apr 2, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

databend – ./

databend.vercel.app
databend.rs
databend-git-main-databend.vercel.app
databend-databend.vercel.app

Please sign in to comment.