This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 13f38435a2 Introduce `TypeSignatureClass::Any` (#19485)
13f38435a2 is described below
commit 13f38435a299931584371509cccb6aaf9fbef3a3
Author: Jeffrey Vo <[email protected]>
AuthorDate: Tue Dec 30 12:54:41 2025 +0900
Introduce `TypeSignatureClass::Any` (#19485)
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->
- Closes #19438
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
See issue.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
Add new `TypeSignatureClass` variant `Any` and refactor `arrow_typeof`
and `arrow_metadata` function signatures to use this.
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Existing tests.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
No.
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
---
datafusion/expr-common/src/signature.rs | 7 ++++++
datafusion/functions/src/core/arrow_cast.rs | 13 +++++++---
datafusion/functions/src/core/arrow_metadata.rs | 28 +++++++++++++++-------
.../sqllogictest/test_files/arrow_typeof.slt | 5 +++-
docs/source/user-guide/sql/scalar_functions.md | 2 +-
5 files changed, 42 insertions(+), 13 deletions(-)
diff --git a/datafusion/expr-common/src/signature.rs
b/datafusion/expr-common/src/signature.rs
index 8893d63943..54bb84f03d 100644
--- a/datafusion/expr-common/src/signature.rs
+++ b/datafusion/expr-common/src/signature.rs
@@ -332,6 +332,8 @@ impl TypeSignature {
/// arguments that can be coerced to a particular class of types.
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash)]
pub enum TypeSignatureClass {
+ /// Allows an arbitrary type argument without coercing the argument.
+ Any,
/// Timestamps, allowing arbitrary (or no) timezones
Timestamp,
/// All time types
@@ -367,6 +369,9 @@ impl TypeSignatureClass {
/// documentation or error messages.
fn get_example_types(&self) -> Vec<DataType> {
match self {
+ // TODO: might be too much info to return every single type here
+ // maybe https://github.com/apache/datafusion/issues/14761
will help here?
+ TypeSignatureClass::Any => vec![],
TypeSignatureClass::Native(l) => get_data_types(l.native()),
TypeSignatureClass::Timestamp => {
vec![
@@ -409,6 +414,7 @@ impl TypeSignatureClass {
}
match self {
+ TypeSignatureClass::Any => true,
TypeSignatureClass::Native(t) if t.native() == logical_type =>
true,
TypeSignatureClass::Timestamp if logical_type.is_timestamp() =>
true,
TypeSignatureClass::Time if logical_type.is_time() => true,
@@ -430,6 +436,7 @@ impl TypeSignatureClass {
origin_type: &DataType,
) -> Result<DataType> {
match self {
+ TypeSignatureClass::Any => Ok(origin_type.to_owned()),
TypeSignatureClass::Native(logical_type) => {
logical_type.native().default_cast_for(origin_type)
}
diff --git a/datafusion/functions/src/core/arrow_cast.rs
b/datafusion/functions/src/core/arrow_cast.rs
index a0101dc09d..04189c0c6f 100644
--- a/datafusion/functions/src/core/arrow_cast.rs
+++ b/datafusion/functions/src/core/arrow_cast.rs
@@ -19,6 +19,7 @@
use arrow::datatypes::{DataType, Field, FieldRef};
use arrow::error::ArrowError;
+use datafusion_common::types::logical_string;
use datafusion_common::{
Result, ScalarValue, arrow_datafusion_err, exec_err, internal_err,
};
@@ -27,8 +28,8 @@ use std::any::Any;
use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};
use datafusion_expr::{
- ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarFunctionArgs,
- ScalarUDFImpl, Signature, Volatility,
+ Coercion, ColumnarValue, Documentation, Expr, ReturnFieldArgs,
ScalarFunctionArgs,
+ ScalarUDFImpl, Signature, TypeSignatureClass, Volatility,
};
use datafusion_macros::user_doc;
@@ -102,7 +103,13 @@ impl Default for ArrowCastFunc {
impl ArrowCastFunc {
pub fn new() -> Self {
Self {
- signature: Signature::any(2, Volatility::Immutable),
+ signature: Signature::coercible(
+ vec![
+ Coercion::new_exact(TypeSignatureClass::Any),
+
Coercion::new_exact(TypeSignatureClass::Native(logical_string())),
+ ],
+ Volatility::Immutable,
+ ),
}
}
}
diff --git a/datafusion/functions/src/core/arrow_metadata.rs
b/datafusion/functions/src/core/arrow_metadata.rs
index 92873889b0..86a6d8c21e 100644
--- a/datafusion/functions/src/core/arrow_metadata.rs
+++ b/datafusion/functions/src/core/arrow_metadata.rs
@@ -17,10 +17,11 @@
use arrow::array::{MapBuilder, StringBuilder};
use arrow::datatypes::{DataType, Field, Fields};
-use datafusion_common::{Result, ScalarValue, exec_err};
+use datafusion_common::types::logical_string;
+use datafusion_common::{Result, ScalarValue, exec_err, internal_err};
use datafusion_expr::{
- ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
- Volatility,
+ Coercion, ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl,
Signature,
+ TypeSignature, TypeSignatureClass, Volatility,
};
use datafusion_macros::user_doc;
use std::any::Any;
@@ -29,7 +30,7 @@ use std::sync::Arc;
#[user_doc(
doc_section(label = "Other Functions"),
description = "Returns the metadata of the input expression. If a key is
provided, returns the value for that key. If no key is provided, returns a Map
of all metadata.",
- syntax_example = "arrow_metadata(expression, [key])",
+ syntax_example = "arrow_metadata(expression[, key])",
sql_example = r#"```sql
> select arrow_metadata(col) from table;
+----------------------------+
@@ -61,7 +62,18 @@ pub struct ArrowMetadataFunc {
impl ArrowMetadataFunc {
pub fn new() -> Self {
Self {
- signature: Signature::variadic_any(Volatility::Immutable),
+ signature: Signature::one_of(
+ vec![
+ TypeSignature::Coercible(vec![Coercion::new_exact(
+ TypeSignatureClass::Any,
+ )]),
+ TypeSignature::Coercible(vec![
+ Coercion::new_exact(TypeSignatureClass::Any),
+
Coercion::new_exact(TypeSignatureClass::Native(logical_string())),
+ ]),
+ ],
+ Volatility::Immutable,
+ ),
}
}
}
@@ -105,7 +117,7 @@ impl ScalarUDFImpl for ArrowMetadataFunc {
false,
))
} else {
- exec_err!("arrow_metadata requires 1 or 2 arguments")
+ internal_err!("arrow_metadata requires 1 or 2 arguments")
}
}
@@ -114,7 +126,7 @@ impl ScalarUDFImpl for ArrowMetadataFunc {
if args.args.len() == 2 {
let key = match &args.args[1] {
- ColumnarValue::Scalar(ScalarValue::Utf8(Some(k))) => k,
+ ColumnarValue::Scalar(ScalarValue::Utf8(Some(key))) => key,
_ => {
return exec_err!(
"Second argument to arrow_metadata must be a string
literal key"
@@ -142,7 +154,7 @@ impl ScalarUDFImpl for ArrowMetadataFunc {
&map_array, 0,
)?))
} else {
- exec_err!("arrow_metadata requires 1 or 2 arguments")
+ internal_err!("arrow_metadata requires 1 or 2 arguments")
}
}
}
diff --git a/datafusion/sqllogictest/test_files/arrow_typeof.slt
b/datafusion/sqllogictest/test_files/arrow_typeof.slt
index c213f2abf7..ee1f204664 100644
--- a/datafusion/sqllogictest/test_files/arrow_typeof.slt
+++ b/datafusion/sqllogictest/test_files/arrow_typeof.slt
@@ -95,9 +95,12 @@ SELECT arrow_cast('1', 'Int16')
query error
SELECT arrow_cast('1')
-query error DataFusion error: Execution error: arrow_cast requires its second
argument to be a non\-empty constant string
+query error Expect TypeSignatureClass::Native\(LogicalType\(Native\(String\),
String\)\) but received NativeType::Int64, DataType: Int64
SELECT arrow_cast('1', 43)
+query error DataFusion error: Execution error: arrow_cast requires its second
argument to be a non\-empty constant string
+SELECT arrow_cast('1', arrow_cast('Utf8', 'Utf8'))
+
query error DataFusion error: Execution error: Unsupported type 'unknown'\.
Must be a supported arrow type name such as 'Int32' or 'Timestamp\(ns\)'\.
Error unknown token: unknown
SELECT arrow_cast('1', 'unknown')
diff --git a/docs/source/user-guide/sql/scalar_functions.md
b/docs/source/user-guide/sql/scalar_functions.md
index cf35b9f3c3..ddf32a5066 100644
--- a/docs/source/user-guide/sql/scalar_functions.md
+++ b/docs/source/user-guide/sql/scalar_functions.md
@@ -5047,7 +5047,7 @@ arrow_cast(expression, datatype)
Returns the metadata of the input expression. If a key is provided, returns
the value for that key. If no key is provided, returns a Map of all metadata.
```sql
-arrow_metadata(expression, [key])
+arrow_metadata(expression[, key])
```
#### Arguments
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]