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): cast/cmp with session timezone #7243

Merged
merged 41 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3f004a8
commit
jon-chuang Jan 5, 2023
ebe08ef
stash
jon-chuang Jan 5, 2023
99aac3a
fix
jon-chuang Jan 5, 2023
e97d28a
Merge branch 'jon/session_timezone' into jon/implicit_cast_with_sessi…
jon-chuang Jan 5, 2023
6e14642
stash
jon-chuang Jan 6, 2023
5dd7e9c
revert default
jon-chuang Jan 6, 2023
d42173e
Merge branch 'jon/session_timezone' into jon/implicit_cast_with_sessi…
jon-chuang Jan 6, 2023
e51dac8
stash
jon-chuang Jan 6, 2023
b9561bb
Merge branch 'main' of https://github.com/singularity-data/risingwave…
jon-chuang Jan 6, 2023
a6ff87e
commit
jon-chuang Jan 6, 2023
1aa991d
minor
jon-chuang Jan 6, 2023
eef6e07
minor
jon-chuang Jan 6, 2023
f18d7b4
minor
jon-chuang Jan 6, 2023
11f68cf
minor
jon-chuang Jan 6, 2023
560b35f
stash
jon-chuang Jan 9, 2023
d57822c
Merge branch 'main' into jon/implicit_cast_with_session_timezone
jon-chuang Jan 9, 2023
533288d
undo
jon-chuang Jan 9, 2023
768af61
changes
jon-chuang Jan 9, 2023
b1e23e0
for all
jon-chuang Jan 9, 2023
5e8207b
add dynamic filter test with timestamptz/timestamp - redundant as we …
jon-chuang Jan 9, 2023
637b86b
add
jon-chuang Jan 9, 2023
7a2bf8e
fix
jon-chuang Jan 10, 2023
3245e97
fix
jon-chuang Jan 10, 2023
2373b3f
add
jon-chuang Jan 10, 2023
63aad62
stash
jon-chuang Jan 10, 2023
9b19e7a
Merge branch 'main' of https://github.com/singularity-data/risingwave…
jon-chuang Jan 10, 2023
1254a6d
fix scan conversion
jon-chuang Jan 10, 2023
2544413
remove
jon-chuang Jan 10, 2023
5d67d75
minor
jon-chuang Jan 10, 2023
a16d522
revert to UTC at end of test
jon-chuang Jan 10, 2023
031ed37
fix kafka
jon-chuang Jan 10, 2023
b9ef349
planner tests, ignore slt
jon-chuang Jan 10, 2023
ceb80b3
Merge branch 'main' of https://github.com/singularity-data/risingwave…
jon-chuang Jan 10, 2023
af2b99f
minor
jon-chuang Jan 10, 2023
cffef1b
minor
jon-chuang Jan 10, 2023
31b8a78
code review
jon-chuang Jan 12, 2023
e054d54
minor
jon-chuang Jan 12, 2023
05a605e
add license
jon-chuang Jan 12, 2023
bb03a47
minor
jon-chuang Jan 12, 2023
f9ec0be
remove
jon-chuang Jan 12, 2023
334bfd8
Merge branch 'main' into jon/implicit_cast_with_session_timezone
mergify[bot] Jan 12, 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
7 changes: 7 additions & 0 deletions dashboard/proto/gen/expr.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions e2e_test/batch/functions/session_timezone.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
statement ok
set timezone = "us/pacific";

# Cast date to timestamptz
query T
select '2022-01-01'::date::timestamp with time zone;
----
2022-01-01 08:00:00+00:00

# Cast timestamp to timestamptz
query T
select '2022-01-01 00:00:00'::timestamp::timestamp with time zone;
----
2022-01-01 08:00:00+00:00
Comment on lines +1 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

We should display 2022-01-01 00:00:00-08:00 or 2022-01-01 08:00:00+00:00? Although they are equal.

Copy link
Contributor Author

@jon-chuang jon-chuang Jan 12, 2023

Choose a reason for hiding this comment

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

Yes, this will be in an upcoming PR (depending on this PR). It will format Pg rows with the session timestamp.


# Cast timestamptz to timestamp
query T
select '2022-01-01 00:00:00+00:00'::timestamp with time zone::timestamp;
----
2021-12-31 16:00:00

# Cast timestamptz to date
query T
select '2022-01-01 00:00:00+00:00'::timestamp with time zone::date;
----
2021-12-31

# Cast timestamptz to time
query T
select '2022-01-01 00:00:00+00:00'::timestamp with time zone::time;
----
16:00:00

