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