jayzhan211 commented on code in PR #9610:
URL: https://github.com/apache/arrow-datafusion/pull/9610#discussion_r1528423492
##########
datafusion/functions/src/core/arrow_cast.rs:
##########
@@ -15,63 +15,123 @@
// specific language governing permissions and limitations
// under the License.
-//! Implementation of the `arrow_cast` function that allows
-//! casting to arbitrary arrow types (rather than SQL types)
+//! [`ArrowCastFunc`]: Implementation of the `arrow_cast`
+use std::any::Any;
use std::{fmt::Display, iter::Peekable, str::Chars, sync::Arc};
use arrow_schema::{DataType, Field, IntervalUnit, TimeUnit};
use datafusion_common::{
- plan_datafusion_err, DFSchema, DataFusionError, Result, ScalarValue,
+ internal_err, plan_datafusion_err, plan_err, DataFusionError, ExprSchema,
Result,
+ ScalarValue,
};
-use datafusion_common::plan_err;
-use datafusion_expr::{Expr, ExprSchemable};
+use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};
+use datafusion_expr::{ColumnarValue, Expr, ScalarUDFImpl, Signature,
Volatility};
-pub const ARROW_CAST_NAME: &str = "arrow_cast";
-
-/// Create an [`Expr`] that evaluates the `arrow_cast` function
+/// Implements casting to arbitrary arrow types (rather than SQL types)
+///
+/// Note that the `arrow_cast` function is somewhat special in that its
+/// return depends only on the *value* of its second argument (not its type)
///
-/// This function is not a [`BuiltinScalarFunction`] because the
-/// return type of [`BuiltinScalarFunction`] depends only on the
-/// *types* of the arguments. However, the type of `arrow_type` depends on
-/// the *value* of its second argument.
+/// It is implemented by calling the same underlying arrow `cast` kernel as
+/// normal SQL casts.
///
-/// Use the `cast` function to cast to SQL type (which is then mapped
-/// to the corresponding arrow type). For example to cast to `int`
-/// (which is then mapped to the arrow type `Int32`)
+/// For example to cast to `int` using SQL (which is then mapped to the arrow
+/// type `Int32`)
///
/// ```sql
/// select cast(column_x as int) ...
/// ```
///
-/// Use the `arrow_cast` functiont to cast to a specfic arrow type
+/// You can use the `arrow_cast` functiont to cast to a specific arrow type
///
/// For example
/// ```sql
/// select arrow_cast(column_x, 'Float64')
/// ```
-/// [`BuiltinScalarFunction`]: datafusion_expr::BuiltinScalarFunction
-pub fn create_arrow_cast(mut args: Vec<Expr>, schema: &DFSchema) ->
Result<Expr> {
+#[derive(Debug)]
+pub(super) struct ArrowCastFunc {
+ signature: Signature,
+}
+
+impl ArrowCastFunc {
+ pub fn new() -> Self {
+ Self {
+ signature: Signature::any(2, Volatility::Immutable),
+ }
+ }
+}
+
+impl ScalarUDFImpl for ArrowCastFunc {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn name(&self) -> &str {
+ "arrow_cast"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+ parse_data_type(&arg_types[1].to_string())
Review Comment:
If `return_type_from_exprs` exists, we don't need `return_type`
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]