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::{exec_datafusion_err, internal_datafusion_err, DataFusionError};
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!("Unsupported Data Type: Spark `{function_name}` function expects {required}, got {provided}")
48}
49
50pub fn unsupported_data_types_exec_err(
51    function_name: &str,
52    required: &str,
53    provided: &[DataType],
54) -> DataFusionError {
55    exec_datafusion_err!(
56        "Unsupported Data Type: Spark `{function_name}` function expects {required}, got {}",
57        provided
58            .iter()
59            .map(|dt| format!("{dt}"))
60            .collect::<Vec<_>>()
61            .join(", ")
62    )
63}
64
65pub fn generic_exec_err(function_name: &str, message: &str) -> DataFusionError {
66    exec_datafusion_err!("Spark `{function_name}` function: {message}")
67}
68
69pub fn generic_internal_err(function_name: &str, message: &str) -> DataFusionError {
70    internal_datafusion_err!("Spark `{function_name}` function: {message}")
71}