Skip to content

Commit 91670e2

Browse files
authored
Doc gen: Attributes to support related_udf, alternative_syntax (#13575)
* Doc gen: Rust custom attributes support `related_udf`, `alternative_syntax`
1 parent acc2c7d commit 91670e2

File tree

141 files changed

+1288
-1360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+1288
-1360
lines changed

datafusion/doc/src/lib.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ pub struct Documentation {
5656

5757
impl Documentation {
5858
/// Returns a new [`DocumentationBuilder`] with no options set.
59-
pub fn builder() -> DocumentationBuilder {
60-
DocumentationBuilder::new()
59+
pub fn builder(
60+
doc_section: DocSection,
61+
description: impl Into<String>,
62+
syntax_example: impl Into<String>,
63+
) -> DocumentationBuilder {
64+
DocumentationBuilder::new(doc_section, description, syntax_example)
6165
}
6266
}
6367

@@ -86,29 +90,30 @@ pub struct DocSection {
8690
/// description: None,
8791
/// };
8892
///
89-
/// let documentation = Documentation::builder()
90-
/// .with_doc_section(doc_section)
91-
/// .with_description("Add one to an int32")
92-
/// .with_syntax_example("add_one(2)")
93+
/// let documentation = Documentation::builder(doc_section, "Add one to an int32".to_owned(), "add_one(2)".to_owned())
9394
/// .with_argument("arg_1", "The int32 number to add one to")
9495
/// .build();
9596
/// # }
9697
pub struct DocumentationBuilder {
97-
pub doc_section: Option<DocSection>,
98-
pub description: Option<String>,
99-
pub syntax_example: Option<String>,
98+
pub doc_section: DocSection,
99+
pub description: String,
100+
pub syntax_example: String,
100101
pub sql_example: Option<String>,
101102
pub arguments: Option<Vec<(String, String)>>,
102103
pub alternative_syntax: Option<Vec<String>>,
103104
pub related_udfs: Option<Vec<String>>,
104105
}
105106

106107
impl DocumentationBuilder {
107-
pub fn new() -> Self {
108+
pub fn new(
109+
doc_section: DocSection,
110+
description: impl Into<String>,
111+
syntax_example: impl Into<String>,
112+
) -> Self {
108113
Self {
109-
doc_section: None,
110-
description: None,
111-
syntax_example: None,
114+
doc_section,
115+
description: description.into(),
116+
syntax_example: syntax_example.into(),
112117
sql_example: None,
113118
arguments: None,
114119
alternative_syntax: None,
@@ -117,17 +122,17 @@ impl DocumentationBuilder {
117122
}
118123

119124
pub fn with_doc_section(mut self, doc_section: DocSection) -> Self {
120-
self.doc_section = Some(doc_section);
125+
self.doc_section = doc_section;
121126
self
122127
}
123128

124129
pub fn with_description(mut self, description: impl Into<String>) -> Self {
125-
self.description = Some(description.into());
130+
self.description = description.into();
126131
self
127132
}
128133

129134
pub fn with_syntax_example(mut self, syntax_example: impl Into<String>) -> Self {
130-
self.syntax_example = Some(syntax_example.into());
135+
self.syntax_example = syntax_example.into();
131136
self
132137
}
133138

@@ -205,30 +210,14 @@ impl DocumentationBuilder {
205210
related_udfs,
206211
} = self;
207212

208-
if doc_section.is_none() {
209-
panic!("Documentation must have a doc section");
210-
}
211-
if description.is_none() {
212-
panic!("Documentation must have a description");
213-
}
214-
if syntax_example.is_none() {
215-
panic!("Documentation must have a syntax_example");
216-
}
217-
218213
Documentation {
219-
doc_section: doc_section.unwrap(),
220-
description: description.unwrap(),
221-
syntax_example: syntax_example.unwrap(),
214+
doc_section,
215+
description,
216+
syntax_example,
222217
sql_example,
223218
arguments,
224219
alternative_syntax,
225220
related_udfs,
226221
}
227222
}
228223
}
229-
230-
impl Default for DocumentationBuilder {
231-
fn default() -> Self {
232-
Self::new()
233-
}
234-
}

datafusion/expr/src/udaf.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ where
333333
///
334334
/// fn get_doc() -> &'static Documentation {
335335
/// DOCUMENTATION.get_or_init(|| {
336-
/// Documentation::builder()
337-
/// .with_doc_section(DOC_SECTION_AGGREGATE)
338-
/// .with_description("calculates a geometric mean")
339-
/// .with_syntax_example("geo_mean(2.0)")
336+
/// Documentation::builder(DOC_SECTION_AGGREGATE, "calculates a geometric mean", "geo_mean(2.0)")
340337
/// .with_argument("arg1", "The Float64 number for the geometric mean")
341338
/// .build()
342339
/// })

datafusion/expr/src/udf.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,7 @@ pub struct ScalarFunctionArgs<'a> {
374374
///
375375
/// fn get_doc() -> &'static Documentation {
376376
/// DOCUMENTATION.get_or_init(|| {
377-
/// Documentation::builder()
378-
/// .with_doc_section(DOC_SECTION_MATH)
379-
/// .with_description("Add one to an int32")
380-
/// .with_syntax_example("add_one(2)")
377+
/// Documentation::builder(DOC_SECTION_MATH, "Add one to an int32", "add_one(2)")
381378
/// .with_argument("arg1", "The int32 number to add one to")
382379
/// .build()
383380
/// })

datafusion/expr/src/udwf.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,7 @@ where
258258
///
259259
/// fn get_doc() -> &'static Documentation {
260260
/// DOCUMENTATION.get_or_init(|| {
261-
/// Documentation::builder()
262-
/// .with_doc_section(DOC_SECTION_ANALYTICAL)
263-
/// .with_description("smooths the windows")
264-
/// .with_syntax_example("smooth_it(2)")
261+
/// Documentation::builder(DOC_SECTION_ANALYTICAL, "smooths the windows", "smooth_it(2)")
265262
/// .with_argument("arg1", "The int32 number to smooth by")
266263
/// .build()
267264
/// })

datafusion/functions-aggregate/src/approx_distinct.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,7 @@ static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
317317

318318
fn get_approx_distinct_doc() -> &'static Documentation {
319319
DOCUMENTATION.get_or_init(|| {
320-
Documentation::builder()
321-
.with_doc_section(DOC_SECTION_APPROXIMATE)
322-
.with_description(
323-
"Returns the approximate number of distinct input values calculated using the HyperLogLog algorithm.",
324-
)
325-
.with_syntax_example("approx_distinct(expression)")
320+
Documentation::builder(DOC_SECTION_APPROXIMATE, "Returns the approximate number of distinct input values calculated using the HyperLogLog algorithm.", "approx_distinct(expression)")
326321
.with_sql_example(r#"```sql
327322
> SELECT approx_distinct(column_name) FROM table_name;
328323
+-----------------------------------+

datafusion/functions-aggregate/src/approx_median.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,11 @@ static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
130130

131131
fn get_approx_median_doc() -> &'static Documentation {
132132
DOCUMENTATION.get_or_init(|| {
133-
Documentation::builder()
134-
.with_doc_section(DOC_SECTION_APPROXIMATE)
135-
.with_description(
133+
Documentation::builder(
134+
DOC_SECTION_APPROXIMATE,
136135
"Returns the approximate median (50th percentile) of input values. It is an alias of `approx_percentile_cont(x, 0.5)`.",
137-
)
138-
.with_syntax_example("approx_median(expression)")
136+
137+
"approx_median(expression)")
139138
.with_sql_example(r#"```sql
140139
> SELECT approx_median(column_name) FROM table_name;
141140
+-----------------------------------+

datafusion/functions-aggregate/src/approx_percentile_cont.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,10 @@ static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
280280

281281
fn get_approx_percentile_cont_doc() -> &'static Documentation {
282282
DOCUMENTATION.get_or_init(|| {
283-
Documentation::builder()
284-
.with_doc_section(DOC_SECTION_APPROXIMATE)
285-
.with_description(
283+
Documentation::builder(
284+
DOC_SECTION_APPROXIMATE,
286285
"Returns the approximate percentile of input values using the t-digest algorithm.",
287-
)
288-
.with_syntax_example("approx_percentile_cont(expression, percentile, centroids)")
286+
"approx_percentile_cont(expression, percentile, centroids)")
289287
.with_sql_example(r#"```sql
290288
> SELECT approx_percentile_cont(column_name, 0.75, 100) FROM table_name;
291289
+-------------------------------------------------+

datafusion/functions-aggregate/src/approx_percentile_cont_with_weight.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,11 @@ static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
165165

166166
fn get_approx_percentile_cont_with_weight_doc() -> &'static Documentation {
167167
DOCUMENTATION.get_or_init(|| {
168-
Documentation::builder()
169-
.with_doc_section(DOC_SECTION_APPROXIMATE)
170-
.with_description(
168+
Documentation::builder(
169+
DOC_SECTION_APPROXIMATE,
171170
"Returns the weighted approximate percentile of input values using the t-digest algorithm.",
172-
)
173-
.with_syntax_example("approx_percentile_cont_with_weight(expression, weight, percentile)")
171+
172+
"approx_percentile_cont_with_weight(expression, weight, percentile)")
174173
.with_sql_example(r#"```sql
175174
> SELECT approx_percentile_cont_with_weight(column_name, weight_column, 0.90) FROM table_name;
176175
+----------------------------------------------------------------------+

datafusion/functions-aggregate/src/array_agg.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,11 @@ static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
154154

155155
fn get_array_agg_doc() -> &'static Documentation {
156156
DOCUMENTATION.get_or_init(|| {
157-
Documentation::builder()
158-
.with_doc_section(DOC_SECTION_GENERAL)
159-
.with_description(
157+
Documentation::builder(
158+
DOC_SECTION_GENERAL,
160159
"Returns an array created from the expression elements. If ordering is required, elements are inserted in the specified order.",
161-
)
162-
.with_syntax_example("array_agg(expression [ORDER BY expression])")
160+
161+
"array_agg(expression [ORDER BY expression])")
163162
.with_sql_example(r#"```sql
164163
> SELECT array_agg(column_name ORDER BY other_column) FROM table_name;
165164
+-----------------------------------------------+

datafusion/functions-aggregate/src/average.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,23 @@ static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
248248

249249
fn get_avg_doc() -> &'static Documentation {
250250
DOCUMENTATION.get_or_init(|| {
251-
Documentation::builder()
252-
.with_doc_section(DOC_SECTION_GENERAL)
253-
.with_description(
254-
"Returns the average of numeric values in the specified column.",
255-
)
256-
.with_syntax_example("avg(expression)")
257-
.with_sql_example(
258-
r#"```sql
251+
Documentation::builder(
252+
DOC_SECTION_GENERAL,
253+
"Returns the average of numeric values in the specified column.",
254+
"avg(expression)",
255+
)
256+
.with_sql_example(
257+
r#"```sql
259258
> SELECT avg(column_name) FROM table_name;
260259
+---------------------------+
261260
| avg(column_name) |
262261
+---------------------------+
263262
| 42.75 |
264263
+---------------------------+
265264
```"#,
266-
)
267-
.with_standard_argument("expression", None)
268-
.build()
265+
)
266+
.with_standard_argument("expression", None)
267+
.build()
269268
})
270269
}
271270

datafusion/functions-aggregate/src/bit_and_or_xor.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,40 +139,41 @@ static BIT_AND_DOC: OnceLock<Documentation> = OnceLock::new();
139139

140140
fn get_bit_and_doc() -> &'static Documentation {
141141
BIT_AND_DOC.get_or_init(|| {
142-
Documentation::builder()
143-
.with_doc_section(DOC_SECTION_GENERAL)
144-
.with_description("Computes the bitwise AND of all non-null input values.")
145-
.with_syntax_example("bit_and(expression)")
146-
.with_standard_argument("expression", Some("Integer"))
147-
.build()
142+
Documentation::builder(
143+
DOC_SECTION_GENERAL,
144+
"Computes the bitwise AND of all non-null input values.",
145+
"bit_and(expression)",
146+
)
147+
.with_standard_argument("expression", Some("Integer"))
148+
.build()
148149
})
149150
}
150151

151152
static BIT_OR_DOC: OnceLock<Documentation> = OnceLock::new();
152153

153154
fn get_bit_or_doc() -> &'static Documentation {
154155
BIT_OR_DOC.get_or_init(|| {
155-
Documentation::builder()
156-
.with_doc_section(DOC_SECTION_GENERAL)
157-
.with_description("Computes the bitwise OR of all non-null input values.")
158-
.with_syntax_example("bit_or(expression)")
159-
.with_standard_argument("expression", Some("Integer"))
160-
.build()
156+
Documentation::builder(
157+
DOC_SECTION_GENERAL,
158+
"Computes the bitwise OR of all non-null input values.",
159+
"bit_or(expression)",
160+
)
161+
.with_standard_argument("expression", Some("Integer"))
162+
.build()
161163
})
162164
}
163165

164166
static BIT_XOR_DOC: OnceLock<Documentation> = OnceLock::new();
165167

166168
fn get_bit_xor_doc() -> &'static Documentation {
167169
BIT_XOR_DOC.get_or_init(|| {
168-
Documentation::builder()
169-
.with_doc_section(DOC_SECTION_GENERAL)
170-
.with_description(
171-
"Computes the bitwise exclusive OR of all non-null input values.",
172-
)
173-
.with_syntax_example("bit_xor(expression)")
174-
.with_standard_argument("expression", Some("Integer"))
175-
.build()
170+
Documentation::builder(
171+
DOC_SECTION_GENERAL,
172+
"Computes the bitwise exclusive OR of all non-null input values.",
173+
"bit_xor(expression)",
174+
)
175+
.with_standard_argument("expression", Some("Integer"))
176+
.build()
176177
})
177178
}
178179

datafusion/functions-aggregate/src/bool_and_or.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,23 @@ static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
186186

187187
fn get_bool_and_doc() -> &'static Documentation {
188188
DOCUMENTATION.get_or_init(|| {
189-
Documentation::builder()
190-
.with_doc_section(DOC_SECTION_GENERAL)
191-
.with_description(
192-
"Returns true if all non-null input values are true, otherwise false.",
193-
)
194-
.with_syntax_example("bool_and(expression)")
195-
.with_sql_example(
196-
r#"```sql
189+
Documentation::builder(
190+
DOC_SECTION_GENERAL,
191+
"Returns true if all non-null input values are true, otherwise false.",
192+
"bool_and(expression)",
193+
)
194+
.with_sql_example(
195+
r#"```sql
197196
> SELECT bool_and(column_name) FROM table_name;
198197
+----------------------------+
199198
| bool_and(column_name) |
200199
+----------------------------+
201200
| true |
202201
+----------------------------+
203202
```"#,
204-
)
205-
.with_standard_argument("expression", None)
206-
.build()
203+
)
204+
.with_standard_argument("expression", None)
205+
.build()
207206
})
208207
}
209208

@@ -334,24 +333,23 @@ impl AggregateUDFImpl for BoolOr {
334333

335334
fn get_bool_or_doc() -> &'static Documentation {
336335
DOCUMENTATION.get_or_init(|| {
337-
Documentation::builder()
338-
.with_doc_section(DOC_SECTION_GENERAL)
339-
.with_description(
340-
"Returns true if any non-null input value is true, otherwise false.",
341-
)
342-
.with_syntax_example("bool_or(expression)")
343-
.with_sql_example(
344-
r#"```sql
336+
Documentation::builder(
337+
DOC_SECTION_GENERAL,
338+
"Returns true if any non-null input value is true, otherwise false.",
339+
"bool_or(expression)",
340+
)
341+
.with_sql_example(
342+
r#"```sql
345343
> SELECT bool_or(column_name) FROM table_name;
346344
+----------------------------+
347345
| bool_or(column_name) |
348346
+----------------------------+
349347
| true |
350348
+----------------------------+
351349
```"#,
352-
)
353-
.with_standard_argument("expression", None)
354-
.build()
350+
)
351+
.with_standard_argument("expression", None)
352+
.build()
355353
})
356354
}
357355

0 commit comments

Comments
 (0)