Skip to content

Commit

Permalink
feat(frontend): optimize constant project with exact input row count …
Browse files Browse the repository at this point in the history
…known into value operator (#7912)

optimize constant project into value operator

Approved-By: fuyufjh
Approved-By: chenzl25
  • Loading branch information
jon-chuang authored Feb 15, 2023
1 parent 0008a07 commit 2cbb8e3
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 141 deletions.
4 changes: 4 additions & 0 deletions src/frontend/planner_test/planner_test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ echo "$(tput setaf 2)Diff applied!$(tput sgr 0)"
'''
category = "RiseDev - Test"

[tasks.dapt]
alias = "do-apply-planner-test"


[tasks.run-planner-test]
description = "Run planner test"
category = "RiseDev - Test"
Expand Down
15 changes: 5 additions & 10 deletions src/frontend/planner_test/tests/testdata/array.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,21 @@
LogicalProject { exprs: [ArrayCat(Array(66:Int32), Array(123:Int32))] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [ArrayCat(Array(66:Int32), Array(123:Int32))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ArrayCat(Array(66:Int32), Array(123:Int32))]] }
- sql: |
select array_cat(array[array[66]], array[233]);
logical_plan: |
LogicalProject { exprs: [ArrayCat(Array(Array(66:Int32)), Array(233:Int32))] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [ArrayCat(Array(Array(66:Int32)), Array(233:Int32))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ArrayCat(Array(Array(66:Int32)), Array(233:Int32))]] }
- sql: |
select array_cat(array[233], array[array[66]]);
logical_plan: |
LogicalProject { exprs: [ArrayCat(Array(233:Int32), Array(Array(66:Int32)))] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [ArrayCat(Array(233:Int32), Array(Array(66:Int32)))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ArrayCat(Array(233:Int32), Array(Array(66:Int32)))]] }
- sql: |
select array_cat(array[233], array[array[array[66]]]);
binder_error: 'Bind error: unable to find least restrictive type between integer[] and integer[][][]'
Expand All @@ -85,8 +82,7 @@
LogicalProject { exprs: [ArrayAppend(Array(66:Int32), 123:Int32)] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [ArrayAppend(Array(66:Int32), 123:Int32)] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ArrayAppend(Array(66:Int32), 123:Int32)]] }
- sql: |
select array_append(123, 234);
binder_error: 'Bind error: Cannot append integer to integer'
Expand All @@ -102,8 +98,7 @@
LogicalProject { exprs: [ArrayPrepend(123:Int32, Array(66:Int32))] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [ArrayPrepend(123:Int32, Array(66:Int32))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ArrayPrepend(123:Int32, Array(66:Int32))]] }
- sql: |
select array_prepend(123, 234);
binder_error: 'Bind error: Cannot prepend integer to integer'
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/planner_test/tests/testdata/basic_query.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@
└─StreamTableScan { table: t, columns: [t.v1, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- sql: select 1
batch_plan: |
BatchProject { exprs: [1:Int32] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[1:Int32]] }
- sql: |
create table t (v1 bigint, v2 double precision);
select a from t as t2(a);
Expand Down
19 changes: 6 additions & 13 deletions src/frontend/planner_test/tests/testdata/cast.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
sql: |
select case when NULL then 1 end;
batch_plan: |
BatchProject { exprs: [Case(null:Boolean, 1:Int32)] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[Case(null:Boolean, 1:Int32)]] }
- name: implicit cast boolean (JOIN ON NULL)
sql: |
create table t1(v1 int);
Expand All @@ -37,8 +36,7 @@
sql: |
select current_schemas(NULL);
batch_plan: |
BatchProject { exprs: [null:List { datatype: Varchar }] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[null:List { datatype: Varchar }]] }
- name: FILTER (FILTER NULL)
sql: |
create table t(v1 int);
Expand All @@ -52,21 +50,17 @@
sql: |
select 1 having 'y';
batch_plan: |
BatchProject { exprs: [1:Int32] }
└─BatchSimpleAgg { aggs: [] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[1:Int32]] }
- name: implicit cast boolean (WHERE with literal 'y' of unknown type)
sql: |
select 1 where 'y';
batch_plan: |
BatchProject { exprs: [1:Int32] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[1:Int32]] }
- name: implicit cast boolean (CASE with literal 'y' of unknown type)
sql: |
select case when 'y' then 1 end;
batch_plan: |
BatchProject { exprs: [Case(true:Boolean, 1:Int32)] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[Case(true:Boolean, 1:Int32)]] }
- name: implicit cast boolean (JOIN ON with literal 'y' of unknown type)
sql: |
create table t1(v1 int);
Expand All @@ -82,8 +76,7 @@
sql: |
select current_schemas('y');
batch_plan: |
BatchProject { exprs: [ARRAY[pg_catalog, public]:List { datatype: Varchar }] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ARRAY[pg_catalog, public]:List { datatype: Varchar }]] }
- name: FILTER (FILTER with literal 'y' of unknown type)
sql: |
create table t(v1 int);
Expand Down
26 changes: 11 additions & 15 deletions src/frontend/planner_test/tests/testdata/explain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,27 @@
LogicalProject { exprs: [1:Int32] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
Project Remove:
apply TrivialProjectToValuesRule 1 time(s)
LogicalValues { rows: [[1:Int32]], schema: Schema { fields: [1:Int32:Int32] } }
To Batch Physical Plan:
BatchProject { exprs: [1:Int32] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[1:Int32]] }
To Batch Local Plan:
BatchProject { exprs: [1:Int32] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[1:Int32]] }
{
"root_stage_id": 0,
"stages": {
"0": {
"root": {
"plan_node_id": 29,
"plan_node_type": "BatchProject",
"plan_node_id": 22,
"plan_node_type": "BatchValues",
"schema": [
{
"dataType": {
Expand All @@ -62,15 +66,7 @@
"name": "1:Int32"
}
],
"children": [
{
"plan_node_id": 27,
"plan_node_type": "BatchValues",
"schema": [],
"children": [],
"source_stage_id": null
}
],
"children": [],
"source_stage_id": null
},
"parallelism": 1,
Expand Down
48 changes: 16 additions & 32 deletions src/frontend/planner_test/tests/testdata/expr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@
sql: |
SELECT 1::real in (3, 1.0, 2);
batch_plan: |
BatchProject { exprs: [In(1:Int32::Float32, 3:Int32::Float32, 1.0:Decimal::Float32, 2:Int32::Float32)] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[In(1:Int32::Float32, 3:Int32::Float32, 1.0:Decimal::Float32, 2:Int32::Float32)]] }
- name: not in-list with aligned types
sql: |
SELECT 1::real not in (3, 1.0, 2);
batch_plan: |
BatchProject { exprs: [Not(In(1:Int32::Float32, 3:Int32::Float32, 1.0:Decimal::Float32, 2:Int32::Float32))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[Not(In(1:Int32::Float32, 3:Int32::Float32, 1.0:Decimal::Float32, 2:Int32::Float32))]] }
- name: in-list with misaligned types
sql: |
SELECT true in (3, 1.0, 2);
Expand Down Expand Up @@ -117,8 +115,7 @@
- sql: |
select +1.0, -2.0;
batch_plan: |
BatchProject { exprs: [1.0:Decimal, -2.0:Decimal] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[1.0:Decimal, -2.0:Decimal]] }
- sql: |
values(round(42.4382, 2));
batch_plan: |
Expand Down Expand Up @@ -164,8 +161,7 @@
- sql: |
select length(trim(trailing '1' from '12'))+length(trim(leading '2' from '23'))+length(trim(both '3' from '34'));
batch_plan: |
BatchProject { exprs: [((Length(Rtrim('12':Varchar, '1':Varchar)) + Length(Ltrim('23':Varchar, '2':Varchar))) + Length(Trim('34':Varchar, '3':Varchar)))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[((Length(Rtrim('12':Varchar, '1':Varchar)) + Length(Ltrim('23':Varchar, '2':Varchar))) + Length(Trim('34':Varchar, '3':Varchar)))]] }
- sql: |
select position(replace('1','1','2'),'123') where '12' like '%1';
batch_plan: |
Expand Down Expand Up @@ -310,48 +306,39 @@
- sql: |
select concat(':', true);
batch_plan: |
BatchProject { exprs: [ConcatWs('':Varchar, ':':Varchar, BoolOut(true:Boolean))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ConcatWs('':Varchar, ':':Varchar, BoolOut(true:Boolean))]] }
- sql: |
select ':' || true;
batch_plan: |
BatchProject { exprs: [ConcatOp(':':Varchar, true:Boolean::Varchar)] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ConcatOp(':':Varchar, true:Boolean::Varchar)]] }
- sql: |
select substr('hello', NULL);
batch_plan: |
BatchProject { exprs: [Substr('hello':Varchar, null:Int32)] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[Substr('hello':Varchar, null:Int32)]] }
- sql: |
select substr(NULL, 1);
batch_plan: |
BatchProject { exprs: [Substr(null:Varchar, 1:Int32)] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[Substr(null:Varchar, 1:Int32)]] }
- sql: |
select pg_typeof('123');
batch_plan: |
BatchProject { exprs: ['unknown':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['unknown':Varchar]] }
- sql: |
select pg_typeof(round(null));
batch_plan: |
BatchProject { exprs: ['double precision':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['double precision':Varchar]] }
- sql: |
select pg_typeof(row(true, 1, 'hello'));
batch_plan: |
BatchProject { exprs: ['record':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['record':Varchar]] }
- sql: |
select pg_typeof(array[1, 2]);
batch_plan: |
BatchProject { exprs: ['integer[]':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['integer[]':Varchar]] }
- sql: |
select pg_typeof(array[array[1, 2], array[3, 4]]);
batch_plan: |
BatchProject { exprs: ['integer[][]':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['integer[][]':Varchar]] }
- sql: |
select 1 = SOME(1);
binder_error: 'Bind error: op ANY/ALL (array) requires array on right side'
Expand Down Expand Up @@ -422,8 +409,7 @@
BatchProject { exprs: [Some((1:Int32 < ArrayCat(Array(1:Int32), Array(2:Int32))))] }
└─BatchNestedLoopJoin { type: LeftOuter, predicate: true, output: all }
├─BatchValues { rows: [[]] }
└─BatchProject { exprs: [Array(1:Int32)] }
└─BatchValues { rows: [[]] }
└─BatchValues { rows: [[Array(1:Int32)]] }
- sql: |
select 1 < ALL(array[null]::integer[]);
logical_plan: |
Expand All @@ -446,8 +432,7 @@
BatchProject { exprs: [All((1:Int32 < ArrayCat(Array(1:Int32), Array(2:Int32))))] }
└─BatchNestedLoopJoin { type: LeftOuter, predicate: true, output: all }
├─BatchValues { rows: [[]] }
└─BatchProject { exprs: [Array(1:Int32)] }
└─BatchValues { rows: [[]] }
└─BatchValues { rows: [[Array(1:Int32)]] }
- name: now expression
sql: |
create table t (v1 timestamp with time zone);
Expand Down Expand Up @@ -540,8 +525,7 @@
sql: |
select false >= 'LN1O0QP1yi' NOT IN (md5('4SeUPZhUbH'))
batch_plan: |
BatchProject { exprs: [(false:Boolean >= Not(In('LN1O0QP1yi':Varchar, Md5('4SeUPZhUbH':Varchar))))] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[(false:Boolean >= Not(In('LN1O0QP1yi':Varchar, Md5('4SeUPZhUbH':Varchar))))]] }
- name: const_eval of const expr
sql: |
create table t(v1 int);
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/planner_test/tests/testdata/pg_catalog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@
LogicalProject { exprs: [2:Int32] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [2:Int32] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[2:Int32]] }
11 changes: 11 additions & 0 deletions src/frontend/planner_test/tests/testdata/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is automatically generated. See `src/frontend/planner_test/README.md` for more information.
- sql: |
select 1 as k, 2 as v;
batch_plan: |
BatchValues { rows: [[1:Int32, 2:Int32]] }
- name: Project over union all of nested project over union all, now, and simple agg
sql: |
create table t(v int);
select 'abc', 1, 1.4 as k from (select 1 as k, 2 from (select 1, 2 union all select 3, 4) union all select * from (select 3, 4) union all select 100, 200 from (select now(), now() - interval '1 hour') union all select count(*)::int, sum(v)::int from t);
batch_plan: |
BatchValues { rows: [['abc':Varchar, 1:Int32, 1.4:Decimal], ['abc':Varchar, 1:Int32, 1.4:Decimal], ['abc':Varchar, 1:Int32, 1.4:Decimal], ['abc':Varchar, 1:Int32, 1.4:Decimal], ['abc':Varchar, 1:Int32, 1.4:Decimal]] }
3 changes: 1 addition & 2 deletions src/frontend/planner_test/tests/testdata/subquery_expr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
optimized_logical_plan: |
LogicalJoin { type: LeftOuter, on: true, output: all }
├─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
└─LogicalProject { exprs: [1:Int32] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
└─LogicalValues { rows: [[1:Int32]], schema: Schema { fields: [1:Int32:Int32] } }
- sql: |
create table t(x int);
select (select x from t), 1 from t;
Expand Down
21 changes: 7 additions & 14 deletions src/frontend/planner_test/tests/testdata/sysinfo_funcs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@
- sql: |
select current_schema();
batch_plan: |
BatchProject { exprs: ['public':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['public':Varchar]] }
- sql: |
select current_schema;
batch_plan: |
BatchProject { exprs: ['public':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['public':Varchar]] }
- sql: |
select session_user;
batch_plan: |
BatchProject { exprs: ['root':Varchar] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [['root':Varchar]] }
- sql: |
select current_schemas(true);
batch_plan: |
BatchProject { exprs: [ARRAY[pg_catalog, public]:List { datatype: Varchar }] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ARRAY[pg_catalog, public]:List { datatype: Varchar }]] }
- sql: |
select current_schemas(false);
batch_plan: |
BatchProject { exprs: [ARRAY[public]:List { datatype: Varchar }] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[ARRAY[public]:List { datatype: Varchar }]] }
- sql: |
select current_schemas(null);
batch_plan: |
BatchProject { exprs: [null:List { datatype: Varchar }] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[null:List { datatype: Varchar }]] }
- sql: |
select current_schemas(true and false);
binder_error: |-
Expand All @@ -37,5 +31,4 @@
- sql: |
select current_timestamp;
batch_plan: |
BatchProject { exprs: [Now] }
└─BatchValues { rows: [[]] }
BatchValues { rows: [[Now('2021-04-01 00:00:00+00:00':Timestamptz)]] }
Loading

0 comments on commit 2cbb8e3

Please sign in to comment.