Skip to main content

datafusion_spark/function/
error_utils.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18// TODO: https://github.com/apache/spark/tree/master/common/utils/src/main/resources/error
19
20use arrow::datatypes::DataType;
21use datafusion_common::{DataFusionError, exec_datafusion_err, internal_datafusion_err};
22
23pub fn invalid_arg_count_exec_err(
24    function_name: &str,
25    required_range: (i32, i32),
26    provided: usize,
27) -> DataFusionError {
28    let (min_required, max_required) = required_range;
29    let required = if min_required == max_required {
30        format!(
31            "{min_required} argument{}",
32            if min_required == 1 { "" } else { "s" }
33        )
34    } else {
35        format!("{min_required} to {max_required} arguments")
36    };
37    exec_datafusion_err!(
38        "Spark `{function_name}` function requires {required}, got {provided}"
39    )
40}
41
42pub fn unsupported_data_type_exec_err(
43    function_name: &str,
44    required: &str,
45    provided: &DataType,
46) -> DataFusionError {
47    exec_datafusion_err!(
48        "Unsupported Data Type: Spark `{function_name}` function expects {required}, got {provided}"
49    )
50}
51
52pub fn unsupported_data_types_exec_err(
53    function_name: &str,
54    required: &str,
55    provided: &[DataType],
56) -> DataFusionError {
57    exec_datafusion_err!(
58        "Unsupported Data Type: Spark `{function_name}` function expects {required}, got {}",
59        provided
60            .iter()
61            .map(|dt| format!("{dt}"))
62            .collect::<Vec<_>>()
63            .join(", ")
64    )
65}
66
67pub fn generic_exec_err(function_name: &str, message: &str) -> DataFusionError {
68    exec_datafusion_err!("Spark `{function_name}` function: {message}")
69}
70
71pub fn generic_internal_err(function_name: &str, message: &str) -> DataFusionError {
72    internal_datafusion_err!("Spark `{function_name}` function: {message}")
73}