Skip to content

Commit 940eec8

Browse files
authored
[Functions] - Add python function binding to functions (apache#73)
1 parent fea91a7 commit 940eec8

1 file changed

Lines changed: 50 additions & 23 deletions

File tree

‎src/functions.rs‎

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ use datafusion_expr::{lit, BuiltinScalarFunction};
2323
use crate::errors;
2424
use crate::expression::PyExpr;
2525

26-
#[pyfunction]
27-
fn array(value: Vec<PyExpr>) -> PyExpr {
28-
PyExpr {
29-
expr: datafusion_expr::array(value.into_iter().map(|x| x.expr).collect::<Vec<_>>()),
30-
}
31-
}
32-
3326
#[pyfunction]
3427
fn in_list(expr: PyExpr, value: Vec<PyExpr>, negated: bool) -> PyExpr {
3528
datafusion_expr::in_list(
@@ -40,22 +33,6 @@ fn in_list(expr: PyExpr, value: Vec<PyExpr>, negated: bool) -> PyExpr {
4033
.into()
4134
}
4235

43-
/// Current date and time
44-
#[pyfunction]
45-
fn now() -> PyExpr {
46-
PyExpr {
47-
expr: datafusion_expr::now(),
48-
}
49-
}
50-
51-
/// Returns a random value in the range 0.0 <= x < 1.0
52-
#[pyfunction]
53-
fn random() -> PyExpr {
54-
PyExpr {
55-
expr: datafusion_expr::random(),
56-
}
57-
}
58-
5936
/// Computes a binary hash of the given data. type is the algorithm to use.
6037
/// Standard algorithms are md5, sha224, sha256, sha384, sha512, blake2s, blake2b, and blake3.
6138
#[pyfunction(value, method)]
@@ -136,6 +113,7 @@ macro_rules! scalar_function {
136113
($NAME: ident, $FUNC: ident) => {
137114
scalar_function!($NAME, $FUNC, stringify!($NAME));
138115
};
116+
139117
($NAME: ident, $FUNC: ident, $DOC: expr) => {
140118
#[doc = $DOC]
141119
#[pyfunction(args = "*")]
@@ -173,6 +151,7 @@ scalar_function!(acos, Acos);
173151
scalar_function!(ascii, Ascii, "Returns the numeric code of the first character of the argument. In UTF8 encoding, returns the Unicode code point of the character. In other multibyte encodings, the argument must be an ASCII character.");
174152
scalar_function!(asin, Asin);
175153
scalar_function!(atan, Atan);
154+
scalar_function!(atan2, Atan2);
176155
scalar_function!(
177156
bit_length,
178157
BitLength,
@@ -185,13 +164,17 @@ scalar_function!(
185164
CharacterLength,
186165
"Returns number of characters in the string."
187166
);
167+
scalar_function!(length, CharacterLength);
168+
scalar_function!(char_length, CharacterLength);
188169
scalar_function!(chr, Chr, "Returns the character with the given code.");
170+
scalar_function!(coalesce, Coalesce);
189171
scalar_function!(cos, Cos);
190172
scalar_function!(exp, Exp);
191173
scalar_function!(floor, Floor);
192174
scalar_function!(initcap, InitCap, "Converts the first letter of each word to upper case and the rest to lower case. Words are sequences of alphanumeric characters separated by non-alphanumeric characters.");
193175
scalar_function!(left, Left, "Returns first n characters in the string, or when n is negative, returns all but last |n| characters.");
194176
scalar_function!(ln, Ln);
177+
scalar_function!(log, Log);
195178
scalar_function!(log10, Log10);
196179
scalar_function!(log2, Log2);
197180
scalar_function!(lower, Lower, "Converts the string to all lower case");
@@ -203,6 +186,8 @@ scalar_function!(
203186
"Computes the MD5 hash of the argument, with the result written in hexadecimal."
204187
);
205188
scalar_function!(octet_length, OctetLength, "Returns number of bytes in the string. Since this version of the function accepts type character directly, it will not strip trailing spaces.");
189+
scalar_function!(power, Power);
190+
scalar_function!(pow, Power);
206191
scalar_function!(regexp_match, RegexpMatch);
207192
scalar_function!(
208193
regexp_replace,
@@ -253,11 +238,30 @@ scalar_function!(
253238
ToHex,
254239
"Converts the number to its equivalent hexadecimal representation."
255240
);
241+
scalar_function!(now, Now);
256242
scalar_function!(to_timestamp, ToTimestamp);
243+
scalar_function!(to_timestamp_millis, ToTimestampMillis);
244+
scalar_function!(to_timestamp_micros, ToTimestampMicros);
245+
scalar_function!(to_timestamp_seconds, ToTimestampSeconds);
246+
scalar_function!(current_date, CurrentDate);
247+
scalar_function!(current_time, CurrentTime);
248+
scalar_function!(datepart, DatePart);
249+
scalar_function!(date_part, DatePart);
250+
scalar_function!(date_trunc, DateTrunc);
251+
scalar_function!(datetrunc, DateTrunc);
252+
scalar_function!(date_bin, DateBin);
257253
scalar_function!(translate, Translate, "Replaces each character in string that matches a character in the from set with the corresponding character in the to set. If from is longer than to, occurrences of the extra characters in from are deleted.");
258254
scalar_function!(trim, Trim, "Removes the longest string containing only characters in characters (a space by default) from the start, end, or both ends (BOTH is the default) of string.");
259255
scalar_function!(trunc, Trunc);
260256
scalar_function!(upper, Upper, "Converts the string to all upper case.");
257+
scalar_function!(make_array, MakeArray);
258+
scalar_function!(array, MakeArray);
259+
scalar_function!(nullif, NullIf);
260+
//scalar_function!(uuid, Uuid);
261+
//scalar_function!(struct, Struct);
262+
scalar_function!(from_unixtime, FromUnixtime);
263+
scalar_function!(arrow_typeof, ArrowTypeof);
264+
scalar_function!(random, Random);
261265

262266
aggregate_function!(avg, Avg);
263267
aggregate_function!(count, Count);
@@ -272,37 +276,55 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
272276
m.add_wrapped(wrap_pyfunction!(approx_distinct))?;
273277
m.add_wrapped(wrap_pyfunction!(alias))?;
274278
m.add_wrapped(wrap_pyfunction!(array))?;
279+
m.add_wrapped(wrap_pyfunction!(arrow_typeof))?;
275280
m.add_wrapped(wrap_pyfunction!(ascii))?;
276281
m.add_wrapped(wrap_pyfunction!(asin))?;
277282
m.add_wrapped(wrap_pyfunction!(atan))?;
283+
m.add_wrapped(wrap_pyfunction!(atan2))?;
278284
m.add_wrapped(wrap_pyfunction!(avg))?;
279285
m.add_wrapped(wrap_pyfunction!(bit_length))?;
280286
m.add_wrapped(wrap_pyfunction!(btrim))?;
281287
m.add_wrapped(wrap_pyfunction!(ceil))?;
282288
m.add_wrapped(wrap_pyfunction!(character_length))?;
283289
m.add_wrapped(wrap_pyfunction!(chr))?;
290+
m.add_wrapped(wrap_pyfunction!(char_length))?;
291+
m.add_wrapped(wrap_pyfunction!(coalesce))?;
284292
m.add_wrapped(wrap_pyfunction!(concat_ws))?;
285293
m.add_wrapped(wrap_pyfunction!(concat))?;
286294
m.add_wrapped(wrap_pyfunction!(cos))?;
287295
m.add_wrapped(wrap_pyfunction!(count))?;
296+
m.add_wrapped(wrap_pyfunction!(current_date))?;
297+
m.add_wrapped(wrap_pyfunction!(current_time))?;
298+
m.add_wrapped(wrap_pyfunction!(date_bin))?;
299+
m.add_wrapped(wrap_pyfunction!(datepart))?;
300+
m.add_wrapped(wrap_pyfunction!(date_part))?;
301+
m.add_wrapped(wrap_pyfunction!(datetrunc))?;
302+
m.add_wrapped(wrap_pyfunction!(date_trunc))?;
288303
m.add_wrapped(wrap_pyfunction!(digest))?;
289304
m.add_wrapped(wrap_pyfunction!(exp))?;
290305
m.add_wrapped(wrap_pyfunction!(floor))?;
306+
m.add_wrapped(wrap_pyfunction!(from_unixtime))?;
291307
m.add_wrapped(wrap_pyfunction!(in_list))?;
292308
m.add_wrapped(wrap_pyfunction!(initcap))?;
293309
m.add_wrapped(wrap_pyfunction!(left))?;
310+
m.add_wrapped(wrap_pyfunction!(length))?;
294311
m.add_wrapped(wrap_pyfunction!(ln))?;
312+
m.add_wrapped(wrap_pyfunction!(log))?;
295313
m.add_wrapped(wrap_pyfunction!(log10))?;
296314
m.add_wrapped(wrap_pyfunction!(log2))?;
297315
m.add_wrapped(wrap_pyfunction!(lower))?;
298316
m.add_wrapped(wrap_pyfunction!(lpad))?;
299317
m.add_wrapped(wrap_pyfunction!(ltrim))?;
300318
m.add_wrapped(wrap_pyfunction!(max))?;
319+
m.add_wrapped(wrap_pyfunction!(make_array))?;
301320
m.add_wrapped(wrap_pyfunction!(md5))?;
302321
m.add_wrapped(wrap_pyfunction!(min))?;
303322
m.add_wrapped(wrap_pyfunction!(now))?;
323+
m.add_wrapped(wrap_pyfunction!(nullif))?;
304324
m.add_wrapped(wrap_pyfunction!(octet_length))?;
305325
m.add_wrapped(wrap_pyfunction!(order_by))?;
326+
m.add_wrapped(wrap_pyfunction!(power))?;
327+
m.add_wrapped(wrap_pyfunction!(pow))?;
306328
m.add_wrapped(wrap_pyfunction!(random))?;
307329
m.add_wrapped(wrap_pyfunction!(regexp_match))?;
308330
m.add_wrapped(wrap_pyfunction!(regexp_replace))?;
@@ -323,15 +345,20 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
323345
m.add_wrapped(wrap_pyfunction!(sqrt))?;
324346
m.add_wrapped(wrap_pyfunction!(starts_with))?;
325347
m.add_wrapped(wrap_pyfunction!(strpos))?;
348+
//m.add_wrapped(wrap_pyfunction!(struct))?;
326349
m.add_wrapped(wrap_pyfunction!(substr))?;
327350
m.add_wrapped(wrap_pyfunction!(sum))?;
328351
m.add_wrapped(wrap_pyfunction!(tan))?;
329352
m.add_wrapped(wrap_pyfunction!(to_hex))?;
330353
m.add_wrapped(wrap_pyfunction!(to_timestamp))?;
354+
m.add_wrapped(wrap_pyfunction!(to_timestamp_millis))?;
355+
m.add_wrapped(wrap_pyfunction!(to_timestamp_micros))?;
356+
m.add_wrapped(wrap_pyfunction!(to_timestamp_seconds))?;
331357
m.add_wrapped(wrap_pyfunction!(translate))?;
332358
m.add_wrapped(wrap_pyfunction!(trim))?;
333359
m.add_wrapped(wrap_pyfunction!(trunc))?;
334360
m.add_wrapped(wrap_pyfunction!(upper))?;
361+
//m.add_wrapped(wrap_pyfunction!(uuid))?;
335362
m.add_wrapped(wrap_pyfunction!(window))?;
336363
Ok(())
337364
}

0 commit comments

Comments
 (0)