Skip to main content

mz_expr/scalar/func/impls/
range.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 std::fmt;
11
12use mz_expr_derive::sqlfunc;
13use mz_repr::adt::range::Range;
14use mz_repr::{Datum, RowArena, SqlColumnType, SqlScalarType};
15use serde::{Deserialize, Serialize};
16
17use crate::scalar::func::{LazyUnaryFunc, stringify_datum};
18use crate::{Eval, EvalError};
19
20#[derive(
21    Ord,
22    PartialOrd,
23    Clone,
24    Debug,
25    Eq,
26    PartialEq,
27    Serialize,
28    Deserialize,
29    Hash
30)]
31pub struct CastRangeToString {
32    pub ty: SqlScalarType,
33}
34
35impl LazyUnaryFunc for CastRangeToString {
36    fn eval<'a>(
37        &'a self,
38        datums: &[Datum<'a>],
39        temp_storage: &'a RowArena,
40        a: &'a impl Eval,
41    ) -> Result<Datum<'a>, EvalError> {
42        let a = a.eval(datums, temp_storage)?;
43        if a.is_null() {
44            return Ok(Datum::Null);
45        }
46        let mut buf = String::new();
47        stringify_datum(&mut buf, a, &self.ty)?;
48        Ok(Datum::String(temp_storage.push_string(buf)))
49    }
50
51    fn output_sql_type(&self, input_type: SqlColumnType) -> SqlColumnType {
52        SqlScalarType::String.nullable(input_type.nullable)
53    }
54
55    fn propagates_nulls(&self) -> bool {
56        true
57    }
58
59    fn introduces_nulls(&self) -> bool {
60        false
61    }
62
63    fn preserves_uniqueness(&self) -> bool {
64        true
65    }
66
67    fn inverse(&self) -> Option<crate::UnaryFunc> {
68        // TODO? if typeconv was in expr, we could determine this
69        None
70    }
71
72    fn is_monotone(&self) -> bool {
73        false
74    }
75
76    fn is_eliminable_cast(&self) -> bool {
77        false
78    }
79}
80
81impl fmt::Display for CastRangeToString {
82    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83        f.write_str("rangetostr")
84    }
85}
86
87#[sqlfunc(sqlname = "rangelower", is_monotone = true)]
88fn range_lower<T>(a: Range<T>) -> Option<T> {
89    a.inner.map(|inner| inner.lower.bound).flatten()
90}
91
92#[sqlfunc(sqlname = "rangeupper")]
93fn range_upper<T>(a: Range<T>) -> Option<T> {
94    a.inner.map(|inner| inner.upper.bound).flatten()
95}
96
97#[sqlfunc(sqlname = "range_empty")]
98fn range_empty<T>(a: Range<T>) -> bool {
99    a.inner.is_none()
100}
101
102#[sqlfunc(sqlname = "range_lower_inc")]
103fn range_lower_inc<T>(a: Range<T>) -> bool {
104    match a.inner {
105        None => false,
106        Some(inner) => inner.lower.inclusive,
107    }
108}
109
110#[sqlfunc(sqlname = "range_upper_inc")]
111fn range_upper_inc<T>(a: Range<T>) -> bool {
112    match a.inner {
113        None => false,
114        Some(inner) => inner.upper.inclusive,
115    }
116}
117
118#[sqlfunc(sqlname = "range_lower_inf")]
119fn range_lower_inf<T>(a: Range<T>) -> bool {
120    match a.inner {
121        None => false,
122        Some(inner) => inner.lower.bound.is_none(),
123    }
124}
125
126#[sqlfunc(sqlname = "range_upper_inf")]
127fn range_upper_inf<T>(a: Range<T>) -> bool {
128    match a.inner {
129        None => false,
130        Some(inner) => inner.upper.bound.is_none(),
131    }
132}