findepi commented on code in PR #14440:
URL: https://github.com/apache/datafusion/pull/14440#discussion_r1959483130


##########
datafusion/expr-common/src/signature.rs:
##########
@@ -466,6 +551,186 @@ fn get_data_types(native_type: &NativeType) -> 
Vec<DataType> {
     }
 }
 
+/// Represents type coercion rules for function arguments, specifying both the 
desired type
+/// and optional implicit coercion rules for source types.
+///
+/// # Examples
+///
+/// ```
+/// use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
+/// use datafusion_common::types::{NativeType, logical_binary, logical_string};
+///
+/// // Exact coercion that only accepts timestamp types
+/// let exact = Coercion::new_exact(TypeSignatureClass::Timestamp);
+///
+/// // Implicit coercion that accepts string types but can coerce from binary 
types
+/// let implicit = Coercion::new_implicit(
+///     TypeSignatureClass::Native(logical_string()),
+///     vec![TypeSignatureClass::Native(logical_binary())],
+///     NativeType::String
+/// );
+/// ```
+///
+/// There are two variants:
+///
+/// * `Exact` - Only accepts arguments that exactly match the desired type
+/// * `Implicit` - Accepts the desired type and can coerce from specified 
source types
+#[derive(Debug, Clone, Eq, PartialOrd)]
+pub enum Coercion {
+    /// Coercion that only accepts arguments exactly matching the desired type.
+    Exact {
+        /// The required type for the argument
+        desired_type: TypeSignatureClass,
+    },
+
+    /// Coercion that accepts the desired type and can implicitly coerce from 
other types.
+    Implicit {
+        /// The primary desired type for the argument
+        desired_type: TypeSignatureClass,
+        /// Rules for implicit coercion from other types
+        implicit_coercion: ImplicitCoercion,
+    },

Review Comment:
   
   
   An implementor of a function `foo(s) wants to say:
   
   -  `foo(s)` accepts s being "a string" (eg via logical type 
`NativeType::String` or old school `DataType::Utf8`)
   
   so the query `SELECT foo(s)` should succeed when s is a string or s is 
coercible to string.
   
   Neither Coercion option gives that.
   In theory Coercion::Implicit gives that, but alas, it requires the function 
implementor to know the coercion rules of the engine, to enumerate the "allowed 
source types".
   
   
   How is it supposed to be used?
   



##########
datafusion/functions/src/datetime/date_part.rs:
##########
@@ -95,24 +96,29 @@ impl DatePartFunc {
             signature: Signature::one_of(
                 vec![
                     TypeSignature::Coercible(vec![
-                        TypeSignatureClass::Native(logical_string()),
-                        TypeSignatureClass::Timestamp,
+                        
Coercion::new_exact(TypeSignatureClass::Native(logical_string())),

Review Comment:
   
   the function used to accept _anything coercible to_ "logical string"
   now it accepts  _anything that is_ "logical string"
   
   Is this a breaking change?



-- 
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-unsubscr...@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to