alamb commented on code in PR #9298:
URL: https://github.com/apache/arrow-datafusion/pull/9298#discussion_r1497030514
##########
datafusion/functions/src/core/arrow_cast.rs:
##########
@@ -19,16 +19,65 @@
//! casting to arbitrary arrow types (rather than SQL types)
use std::{fmt::Display, iter::Peekable, str::Chars, sync::Arc};
+use std::any::Any;
+use arrow::compute::cast;
use arrow_schema::{DataType, Field, IntervalUnit, TimeUnit};
-use datafusion_common::{
- plan_datafusion_err, DFSchema, DataFusionError, Result, ScalarValue,
-};
+use datafusion_common::{plan_datafusion_err, DataFusionError, Result,
ScalarValue, ExprSchema, plan_err};
-use datafusion_common::plan_err;
-use datafusion_expr::{Expr, ExprSchemable};
+use datafusion_expr::{ColumnarValue, Expr, ScalarUDFImpl, Signature,
Volatility};
-pub const ARROW_CAST_NAME: &str = "arrow_cast";
+#[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())
+ }
+
+ fn return_type_from_exprs(&self, args: &[Expr], _schema: &dyn ExprSchema)
-> Result<DataType> {
+ if args.len() != 2 {
+ return plan_err!("arrow_cast needs 2 arguments, {} provided",
args.len());
+ }
+ let arg1 = args.get(1);
+
+ match arg1 {
+ Some(Expr::Literal(ScalarValue::Utf8(arg1))) => {
+ let data_type = parse_data_type(
arg1.clone().unwrap().as_str())?;
Review Comment:
Can we avoid this `unwrap`? As written I think this will panic if a `null`
is passed in
I think you can do so by matching on `Some`
```suggestion
Some(Expr::Literal(ScalarValue::Utf8(Some(arg1)))) => {
let data_type = parse_data_type(arg1)?;
```
--
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]