|
| 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 | + |
| 18 | +//! # User-Defined Functions Examples |
| 19 | +//! |
| 20 | +//! These examples demonstrate user-defined functions in DataFusion. |
| 21 | +//! |
| 22 | +//! Each subcommand runs a corresponding example: |
| 23 | +//! - `adv_udaf` — user defined aggregate function example |
| 24 | +//! - `adv_udf` — user defined scalar function example |
| 25 | +//! - `adv_udwf` — user defined window function example |
| 26 | +//! - `async_udf` — asynchronous user defined function example |
| 27 | +//! - `udaf` — simple user defined aggregate function example |
| 28 | +//! - `udf` — simple user defined scalar function example |
| 29 | +//! - `udtf` — simple user defined table function example |
| 30 | +//! - `udwf` — simple user defined window function example |
| 31 | +
|
| 32 | +mod advanced_udaf; |
| 33 | +mod advanced_udf; |
| 34 | +mod advanced_udwf; |
| 35 | +mod async_udf; |
| 36 | +mod simple_udaf; |
| 37 | +mod simple_udf; |
| 38 | +mod simple_udtf; |
| 39 | +mod simple_udwf; |
| 40 | + |
| 41 | +use std::str::FromStr; |
| 42 | + |
| 43 | +use datafusion::error::{DataFusionError, Result}; |
| 44 | + |
| 45 | +enum ExampleKind { |
| 46 | + AdvUdaf, |
| 47 | + AdvUdf, |
| 48 | + AdvUdwf, |
| 49 | + AsyncUdf, |
| 50 | + Udf, |
| 51 | + Udaf, |
| 52 | + Udwf, |
| 53 | + Udtf, |
| 54 | +} |
| 55 | + |
| 56 | +impl AsRef<str> for ExampleKind { |
| 57 | + fn as_ref(&self) -> &str { |
| 58 | + match self { |
| 59 | + Self::AdvUdaf => "adv_udaf", |
| 60 | + Self::AdvUdf => "adv_udf", |
| 61 | + Self::AdvUdwf => "adv_udwf", |
| 62 | + Self::AsyncUdf => "async_udf", |
| 63 | + Self::Udf => "udf", |
| 64 | + Self::Udaf => "udaf", |
| 65 | + Self::Udwf => "udwt", |
| 66 | + Self::Udtf => "udtf", |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl FromStr for ExampleKind { |
| 72 | + type Err = DataFusionError; |
| 73 | + |
| 74 | + fn from_str(s: &str) -> Result<Self> { |
| 75 | + match s { |
| 76 | + "adv_udaf" => Ok(Self::AdvUdaf), |
| 77 | + "adv_udf" => Ok(Self::AdvUdf), |
| 78 | + "adv_udwf" => Ok(Self::AdvUdwf), |
| 79 | + "async_udf" => Ok(Self::AsyncUdf), |
| 80 | + "udaf" => Ok(Self::Udaf), |
| 81 | + "udf" => Ok(Self::Udf), |
| 82 | + "udtf" => Ok(Self::Udtf), |
| 83 | + "udwf" => Ok(Self::Udwf), |
| 84 | + _ => Err(DataFusionError::Execution(format!("Unknown example: {s}"))), |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl ExampleKind { |
| 90 | + const ALL: [Self; 8] = [ |
| 91 | + Self::AdvUdaf, |
| 92 | + Self::AdvUdf, |
| 93 | + Self::AdvUdwf, |
| 94 | + Self::AsyncUdf, |
| 95 | + Self::Udaf, |
| 96 | + Self::Udf, |
| 97 | + Self::Udtf, |
| 98 | + Self::Udwf, |
| 99 | + ]; |
| 100 | + |
| 101 | + const EXAMPLE_NAME: &str = "udf"; |
| 102 | + |
| 103 | + fn variants() -> Vec<&'static str> { |
| 104 | + Self::ALL.iter().map(|x| x.as_ref()).collect() |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[tokio::main] |
| 109 | +async fn main() -> Result<()> { |
| 110 | + let usage = format!( |
| 111 | + "Usage: cargo run --example {} -- [{}]", |
| 112 | + ExampleKind::EXAMPLE_NAME, |
| 113 | + ExampleKind::variants().join("|") |
| 114 | + ); |
| 115 | + |
| 116 | + let arg = std::env::args().nth(1).ok_or_else(|| { |
| 117 | + eprintln!("{usage}"); |
| 118 | + DataFusionError::Execution("Missing argument".to_string()) |
| 119 | + })?; |
| 120 | + |
| 121 | + match arg.parse::<ExampleKind>()? { |
| 122 | + ExampleKind::AdvUdaf => advanced_udaf::advanced_udaf().await?, |
| 123 | + ExampleKind::AdvUdf => advanced_udf::advanced_udf().await?, |
| 124 | + ExampleKind::AdvUdwf => advanced_udwf::advanced_udwf().await?, |
| 125 | + ExampleKind::AsyncUdf => async_udf::async_udf().await?, |
| 126 | + ExampleKind::Udaf => simple_udaf::simple_udaf().await?, |
| 127 | + ExampleKind::Udf => simple_udf::simple_udf().await?, |
| 128 | + ExampleKind::Udtf => simple_udtf::simple_udtf().await?, |
| 129 | + ExampleKind::Udwf => simple_udwf::simple_udwf().await?, |
| 130 | + } |
| 131 | + |
| 132 | + Ok(()) |
| 133 | +} |
0 commit comments