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 next_day;
30pub mod time_trunc;
31pub mod to_utc_timestamp;
32pub mod trunc;
33pub mod unix;
34
35use datafusion_expr::ScalarUDF;
36use datafusion_functions::make_udf_function;
37use std::sync::Arc;
38
39make_udf_function!(add_months::SparkAddMonths, add_months);
40make_udf_function!(date_add::SparkDateAdd, date_add);
41make_udf_function!(date_diff::SparkDateDiff, date_diff);
42make_udf_function!(date_part::SparkDatePart, date_part);
43make_udf_function!(date_sub::SparkDateSub, date_sub);
44make_udf_function!(date_trunc::SparkDateTrunc, date_trunc);
45make_udf_function!(
46    from_utc_timestamp::SparkFromUtcTimestamp,
47    from_utc_timestamp
48);
49make_udf_function!(extract::SparkHour, hour);
50make_udf_function!(extract::SparkMinute, minute);
51make_udf_function!(extract::SparkSecond, second);
52make_udf_function!(last_day::SparkLastDay, last_day);
53make_udf_function!(make_dt_interval::SparkMakeDtInterval, make_dt_interval);
54make_udf_function!(make_interval::SparkMakeInterval, make_interval);
55make_udf_function!(next_day::SparkNextDay, next_day);
56make_udf_function!(time_trunc::SparkTimeTrunc, time_trunc);
57make_udf_function!(to_utc_timestamp::SparkToUtcTimestamp, to_utc_timestamp);
58make_udf_function!(trunc::SparkTrunc, trunc);
59make_udf_function!(unix::SparkUnixDate, unix_date);
60make_udf_function!(
61    unix::SparkUnixTimestamp,
62    unix_micros,
63    unix::SparkUnixTimestamp::microseconds
64);
65make_udf_function!(
66    unix::SparkUnixTimestamp,
67    unix_millis,
68    unix::SparkUnixTimestamp::milliseconds
69);
70make_udf_function!(
71    unix::SparkUnixTimestamp,
72    unix_seconds,
73    unix::SparkUnixTimestamp::seconds
74);
75
76pub mod expr_fn {
77    use datafusion_functions::export_functions;
78
79    export_functions!((
80        add_months,
81        "Returns the date that is months months after start. The function returns NULL if at least one of the input parameters is NULL.",
82        arg1 arg2
83    ));
84    export_functions!((
85        date_add,
86        "Returns the date that is days days after start. The function returns NULL if at least one of the input parameters is NULL.",
87        arg1 arg2
88    ));
89    export_functions!((
90        date_sub,
91        "Returns the date that is days days before start. The function returns NULL if at least one of the input parameters is NULL.",
92        arg1 arg2
93    ));
94    export_functions!((hour, "Extracts the hour component of a timestamp.", arg1));
95    export_functions!((
96        minute,
97        "Extracts the minute component of a timestamp.",
98        arg1
99    ));
100    export_functions!((
101        second,
102        "Extracts the second component of a timestamp.",
103        arg1
104    ));
105    export_functions!((
106        last_day,
107        "Returns the last day of the month which the date belongs to.",
108        arg1
109    ));
110    export_functions!((
111        make_dt_interval,
112        "Make a day time interval from given days, hours, mins and secs (return type is actually a Duration(Microsecond))",
113         days hours mins secs
114    ));
115    export_functions!((
116        make_interval,
117        "Make interval from years, months, weeks, days, hours, mins and secs.",
118        years months weeks days hours mins secs
119    ));
120    // TODO: add once ANSI support is added:
121    // "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."
122    export_functions!((
123        next_day,
124        "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.",
125        arg1 arg2
126    ));
127    export_functions!((
128        date_diff,
129        "Returns the number of days from start `start` to end `end`.",
130        end start
131    ));
132    export_functions!((
133        date_trunc,
134        "Truncates a timestamp `ts` to the unit specified by the format `fmt`.",
135        fmt ts
136    ));
137    export_functions!((
138        time_trunc,
139        "Truncates a time `t` to the unit specified by the format `fmt`.",
140        fmt t
141    ));
142    export_functions!((
143        trunc,
144        "Truncates a date `dt` to the unit specified by the format `fmt`.",
145        dt fmt
146    ));
147    export_functions!((
148        date_part,
149        "Extracts a part of the date or time from a date, time, or timestamp expression.",
150        arg1 arg2
151    ));
152    export_functions!((
153        from_utc_timestamp,
154        "Interpret a given timestamp `ts` in UTC timezone and then convert it to timezone `tz`.",
155        ts tz
156    ));
157    export_functions!((
158        to_utc_timestamp,
159        "Interpret a given timestamp `ts` in timezone `tz` and then convert it to UTC timezone.",
160        ts tz
161    ));
162    export_functions!((
163        unix_date,
164        "Returns the number of days since epoch (1970-01-01) for the given date `dt`.",
165        dt
166    ));
167    export_functions!((
168        unix_micros,
169        "Returns the number of microseconds since epoch (1970-01-01 00:00:00 UTC) for the given timestamp `ts`.",
170        ts
171    ));
172    export_functions!((
173        unix_millis,
174        "Returns the number of milliseconds since epoch (1970-01-01 00:00:00 UTC) for the given timestamp `ts`.",
175        ts
176    ));
177    export_functions!((
178        unix_seconds,
179        "Returns the number of seconds since epoch (1970-01-01 00:00:00 UTC) for the given timestamp `ts`.",
180        ts
181    ));
182}
183
184pub fn functions() -> Vec<Arc<ScalarUDF>> {
185    vec![
186        add_months(),
187        date_add(),
188        date_diff(),
189        date_part(),
190        date_sub(),
191        date_trunc(),
192        from_utc_timestamp(),
193        hour(),
194        last_day(),
195        make_dt_interval(),
196        make_interval(),
197        minute(),
198        next_day(),
199        second(),
200        time_trunc(),
201        to_utc_timestamp(),
202        trunc(),
203        unix_date(),
204        unix_micros(),
205        unix_millis(),
206        unix_seconds(),
207    ]
208}