query/range_select/
plan_rewrite.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::BTreeSet;
16use std::sync::Arc;
17use std::time::Duration;
18
19use arrow_schema::DataType;
20use async_recursion::async_recursion;
21use catalog::table_source::DfTableSourceProvider;
22use chrono::Utc;
23use common_time::interval::{MS_PER_DAY, NANOS_PER_MILLI};
24use common_time::timestamp::TimeUnit;
25use common_time::{IntervalDayTime, IntervalMonthDayNano, IntervalYearMonth, Timestamp, Timezone};
26use datafusion::datasource::DefaultTableSource;
27use datafusion::prelude::Column;
28use datafusion::scalar::ScalarValue;
29use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRecursion, TreeNodeRewriter};
30use datafusion_common::{DFSchema, DataFusionError, Result as DFResult};
31use datafusion_expr::execution_props::ExecutionProps;
32use datafusion_expr::expr::WildcardOptions;
33use datafusion_expr::simplify::SimplifyContext;
34use datafusion_expr::{
35    Aggregate, Analyze, Cast, Distinct, DistinctOn, Explain, Expr, ExprSchemable, Extension,
36    Literal, LogicalPlan, LogicalPlanBuilder, Projection,
37};
38use datafusion_optimizer::simplify_expressions::ExprSimplifier;
39use datatypes::prelude::ConcreteDataType;
40use promql_parser::util::parse_duration;
41use session::context::QueryContextRef;
42use snafu::{ensure, OptionExt, ResultExt};
43use table::table::adapter::DfTableProviderAdapter;
44
45use crate::error::{
46    CatalogSnafu, RangeQuerySnafu, Result, TimeIndexNotFoundSnafu, UnknownTableSnafu,
47};
48use crate::plan::ExtractExpr;
49use crate::range_select::plan::{Fill, RangeFn, RangeSelect};
50
51/// `RangeExprRewriter` will recursively search certain `Expr`, find all `range_fn` scalar udf contained in `Expr`,
52/// and collect the information required by the RangeSelect query,
53/// and finally modify the `range_fn` scalar udf to an ordinary column field.
54pub struct RangeExprRewriter<'a> {
55    input_plan: &'a Arc<LogicalPlan>,
56    align: Duration,
57    align_to: i64,
58    by: Vec<Expr>,
59    /// Use `BTreeSet` to avoid in case like `avg(a) RANGE '5m' + avg(a) RANGE '5m'`, duplicate range expr `avg(a) RANGE '5m'` be calculate twice
60    range_fn: BTreeSet<RangeFn>,
61    sub_aggr: &'a Aggregate,
62    query_ctx: &'a QueryContextRef,
63}
64
65impl RangeExprRewriter<'_> {
66    pub fn get_range_expr(&self, args: &[Expr], i: usize) -> DFResult<Expr> {
67        match args.get(i) {
68            Some(Expr::Column(column)) => {
69                let index = self.sub_aggr.schema.index_of_column(column)?;
70                let len = self.sub_aggr.group_expr.len();
71                self.sub_aggr
72                    .aggr_expr
73                    .get(index - len)
74                    .cloned()
75                    .ok_or(DataFusionError::Plan(
76                        "Range expr not found in underlying Aggregate Plan".into(),
77                    ))
78            }
79            Some(Expr::Alias(alias)) => {
80                self.get_range_expr(std::slice::from_ref(alias.expr.as_ref()), 0)
81            }
82            other => Err(dispose_parse_error(other)),
83        }
84    }
85}
86
87#[inline]
88fn dispose_parse_error(expr: Option<&Expr>) -> DataFusionError {
89    DataFusionError::Plan(
90        expr.map(|x| {
91            format!(
92                "Illegal argument `{}` in range select query",
93                x.schema_name()
94            )
95        })
96        .unwrap_or("Missing argument in range select query".into()),
97    )
98}
99
100fn parse_str_expr(args: &[Expr], i: usize) -> DFResult<&str> {
101    match args.get(i) {
102        Some(Expr::Literal(ScalarValue::Utf8(Some(str)), _)) => Ok(str.as_str()),
103        other => Err(dispose_parse_error(other)),
104    }
105}
106
107fn parse_expr_to_string(args: &[Expr], i: usize) -> DFResult<String> {
108    match args.get(i) {
109        Some(Expr::Literal(ScalarValue::Utf8(Some(str)), _)) => Ok(str.to_string()),
110        Some(expr) => Ok(expr.schema_name().to_string()),
111        None => Err(dispose_parse_error(None)),
112    }
113}
114
115/// Parse a duraion expr:
116/// 1. duration string (e.g. `'1h'`)
117/// 2. Interval expr (e.g. `INTERVAL '1 year 3 hours 20 minutes'`)
118/// 3. An interval expr can be evaluated at the logical plan stage (e.g. `INTERVAL '2' day - INTERVAL '1' day`)
119fn parse_duration_expr(args: &[Expr], i: usize) -> DFResult<Duration> {
120    match args.get(i) {
121        Some(Expr::Literal(ScalarValue::Utf8(Some(str)), _)) => {
122            parse_duration(str).map_err(DataFusionError::Plan)
123        }
124        Some(expr) => {
125            let ms = evaluate_expr_to_millisecond(args, i, true)?;
126            if ms <= 0 {
127                return Err(dispose_parse_error(Some(expr)));
128            }
129            Ok(Duration::from_millis(ms as u64))
130        }
131        None => Err(dispose_parse_error(None)),
132    }
133}
134
135/// Evaluate a time calculation expr, case like:
136/// 1. `INTERVAL '1' day + INTERVAL '1 year 2 hours 3 minutes'`
137/// 2. `now() - INTERVAL '1' day` (when `interval_only==false`)
138///
139/// Output a millisecond timestamp
140///
141/// if `interval_only==true`, only accept expr with all interval type (case 2 will return a error)
142fn evaluate_expr_to_millisecond(args: &[Expr], i: usize, interval_only: bool) -> DFResult<i64> {
143    let Some(expr) = args.get(i) else {
144        return Err(dispose_parse_error(None));
145    };
146    if interval_only && !interval_only_in_expr(expr) {
147        return Err(dispose_parse_error(Some(expr)));
148    }
149    let execution_props = ExecutionProps::new().with_query_execution_start_time(Utc::now());
150    let info = SimplifyContext::new(&execution_props).with_schema(Arc::new(DFSchema::empty()));
151    let simplify_expr = ExprSimplifier::new(info).simplify(expr.clone())?;
152    match simplify_expr {
153        Expr::Literal(ScalarValue::TimestampNanosecond(ts_nanos, _), _)
154        | Expr::Literal(ScalarValue::DurationNanosecond(ts_nanos), _) => {
155            ts_nanos.map(|v| v / 1_000_000)
156        }
157        Expr::Literal(ScalarValue::TimestampMicrosecond(ts_micros, _), _)
158        | Expr::Literal(ScalarValue::DurationMicrosecond(ts_micros), _) => {
159            ts_micros.map(|v| v / 1_000)
160        }
161        Expr::Literal(ScalarValue::TimestampMillisecond(ts_millis, _), _)
162        | Expr::Literal(ScalarValue::DurationMillisecond(ts_millis), _) => ts_millis,
163        Expr::Literal(ScalarValue::TimestampSecond(ts_secs, _), _)
164        | Expr::Literal(ScalarValue::DurationSecond(ts_secs), _) => ts_secs.map(|v| v * 1_000),
165        // We don't support interval with months as days in a month is unclear.
166        Expr::Literal(ScalarValue::IntervalYearMonth(interval), _) => interval
167            .map(|v| {
168                let interval = IntervalYearMonth::from_i32(v);
169                if interval.months != 0 {
170                    return Err(DataFusionError::Plan(format!(
171                        "Year or month interval is not allowed in range query: {}",
172                        expr.schema_name()
173                    )));
174                }
175
176                Ok(0)
177            })
178            .transpose()?,
179        Expr::Literal(ScalarValue::IntervalDayTime(interval), _) => interval.map(|v| {
180            let interval = IntervalDayTime::from(v);
181            interval.as_millis()
182        }),
183        Expr::Literal(ScalarValue::IntervalMonthDayNano(interval), _) => interval
184            .map(|v| {
185                let interval = IntervalMonthDayNano::from(v);
186                if interval.months != 0 {
187                    return Err(DataFusionError::Plan(format!(
188                        "Year or month interval is not allowed in range query: {}",
189                        expr.schema_name()
190                    )));
191                }
192
193                Ok(interval.days as i64 * MS_PER_DAY + interval.nanoseconds / NANOS_PER_MILLI)
194            })
195            .transpose()?,
196        _ => None,
197    }
198    .ok_or_else(|| {
199        DataFusionError::Plan(format!(
200            "{} is not a expr can be evaluate and use in range query",
201            expr.schema_name()
202        ))
203    })
204}
205
206/// Parse the `align to` clause and return a UTC timestamp with unit of millisecond,
207/// which is used as the basis for dividing time slot during the align operation.
208/// 1. NOW: align to current execute time
209/// 2. Timestamp string: align to specific timestamp
210/// 3. An expr can be evaluated at the logical plan stage (e.g. `now() - INTERVAL '1' day`)
211/// 4. leave empty (as Default Option): align to unix epoch 0 (timezone aware)
212fn parse_align_to(args: &[Expr], i: usize, timezone: Option<&Timezone>) -> DFResult<i64> {
213    let Ok(s) = parse_str_expr(args, i) else {
214        return evaluate_expr_to_millisecond(args, i, false);
215    };
216    let upper = s.to_uppercase();
217    match upper.as_str() {
218        "NOW" => return Ok(Timestamp::current_millis().value()),
219        // default align to unix epoch 0 (timezone aware)
220        "" => return Ok(timezone.map(|tz| tz.local_minus_utc() * 1000).unwrap_or(0)),
221        _ => (),
222    }
223
224    Timestamp::from_str(s, timezone)
225        .map_err(|e| {
226            DataFusionError::Plan(format!(
227                "Illegal `align to` argument `{}` in range select query, can't be parse as NOW/CALENDAR/Timestamp, error: {}",
228                s, e
229            ))
230        })?.convert_to(TimeUnit::Millisecond).map(|x|x.value()).ok_or(DataFusionError::Plan(format!(
231            "Illegal `align to` argument `{}` in range select query, can't be convert to a valid Timestamp",
232            s
233        ))
234        )
235}
236
237fn parse_expr_list(args: &[Expr], start: usize, len: usize) -> DFResult<Vec<Expr>> {
238    let mut outs = Vec::with_capacity(len);
239    for i in start..start + len {
240        outs.push(match &args.get(i) {
241            Some(
242                Expr::Column(_)
243                | Expr::Literal(_, _)
244                | Expr::BinaryExpr(_)
245                | Expr::ScalarFunction(_),
246            ) => args[i].clone(),
247            other => {
248                return Err(dispose_parse_error(*other));
249            }
250        });
251    }
252    Ok(outs)
253}
254
255macro_rules! inconsistent_check {
256    ($self: ident.$name: ident, $cond: expr) => {
257        if $cond && $self.$name != $name {
258            return Err(DataFusionError::Plan(
259                concat!(
260                    "Inconsistent ",
261                    stringify!($name),
262                    " given in Range Function Rewrite"
263                )
264                .into(),
265            ));
266        } else {
267            $self.$name = $name;
268        }
269    };
270}
271
272impl TreeNodeRewriter for RangeExprRewriter<'_> {
273    type Node = Expr;
274
275    fn f_down(&mut self, node: Expr) -> DFResult<Transformed<Expr>> {
276        if let Expr::ScalarFunction(func) = &node {
277            if func.name() == "range_fn" {
278                // `range_fn(func, range, fill, byc, [byv], align, to)`
279                // `[byv]` are variadic arguments, byc indicate the length of arguments
280                let range_expr = self.get_range_expr(&func.args, 0)?;
281                let range = parse_duration_expr(&func.args, 1)?;
282                let byc = str::parse::<usize>(parse_str_expr(&func.args, 3)?)
283                    .map_err(|e| DataFusionError::Plan(e.to_string()))?;
284                let by = parse_expr_list(&func.args, 4, byc)?;
285                let align = parse_duration_expr(&func.args, byc + 4)?;
286                let align_to =
287                    parse_align_to(&func.args, byc + 5, Some(&self.query_ctx.timezone()))?;
288                let mut data_type = range_expr.get_type(self.input_plan.schema())?;
289                let mut need_cast = false;
290                let fill = Fill::try_from_str(parse_str_expr(&func.args, 2)?, &data_type)?;
291                if matches!(fill, Some(Fill::Linear)) && data_type.is_integer() {
292                    data_type = DataType::Float64;
293                    need_cast = true;
294                }
295                inconsistent_check!(self.by, !self.by.is_empty());
296                inconsistent_check!(self.align, self.align != Duration::default());
297                inconsistent_check!(self.align_to, self.align_to != 0);
298                let range_fn = RangeFn {
299                    name: if let Some(fill) = &fill {
300                        format!(
301                            "{} RANGE {} FILL {}",
302                            range_expr.schema_name(),
303                            parse_expr_to_string(&func.args, 1)?,
304                            fill
305                        )
306                    } else {
307                        format!(
308                            "{} RANGE {}",
309                            range_expr.schema_name(),
310                            parse_expr_to_string(&func.args, 1)?,
311                        )
312                    },
313                    data_type,
314                    expr: range_expr,
315                    range,
316                    fill,
317                    need_cast,
318                };
319                let alias = Expr::Column(Column::from_name(range_fn.name.clone()));
320                self.range_fn.insert(range_fn);
321                return Ok(Transformed::yes(alias));
322            }
323        }
324        Ok(Transformed::no(node))
325    }
326}
327
328/// In order to implement RangeSelect query like `avg(field_0) RANGE '5m' FILL NULL`,
329/// All RangeSelect query items are converted into udf scalar function in sql parse stage, with format like `range_fn(avg(field_0), .....)`.
330/// `range_fn` contains all the parameters we need to execute RangeSelect.
331/// In order to correctly execute the query process of range select, we need to modify the query plan generated by datafusion.
332/// We need to recursively find the entire LogicalPlan, and find all `range_fn` scalar udf contained in the project plan,
333/// collecting info we need to generate RangeSelect Query LogicalPlan and rewrite th original LogicalPlan.
334pub struct RangePlanRewriter {
335    table_provider: DfTableSourceProvider,
336    query_ctx: QueryContextRef,
337}
338
339impl RangePlanRewriter {
340    pub fn new(table_provider: DfTableSourceProvider, query_ctx: QueryContextRef) -> Self {
341        Self {
342            table_provider,
343            query_ctx,
344        }
345    }
346
347    pub async fn rewrite(&mut self, plan: LogicalPlan) -> Result<LogicalPlan> {
348        match self.rewrite_logical_plan(&plan).await? {
349            Some(new_plan) => Ok(new_plan),
350            None => Ok(plan),
351        }
352    }
353
354    #[async_recursion]
355    async fn rewrite_logical_plan(&mut self, plan: &LogicalPlan) -> Result<Option<LogicalPlan>> {
356        let inputs = plan.inputs();
357        let mut new_inputs = Vec::with_capacity(inputs.len());
358        for input in &inputs {
359            new_inputs.push(self.rewrite_logical_plan(input).await?)
360        }
361        match plan {
362            LogicalPlan::Projection(Projection { expr, input, .. })
363                if have_range_in_exprs(expr) =>
364            {
365                let (aggr_plan, input) = if let LogicalPlan::Aggregate(aggr) = input.as_ref() {
366                    // Expr like `rate(max(a) RANGE '6m') RANGE '6m'` have legal syntax but illegal semantic.
367                    if have_range_in_exprs(&aggr.aggr_expr) {
368                        return RangeQuerySnafu {
369                            msg: "Nest Range Query is not allowed",
370                        }
371                        .fail();
372                    }
373                    (aggr, aggr.input.clone())
374                } else {
375                    return RangeQuerySnafu {
376                        msg: "Window functions is not allowed in Range Query",
377                    }
378                    .fail();
379                };
380                let (time_index, default_by) = self.get_index_by(input.schema()).await?;
381                let mut range_rewriter = RangeExprRewriter {
382                    input_plan: &input,
383                    align: Duration::default(),
384                    align_to: 0,
385                    by: vec![],
386                    range_fn: BTreeSet::new(),
387                    sub_aggr: aggr_plan,
388                    query_ctx: &self.query_ctx,
389                };
390                let new_expr = expr
391                    .iter()
392                    .map(|expr| expr.clone().rewrite(&mut range_rewriter).map(|x| x.data))
393                    .collect::<DFResult<Vec<_>>>()?;
394                if range_rewriter.by.is_empty() {
395                    range_rewriter.by = default_by;
396                }
397                let range_select = RangeSelect::try_new(
398                    input.clone(),
399                    range_rewriter.range_fn.into_iter().collect(),
400                    range_rewriter.align,
401                    range_rewriter.align_to,
402                    time_index,
403                    range_rewriter.by,
404                    &new_expr,
405                )?;
406                let no_additional_project = range_select.schema_project.is_some();
407                let range_plan = LogicalPlan::Extension(Extension {
408                    node: Arc::new(range_select),
409                });
410                if no_additional_project {
411                    Ok(Some(range_plan))
412                } else {
413                    let project_plan = LogicalPlanBuilder::from(range_plan)
414                        .project(new_expr)
415                        .and_then(|x| x.build())?;
416                    Ok(Some(project_plan))
417                }
418            }
419            _ => {
420                if new_inputs.iter().any(|x| x.is_some()) {
421                    let inputs: Vec<LogicalPlan> = new_inputs
422                        .into_iter()
423                        .zip(inputs)
424                        .map(|(x, y)| match x {
425                            Some(plan) => plan,
426                            None => y.clone(),
427                        })
428                        .collect();
429                    // Due to the limitations of Datafusion, for `LogicalPlan::Analyze` and `LogicalPlan::Explain`,
430                    // directly using the method `with_new_inputs` to rebuild a new `LogicalPlan` will cause an error,
431                    // so here we directly use the `LogicalPlanBuilder` to build a new plan.
432                    let plan = match plan {
433                        LogicalPlan::Analyze(Analyze { verbose, .. }) => {
434                            ensure!(
435                                inputs.len() == 1,
436                                RangeQuerySnafu {
437                                    msg: "Illegal subplan nums when rewrite Analyze logical plan",
438                                }
439                            );
440                            LogicalPlanBuilder::from(inputs[0].clone())
441                                .explain(*verbose, true)?
442                                .build()
443                        }
444                        LogicalPlan::Explain(Explain { verbose, .. }) => {
445                            ensure!(
446                                inputs.len() == 1,
447                                RangeQuerySnafu {
448                                    msg: "Illegal subplan nums when rewrite Explain logical plan",
449                                }
450                            );
451                            LogicalPlanBuilder::from(inputs[0].clone())
452                                .explain(*verbose, false)?
453                                .build()
454                        }
455                        LogicalPlan::Distinct(Distinct::On(DistinctOn {
456                            on_expr,
457                            select_expr,
458                            sort_expr,
459                            ..
460                        })) => {
461                            ensure!(
462                                inputs.len() == 1,
463                                RangeQuerySnafu {
464                                    msg:
465                                        "Illegal subplan nums when rewrite DistinctOn logical plan",
466                                }
467                            );
468                            LogicalPlanBuilder::from(inputs[0].clone())
469                                .distinct_on(
470                                    on_expr.clone(),
471                                    select_expr.clone(),
472                                    sort_expr.clone(),
473                                )?
474                                .build()
475                        }
476                        _ => plan.with_new_exprs(plan.expressions_consider_join(), inputs),
477                    }?;
478                    Ok(Some(plan))
479                } else {
480                    Ok(None)
481                }
482            }
483        }
484    }
485
486    /// this function use to find the time_index column and row columns from input schema,
487    /// return `(time_index, [row_columns])` to the rewriter.
488    /// If the user does not explicitly use the `by` keyword to indicate time series,
489    /// `[row_columns]` will be use as default time series
490    async fn get_index_by(&mut self, schema: &Arc<DFSchema>) -> Result<(Expr, Vec<Expr>)> {
491        #[allow(deprecated)]
492        let mut time_index_expr = Expr::Wildcard {
493            qualifier: None,
494            options: Box::new(WildcardOptions::default()),
495        };
496        let mut default_by = vec![];
497        for i in 0..schema.fields().len() {
498            let (qualifier, _) = schema.qualified_field(i);
499            if let Some(table_ref) = qualifier {
500                let table = self
501                    .table_provider
502                    .resolve_table(table_ref.clone())
503                    .await
504                    .context(CatalogSnafu)?
505                    .as_any()
506                    .downcast_ref::<DefaultTableSource>()
507                    .context(UnknownTableSnafu)?
508                    .table_provider
509                    .as_any()
510                    .downcast_ref::<DfTableProviderAdapter>()
511                    .context(UnknownTableSnafu)?
512                    .table();
513                let schema = table.schema();
514                let time_index_column =
515                    schema
516                        .timestamp_column()
517                        .with_context(|| TimeIndexNotFoundSnafu {
518                            table: table_ref.to_string(),
519                        })?;
520                // assert time_index's datatype is timestamp
521                if let ConcreteDataType::Timestamp(_) = time_index_column.data_type {
522                    default_by = table
523                        .table_info()
524                        .meta
525                        .row_key_column_names()
526                        .map(|key| Expr::Column(Column::new(Some(table_ref.clone()), key)))
527                        .collect();
528                    // If the user does not specify a primary key when creating a table,
529                    // then by default all data will be aggregated into one time series,
530                    // which is equivalent to using `by(1)` in SQL
531                    if default_by.is_empty() {
532                        default_by = vec![1.lit()];
533                    }
534                    time_index_expr = Expr::Column(Column::new(
535                        Some(table_ref.clone()),
536                        time_index_column.name.clone(),
537                    ));
538                }
539            }
540        }
541        #[allow(deprecated)]
542        if matches!(time_index_expr, Expr::Wildcard { .. }) {
543            TimeIndexNotFoundSnafu {
544                table: schema.to_string(),
545            }
546            .fail()
547        } else {
548            Ok((time_index_expr, default_by))
549        }
550    }
551}
552
553fn have_range_in_exprs(exprs: &[Expr]) -> bool {
554    exprs.iter().any(|expr| {
555        let mut find_range = false;
556        let _ = expr.apply(|expr| {
557            Ok(match expr {
558                Expr::ScalarFunction(func) if func.name() == "range_fn" => {
559                    find_range = true;
560                    TreeNodeRecursion::Stop
561                }
562                _ => TreeNodeRecursion::Continue,
563            })
564        });
565        find_range
566    })
567}
568
569fn interval_only_in_expr(expr: &Expr) -> bool {
570    let mut all_interval = true;
571    let _ = expr.apply(|expr| {
572        // A cast expression for an interval.
573        if matches!(
574            expr,
575            Expr::Cast(Cast{
576                expr,
577                data_type: DataType::Interval(_)
578            }) if matches!(&**expr, Expr::Literal(ScalarValue::Utf8(_), _))
579        ) {
580            // Stop checking the sub `expr`,
581            // which is a `Utf8` type and has already been tested above.
582            return Ok(TreeNodeRecursion::Stop);
583        }
584
585        if !matches!(
586            expr,
587            Expr::Literal(ScalarValue::IntervalDayTime(_), _)
588                | Expr::Literal(ScalarValue::IntervalMonthDayNano(_), _)
589                | Expr::Literal(ScalarValue::IntervalYearMonth(_), _)
590                | Expr::BinaryExpr(_)
591                | Expr::Cast(Cast {
592                    data_type: DataType::Interval(_),
593                    ..
594                })
595        ) {
596            all_interval = false;
597            Ok(TreeNodeRecursion::Stop)
598        } else {
599            Ok(TreeNodeRecursion::Continue)
600        }
601    });
602
603    all_interval
604}
605
606#[cfg(test)]
607mod test {
608
609    use arrow::datatypes::IntervalUnit;
610    use catalog::memory::MemoryCatalogManager;
611    use catalog::RegisterTableRequest;
612    use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
613    use common_time::IntervalYearMonth;
614    use datafusion_expr::{BinaryExpr, Literal, Operator};
615    use datatypes::prelude::ConcreteDataType;
616    use datatypes::schema::{ColumnSchema, Schema};
617    use session::context::QueryContext;
618    use table::metadata::{TableInfoBuilder, TableMetaBuilder};
619    use table::test_util::EmptyTable;
620
621    use super::*;
622    use crate::options::QueryOptions;
623    use crate::parser::QueryLanguageParser;
624    use crate::{QueryEngineFactory, QueryEngineRef};
625
626    async fn create_test_engine() -> QueryEngineRef {
627        let table_name = "test".to_string();
628        let mut columns = vec![];
629        for i in 0..5 {
630            columns.push(ColumnSchema::new(
631                format!("tag_{i}"),
632                ConcreteDataType::string_datatype(),
633                false,
634            ));
635        }
636        columns.push(
637            ColumnSchema::new(
638                "timestamp".to_string(),
639                ConcreteDataType::timestamp_millisecond_datatype(),
640                false,
641            )
642            .with_time_index(true),
643        );
644        for i in 0..5 {
645            columns.push(ColumnSchema::new(
646                format!("field_{i}"),
647                ConcreteDataType::float64_datatype(),
648                true,
649            ));
650        }
651        let schema = Arc::new(Schema::new(columns));
652        let table_meta = TableMetaBuilder::empty()
653            .schema(schema)
654            .primary_key_indices((0..5).collect())
655            .value_indices((6..11).collect())
656            .next_column_id(1024)
657            .build()
658            .unwrap();
659        let table_info = TableInfoBuilder::default()
660            .name(&table_name)
661            .meta(table_meta)
662            .build()
663            .unwrap();
664        let table = EmptyTable::from_table_info(&table_info);
665        let catalog_list = MemoryCatalogManager::with_default_setup();
666        assert!(catalog_list
667            .register_table_sync(RegisterTableRequest {
668                catalog: DEFAULT_CATALOG_NAME.to_string(),
669                schema: DEFAULT_SCHEMA_NAME.to_string(),
670                table_name,
671                table_id: 1024,
672                table,
673            })
674            .is_ok());
675        QueryEngineFactory::new(
676            catalog_list,
677            None,
678            None,
679            None,
680            None,
681            false,
682            QueryOptions::default(),
683        )
684        .query_engine()
685    }
686
687    async fn do_query(sql: &str) -> Result<LogicalPlan> {
688        let stmt = QueryLanguageParser::parse_sql(sql, &QueryContext::arc()).unwrap();
689        let engine = create_test_engine().await;
690        engine.planner().plan(&stmt, QueryContext::arc()).await
691    }
692
693    async fn query_plan_compare(sql: &str, expected: String) {
694        let plan = do_query(sql).await.unwrap();
695        assert_eq!(plan.display_indent_schema().to_string(), expected);
696    }
697
698    #[tokio::test]
699    async fn range_no_project() {
700        let query = r#"SELECT timestamp, tag_0, tag_1, avg(field_0 + field_1) RANGE '5m' FROM test ALIGN '1h' by (tag_0,tag_1);"#;
701        let expected = String::from(
702            "RangeSelect: range_exprs=[avg(test.field_0 + test.field_1) RANGE 5m], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8, avg(test.field_0 + test.field_1) RANGE 5m:Float64;N]\
703            \n  TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
704        );
705        query_plan_compare(query, expected).await;
706    }
707
708    #[tokio::test]
709    async fn range_expr_calculation() {
710        let query = r#"SELECT (avg(field_0 + field_1)/4) RANGE '5m' FROM test ALIGN '1h' by (tag_0,tag_1);"#;
711        let expected = String::from(
712            "Projection: avg(test.field_0 + test.field_1) RANGE 5m / Int64(4) [avg(test.field_0 + test.field_1) RANGE 5m / Int64(4):Float64;N]\
713            \n  RangeSelect: range_exprs=[avg(test.field_0 + test.field_1) RANGE 5m], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [avg(test.field_0 + test.field_1) RANGE 5m:Float64;N, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8]\
714            \n    TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
715        );
716        query_plan_compare(query, expected).await;
717    }
718
719    #[tokio::test]
720    async fn range_multi_args() {
721        let query =
722            r#"SELECT (covar(field_0 + field_1, field_1)/4) RANGE '5m' FROM test ALIGN '1h';"#;
723        let expected = String::from(
724            "Projection: covar_samp(test.field_0 + test.field_1,test.field_1) RANGE 5m / Int64(4) [covar_samp(test.field_0 + test.field_1,test.field_1) RANGE 5m / Int64(4):Float64;N]\
725            \n  RangeSelect: range_exprs=[covar_samp(test.field_0 + test.field_1,test.field_1) RANGE 5m], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1, test.tag_2, test.tag_3, test.tag_4], time_index=timestamp [covar_samp(test.field_0 + test.field_1,test.field_1) RANGE 5m:Float64;N, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8]\
726            \n    TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
727        );
728        query_plan_compare(query, expected).await;
729    }
730
731    #[tokio::test]
732    async fn range_calculation() {
733        let query = r#"SELECT ((avg(field_0)+sum(field_1))/4) RANGE '5m' FROM test ALIGN '1h' by (tag_0,tag_1) FILL NULL;"#;
734        let expected = String::from(
735            "Projection: (avg(test.field_0) RANGE 5m FILL NULL + sum(test.field_1) RANGE 5m FILL NULL) / Int64(4) [avg(test.field_0) RANGE 5m FILL NULL + sum(test.field_1) RANGE 5m FILL NULL / Int64(4):Float64;N]\
736            \n  RangeSelect: range_exprs=[avg(test.field_0) RANGE 5m FILL NULL, sum(test.field_1) RANGE 5m FILL NULL], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [avg(test.field_0) RANGE 5m FILL NULL:Float64;N, sum(test.field_1) RANGE 5m FILL NULL:Float64;N, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8]\
737            \n    TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
738        );
739        query_plan_compare(query, expected).await;
740    }
741
742    #[tokio::test]
743    async fn range_as_sub_query() {
744        let query = r#"SELECT foo + 1 from (SELECT ((avg(field_0)+sum(field_1))/4) RANGE '5m' as foo FROM test ALIGN '1h' by (tag_0,tag_1) FILL NULL) where foo > 1;"#;
745        let expected = String::from(
746            "Projection: foo + Int64(1) [foo + Int64(1):Float64;N]\
747            \n  Filter: foo > Int64(1) [foo:Float64;N]\
748            \n    Projection: (avg(test.field_0) RANGE 5m FILL NULL + sum(test.field_1) RANGE 5m FILL NULL) / Int64(4) AS foo [foo:Float64;N]\
749            \n      RangeSelect: range_exprs=[avg(test.field_0) RANGE 5m FILL NULL, sum(test.field_1) RANGE 5m FILL NULL], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [avg(test.field_0) RANGE 5m FILL NULL:Float64;N, sum(test.field_1) RANGE 5m FILL NULL:Float64;N, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8]\
750            \n        TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
751        );
752        query_plan_compare(query, expected).await;
753    }
754
755    #[tokio::test]
756    async fn range_from_nest_query() {
757        let query = r#"SELECT ((avg(a)+sum(b))/4) RANGE '5m' FROM (SELECT field_0 as a, field_1 as b, tag_0 as c, tag_1 as d, timestamp from test where field_0 > 1.0) ALIGN '1h' by (c, d) FILL NULL;"#;
758        let expected = String::from(
759            "Projection: (avg(a) RANGE 5m FILL NULL + sum(b) RANGE 5m FILL NULL) / Int64(4) [avg(a) RANGE 5m FILL NULL + sum(b) RANGE 5m FILL NULL / Int64(4):Float64;N]\
760            \n  RangeSelect: range_exprs=[avg(a) RANGE 5m FILL NULL, sum(b) RANGE 5m FILL NULL], align=3600000ms, align_to=0ms, align_by=[c, d], time_index=timestamp [avg(a) RANGE 5m FILL NULL:Float64;N, sum(b) RANGE 5m FILL NULL:Float64;N, timestamp:Timestamp(Millisecond, None), c:Utf8, d:Utf8]\
761            \n    Projection: test.field_0 AS a, test.field_1 AS b, test.tag_0 AS c, test.tag_1 AS d, test.timestamp [a:Float64;N, b:Float64;N, c:Utf8, d:Utf8, timestamp:Timestamp(Millisecond, None)]\
762            \n      Filter: test.field_0 > Float64(1) [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]\
763            \n        TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
764         );
765        query_plan_compare(query, expected).await;
766    }
767
768    #[tokio::test]
769    async fn range_in_expr() {
770        let query = r#"SELECT sin(avg(field_0 + field_1) RANGE '5m' + 1) FROM test ALIGN '1h' by (tag_0,tag_1);"#;
771        let expected = String::from(
772            "Projection: sin(avg(test.field_0 + test.field_1) RANGE 5m + Int64(1)) [sin(avg(test.field_0 + test.field_1) RANGE 5m + Int64(1)):Float64;N]\
773            \n  RangeSelect: range_exprs=[avg(test.field_0 + test.field_1) RANGE 5m], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [avg(test.field_0 + test.field_1) RANGE 5m:Float64;N, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8]\
774            \n    TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
775        );
776        query_plan_compare(query, expected).await;
777    }
778
779    #[tokio::test]
780    async fn duplicate_range_expr() {
781        let query = r#"SELECT avg(field_0) RANGE '5m' FILL 6.0 + avg(field_0) RANGE '5m' FILL 6.0 FROM test ALIGN '1h' by (tag_0,tag_1);"#;
782        let expected = String::from(
783            "Projection: avg(test.field_0) RANGE 5m FILL 6 + avg(test.field_0) RANGE 5m FILL 6 [avg(test.field_0) RANGE 5m FILL 6 + avg(test.field_0) RANGE 5m FILL 6:Float64]\
784            \n  RangeSelect: range_exprs=[avg(test.field_0) RANGE 5m FILL 6], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [avg(test.field_0) RANGE 5m FILL 6:Float64, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8]\
785            \n    TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
786        );
787        query_plan_compare(query, expected).await;
788    }
789
790    #[tokio::test]
791    async fn deep_nest_range_expr() {
792        let query = r#"SELECT round(sin(avg(field_0 + field_1) RANGE '5m' + 1)) FROM test ALIGN '1h' by (tag_0,tag_1);"#;
793        let expected = String::from(
794            "Projection: round(sin(avg(test.field_0 + test.field_1) RANGE 5m + Int64(1))) [round(sin(avg(test.field_0 + test.field_1) RANGE 5m + Int64(1))):Float64;N]\
795            \n  RangeSelect: range_exprs=[avg(test.field_0 + test.field_1) RANGE 5m], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [avg(test.field_0 + test.field_1) RANGE 5m:Float64;N, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8]\
796            \n    TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
797        );
798        query_plan_compare(query, expected).await;
799    }
800
801    #[tokio::test]
802    async fn complex_range_expr() {
803        let query = r#"SELECT gcd(CAST(max(field_0 + 1) Range '5m' FILL NULL AS Int64), CAST(tag_0 AS Int64)) + round(max(field_2+1) Range '6m' FILL NULL + 1) + max(field_2+3) Range '10m' FILL NULL * CAST(tag_1 AS Float64) + 1 FROM test ALIGN '1h' by (tag_0, tag_1);"#;
804        let expected = String::from(
805            "Projection: gcd(arrow_cast(max(test.field_0 + Int64(1)) RANGE 5m FILL NULL, Utf8(\"Int64\")), arrow_cast(test.tag_0, Utf8(\"Int64\"))) + round(max(test.field_2 + Int64(1)) RANGE 6m FILL NULL + Int64(1)) + max(test.field_2 + Int64(3)) RANGE 10m FILL NULL * arrow_cast(test.tag_1, Utf8(\"Float64\")) + Int64(1) [gcd(arrow_cast(max(test.field_0 + Int64(1)) RANGE 5m FILL NULL,Utf8(\"Int64\")),arrow_cast(test.tag_0,Utf8(\"Int64\"))) + round(max(test.field_2 + Int64(1)) RANGE 6m FILL NULL + Int64(1)) + max(test.field_2 + Int64(3)) RANGE 10m FILL NULL * arrow_cast(test.tag_1,Utf8(\"Float64\")) + Int64(1):Float64;N]\
806            \n  RangeSelect: range_exprs=[max(test.field_0 + Int64(1)) RANGE 5m FILL NULL, max(test.field_2 + Int64(1)) RANGE 6m FILL NULL, max(test.field_2 + Int64(3)) RANGE 10m FILL NULL], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [max(test.field_0 + Int64(1)) RANGE 5m FILL NULL:Float64;N, max(test.field_2 + Int64(1)) RANGE 6m FILL NULL:Float64;N, max(test.field_2 + Int64(3)) RANGE 10m FILL NULL:Float64;N, timestamp:Timestamp(Millisecond, None), tag_0:Utf8, tag_1:Utf8]\
807            \n    TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
808        );
809        query_plan_compare(query, expected).await;
810    }
811
812    #[tokio::test]
813    async fn range_linear_on_integer() {
814        let query = r#"SELECT min(CAST(field_0 AS Int64) + CAST(field_1 AS Int64)) RANGE '5m' FILL LINEAR FROM test ALIGN '1h' by (tag_0,tag_1);"#;
815        let expected = String::from(
816            "RangeSelect: range_exprs=[min(arrow_cast(test.field_0,Utf8(\"Int64\")) + arrow_cast(test.field_1,Utf8(\"Int64\"))) RANGE 5m FILL LINEAR], align=3600000ms, align_to=0ms, align_by=[test.tag_0, test.tag_1], time_index=timestamp [min(arrow_cast(test.field_0,Utf8(\"Int64\")) + arrow_cast(test.field_1,Utf8(\"Int64\"))) RANGE 5m FILL LINEAR:Float64;N]\
817            \n  TableScan: test [tag_0:Utf8, tag_1:Utf8, tag_2:Utf8, tag_3:Utf8, tag_4:Utf8, timestamp:Timestamp(Millisecond, None), field_0:Float64;N, field_1:Float64;N, field_2:Float64;N, field_3:Float64;N, field_4:Float64;N]"
818        );
819        query_plan_compare(query, expected).await;
820    }
821
822    #[tokio::test]
823    async fn range_nest_range_err() {
824        let query = r#"SELECT sum(avg(field_0 + field_1) RANGE '5m' + 1) RANGE '5m' + 1 FROM test ALIGN '1h' by (tag_0,tag_1);"#;
825        assert_eq!(
826            do_query(query).await.unwrap_err().to_string(),
827            "Range Query: Nest Range Query is not allowed"
828        )
829    }
830
831    #[tokio::test]
832    /// Start directly from the rewritten SQL and check whether the error reported by the range expression rewriting is as expected.
833    /// the right argument is `range_fn(avg(field_0), '5m', 'NULL', '0', '1h')`
834    async fn range_argument_err_1() {
835        let query = r#"SELECT range_fn('5m', avg(field_0), 'NULL', '1', tag_0, '1h') FROM test group by tag_0;"#;
836        let error = do_query(query).await.unwrap_err().to_string();
837        assert_eq!(
838            error,
839            "Error during planning: Illegal argument `Utf8(\"5m\")` in range select query"
840        )
841    }
842
843    #[tokio::test]
844    async fn range_argument_err_2() {
845        let query = r#"SELECT range_fn(avg(field_0), 5, 'NULL', '1', tag_0, '1h') FROM test group by tag_0;"#;
846        let error = do_query(query).await.unwrap_err().to_string();
847        assert_eq!(
848            error,
849            "Error during planning: Illegal argument `Int64(5)` in range select query"
850        )
851    }
852
853    #[test]
854    fn test_parse_duration_expr() {
855        // test IntervalYearMonth
856        let interval = IntervalYearMonth::new(10);
857        let args = vec![ScalarValue::IntervalYearMonth(Some(interval.to_i32())).lit()];
858        assert!(parse_duration_expr(&args, 0).is_err(),);
859        // test IntervalDayTime
860        let interval = IntervalDayTime::new(10, 10);
861        let args = vec![ScalarValue::IntervalDayTime(Some(interval.into())).lit()];
862        assert_eq!(
863            parse_duration_expr(&args, 0).unwrap().as_millis() as i64,
864            interval.as_millis()
865        );
866        // test IntervalMonthDayNano
867        let interval = IntervalMonthDayNano::new(0, 10, 10);
868        let args = vec![ScalarValue::IntervalMonthDayNano(Some(interval.into())).lit()];
869        assert_eq!(
870            parse_duration_expr(&args, 0).unwrap().as_millis() as i64,
871            interval.days as i64 * MS_PER_DAY + interval.nanoseconds / NANOS_PER_MILLI,
872        );
873        // test Duration
874        let args = vec!["1y4w".lit()];
875        assert_eq!(
876            parse_duration_expr(&args, 0).unwrap(),
877            parse_duration("1y4w").unwrap()
878        );
879        // test cast expression
880        let args = vec![Expr::Cast(Cast {
881            expr: Box::new("15 minutes".lit()),
882            data_type: DataType::Interval(IntervalUnit::MonthDayNano),
883        })];
884        assert_eq!(
885            parse_duration_expr(&args, 0).unwrap(),
886            parse_duration("15m").unwrap()
887        );
888        // test index err
889        assert!(parse_duration_expr(&args, 10).is_err());
890        // test evaluate expr
891        let args = vec![Expr::BinaryExpr(BinaryExpr {
892            left: Box::new(
893                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(0, 10).into())).lit(),
894            ),
895            op: Operator::Plus,
896            right: Box::new(
897                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(0, 10).into())).lit(),
898            ),
899        })];
900        assert_eq!(
901            parse_duration_expr(&args, 0).unwrap(),
902            Duration::from_millis(20)
903        );
904        let args = vec![Expr::BinaryExpr(BinaryExpr {
905            left: Box::new(
906                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(0, 10).into())).lit(),
907            ),
908            op: Operator::Minus,
909            right: Box::new(
910                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(0, 10).into())).lit(),
911            ),
912        })];
913        // test zero interval error
914        assert!(parse_duration_expr(&args, 0).is_err());
915        // test must all be interval
916        let args = vec![Expr::BinaryExpr(BinaryExpr {
917            left: Box::new(
918                ScalarValue::IntervalYearMonth(Some(IntervalYearMonth::new(10).to_i32())).lit(),
919            ),
920            op: Operator::Minus,
921            right: Box::new(ScalarValue::Time64Microsecond(Some(0)).lit()),
922        })];
923        assert!(parse_duration_expr(&args, 0).is_err());
924    }
925
926    #[test]
927    fn test_parse_align_to() {
928        // test NOW
929        let args = vec!["NOW".lit()];
930        let epsinon = parse_align_to(&args, 0, None).unwrap() - Timestamp::current_millis().value();
931        assert!(epsinon.abs() < 100);
932        // test default
933        let args = vec!["".lit()];
934        assert_eq!(0, parse_align_to(&args, 0, None).unwrap());
935        // test default with timezone
936        let args = vec!["".lit()];
937        assert_eq!(
938            -36000 * 1000,
939            parse_align_to(&args, 0, Some(&Timezone::from_tz_string("HST").unwrap())).unwrap()
940        );
941        assert_eq!(
942            28800 * 1000,
943            parse_align_to(
944                &args,
945                0,
946                Some(&Timezone::from_tz_string("Asia/Shanghai").unwrap())
947            )
948            .unwrap()
949        );
950
951        // test Timestamp
952        let args = vec!["1970-01-01T00:00:00+08:00".lit()];
953        assert_eq!(parse_align_to(&args, 0, None).unwrap(), -8 * 60 * 60 * 1000);
954        // timezone
955        let args = vec!["1970-01-01T00:00:00".lit()];
956        assert_eq!(
957            parse_align_to(
958                &args,
959                0,
960                Some(&Timezone::from_tz_string("Asia/Shanghai").unwrap())
961            )
962            .unwrap(),
963            -8 * 60 * 60 * 1000
964        );
965        // test evaluate expr
966        let args = vec![Expr::BinaryExpr(BinaryExpr {
967            left: Box::new(
968                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(0, 10).into())).lit(),
969            ),
970            op: Operator::Plus,
971            right: Box::new(
972                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(0, 10).into())).lit(),
973            ),
974        })];
975        assert_eq!(parse_align_to(&args, 0, None).unwrap(), 20);
976    }
977
978    #[test]
979    fn test_interval_only() {
980        let expr = Expr::BinaryExpr(BinaryExpr {
981            left: Box::new(ScalarValue::DurationMillisecond(Some(20)).lit()),
982            op: Operator::Minus,
983            right: Box::new(
984                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(10, 0).into())).lit(),
985            ),
986        });
987        assert!(!interval_only_in_expr(&expr));
988        let expr = Expr::BinaryExpr(BinaryExpr {
989            left: Box::new(
990                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(10, 0).into())).lit(),
991            ),
992            op: Operator::Minus,
993            right: Box::new(
994                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(10, 0).into())).lit(),
995            ),
996        });
997        assert!(interval_only_in_expr(&expr));
998
999        let expr = Expr::BinaryExpr(BinaryExpr {
1000            left: Box::new(Expr::Cast(Cast {
1001                expr: Box::new("15 minute".lit()),
1002                data_type: DataType::Interval(IntervalUnit::MonthDayNano),
1003            })),
1004            op: Operator::Minus,
1005            right: Box::new(
1006                ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(10, 0).into())).lit(),
1007            ),
1008        });
1009        assert!(interval_only_in_expr(&expr));
1010
1011        let expr = Expr::Cast(Cast {
1012            expr: Box::new(Expr::BinaryExpr(BinaryExpr {
1013                left: Box::new(Expr::Cast(Cast {
1014                    expr: Box::new("15 minute".lit()),
1015                    data_type: DataType::Interval(IntervalUnit::MonthDayNano),
1016                })),
1017                op: Operator::Minus,
1018                right: Box::new(
1019                    ScalarValue::IntervalDayTime(Some(IntervalDayTime::new(10, 0).into())).lit(),
1020                ),
1021            })),
1022            data_type: DataType::Interval(IntervalUnit::MonthDayNano),
1023        });
1024
1025        assert!(interval_only_in_expr(&expr));
1026    }
1027}