datafusion_doc/
lib.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
18#![doc(
19    html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
20    html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
21)]
22#![cfg_attr(docsrs, feature(doc_cfg))]
23
24mod udaf;
25mod udf;
26mod udwf;
27
28pub use udaf::aggregate_doc_sections;
29pub use udf::scalar_doc_sections;
30pub use udwf::window_doc_sections;
31
32#[allow(rustdoc::broken_intra_doc_links)]
33/// Documentation for use by [`ScalarUDFImpl`](ScalarUDFImpl),
34/// [`AggregateUDFImpl`](AggregateUDFImpl) and [`WindowUDFImpl`](WindowUDFImpl) functions.
35///
36/// See the [`DocumentationBuilder`] to create a new [`Documentation`] struct.
37///
38/// The DataFusion [SQL function documentation] is automatically  generated from these structs.
39/// The name of the udf will be pulled from the [`ScalarUDFImpl::name`](ScalarUDFImpl::name),
40/// [`AggregateUDFImpl::name`](AggregateUDFImpl::name) or [`WindowUDFImpl::name`](WindowUDFImpl::name)
41/// function as appropriate.
42///
43/// All strings in the documentation are required to be
44/// in [markdown format](https://www.markdownguide.org/basic-syntax/).
45///
46/// Currently, documentation only supports a single language
47/// thus all text should be in English.
48///
49/// [SQL function documentation]: https://datafusion.apache.org/user-guide/sql/index.html
50#[derive(Debug, Clone, PartialEq, Eq, Hash)]
51pub struct Documentation {
52    /// The section in the documentation where the UDF will be documented
53    pub doc_section: DocSection,
54    /// The description for the UDF
55    pub description: String,
56    /// A brief example of the syntax. For example "ascii(str)"
57    pub syntax_example: String,
58    /// A sql example for the UDF, usually in the form of a sql prompt
59    /// query and output. It is strongly recommended to provide an
60    /// example for anything but the most basic UDF's
61    pub sql_example: Option<String>,
62    /// Arguments for the UDF which will be displayed in array order.
63    /// Left member of a pair is the argument name, right is a
64    /// description for the argument
65    pub arguments: Option<Vec<(String, String)>>,
66    /// A list of alternative syntax examples for a function
67    pub alternative_syntax: Option<Vec<String>>,
68    /// Related functions if any. Values should match the related
69    /// udf's name exactly. Related udf's must be of the same
70    /// UDF type (scalar, aggregate or window) for proper linking to
71    /// occur
72    pub related_udfs: Option<Vec<String>>,
73}
74
75impl Documentation {
76    /// Returns a new [`DocumentationBuilder`] with no options set.
77    pub fn builder(
78        doc_section: DocSection,
79        description: impl Into<String>,
80        syntax_example: impl Into<String>,
81    ) -> DocumentationBuilder {
82        DocumentationBuilder::new_with_details(doc_section, description, syntax_example)
83    }
84
85    /// Output the `Documentation` struct in form of custom Rust documentation attributes
86    /// It is useful to semi automate during tmigration of UDF documentation
87    /// generation from code based to attribute based and can be safely removed after
88    pub fn to_doc_attribute(&self) -> String {
89        let mut result = String::new();
90
91        result.push_str("#[user_doc(");
92        // Doc Section
93        result.push_str(
94            format!(
95                "\n    doc_section({}label = \"{}\"{}),",
96                if !self.doc_section.include {
97                    "include = \"false\", "
98                } else {
99                    ""
100                },
101                self.doc_section.label,
102                self.doc_section
103                    .description
104                    .map(|s| format!(", description = \"{s}\""))
105                    .unwrap_or_default(),
106            )
107            .as_ref(),
108        );
109
110        // Description
111        result.push_str(format!("\n    description=\"{}\",", self.description).as_ref());
112        // Syntax Example
113        result.push_str(
114            format!("\n    syntax_example=\"{}\",", self.syntax_example).as_ref(),
115        );
116        // SQL Example
117        result.push_str(
118            &self
119                .sql_example
120                .clone()
121                .map(|s| format!("\n    sql_example = r#\"{s}\"#,"))
122                .unwrap_or_default(),
123        );
124
125        let st_arg_token = " expression to operate on. Can be a constant, column, or function, and any combination of operators.";
126        // Standard Arguments
127        if let Some(args) = self.arguments.clone() {
128            args.iter().for_each(|(name, value)| {
129                if value.contains(st_arg_token) {
130                    if name.starts_with("The ") {
131                        result.push_str(format!("\n    standard_argument(\n        name = \"{name}\"),").as_ref());
132                    } else {
133                        result.push_str(format!("\n    standard_argument(\n        name = \"{}\",\n        prefix = \"{}\"\n    ),", name, value.replace(st_arg_token, "")).as_ref());
134                    }
135                }
136            });
137        }
138
139        // Arguments
140        if let Some(args) = self.arguments.clone() {
141            args.iter().for_each(|(name, value)| {
142                if !value.contains(st_arg_token) {
143                    result.push_str(format!("\n    argument(\n        name = \"{name}\",\n        description = \"{value}\"\n    ),").as_ref());
144                }
145            });
146        }
147
148        if let Some(alt_syntax) = self.alternative_syntax.clone() {
149            alt_syntax.iter().for_each(|syntax| {
150                result.push_str(
151                    format!("\n    alternative_syntax = \"{syntax}\",").as_ref(),
152                );
153            });
154        }
155
156        // Related UDFs
157        if let Some(related_udf) = self.related_udfs.clone() {
158            related_udf.iter().for_each(|udf| {
159                result.push_str(format!("\n    related_udf(name = \"{udf}\"),").as_ref());
160            });
161        }
162
163        result.push_str("\n)]");
164
165        result
166    }
167}
168
169#[derive(Debug, Clone, PartialEq, Eq, Hash)]
170pub struct DocSection {
171    /// True to include this doc section in the public
172    /// documentation, false otherwise
173    pub include: bool,
174    /// A display label for the doc section. For example: "Math Expressions"
175    pub label: &'static str,
176    /// An optional description for the doc section
177    pub description: Option<&'static str>,
178}
179
180impl Default for DocSection {
181    /// Returns a "default" Doc section.
182    ///
183    /// This is suitable for user defined functions that do not appear in the
184    /// DataFusion documentation.
185    fn default() -> Self {
186        Self {
187            include: true,
188            label: "Default",
189            description: None,
190        }
191    }
192}
193
194/// A builder for [`Documentation`]'s.
195///
196/// Example:
197///
198/// ```rust
199///
200/// # fn main() {
201///     use datafusion_doc::{DocSection, Documentation};
202///     let doc_section = DocSection {
203///         include: true,
204///         label: "Display Label",
205///         description: None,
206///     };
207///
208///     let documentation = Documentation::builder(doc_section, "Add one to an int32".to_owned(), "add_one(2)".to_owned())
209///           .with_argument("arg_1", "The int32 number to add one to")
210///           .build();
211/// # }
212pub struct DocumentationBuilder {
213    pub doc_section: DocSection,
214    pub description: String,
215    pub syntax_example: String,
216    pub sql_example: Option<String>,
217    pub arguments: Option<Vec<(String, String)>>,
218    pub alternative_syntax: Option<Vec<String>>,
219    pub related_udfs: Option<Vec<String>>,
220}
221
222impl DocumentationBuilder {
223    /// Creates a new [`DocumentationBuilder`] with all required fields
224    pub fn new_with_details(
225        doc_section: DocSection,
226        description: impl Into<String>,
227        syntax_example: impl Into<String>,
228    ) -> Self {
229        Self {
230            doc_section,
231            description: description.into(),
232            syntax_example: syntax_example.into(),
233            sql_example: None,
234            arguments: None,
235            alternative_syntax: None,
236            related_udfs: None,
237        }
238    }
239
240    pub fn with_doc_section(mut self, doc_section: DocSection) -> Self {
241        self.doc_section = doc_section;
242        self
243    }
244
245    pub fn with_description(mut self, description: impl Into<String>) -> Self {
246        self.description = description.into();
247        self
248    }
249
250    pub fn with_syntax_example(mut self, syntax_example: impl Into<String>) -> Self {
251        self.syntax_example = syntax_example.into();
252        self
253    }
254
255    pub fn with_sql_example(mut self, sql_example: impl Into<String>) -> Self {
256        self.sql_example = Some(sql_example.into());
257        self
258    }
259
260    /// Adds documentation for a specific argument to the documentation.
261    ///
262    /// Arguments are displayed in the order they are added.
263    pub fn with_argument(
264        mut self,
265        arg_name: impl Into<String>,
266        arg_description: impl Into<String>,
267    ) -> Self {
268        let mut args = self.arguments.unwrap_or_default();
269        args.push((arg_name.into(), arg_description.into()));
270        self.arguments = Some(args);
271        self
272    }
273
274    /// Add a standard "expression" argument to the documentation
275    ///
276    /// The argument is rendered like below if Some() is passed through:
277    ///
278    /// ```text
279    /// <arg_name>:
280    ///   <expression_type> expression to operate on. Can be a constant, column, or function, and any combination of operators.
281    /// ```
282    ///
283    /// The argument is rendered like below if None is passed through:
284    ///
285    ///  ```text
286    /// <arg_name>:
287    ///   The expression to operate on. Can be a constant, column, or function, and any combination of operators.
288    /// ```
289    pub fn with_standard_argument(
290        self,
291        arg_name: impl Into<String>,
292        expression_type: Option<&str>,
293    ) -> Self {
294        let description = format!(
295            "{} expression to operate on. Can be a constant, column, or function, and any combination of operators.",
296            expression_type.unwrap_or("The")
297        );
298        self.with_argument(arg_name, description)
299    }
300
301    pub fn with_alternative_syntax(mut self, syntax_name: impl Into<String>) -> Self {
302        let mut alternative_syntax_array = self.alternative_syntax.unwrap_or_default();
303        alternative_syntax_array.push(syntax_name.into());
304        self.alternative_syntax = Some(alternative_syntax_array);
305        self
306    }
307
308    pub fn with_related_udf(mut self, related_udf: impl Into<String>) -> Self {
309        let mut related = self.related_udfs.unwrap_or_default();
310        related.push(related_udf.into());
311        self.related_udfs = Some(related);
312        self
313    }
314
315    /// Build the documentation from provided components
316    ///
317    /// Panics if `doc_section`, `description` or `syntax_example` is not set
318    pub fn build(self) -> Documentation {
319        let Self {
320            doc_section,
321            description,
322            syntax_example,
323            sql_example,
324            arguments,
325            alternative_syntax,
326            related_udfs,
327        } = self;
328
329        Documentation {
330            doc_section,
331            description,
332            syntax_example,
333            sql_example,
334            arguments,
335            alternative_syntax,
336            related_udfs,
337        }
338    }
339}