Skip to main content

datafusion_spark/function/datetime/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18pub mod add_months;
19pub mod date_add;
20pub mod date_diff;
21pub mod date_part;
22pub mod date_sub;
23pub mod date_trunc;
24pub mod extract;
25pub mod from_utc_timestamp;
26pub mod last_day;
27pub mod make_dt_interval;
28pub mod make_interval;
29pub mod monthname;
30pub mod next_day;
31pub mod time_trunc;
32pub mod to_utc_timestamp;
33pub mod trunc;
34pub mod unix;
35pub mod weekday;
36
37use datafusion_expr::ScalarUDF;
38use datafusion_functions::make_udf_function;
39use std::sync::Arc;
40
41make_udf_function!(add_months::SparkAddMonths, add_months);
42make_udf_function!(date_add::SparkDateAdd, date_add);
43make_udf_function!(date_diff::SparkDateDiff, date_diff);
44make_udf_function!(date_part::SparkDatePart, date_part);
45make_udf_function!(date_sub::SparkDateSub, date_sub);
46make_udf_function!(date_trunc::SparkDateTrunc, date_trunc);
47make_udf_function!(
48    from_utc_timestamp::SparkFromUtcTimestamp,
49    from_utc_timestamp
50);
51make_udf_function!(extract::SparkHour, hour);
52make_udf_function!(extract::SparkMinute, minute);
53make_udf_function!(extract::SparkSecond, second);
54make_udf_function!(last_day::SparkLastDay, last_day);
55make_udf_function!(make_dt_interval::SparkMakeDtInterval, make_dt_interval);
56make_udf_function!(make_interval::SparkMakeInterval, make_interval);
57make_udf_function!(monthname::SparkMonthName, monthname);
58make_udf_function!(next_day::SparkNextDay, next_day);
59make_udf_function!(time_trunc::SparkTimeTrunc, time_trunc);
60make_udf_function!(to_utc_timestamp::SparkToUtcTimestamp, to_utc_timestamp);
61make_udf_function!(trunc::SparkTrunc, trunc);
62make_udf_function!(unix::SparkUnixDate, unix_date);
63make_udf_function!(weekday::SparkWeekDay, weekday);
64make_udf_function!(
65    unix::SparkUnixTimestamp,
66    unix_micros,
67    unix::SparkUnixTimestamp::microseconds
68);
69make_udf_function!(
70    unix::SparkUnixTimestamp,
71    unix_millis,
72    unix::SparkUnixTimestamp::milliseconds
73);
74make_udf_function!(
75    unix::SparkUnixTimestamp,
76    unix_seconds,
77    unix::SparkUnixTimestamp::seconds
78);
79
80pub mod expr_fn {
81    use datafusion_functions::export_functions;
82
83    export_functions!((
84        add_months,
85        "Returns the date that is months months after start. The function returns NULL if at least one of the input parameters is NULL.",
86        arg1 arg2
87    ));
88    export_functions!((
89        date_add,
90        "Returns the date that is days days after start. The function returns NULL if at least one of the input parameters is NULL.",
91        arg1 arg2
92    ));
93    export_functions!((
94        date_sub,
95        "Returns the date that is days days before start. The function returns NULL if at least one of the input parameters is NULL.",
96        arg1 arg2
97    ));
98    export_functions!((hour, "Extracts the hour component of a timestamp.", arg1));
99    export_functions!((
100        minute,
101        "Extracts the minute component of a timestamp.",
102        arg1
103    ));
104    export_functions!((
105        second,
106        "Extracts the second component of a timestamp.",
107        arg1
108    ));
109    export_functions!((
110        last_day,
111        "Returns the last day of the month which the date belongs to.",
112        arg1
113    ));
114    export_functions!((
115        make_dt_interval,
116        "Make a day time interval from given days, hours, mins and secs (return type is actually a Duration(Microsecond))",
117         days hours mins secs
118    ));
119    export_functions!((
120        make_interval,
121        "Make interval from years, months, weeks, days, hours, mins and secs.",
122        years months weeks days hours mins secs
123    ));
124    export_functions!((
125        monthname,
126        "Returns the three-letter abbreviated month name from a date or timestamp.",
127        arg1
128    ));
129    // TODO: add once ANSI support is added:
130    // "When both of the input parameters are not NULL and day_of_week is an invalid input, the function throws SparkIllegalArgumentException if spark.sql.ansi.enabled is set to true, otherwise NULL."
131    export_functions!((
132        next_day,
133        "Returns the first date which is later than start_date and named as indicated. The function returns NULL if at least one of the input parameters is NULL.",
134        arg1 arg2
135    ));
136    export_functions!((
137        date_diff,
138        "Returns the number of days from start `start` to end `end`.",
139        end start
140    ));
141    export_functions!((
142        date_trunc,
143        "Truncates a timestamp `ts` to the unit specified by the format `fmt`.",
144        fmt ts
145    ));
146    export_functions!((
147        time_trunc,
148        "Truncates a time `t` to the unit specified by the format `fmt`.",
149        fmt t
150    ));
151    export_functions!((
152        trunc,
153        "Truncates a date `dt` to the unit specified by the format `fmt`.",
154        dt fmt
155    ));
156    export_functions!((
157        date_part,
158        "Extracts a part of the date or time from a date, time, or timestamp expression.",
159        arg1 arg2
160    ));
161    export_functions!((
162        from_utc_timestamp,
163        "Interpret a given timestamp `ts` in UTC timezone and then convert it to timezone `tz`.",
164        ts tz
165    ));
166    export_functions!((
167        to_utc_timestamp,
168        "Interpret a given timestamp `ts` in timezone `tz` and then convert it to UTC timezone.",
169        ts tz
170    ));
171    export_functions!((
172        unix_date,
173        "Returns the number of days since epoch (1970-01-01) for the given date `dt`.",
174        dt
175    ));
176    export_functions!((
177        unix_micros,
178        "Returns the number of microseconds since epoch (1970-01-01 00:00:00 UTC) for the given timestamp `ts`.",
179        ts
180    ));
181    export_functions!((
182        unix_millis,
183        "Returns the number of milliseconds since epoch (1970-01-01 00:00:00 UTC) for the given timestamp `ts`.",
184        ts
185    ));
186    export_functions!((
187        unix_seconds,
188        "Returns the number of seconds since epoch (1970-01-01 00:00:00 UTC) for the given timestamp `ts`.",
189        ts
190    ));
191    export_functions!((
192        weekday,
193        "Returns the day of the week for date/timestamp as an integer where Monday = 0, Tuesday = 1, ..., Sunday = 6.",
194        arg1
195    ));
196}
197
198pub fn functions() -> Vec<Arc<ScalarUDF>> {
199    vec![
200        add_months(),
201        date_add(),
202        date_diff(),
203        date_part(),
204        date_sub(),
205        date_trunc(),
206        from_utc_timestamp(),
207        hour(),
208        last_day(),
209        make_dt_interval(),
210        make_interval(),
211        minute(),
212        monthname(),
213        next_day(),
214        second(),
215        time_trunc(),
216        to_utc_timestamp(),
217        trunc(),
218        unix_date(),
219        unix_micros(),
220        unix_millis(),
221        unix_seconds(),
222        weekday(),
223    ]
224}