You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "findepi (via GitHub)" <gi...@apache.org> on 2024/11/05 10:15:24 UTC

Re: [PR] Use LogicalType for TypeSignature `Numeric` and `String`, `Coercible` and introduce `NumericAndNumericString` [datafusion]

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


##########
datafusion/expr-common/src/signature.rs:
##########
@@ -123,8 +124,19 @@ pub enum TypeSignature {
     /// Specifies Signatures for array functions
     ArraySignature(ArrayFunctionSignature),
     /// Fixed number of arguments of numeric types.
-    /// See <https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html#method.is_numeric> to know which type is considered numeric
+    /// See [`NativeType::is_numeric`] to know which type is considered numeric
+    ///
+    /// [`NativeType::is_numeric`]: datafusion_common
     Numeric(usize),
+    /// Fixed number of arguments of numeric types.
+    /// See [`NativeType::is_numeric`] to know which type is considered numeric
+    /// This signature accepts numeric string
+    /// Example of functions In Postgres that support numeric string
+    /// 1. Mathematical Functions, like `abs`

Review Comment:
   i confirm this works in PostgreSQL (was testing with PostgreSQL v 17)
   ```sql
   select abs('-123');
   ```
   https://www.postgresql.org/docs/17/typeconv-oper.html#id-1.5.9.7.8 suggests why this works
   '...' is not a varchar literal in PostgreSQL.
   it's "unknown-type" literal, which gets interpreted as float8
   
   > Here the system has implicitly resolved the unknown-type literal as type float8 before applying the chosen operator.
   
   indeed, `select pg_typeof(abs('-123'));` returns `double precision`
   
   
   However, this doesn't work in PostgreSQL
   ```sql
   select abs(CAST('-123' AS varchar));
   ```
   
   This fails with "Query Error: function abs(character varying) does not exist"
   Indicating that there is no special coercion rules from varchar to numbers when calling functions like `abs()`.
   
   -----------
   
   BTW, coercion rules can be retrieved from PostgreSQL from https://www.postgresql.org/docs/17/catalog-pg-cast.html
   
   PostgreSQL doesn't declare any implicit coercions between (selected) numeric types and (selected) varchar type, and `abs()` function resolution behavior as described above matches that.
   
   ```sql
   with selected_types(name) AS (VALUES ('int4'), ('int8'), ('float8'), ('varchar'))
   select 
   	src.typname src_type,
       dst.typname dst_type,
       castcontext,
       case castcontext
       	when 'e' then 'only explicit'
           when 'a' then 'explicit | implicitly in assignment'
           when 'i' then 'implicitly in expressions, as well as the other cases'
           else '???' -- undocumented @ https://www.postgresql.org/docs/17/catalog-pg-cast.html
       end castcontext_explained
   from pg_cast
   join pg_type src on pg_cast.castsource = src.oid
   join pg_type dst on pg_cast.casttarget = dst.oid
   where true
   and src.oid != dst.oid
   and src.typname in (select name from selected_types)
   and dst.typname in (select name from selected_types)
   order by src.typname, dst.typname
   ;
   ```
   
   
   src_type | dst_type | castcontext | castcontext_explained
   -- | -- | -- | --
   float8 | int4 | a | explicit \| implicitly in assignment
   float8 | int8 | a | explicit \| implicitly in assignment
   int4 | float8 | i | implicitly in expressions, as well as the other cases
   int4 | int8 | i | implicitly in expressions, as well as the other cases
   int8 | float8 | i | implicitly in expressions, as well as the other cases
   int8 | int4 | a | explicit \| implicitly in assignment
   
   



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

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


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