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

feat(frontend): apply SessionTimezone and ConstEvalRewriter expr rewriters to during gen_{batch,stream}_plan #7761

Merged
merged 39 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4b24e1b
apply for stream
jon-chuang Feb 8, 2023
53cd4f4
fix
jon-chuang Feb 8, 2023
7e2e10a
remove the const eval, still need session timezone
jon-chuang Feb 8, 2023
41632e4
commit
jon-chuang Feb 8, 2023
7fd06c6
fix clippy
jon-chuang Feb 8, 2023
e3fac9d
stash
jon-chuang Feb 8, 2023
ec5ea0d
fix
jon-chuang Feb 8, 2023
df8a231
try disable
jon-chuang Feb 8, 2023
36970f0
fix
jon-chuang Feb 8, 2023
f942903
minor
jon-chuang Feb 8, 2023
a94e7c5
Merge branch 'main' of https://github.com/singularity-data/risingwave…
jon-chuang Feb 8, 2023
2c49c0a
add test changes
jon-chuang Feb 8, 2023
7fa7c22
add fix
jon-chuang Feb 8, 2023
f7c6bea
Merge branch 'jon/correclty-evaluate-field-expr' into jon/expr-rewrit…
jon-chuang Feb 8, 2023
e286e1b
fix
jon-chuang Feb 8, 2023
1c63997
minor
jon-chuang Feb 8, 2023
3e483d2
fix
jon-chuang Feb 8, 2023
4ffa9e5
minor
jon-chuang Feb 8, 2023
83a24a6
Merge branch 'jon/fix-interval' into jon/expr-rewrite-session-timezone
jon-chuang Feb 8, 2023
8901f4f
Merge branch 'main' of https://github.com/singularity-data/risingwave…
jon-chuang Feb 20, 2023
d4cad1e
fix
jon-chuang Feb 20, 2023
375b87c
fix
jon-chuang Feb 20, 2023
d3070b6
minor
jon-chuang Feb 20, 2023
ecd865a
minor
jon-chuang Feb 20, 2023
c10eed3
minor
jon-chuang Feb 20, 2023
5960682
fix
jon-chuang Feb 21, 2023
4cb1966
Merge branch 'main' of https://github.com/singularity-data/risingwave…
jon-chuang Feb 21, 2023
9bf89fa
Merge branch 'main' of https://github.com/singularity-data/risingwave…
jon-chuang Feb 22, 2023
a8e05f7
apply
jon-chuang Feb 22, 2023
e542b15
fix
jon-chuang Feb 22, 2023
68234db
fix
jon-chuang Feb 22, 2023
305803f
fix
jon-chuang Feb 22, 2023
de81675
minor
jon-chuang Feb 22, 2023
b35f4cc
fix
jon-chuang Feb 22, 2023
9b3d394
minor
jon-chuang Feb 22, 2023
0c89a49
fix
jon-chuang Feb 22, 2023
d9d0301
Merge branch 'jon/fix-array-to-string' into jon/expr-rewrite-session-…
jon-chuang Feb 22, 2023
fd65a00
Merge branch 'main' into jon/expr-rewrite-session-timezone
jon-chuang Feb 22, 2023
ba62977
Merge branch 'main' into jon/expr-rewrite-session-timezone
jon-chuang Feb 22, 2023
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
10 changes: 5 additions & 5 deletions e2e_test/batch/functions/now.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ statement ok
insert into t values(now());

# constant eval of now in batch plan
# query T
# explain select now() + interval '1 hour' = now() + interval '30 minutes' + interval '30 minutes' true;
# ----
# BatchProject { exprs: [true:Boolean] }
# └─BatchValues { rows: [[]] }
query T
explain select now() + interval '1 hour' = now() + interval '30 minutes' + interval '30 minutes' true;
----
BatchProject { exprs: [true:Boolean] }
└─BatchValues { rows: [[]] }

