|
| 1 | +# datafusion-functions-extra |
| 2 | + |
| 3 | +[](https://github.com/datafusion-contrib/datafusion-functions-extra/actions/workflows/ci.yml?query=branch%3Amain) |
| 4 | +<!-- [](https://crates.io/crates/datafusion-functions-extra) --> |
| 5 | + |
| 6 | +**Note:** This is not an official Apache Software Foundation release. |
| 7 | + |
| 8 | +This crate provides extra functions for DataFusion, specifically focusing on advanced aggregations. These extensions are inspired by other projects like DuckDB and Spark SQL. |
| 9 | + |
| 10 | +To use these functions, you'll just need to call: |
| 11 | + |
| 12 | +```rust |
| 13 | +datafusion_functions_extra::register_all_extra_functions(&mut ctx)?; |
| 14 | +``` |
| 15 | + |
| 16 | +# Examples |
| 17 | + |
| 18 | +```sql |
| 19 | +-- Create a table with various columns containing strings, integers, floats, dates, and times |
| 20 | +CREATE TABLE test_table ( |
| 21 | + utf8_col VARCHAR, |
| 22 | + int64_col INT, |
| 23 | + float64_col FLOAT, |
| 24 | + date64_col DATE, |
| 25 | + time64_col TIME |
| 26 | +) AS VALUES |
| 27 | +('apple', 1, 1.0, '2021-01-01', '01:00:00'), |
| 28 | +('banana', 2, 2.0, '2021-01-02', '02:00:00'), |
| 29 | +('apple', 2, 2.0, '2021-01-02', '02:00:00'), |
| 30 | +('orange', 3, 3.0, '2021-01-03', '03:00:00'), |
| 31 | +('banana', 3, 3.0, '2021-01-03', '03:00:00'), |
| 32 | +('apple', 3, 3.0, '2021-01-03', '03:00:00'); |
| 33 | + |
| 34 | +-- Get the mode of the utf8_col column |
| 35 | +SELECT mode(utf8_col) as mode_utf8 FROM test_table; |
| 36 | +-- Results in |
| 37 | +-- +----------+ |
| 38 | +-- | mode_utf8| |
| 39 | +-- +----------+ |
| 40 | +-- | apple | |
| 41 | +-- +----------+ |
| 42 | + |
| 43 | +-- Get the mode of the date64_col column |
| 44 | +SELECT mode(date64_col) as mode_date FROM test_table; |
| 45 | +-- Results in |
| 46 | +-- +-----------+ |
| 47 | +-- | mode_date | |
| 48 | +-- +-----------+ |
| 49 | +-- | 2021-01-03| |
| 50 | +-- +-----------+ |
| 51 | + |
| 52 | +-- Get the mode of the time64_col column |
| 53 | +SELECT mode(time64_col) as mode_time FROM test_table; |
| 54 | +-- Results in |
| 55 | +-- +-----------+ |
| 56 | +-- | mode_time | |
| 57 | +-- +-----------+ |
| 58 | +-- | 03:00:00 | |
| 59 | +-- +-----------+ |
| 60 | +``` |
| 61 | + |
| 62 | +## Done |
| 63 | + |
| 64 | +* [x] `mode(expression) -> scalar` - Returns the most frequent (mode) value from a column of data. |
0 commit comments