Skip to main content

mz_sql/plan/
literal.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use mz_repr::adt::interval::Interval;
11use mz_sql_parser::ast::IntervalValue;
12
13use crate::plan::PlanError;
14
15/// Convert an [`IntervalValue`] into an [`Interval`].
16///
17/// The reverse of [`unplan_interval`].
18pub fn plan_interval(iv: &IntervalValue) -> Result<Interval, PlanError> {
19    Ok(Interval::from_literal(
20        &iv.value,
21        parser_datetimefield_to_adt(iv.precision_high),
22        parser_datetimefield_to_adt(iv.precision_low),
23        iv.fsec_max_precision,
24    )?)
25}
26
27/// Convert an [`Interval`] into an [`IntervalValue`].
28///
29/// The reverse of [`plan_interval`].
30pub fn unplan_interval(i: &Interval) -> IntervalValue {
31    let mut iv = IntervalValue::default();
32    iv.value = i.to_string();
33    iv
34}
35
36fn parser_datetimefield_to_adt(
37    dtf: mz_sql_parser::ast::DateTimeField,
38) -> mz_repr::adt::datetime::DateTimeField {
39    use mz_sql_parser::ast::DateTimeField::*;
40    match dtf {
41        Millennium => mz_repr::adt::datetime::DateTimeField::Millennium,
42        Century => mz_repr::adt::datetime::DateTimeField::Century,
43        Decade => mz_repr::adt::datetime::DateTimeField::Decade,
44        Year => mz_repr::adt::datetime::DateTimeField::Year,
45        Month => mz_repr::adt::datetime::DateTimeField::Month,
46        Day => mz_repr::adt::datetime::DateTimeField::Day,
47        Hour => mz_repr::adt::datetime::DateTimeField::Hour,
48        Minute => mz_repr::adt::datetime::DateTimeField::Minute,
49        Second => mz_repr::adt::datetime::DateTimeField::Second,
50        Milliseconds => mz_repr::adt::datetime::DateTimeField::Milliseconds,
51        Microseconds => mz_repr::adt::datetime::DateTimeField::Microseconds,
52    }
53}