risingwave_frontend/optimizer/rule/stream/
generate_series_with_now_rule.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2024 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 risingwave_common::types::DataType;
use risingwave_pb::expr::table_function::PbType as PbTableFuncType;

use crate::expr::{Expr, ExprRewriter};
use crate::optimizer::plan_node::{generic, LogicalNow};
use crate::optimizer::rule::{BoxedRule, Rule};
use crate::PlanRef;

pub struct GenerateSeriesWithNowRule {}
impl Rule for GenerateSeriesWithNowRule {
    fn apply(&self, plan: PlanRef) -> Option<PlanRef> {
        let ctx = plan.ctx();
        let table_func = plan.as_logical_table_function()?.table_function();

        if !table_func.args.iter().any(|arg| arg.has_now()) {
            return None;
        }

        if !(table_func.function_type == PbTableFuncType::GenerateSeries
            && table_func.args.len() == 3
            && table_func.args[0].return_type() == DataType::Timestamptz
            && table_func.args[1].is_now()
            && table_func.args[2].return_type() == DataType::Interval)
        {
            // only convert `generate_series(const timestamptz, now(), const interval)`
            ctx.warn_to_user(
                "`now()` is currently only supported in `generate_series(timestamptz, timestamptz, interval)` function as `stop`. \
                You may not using it correctly. Please kindly check the document."
            );
            return None;
        }

        let start_timestamp = ctx
            .session_timezone()
            .rewrite_expr(table_func.args[0].clone())
            .try_fold_const()
            .transpose()
            .ok()
            .flatten()
            .flatten();
        let interval = ctx
            .session_timezone()
            .rewrite_expr(table_func.args[2].clone())
            .try_fold_const()
            .transpose()
            .ok()
            .flatten()
            .flatten();

        if start_timestamp.is_none() || interval.is_none() {
            ctx.warn_to_user(
                "When using `generate_series` with `now()`, the `start` and `step` must be non-NULL constants",
            );
            return None;
        }

        Some(
            LogicalNow::new(generic::Now::generate_series(
                ctx,
                start_timestamp.unwrap().into_timestamptz(),
                interval.unwrap().into_interval(),
            ))
            .into(),
        )
    }
}

impl GenerateSeriesWithNowRule {
    pub fn create() -> BoxedRule {
        Box::new(Self {})
    }
}