Skip to content

Commit

Permalink
Add DocumentationBuilder::with_standard_argument to reduce copy/pas…
Browse files Browse the repository at this point in the history
…te (#12747)

* Add `DocumentationBuilder::with_standard_expression` to reduce copy/paste

* fix doc

* fix standard argument

* Update docs

* Improve documentation to explain what is different
  • Loading branch information
alamb authored Oct 5, 2024
1 parent 8aafa54 commit 030c4e9
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 39 deletions.
24 changes: 24 additions & 0 deletions datafusion/expr/src/udf_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ impl DocumentationBuilder {
self
}

/// Adds documentation for a specific argument to the documentation.
///
/// Arguments are displayed in the order they are added.
pub fn with_argument(
mut self,
arg_name: impl Into<String>,
Expand All @@ -142,6 +145,27 @@ impl DocumentationBuilder {
self
}

/// Add a standard "expression" argument to the documentation
///
/// This is similar to [`Self::with_argument`] except that a standard
/// description is appended to the end: `"Can be a constant, column, or
/// function, and any combination of arithmetic operators."`
///
/// The argument is rendered like
///
/// ```text
/// <arg_name>:
/// <expression_type> expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
/// ```
pub fn with_standard_argument(
self,
arg_name: impl Into<String>,
expression_type: impl AsRef<str>,
) -> Self {
let expression_type = expression_type.as_ref();
self.with_argument(arg_name, format!("{expression_type} expression to operate on. Can be a constant, column, or function, and any combination of operators."))
}

pub fn with_related_udf(mut self, related_udf: impl Into<String>) -> Self {
let mut related = self.related_udfs.unwrap_or_default();
related.push(related_udf.into());
Expand Down
19 changes: 6 additions & 13 deletions datafusion/functions-aggregate/src/bit_and_or_xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ fn get_bit_and_doc() -> &'static Documentation {
.with_doc_section(DOC_SECTION_GENERAL)
.with_description("Computes the bitwise AND of all non-null input values.")
.with_syntax_example("bit_and(expression)")
.with_argument(
"expression",
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
)
.with_standard_argument("expression", "Integer")
.build()
.unwrap()
})
Expand All @@ -159,10 +156,7 @@ fn get_bit_or_doc() -> &'static Documentation {
.with_doc_section(DOC_SECTION_GENERAL)
.with_description("Computes the bitwise OR of all non-null input values.")
.with_syntax_example("bit_or(expression)")
.with_argument(
"expression",
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
)
.with_standard_argument("expression", "Integer")
.build()
.unwrap()
})
Expand All @@ -174,12 +168,11 @@ fn get_bit_xor_doc() -> &'static Documentation {
BIT_XOR_DOC.get_or_init(|| {
Documentation::builder()
.with_doc_section(DOC_SECTION_GENERAL)
.with_description("Computes the bitwise exclusive OR of all non-null input values.")
.with_syntax_example("bit_xor(expression)")
.with_argument(
"expression",
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
.with_description(
"Computes the bitwise exclusive OR of all non-null input values.",
)
.with_syntax_example("bit_xor(expression)")
.with_standard_argument("expression", "Integer")
.build()
.unwrap()
})
Expand Down
3 changes: 1 addition & 2 deletions datafusion/functions/src/crypto/sha224.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ fn get_sha224_doc() -> &'static Documentation {
.with_doc_section(DOC_SECTION_HASHING)
.with_description("Computes the SHA-224 hash of a binary string.")
.with_syntax_example("sha224(expression)")
.with_argument("expression",
"String expression to operate on. Can be a constant, column, or function, and any combination of string operators.")
.with_standard_argument("expression", "String")
.build()
.unwrap()
})
Expand Down
5 changes: 1 addition & 4 deletions datafusion/functions/src/datetime/to_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ Note: `to_date` returns Date32, which represents its values as the number of day
Additional examples can be found [here](https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/to_date.rs)
"#)
.with_argument(
"expression",
"Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.",
)
.with_standard_argument("expression", "String")
.with_argument(
"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
Expand Down
6 changes: 2 additions & 4 deletions datafusion/functions/src/math/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ fn get_log_doc() -> &'static Documentation {
.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.")
.with_syntax_example(r#"log(base, numeric_expression)
log(numeric_expression)"#)
.with_argument("base",
"Base numeric expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.")
.with_argument("numeric_expression",
"Numeric expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.")
.with_standard_argument("base", "Base numeric")
.with_standard_argument("numeric_expression", "Numeric")
.build()
.unwrap()
})
Expand Down
6 changes: 2 additions & 4 deletions datafusion/functions/src/regex/regexplike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ SELECT regexp_like('aBc', '(b|d)', 'i');
```
Additional examples can be found [here](https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/regexp.rs)
"#)
.with_argument("str",
"String expression to operate on. Can be a constant, column, or function, and any combination of string operators.")
.with_argument("regexp",
"Regular expression to test against the string expression. Can be a constant, column, or function.")
.with_standard_argument("str", "String")
.with_standard_argument("regexp","Regular")
.with_argument("flags",
r#"Optional regular expression flags that control the behavior of the regular expression. The following flags are supported:
- **i**: case-insensitive: letters match both upper and lower case
Expand Down
4 changes: 2 additions & 2 deletions datafusion/functions/src/unicode/rpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ fn get_rpad_doc() -> &'static Documentation {
.with_doc_section(DOC_SECTION_STRING)
.with_description("Pads the right side of a string with another string to a specified string length.")
.with_syntax_example("rpad(str, n[, padding_str])")
.with_argument(
.with_standard_argument(
"str",
"String expression to operate on. Can be a constant, column, or function, and any combination of string operators.",
"String",
)
.with_argument("n", "String length to pad to.")
.with_argument("padding_str",
Expand Down
6 changes: 3 additions & 3 deletions docs/source/user-guide/sql/aggregate_functions_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bit_and(expression)

#### Arguments

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

### `bit_or`

Expand All @@ -59,7 +59,7 @@ bit_or(expression)

#### Arguments

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

### `bit_xor`

Expand All @@ -71,4 +71,4 @@ bit_xor(expression)

#### Arguments

- **expression**: Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
- **expression**: Integer expression to operate on. Can be a constant, column, or function, and any combination of operators.
14 changes: 7 additions & 7 deletions docs/source/user-guide/sql/scalar_functions_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ log(numeric_expression)

#### Arguments

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

## Conditional Functions

Expand Down Expand Up @@ -94,7 +94,7 @@ rpad(str, n[, padding_str])

#### Arguments

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

Expand Down Expand Up @@ -160,8 +160,8 @@ regexp_like(str, regexp[, flags])

#### Arguments

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

#### Arguments

- **expression**: Expression to operate on. Can be a constant, column, or function, and any combination of arithmetic operators.
- **expression**: String expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **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
they appear with the first successful one being returned. If none of the formats successfully parse the expression
an error will be returned.
Expand Down Expand Up @@ -246,4 +246,4 @@ sha224(expression)

#### Arguments

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

0 comments on commit 030c4e9

Please sign in to comment.