# Compare timestamp with timestamptz
query T
select '2022-01-01 00:00:00-08:00'::timestamp with time zone = '2022-01-01 00:00:00'::timestamp;
----
t

# Compare date with timestamptz
query T
select '2022-01-01 00:00:00-08:00'::timestamp with time zone = '2022-01-01'::date;
----
t

# Cast varchar without timezone to timestamptz
query T
select '2022-01-01 00:00:00'::timestamp with time zone;
----
2022-01-01 08:00:00+00:00

# Cast timestamptz to varchar, should display with timezone information
query T
select '2022-01-01 00:00:00-08:00'::timestamp with time zone::varchar;
----
2022-01-01 00:00:00-08:00

statement ok
set timezone = 'UTC';
45 changes: 45 additions & 0 deletions e2e_test/streaming/dynamic_filter.slt
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,48 @@ drop table t1;

statement ok
drop table t2;

# ~ Simple Agg with timestamp/timestamptz ~
statement ok
create table t1 (v1 timestamp);

statement ok
create table t2 (v2 timestamp with time zone);

statement ok
create materialized view mv1 as with max_v2 as (select max(v2) max from t2) select v1 from t1, max_v2 where v1 > max;

statement ok
insert into t1 values ('2001-01-01 19:00:00'), ('2001-01-01 20:00:00'), ('2001-01-01 21:00:00');

# RHS is NULL means predicate is not true
query I
select * from mv1 order by v1;
----

statement ok
insert into t2 values ('2001-01-01 18:00:00+00:00');

query I
select * from mv1 order by v1;
----
2001-01-01 19:00:00
2001-01-01 20:00:00
2001-01-01 21:00:00

statement ok
insert into t2 values ('2001-01-01 20:00:00+00:00');

query I
select * from mv1 order by v1;
----
2001-01-01 21:00:00

statement ok
drop materialized view mv1;

statement ok
drop table t1;

