common_function/scalars/expression/
unary.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 common_query::error::{self, Result};
16use datatypes::prelude::*;
17use datatypes::vectors::Helper;
18use snafu::ResultExt;
19
20use crate::scalars::expression::ctx::EvalContext;
21
22pub fn scalar_unary_op<L: Scalar, O: Scalar, F>(
23    l: &VectorRef,
24    f: F,
25    ctx: &mut EvalContext,
26) -> Result<<O as Scalar>::VectorType>
27where
28    F: Fn(Option<L::RefType<'_>>, &mut EvalContext) -> Option<O>,
29{
30    let left = Helper::check_get_scalar::<L>(l).context(error::GetScalarVectorSnafu)?;
31    let it = left.iter_data().map(|a| f(a, ctx));
32    let result = <O as Scalar>::VectorType::from_owned_iterator(it);
33
34    if let Some(error) = ctx.error.take() {
35        return Err(error);
36    }
37
38    Ok(result)
39}