You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/05/22 20:34:17 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6415: Improve error message for wrong built-in scalar function signatures.

alamb commented on code in PR #6415:
URL: https://github.com/apache/arrow-datafusion/pull/6415#discussion_r1201009613


##########
datafusion/expr/src/function_err.rs:
##########
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Function_err module enhances frontend error messages for unresolved functions due to incorrect parameters,
+//! by providing the correct function signatures.
+//!
+//! For example, a query like `select round(3.14, 1.1);` would yield:
+//! ```text
+//! Error during planning: No function matches 'round(Float64, Float64)'. You might need to add explicit type casts.
+//!     Candidate functions:
+//!     round(Float64, Int64)
+//!     round(Float32, Int64)
+//!     round(Float64)
+//!     round(Float32)
+//! ```
+
+use crate::function::signature;
+use crate::{BuiltinScalarFunction, TypeSignature};
+use arrow::datatypes::DataType;
+
+impl TypeSignature {
+    fn to_string_repr(&self) -> Vec<String> {
+        match self {
+            TypeSignature::Variadic(types) => {
+                vec![format!("{}, ..", join_types(types, "/"))]
+            }
+            TypeSignature::Uniform(arg_count, valid_types) => {
+                vec![std::iter::repeat(join_types(valid_types, "/"))
+                    .take(*arg_count)
+                    .collect::<Vec<String>>()
+                    .join(", ")]
+            }
+            TypeSignature::Exact(types) => {
+                vec![join_types(types, ", ")]
+            }
+            TypeSignature::Any(arg_count) => {
+                vec![std::iter::repeat("Any")
+                    .take(*arg_count)
+                    .collect::<Vec<&str>>()
+                    .join(", ")]
+            }
+            TypeSignature::VariadicEqual => vec!["T, .., T".to_string()],
+            TypeSignature::VariadicAny => vec!["Any, .., Any".to_string()],
+            TypeSignature::OneOf(sigs) => {
+                sigs.iter().flat_map(|s| s.to_string_repr()).collect()
+            }
+        }
+    }
+}
+
+/// Helper function to join types with specified delimiter.
+fn join_types<T: std::fmt::Debug>(types: &[T], delimiter: &str) -> String {
+    types
+        .iter()
+        .map(|t| format!("{:?}", t))

Review Comment:
   I suggest we use the [`Display` impl ](https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html#impl-Display-for-DataType) here to better follow the Rust convention of `Display` for users and `Debug` for developers. 
   
   ```suggestion
           .map(|t| t.to_string())
   ```
   
   However, under the covers that simply calls the `Debug` impl 🤦 so it won't make any practical difference
   
   https://docs.rs/arrow-schema/40.0.0/src/arrow_schema/datatype.rs.html#307



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org