statement ok
drop table tz
Expand Down
25 changes: 20 additions & 5 deletions src/common/src/types/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,17 @@ impl Display for IntervalUnit {
write(format_args!("{days} days"))?;
}
if self.ms != 0 || self.months == 0 && self.days == 0 {
let hours = self.ms / 1000 / 3600;
let minutes = (self.ms / 1000 / 60) % 60;
let seconds = self.ms % 60000 / 1000;
let secs_fract = self.ms % 1000;
write(format_args!("{hours:0>2}:{minutes:0>2}:{seconds:0>2}"))?;
let ms = self.ms.abs();
let hours = ms / 1000 / 3600;
let minutes = (ms / 1000 / 60) % 60;
let seconds = ms % 60000 / 1000;
let secs_fract = ms % 1000;

if self.ms < 0 {
write(format_args!("-{hours:0>2}:{minutes:0>2}:{seconds:0>2}"))?;
} else {
write(format_args!("{hours:0>2}:{minutes:0>2}:{seconds:0>2}"))?;
}
if secs_fract != 0 {
let mut buf = [0u8; 4];
write!(buf.as_mut_slice(), ".{:03}", secs_fract).unwrap();
Expand Down Expand Up @@ -1122,6 +1128,15 @@ mod tests {
"-1 years -2 mons 3 days"
);
assert_eq!(IntervalUnit::default().to_string(), "00:00:00");
assert_eq!(
IntervalUnit::new(
-14,
3,
-(11 * 3600 * 1000 + 45 * 60 * 1000 + 14 * 1000 + 233)
)
.to_string(),
"-1 years -2 mons 3 days -11:45:14.233"
);
}

#[test]
Expand Down
13 changes: 10 additions & 3 deletions src/expr/src/expr/expr_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::convert::TryFrom;
use anyhow::anyhow;
use risingwave_common::array::{ArrayImpl, ArrayRef, DataChunk};
use risingwave_common::row::OwnedRow;
use risingwave_common::types::{DataType, Datum};
use risingwave_common::types::{DataType, Datum, ScalarImpl};
use risingwave_common::util::value_encoding::deserialize_datum;
use risingwave_pb::expr::expr_node::{RexNode, Type};
use risingwave_pb::expr::ExprNode;
Expand Down Expand Up @@ -47,8 +47,15 @@ impl Expression for FieldExpression {
}
}

