Skip to content

Commit 83ed192

Browse files
authored
refactor: Spark ascii signature away from user_defined (#19513)
## 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. --> - Part of #12725 ## Rationale for this change As per the goal stated that we should avoid using the `user_defined` in useful places. <!-- 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. --> ## What changes are included in this PR? - Refactored Spark `ascii` to not use `user_defined`. - Checked the Spark code to make sure it follows the same convention <!-- 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. --> ## Are these changes tested? - This is a refactoring so no new tests were added but all previous test pass. <!-- 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)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent a95c7fc commit 83ed192

File tree

1 file changed

+15
-6
lines changed
  • datafusion/spark/src/function/string

1 file changed

+15
-6
lines changed

datafusion/spark/src/function/string/ascii.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
use arrow::datatypes::DataType;
1919
use datafusion_common::Result;
20+
use datafusion_common::types::{NativeType, logical_string};
2021
use datafusion_expr::ColumnarValue;
21-
use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
22+
use datafusion_expr::{
23+
Coercion, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignatureClass,
24+
Volatility,
25+
};
2226
use datafusion_functions::string::ascii::ascii;
2327
use datafusion_functions::utils::make_scalar_function;
2428
use std::any::Any;
@@ -42,8 +46,17 @@ impl Default for SparkAscii {
4246

4347
impl SparkAscii {
4448
pub fn new() -> Self {
49+
// Spark's ascii uses ImplicitCastInputTypes with StringType,
50+
// which allows numeric types to be implicitly cast to String.
51+
// See: https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
52+
let string_coercion = Coercion::new_implicit(
53+
TypeSignatureClass::Native(logical_string()),
54+
vec![TypeSignatureClass::Numeric],
55+
NativeType::String,
56+
);
57+
4558
Self {
46-
signature: Signature::user_defined(Volatility::Immutable),
59+
signature: Signature::coercible(vec![string_coercion], Volatility::Immutable),
4760
}
4861
}
4962
}
@@ -68,8 +81,4 @@ impl ScalarUDFImpl for SparkAscii {
6881
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
6982
make_scalar_function(ascii, vec![])(&args.args)
7083
}
71-
72-
fn coerce_types(&self, _arg_types: &[DataType]) -> Result<Vec<DataType>> {
73-
Ok(vec![DataType::Utf8])
74-
}
7584
}

0 commit comments

Comments
 (0)