datafusion_spark/function/datetime/
date_part.rs1use arrow::datatypes::{DataType, Field, FieldRef};
19use datafusion_common::types::logical_date;
20use datafusion_common::{
21 Result, ScalarValue, internal_err, types::logical_string, utils::take_function_args,
22};
23use datafusion_expr::expr::ScalarFunction;
24use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext};
25use datafusion_expr::{
26 Coercion, ColumnarValue, Expr, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl,
27 Signature, TypeSignature, TypeSignatureClass, Volatility,
28};
29use std::sync::Arc;
30
31#[derive(Debug, PartialEq, Eq, Hash)]
35pub struct SparkDatePart {
36 signature: Signature,
37 aliases: Vec<String>,
38}
39
40impl Default for SparkDatePart {
41 fn default() -> Self {
42 Self::new()
43 }
44}
45
46impl SparkDatePart {
47 pub fn new() -> Self {
48 Self {
49 signature: Signature::one_of(
50 vec![
51 TypeSignature::Coercible(vec![
52 Coercion::new_exact(TypeSignatureClass::Native(logical_string())),
53 Coercion::new_exact(TypeSignatureClass::Timestamp),
54 ]),
55 TypeSignature::Coercible(vec![
56 Coercion::new_exact(TypeSignatureClass::Native(logical_string())),
57 Coercion::new_exact(TypeSignatureClass::Native(logical_date())),
58 ]),
59 ],
60 Volatility::Immutable,
61 ),
62 aliases: vec![String::from("datepart")],
63 }
64 }
65}
66
67impl ScalarUDFImpl for SparkDatePart {
68 fn name(&self) -> &str {
69 "date_part"
70 }
71
72 fn aliases(&self) -> &[String] {
73 &self.aliases
74 }
75
76 fn signature(&self) -> &Signature {
77 &self.signature
78 }
79
80 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
81 internal_err!("Use return_field_from_args in this case instead.")
82 }
83
84 fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
85 let nullable = args.arg_fields.iter().any(|f| f.is_nullable());
86
87 Ok(Arc::new(Field::new(self.name(), DataType::Int32, nullable)))
88 }
89
90 fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
91 internal_err!("spark date_part should have been simplified to standard date_part")
92 }
93
94 fn simplify(
95 &self,
96 args: Vec<Expr>,
97 _info: &SimplifyContext,
98 ) -> Result<ExprSimplifyResult> {
99 let [part_expr, date_expr] = take_function_args(self.name(), args)?;
100
101 let part = match part_expr.as_literal() {
102 Some(ScalarValue::Utf8(Some(v)))
103 | Some(ScalarValue::Utf8View(Some(v)))
104 | Some(ScalarValue::LargeUtf8(Some(v))) => v.to_lowercase(),
105 _ => {
106 return internal_err!(
107 "First argument of `DATE_PART` must be non-null scalar Utf8"
108 );
109 }
110 };
111
112 let part = match part.as_str() {
114 "yearofweek" | "year_iso" => "isoyear",
115 "dayofweek" => "dow",
116 "dayofweek_iso" | "dow_iso" => "isodow",
117 other => other,
118 };
119
120 let part_expr = Expr::Literal(ScalarValue::new_utf8(part), None);
121
122 let date_part_expr = Expr::ScalarFunction(ScalarFunction::new_udf(
123 datafusion_functions::datetime::date_part(),
124 vec![part_expr, date_expr],
125 ));
126
127 match part {
128 "dow" => Ok(ExprSimplifyResult::Simplified(
133 date_part_expr + Expr::Literal(ScalarValue::Int32(Some(1)), None),
134 )),
135 _ => Ok(ExprSimplifyResult::Simplified(date_part_expr)),
136 }
137 }
138}