statement ok
drop table t2;
2 changes: 2 additions & 0 deletions proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ message ExprNode {
// Parse text to timestamp by format string.
// e.g. `select to_timestamp('2022 08 21', 'YYYY MM DD')`
TO_TIMESTAMP1 = 107;
// Performs a cast with additional timezone information.
CAST_WITH_TIME_ZONE = 108;
// other functions
CAST = 201;
SUBSTR = 202;
Expand Down
16 changes: 15 additions & 1 deletion src/expr/src/expr/build_expr_from_prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use risingwave_common::try_match_expand;
use risingwave_common::types::DataType;
use risingwave_common::util::value_encoding::deserialize_datum;
use risingwave_pb::expr::expr_node::{RexNode, Type};
Expand All @@ -38,7 +39,9 @@ use crate::expr::expr_to_timestamp_const_tmpl::{
use crate::expr::expr_unary::{
new_length_default, new_ltrim_expr, new_rtrim_expr, new_trim_expr, new_unary_expr,
};
use crate::expr::{build_from_prost as expr_build_from_prost, BoxedExpression, Expression};
use crate::expr::{
build_from_prost as expr_build_from_prost, BoxedExpression, Expression, LiteralExpression,
};
use crate::vector_op::to_char::compile_pattern_to_chrono;
use crate::{bail, ensure, Result};

Expand Down Expand Up @@ -252,6 +255,17 @@ pub fn build_to_char_expr(prost: &ExprNode) -> Result<BoxedExpression> {
}
}

pub fn build_now_expr(prost: &ExprNode) -> Result<BoxedExpression> {
let rex_node = try_match_expand!(prost.get_rex_node(), Ok)?;
let RexNode::FuncCall(func_call_node) = rex_node else {
bail!("Expected RexNode::FuncCall in Now");
};
let Some(bind_timestamp) = func_call_node.children.first() else {
bail!("Expected epoch timestamp bound into Now");
};
LiteralExpression::try_from(bind_timestamp).map(Expression::boxed)
}

pub fn build_to_timestamp_expr(prost: &ExprNode) -> Result<BoxedExpression> {
let (children, ret_type) = get_children_and_return_type(prost)?;
ensure!(children.len() == 2);
Expand Down
39 changes: 37 additions & 2 deletions src/expr/src/expr/expr_binary_nonnull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use risingwave_pb::expr::expr_node::Type;

use super::Expression;
use crate::expr::expr_binary_bytes::new_concat_op;
use crate::expr::template::BinaryExpression;
use crate::expr::template::{BinaryBytesExpression, BinaryExpression};
use crate::expr::{template_fast, BoxedExpression};
use crate::vector_op::arithmetic_op::*;
use crate::vector_op::bitwise_op::*;
Expand All @@ -33,7 +33,9 @@ use crate::vector_op::extract::{
use crate::vector_op::like::like_default;
use crate::vector_op::position::position;
use crate::vector_op::round::round_digits;
use crate::vector_op::timestamptz::{timestamp_at_time_zone, timestamptz_at_time_zone};
use crate::vector_op::timestamptz::{
str_to_timestamptz, timestamp_at_time_zone, timestamptz_at_time_zone, timestamptz_to_string,
};
use crate::vector_op::to_timestamp::to_timestamp;
use crate::vector_op::tumble::{
tumble_start_date, tumble_start_date_time, tumble_start_timestamptz,
Expand Down Expand Up @@ -415,6 +417,38 @@ fn build_at_time_zone_expr(
Ok(expr)
}

fn build_cast_with_time_zone_expr(
ret: DataType,
l: BoxedExpression,
r: BoxedExpression,
) -> Result<BoxedExpression> {
let expr: BoxedExpression = match (ret.clone(), l.return_type()) {
(DataType::Varchar, DataType::Timestamptz) => Box::new(BinaryBytesExpression::<
I64Array,
Utf8Array,
_,
>::new(
l, r, ret, timestamptz_to_string
)),
(DataType::Timestamptz, DataType::Varchar) => {
Box::new(BinaryExpression::<Utf8Array, Utf8Array, I64Array, _>::new(
l,
r,
ret,
str_to_timestamptz,
))
}
_ => {
return Err(ExprError::UnsupportedFunction(format!(
"cannot cast at time zone (input type: {:?}, output type: {:?}",
l.return_type(),
ret,
)))
}
};
Ok(expr)
}

pub fn new_date_trunc_expr(
ret: DataType,
field: BoxedExpression,
Expand Down Expand Up @@ -630,6 +664,7 @@ pub fn new_binary_expr(
}
Type::Extract => build_extract_expr(ret, l, r)?,
Type::AtTimeZone => build_at_time_zone_expr(ret, l, r)?,
Type::CastWithTimeZone => build_cast_with_time_zone_expr(ret, l, r)?,
Type::RoundDigit => Box::new(template_fast::BinaryExpression::new(
l,
r,
Expand Down
15 changes: 2 additions & 13 deletions src/expr/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub use expr_literal::*;
use risingwave_common::array::{ArrayRef, DataChunk};
use risingwave_common::row::OwnedRow;
use risingwave_common::types::{DataType, Datum};
use risingwave_common::{bail, try_match_expand};
use risingwave_pb::expr::expr_node::RexNode;
use risingwave_pb::expr::ExprNode;

use super::Result;
Expand Down Expand Up @@ -114,7 +112,7 @@ pub fn build_from_prost(prost: &ExprNode) -> Result<BoxedExpression> {
Equal | NotEqual | LessThan | LessThanOrEqual | GreaterThan | GreaterThanOrEqual | Add
| Subtract | Multiply | Divide | Modulus | Extract | RoundDigit | TumbleStart
| Position | BitwiseShiftLeft | BitwiseShiftRight | BitwiseAnd | BitwiseOr | BitwiseXor
| ConcatOp | AtTimeZone => build_binary_expr_prost(prost),
| ConcatOp | AtTimeZone | CastWithTimeZone => build_binary_expr_prost(prost),
And | Or | IsDistinctFrom | IsNotDistinctFrom | ArrayAccess => {
build_nullable_binary_expr_prost(prost)
}
Expand Down Expand Up @@ -154,16 +152,7 @@ pub fn build_from_prost(prost: &ExprNode) -> Result<BoxedExpression> {
ArrayConcatExpression::try_from(prost).map(Expression::boxed)
}
Vnode => VnodeExpression::try_from(prost).map(Expression::boxed),
Now => {
let rex_node = try_match_expand!(prost.get_rex_node(), Ok)?;
let RexNode::FuncCall(func_call_node) = rex_node else {
bail!("Expected RexNode::FuncCall in Now");
};
let Option::Some(bind_timestamp) = func_call_node.children.first() else {
bail!("Expected epoch timestamp bound into Now");
};
LiteralExpression::try_from(bind_timestamp).map(Expression::boxed)
}
Now => build_now_expr(prost),
Udf => UdfExpression::try_from(prost).map(Expression::boxed),
_ => Err(ExprError::UnsupportedFunction(format!(
"{:?}",
Expand Down
Loading