This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
feat(core): support to_char format numeric values
#1349
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /// Creates a singleton `ScalarUDF` of the `$UDF` function and a function | ||
| /// named `$NAME` which returns that singleton. | ||
| /// | ||
| /// This is used to ensure creating the list of `ScalarUDF` only happens once. | ||
| #[macro_export] | ||
| macro_rules! make_udf_function { | ||
| ($UDF:ty, $NAME:ident) => { | ||
| #[doc = concat!("Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation of ", stringify!($NAME))] | ||
| pub fn $NAME() -> std::sync::Arc<datafusion::logical_expr::ScalarUDF> { | ||
| // Singleton instance of the function | ||
| static INSTANCE: std::sync::LazyLock< | ||
| std::sync::Arc<datafusion::logical_expr::ScalarUDF>, | ||
| > = std::sync::LazyLock::new(|| { | ||
| std::sync::Arc::new(datafusion::logical_expr::ScalarUDF::new_from_impl( | ||
| <$UDF>::new(), | ||
| )) | ||
| }); | ||
| std::sync::Arc::clone(&INSTANCE) | ||
| } | ||
|
goldmedal marked this conversation as resolved.
|
||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| mod aggregate; | ||
| mod macros; | ||
| mod remote_function; | ||
| mod scalar; | ||
| mod table; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| use std::any::Any; | ||
|
|
||
| use datafusion::arrow::datatypes::DataType; | ||
| use datafusion::common::not_impl_err; | ||
| use datafusion::common::types::{ | ||
| logical_date, logical_float32, logical_float64, logical_string, | ||
| }; | ||
| use datafusion::error::Result; | ||
| use datafusion::logical_expr::TypeSignatureClass; | ||
| use datafusion::logical_expr::{ | ||
| Coercion, ColumnarValue, DocSection, Documentation, ScalarFunctionArgs, | ||
| ScalarUDFImpl, Signature, TypeSignature, Volatility, | ||
| }; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ToCharFunc { | ||
| signature: Signature, | ||
| aliases: Vec<String>, | ||
| doc: Option<Documentation>, | ||
| } | ||
|
|
||
| impl Default for ToCharFunc { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl ToCharFunc { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::one_of(vec![ | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Duration), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Interval), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Native(logical_date())), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Time), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Timestamp), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Integer), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Native(logical_float64())), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Coercible(vec![Coercion::new_exact(TypeSignatureClass::Native(logical_float32())), Coercion::new_exact(TypeSignatureClass::Native(logical_string()))]), | ||
| TypeSignature::Exact(vec![DataType::Decimal128(38, 10), DataType::Utf8]), | ||
| TypeSignature::Exact(vec![DataType::Decimal256(38, 10), DataType::Utf8]), | ||
|
goldmedal marked this conversation as resolved.
|
||
| ], Volatility::Immutable,), | ||
| aliases: vec!["date_format".to_string()], | ||
| doc: Some(Documentation::builder( | ||
| DocSection { include: false, label: "Time and Date Functions", description: None }, | ||
| "Returns a string representation of a date, time, timestamp or duration based on a [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html).", | ||
| "to_char(expression, format)") | ||
| .with_argument("expression", "The date, time, timestamp or duration to format.") | ||
| .with_argument("format", "The [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) string to use for formatting.") | ||
| .build()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for ToCharFunc { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "to_char" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(DataType::Utf8) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| not_impl_err!("to_char is not implemented") | ||
| } | ||
|
|
||
| fn aliases(&self) -> &[String] { | ||
| &self.aliases | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
| self.doc.as_ref() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.