Skip to content

Commit 228a24e

Browse files
author
B Vadlamani
committed
schema_changes_utils
1 parent 91f202a commit 228a24e

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

  • datafusion/spark/src/function/conversion

datafusion/spark/src/function/conversion/cast.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ use datafusion_common::utils::take_function_args;
2424
use datafusion_common::{
2525
Result as DataFusionResult, ScalarValue, exec_err, internal_err,
2626
};
27-
use datafusion_expr::{
28-
ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, Signature,
29-
TypeSignature, Volatility,
30-
};
27+
use datafusion_expr::{ColumnarValue, Expr, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility};
3128
use std::any::Any;
3229
use std::sync::Arc;
30+
use datafusion::logical_expr::{Coercion, TypeSignatureClass};
31+
use datafusion_common::types::{logical_int64, logical_string};
32+
use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext};
3333

3434
const MICROS_PER_SECOND: i64 = 1_000_000;
3535

@@ -73,12 +73,14 @@ impl Default for SparkCast {
7373

7474
impl SparkCast {
7575
pub fn new() -> Self {
76+
// First arg: value to cast (only ints for now with potential to add further support later)
77+
// Second arg: target datatype as Utf8 string literal (ex : 'timestamp')
78+
let int_arg = Coercion::new_exact(TypeSignatureClass::Native(logical_int64()));
79+
let string_arg = Coercion::new_exact(TypeSignatureClass::Native(logical_string()));
7680
Self {
77-
// First arg: value to cast (only ints for now with potential to add further support later)
78-
// Second arg: target datatype as Utf8 string literal (ex : 'timestamp')
7981
signature: Signature::one_of(
80-
vec![TypeSignature::Any(2)],
81-
Volatility::Immutable,
82+
vec![TypeSignature::Coercible(vec![int_arg, string_arg])],
83+
Volatility::Stable,
8284
),
8385
}
8486
}
@@ -152,15 +154,6 @@ impl ScalarUDFImpl for SparkCast {
152154
internal_err!("return_field_from_args should be used instead")
153155
}
154156

155-
fn return_field_from_args(
156-
&self,
157-
args: ReturnFieldArgs,
158-
) -> DataFusionResult<FieldRef> {
159-
let nullable = args.arg_fields.iter().any(|f| f.is_nullable());
160-
let target_type = get_target_type_from_scalar_args(args.scalar_arguments)?;
161-
Ok(Arc::new(Field::new(self.name(), target_type, nullable)))
162-
}
163-
164157
fn invoke_with_args(
165158
&self,
166159
args: ScalarFunctionArgs,
@@ -176,7 +169,7 @@ impl ScalarUDFImpl for SparkCast {
176169
.unwrap_or_else(|| Arc::from("UTC"));
177170

178171
match target_type {
179-
DataType::Timestamp(TimeUnit::Microsecond, None) => {
172+
DataType::Timestamp(TimeUnit::Microsecond, _) => {
180173
cast_to_timestamp(&args.args[0], Some(session_tz))
181174
}
182175
other => exec_err!("Unsupported spark_cast target type: {:?}", other),

0 commit comments

Comments
 (0)