common_function/
helper.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::{InvalidInputTypeSnafu, Result};
16use common_query::prelude::{Signature, TypeSignature, Volatility};
17use datatypes::prelude::ConcreteDataType;
18use datatypes::types::cast::cast;
19use datatypes::value::ValueRef;
20use snafu::ResultExt;
21
22/// Create a function signature with oneof signatures of interleaving two arguments.
23pub fn one_of_sigs2(args1: Vec<ConcreteDataType>, args2: Vec<ConcreteDataType>) -> Signature {
24    let mut sigs = Vec::with_capacity(args1.len() * args2.len());
25
26    for arg1 in &args1 {
27        for arg2 in &args2 {
28            sigs.push(TypeSignature::Exact(vec![arg1.clone(), arg2.clone()]));
29        }
30    }
31
32    Signature::one_of(sigs, Volatility::Immutable)
33}
34
35/// Cast a [`ValueRef`] to u64, returns `None` if fails
36pub fn cast_u64(value: &ValueRef) -> Result<Option<u64>> {
37    cast((*value).into(), &ConcreteDataType::uint64_datatype())
38        .context(InvalidInputTypeSnafu {
39            err_msg: format!(
40                "Failed to cast input into uint64, actual type: {:#?}",
41                value.data_type(),
42            ),
43        })
44        .map(|v| v.as_u64())
45}