Skip to content

Commit 030c4e9

Browse files
authored
Add DocumentationBuilder::with_standard_argument to reduce copy/paste (#12747)
* Add `DocumentationBuilder::with_standard_expression` to reduce copy/paste * fix doc * fix standard argument * Update docs * Improve documentation to explain what is different
1 parent 8aafa54 commit 030c4e9

File tree

9 files changed

+48
-39
lines changed

9 files changed

+48
-39
lines changed

datafusion/expr/src/udf_docs.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ impl DocumentationBuilder {
131131
self
132132
}
133133

134+
/// Adds documentation for a specific argument to the documentation.
135+
///
136+
/// Arguments are displayed in the order they are added.
134137
pub fn with_argument(
135138
mut self,
136139
arg_name: impl Into<String>,
@@ -142,6 +145,27 @@ impl DocumentationBuilder {
142145
self
143146
}
144147

148+
/// Add a standard "expression" argument to the documentation
149+
///
150+
/// This is similar to [`Self::with_argument`] except that a standard
151+
/// description is appended to the end: `"Can be a constant, column, or
152+
/// function, and any combination of arithmetic operators."`
153+
///
154+
/// The argument is rendered like
155+
///
156+
/// ```text
157+
/// <arg_name>:
158+
/// <expression_type> expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
159+
/// ```
160+
pub fn with_standard_argument(
161+
self,
162+
arg_name: impl Into<String>,
163+
expression_type: impl AsRef<str>,
164+
) -> Self {
165+
let expression_type = expression_type.as_ref();
166+
self.with_argument(arg_name, format!("{expression_type} expression to operate on. Can be a constant, column, or function, and any combination of operators."))
167+
}
168+
145169
pub fn with_related_udf(mut self, related_udf: impl Into<String>) -> Self {
146170
let mut related = self.related_udfs.unwrap_or_default();
147171
related.push(related_udf.into());

datafusion/functions-aggregate/src/bit_and_or_xor.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ fn get_bit_and_doc() -> &'static Documentation {
142142
.with_doc_section(DOC_SECTION_GENERAL)
143143
.with_description("Computes the bitwise AND of all non-null input values.")
144144
.with_syntax_example("bit_and(expression)")
145-
.with_argument(
146-
"expression",
147-
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
148-
)
145+
.with_standard_argument("expression", "Integer")
149146
.build()
150147
.unwrap()
151148
})
@@ -159,10 +156,7 @@ fn get_bit_or_doc() -> &'static Documentation {
159156
.with_doc_section(DOC_SECTION_GENERAL)
160157
.with_description("Computes the bitwise OR of all non-null input values.")
161158
.with_syntax_example("bit_or(expression)")
162-
.with_argument(
163-
"expression",
164-
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
165-
)
159+
.with_standard_argument("expression", "Integer")
166160
.build()
167161
.unwrap()
168162
})
@@ -174,12 +168,11 @@ fn get_bit_xor_doc() -> &'static Documentation {
174168
BIT_XOR_DOC.get_or_init(|| {
175169
Documentation::builder()
176170
.with_doc_section(DOC_SECTION_GENERAL)
177-
.with_description("Computes the bitwise exclusive OR of all non-null input values.")
178-
.with_syntax_example("bit_xor(expression)")
179-
.with_argument(
180-
"expression",
181-
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
171+
.with_description(
172+
"Computes the bitwise exclusive OR of all non-null input values.",
182173
)
174+
.with_syntax_example("bit_xor(expression)")
175+
.with_standard_argument("expression", "Integer")
183176
.build()
184177
.unwrap()
185178
})

datafusion/functions/src/crypto/sha224.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ fn get_sha224_doc() -> &'static Documentation {
5858
.with_doc_section(DOC_SECTION_HASHING)
5959
.with_description("Computes the SHA-224 hash of a binary string.")
6060
.with_syntax_example("sha224(expression)")
61-
.with_argument("expression",
62-
"String expression to operate on. Can be a constant, column, or function, and any combination of string operators.")
61+
.with_standard_argument("expression", "String")
6362
.build()
6463
.unwrap()
6564
})

datafusion/functions/src/datetime/to_date.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ Note: `to_date` returns Date32, which represents its values as the number of day
111111
112112
Additional examples can be found [here](https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/to_date.rs)
113113
"#)
114-
.with_argument(
115-
"expression",
116-
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
117-
)
114+
.with_standard_argument("expression", "String")
118115
.with_argument(
119116
"format_n",
120117
"Optional [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) strings to use to parse the expression. Formats will be tried in the order

datafusion/functions/src/math/log.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ fn get_log_doc() -> &'static Documentation {
5757
.with_description("Returns the base-x logarithm of a number. Can either provide a specified base, or if omitted then takes the base-10 of a number.")
5858
.with_syntax_example(r#"log(base, numeric_expression)
5959
log(numeric_expression)"#)
60-
.with_argument("base",
61-
"Base numeric expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.")
62-
.with_argument("numeric_expression",
63-
"Numeric expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.")
60+
.with_standard_argument("base", "Base numeric")
61+
.with_standard_argument("numeric_expression", "Numeric")
6462
.build()
6563
.unwrap()
6664
})

