common_function/scalars/geo/
helpers.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
15macro_rules! ensure_columns_len {
16    ($columns:ident) => {
17        snafu::ensure!(
18            $columns.windows(2).all(|c| c[0].len() == c[1].len()),
19            common_query::error::InvalidFuncArgsSnafu {
20                err_msg: "The length of input columns are in different size"
21            }
22        )
23    };
24    ($column_a:ident, $column_b:ident, $($column_n:ident),*) => {
25        snafu::ensure!(
26            {
27                let mut result = $column_a.len() == $column_b.len();
28                $(
29                result = result && ($column_a.len() == $column_n.len());
30                )*
31                result
32            }
33            common_query::error::InvalidFuncArgsSnafu {
34                err_msg: "The length of input columns are in different size"
35            }
36        )
37    };
38}
39
40pub(super) use ensure_columns_len;
41
42macro_rules! ensure_columns_n {
43    ($columns:ident, $n:literal) => {
44        snafu::ensure!(
45            $columns.len() == $n,
46            common_query::error::InvalidFuncArgsSnafu {
47                err_msg: format!(
48                    "The length of arguments is not correct, expect {}, provided : {}",
49                    stringify!($n),
50                    $columns.len()
51                ),
52            }
53        );
54
55        if $n > 1 {
56            ensure_columns_len!($columns);
57        }
58    };
59}
60
61pub(super) use ensure_columns_n;
62
63macro_rules! ensure_and_coerce {
64    ($compare:expr, $coerce:expr) => {{
65        snafu::ensure!(
66            $compare,
67            common_query::error::InvalidFuncArgsSnafu {
68                err_msg: "Argument was outside of acceptable range "
69            }
70        );
71        Ok($coerce)
72    }};
73}
74
75pub(super) use ensure_and_coerce;