fn eval_row(&self, _input: &OwnedRow) -> Result<Datum> {
Err(anyhow!("expects a struct array ref").into())
fn eval_row(&self, input: &OwnedRow) -> Result<Datum> {
let struct_datum = self.input.eval_row(input)?;
struct_datum
.map(|s| match s {
ScalarImpl::Struct(v) => Ok(v.fields()[self.index].clone()),
_ => Err(anyhow!("expects a struct array ref").into()),
})
.transpose()
.map(|x| x.flatten())
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/frontend/planner_test/tests/testdata/array.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
logical_plan: |
LogicalValues { rows: [[Array('foo':Varchar, 'bar':Varchar)]], schema: Schema { fields: [*VALUES*_0.column_0:List { datatype: Varchar }] } }
batch_plan: |
BatchValues { rows: [[Array('foo':Varchar, 'bar':Varchar)]] }
BatchValues { rows: [[ARRAY[foo, bar]:List { datatype: Varchar }]] }
- sql: |
values (ARRAY[1, 2+3, 4*5+1]);
logical_plan: |
LogicalValues { rows: [[Array(1:Int32, (2:Int32 + 3:Int32), ((4:Int32 * 5:Int32) + 1:Int32))]], schema: Schema { fields: [*VALUES*_0.column_0:List { datatype: Int32 }] } }
batch_plan: |
BatchValues { rows: [[Array(1:Int32, (2:Int32 + 3:Int32), ((4:Int32 * 5:Int32) + 1:Int32))]] }
BatchValues { rows: [[ARRAY[1, 5, 21]:List { datatype: Int32 }]] }
- sql: |
create table t (v1 int);
select (ARRAY[1, v1]) from t;
Expand Down Expand Up @@ -52,23 +52,23 @@
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))] }
BatchProject { exprs: [ARRAY[66, 123]:List { datatype: Int32 }] }
└─BatchValues { rows: [[]] }
- 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))] }
BatchProject { exprs: [ARRAY[{66}, {233}]:List { datatype: List { datatype: Int32 } }] }
└─BatchValues { rows: [[]] }
- 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)))] }
BatchProject { exprs: [ARRAY[{233}, {66}]:List { datatype: List { datatype: Int32 } }] }
└─BatchValues { rows: [[]] }
- sql: |
select array_cat(array[233], array[array[array[66]]]);
Expand All @@ -85,7 +85,7 @@
LogicalProject { exprs: [ArrayAppend(Array(66:Int32), 123:Int32)] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [ArrayAppend(Array(66:Int32), 123:Int32)] }
BatchProject { exprs: [ARRAY[66, 123]:List { datatype: Int32 }] }
└─BatchValues { rows: [[]] }
- sql: |
select array_append(123, 234);
Expand All @@ -102,7 +102,7 @@
LogicalProject { exprs: [ArrayPrepend(123:Int32, Array(66:Int32))] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: |
BatchProject { exprs: [ArrayPrepend(123:Int32, Array(66:Int32))] }
BatchProject { exprs: [ARRAY[123, 66]:List { datatype: Int32 }] }
└─BatchValues { rows: [[]] }
- sql: |
select array_prepend(123, 234);
Expand Down
10 changes: 5 additions & 5 deletions src/frontend/planner_test/tests/testdata/basic_query.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is automatically generated. See `src/frontend/planner_test/README.md` for more information.
- sql: values (11, 22), (33+(1+2), 44);
batch_plan: |
BatchValues { rows: [[11:Int32, 22:Int32], [(33:Int32 + (1:Int32 + 2:Int32)), 44:Int32]] }
BatchValues { rows: [[11:Int32, 22:Int32], [36:Int32, 44:Int32]] }
- sql: select * from t
binder_error: 'Catalog error: table or source not found: t'
- sql: |
Expand All @@ -22,11 +22,11 @@
select * from t where 1>2 and 1=1 and 3<1 and 4<>1 or 1=1 and 2>=1 and 1<=2;
batch_plan: |
BatchExchange { order: [], dist: Single }
└─BatchFilter { predicate: (1:Int32 = 1:Int32) AND ((((1:Int32 > 2:Int32) AND (3:Int32 < 1:Int32)) AND (4:Int32 <> 1:Int32)) OR ((2:Int32 >= 1:Int32) AND (1:Int32 <= 2:Int32))) }
└─BatchFilter { predicate: true:Boolean AND true:Boolean }
Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessary to fix in this PR, but we need to investigate why true AND true is not folded into true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is because condition contains conjunctive expressions that are not folded. We can apply the reduction on condition after expressions are rewritten to only retain non-True predicates.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make sense. The predicate is a Vec<Expr> of conjunctions rather than a single expr. So this is expected rather than a bug at some unknown places. We can add this optimization separately (maybe same phase but also handles Condition(Vec<Expr>) as an expr).

Copy link
Contributor Author

@jon-chuang jon-chuang Feb 10, 2023

Choose a reason for hiding this comment

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

Semantically, a Condition with an expression True is as good as it without that expression. So I think folding the expression away after rewriting it in the same phase is good enough.

└─BatchScan { table: t, columns: [], distribution: SomeShard }
stream_plan: |
StreamMaterialize { columns: [t._row_id(hidden)], pk_columns: [t._row_id] }
└─StreamFilter { predicate: (1:Int32 = 1:Int32) AND ((((1:Int32 > 2:Int32) AND (3:Int32 < 1:Int32)) AND (4:Int32 <> 1:Int32)) OR ((2:Int32 >= 1:Int32) AND (1:Int32 <= 2:Int32))) }
└─StreamFilter { predicate: true:Boolean AND true:Boolean }
└─StreamTableScan { table: t, columns: [t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- sql: |
create table t (v1 int);
Expand Down Expand Up @@ -130,11 +130,11 @@
- sql: |
select * from unnest(Array[1,2,3]);
batch_plan: |
BatchTableFunction { Unnest(Array(1:Int32, 2:Int32, 3:Int32)) }
BatchTableFunction { Unnest(ARRAY[1, 2, 3]:List { datatype: Int32 }) }
- sql: |
select * from unnest(Array[Array[1,2,3], Array[4,5,6]]);
batch_plan: |
BatchTableFunction { Unnest(Array(Array(1:Int32, 2:Int32, 3:Int32), Array(4:Int32, 5:Int32, 6:Int32))) }
BatchTableFunction { Unnest(ARRAY[{1,2,3}, {4,5,6}]:List { datatype: List { datatype: Int32 } }) }
- sql: |
create table t1 (x int);
select * from t1 where EXISTS(select * where t1.x=1);
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/planner_test/tests/testdata/cast.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
sql: |
select case when NULL then 1 end;
batch_plan: |
BatchProject { exprs: [Case(null:Boolean, 1:Int32)] }
BatchProject { exprs: [null:Int32] }
└─BatchValues { rows: [[]] }
- name: implicit cast boolean (JOIN ON NULL)
sql: |
Expand Down Expand Up @@ -65,7 +65,7 @@
sql: |
select case when 'y' then 1 end;
batch_plan: |
BatchProject { exprs: [Case(true:Boolean, 1:Int32)] }
BatchProject { exprs: [1:Int32] }
└─BatchValues { rows: [[]] }
- name: implicit cast boolean (JOIN ON with literal 'y' of unknown type)
sql: |
Expand Down
14 changes: 12 additions & 2 deletions src/frontend/planner_test/tests/testdata/explain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
LogicalProject { exprs: [1:Int32] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }

Const eval exprs:
jon-chuang marked this conversation as resolved.
Show resolved Hide resolved

BatchProject { exprs: [1:Int32] }
└─BatchValues { rows: [[]] }

Const eval exprs:

BatchProject { exprs: [1:Int32] }
└─BatchValues { rows: [[]] }

To Batch Physical Plan:

BatchProject { exprs: [1:Int32] }
Expand All @@ -51,7 +61,7 @@
"stages": {
"0": {
"root": {
"plan_node_id": 28,
"plan_node_id": 40,
"plan_node_type": "BatchProject",
"schema": [
{
Expand All @@ -64,7 +74,7 @@
],
"children": [
{
"plan_node_id": 26,
"plan_node_id": 38,
"plan_node_type": "BatchValues",
"schema": [],
"children": [],
Expand Down
Loading