datafusion/functions/src/regex/regexplike.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ SELECT regexp_like('aBc', '(b|d)', 'i');
6767
```
6868
Additional examples can be found [here](https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/regexp.rs)
6969
"#)
70-
.with_argument("str",
71-
"String expression to operate on. Can be a constant, column, or function, and any combination of string operators.")
72-
.with_argument("regexp",
73-
"Regular expression to test against the string expression. Can be a constant, column, or function.")
70+
.with_standard_argument("str", "String")
71+
.with_standard_argument("regexp","Regular")
7472
.with_argument("flags",
7573
r#"Optional regular expression flags that control the behavior of the regular expression. The following flags are supported:
7674
- **i**: case-insensitive: letters match both upper and lower case

datafusion/functions/src/unicode/rpad.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ fn get_rpad_doc() -> &'static Documentation {
5555
.with_doc_section(DOC_SECTION_STRING)
5656
.with_description("Pads the right side of a string with another string to a specified string length.")
5757
.with_syntax_example("rpad(str, n[, padding_str])")
58-
.with_argument(
58+
.with_standard_argument(
5959
"str",
60-
"String expression to operate on. Can be a constant, column, or function, and any combination of string operators.",
60+
"String",
6161
)
6262
.with_argument("n", "String length to pad to.")
6363
.with_argument("padding_str",

docs/source/user-guide/sql/aggregate_functions_new.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bit_and(expression)
4747

4848
#### Arguments
4949

50-
- **expression**: Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
50+
- **expression**: Integer expression to operate on. Can be a constant, column, or function, and any combination of operators.
5151

5252
### `bit_or`
5353

@@ -59,7 +59,7 @@ bit_or(expression)
5959

6060
#### Arguments
6161

62-
- **expression**: Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
62+
- **expression**: Integer expression to operate on. Can be a constant, column, or function, and any combination of operators.
6363

6464
### `bit_xor`
6565

@@ -71,4 +71,4 @@ bit_xor(expression)
7171

7272
#### Arguments
7373

74-
- **expression**: Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
74+
- **expression**: Integer expression to operate on. Can be a constant, column, or function, and any combination of operators.

docs/source/user-guide/sql/scalar_functions_new.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ log(numeric_expression)
4444

4545
#### Arguments
4646

47-
- **base**: Base numeric expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
48-
- **numeric_expression**: Numeric expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
47+
- **base**: Base numeric expression to operate on. Can be a constant, column, or function, and any combination of operators.
48+
- **numeric_expression**: Numeric expression to operate on. Can be a constant, column, or function, and any combination of operators.
4949

5050
## Conditional Functions
5151

@@ -94,7 +94,7 @@ rpad(str, n[, padding_str])
9494

9595
#### Arguments
9696

97-
- **str**: String expression to operate on. Can be a constant, column, or function, and any combination of string operators.
97+
- **str**: String expression to operate on. Can be a constant, column, or function, and any combination of operators.
9898
- **n**: String length to pad to.
9999
- **padding_str**: String expression to pad with. Can be a constant, column, or function, and any combination of string operators. _Default is a space._
100100

@@ -160,8 +160,8 @@ regexp_like(str, regexp[, flags])
160160

161161
#### Arguments
162162

163-
- **str**: String expression to operate on. Can be a constant, column, or function, and any combination of string operators.
164-
- **regexp**: Regular expression to test against the string expression. Can be a constant, column, or function.
163+
- **str**: String expression to operate on. Can be a constant, column, or function, and any combination of operators.
164+
- **regexp**: Regular expression to operate on. Can be a constant, column, or function, and any combination of operators.
165165
- **flags**: Optional regular expression flags that control the behavior of the regular expression. The following flags are supported:
166166
- **i**: case-insensitive: letters match both upper and lower case
167167
- **m**: multi-line mode: ^ and $ match begin/end of line
@@ -208,7 +208,7 @@ to_date('2017-05-31', '%Y-%m-%d')
208208

209209
#### Arguments
210210

211-
- **expression**: Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
211+
- **expression**: String expression to operate on. Can be a constant, column, or function, and any combination of operators.
212212
- **format_n**: Optional [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) strings to use to parse the expression. Formats will be tried in the order
213213
they appear with the first successful one being returned. If none of the formats successfully parse the expression
214214
an error will be returned.
@@ -246,4 +246,4 @@ sha224(expression)
246246

247247
#### Arguments
248248

249-
- **expression**: String expression to operate on. Can be a constant, column, or function, and any combination of string operators.
249+
- **expression**: String expression to operate on. Can be a constant, column, or function, and any combination of operators.

0 commit comments

Comments
 (0)