Skip to content

Commit 9d39358

Browse files
authored
migrate btrim to user_doc macro (#13952)
1 parent 2b15ad1 commit 9d39358

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

datafusion/functions/src/string/btrim.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ use arrow::array::{ArrayRef, OffsetSizeTrait};
2121
use arrow::datatypes::DataType;
2222
use datafusion_common::{exec_err, Result};
2323
use datafusion_expr::function::Hint;
24-
use datafusion_expr::scalar_doc_sections::DOC_SECTION_STRING;
2524
use datafusion_expr::{
2625
ColumnarValue, Documentation, ScalarUDFImpl, Signature, TypeSignature, Volatility,
2726
};
27+
use datafusion_macros::user_doc;
2828
use std::any::Any;
29-
use std::sync::OnceLock;
3029

3130
/// Returns the longest string with leading and trailing characters removed. If the characters are not specified, whitespace is removed.
3231
/// btrim('xyxtrimyyx', 'xyz') = 'trim'
@@ -35,6 +34,28 @@ fn btrim<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
3534
general_trim::<T>(args, TrimType::Both, use_string_view)
3635
}
3736

37+
#[user_doc(
38+
doc_section(label = "String Functions"),
39+
description = "Trims the specified trim string from the start and end of a string. If no trim string is provided, all whitespace is removed from the start and end of the input string.",
40+
syntax_example = "btrim(str[, trim_str])",
41+
sql_example = r#"```sql
42+
> select btrim('__datafusion____', '_');
43+
+-------------------------------------------+
44+
| btrim(Utf8("__datafusion____"),Utf8("_")) |
45+
+-------------------------------------------+
46+
| datafusion |
47+
+-------------------------------------------+
48+
```"#,
49+
standard_argument(name = "str", prefix = "String"),
50+
argument(
51+
name = "trim_str",
52+
description = r"String expression to operate on. Can be a constant, column, or function, and any combination of operators. _Default is whitespace characters._"
53+
),
54+
alternative_syntax = "trim(BOTH trim_str FROM str)",
55+
alternative_syntax = "trim(trim_str FROM str)",
56+
related_udf(name = "ltrim"),
57+
related_udf(name = "rtrim")
58+
)]
3859
#[derive(Debug)]
3960
pub struct BTrimFunc {
4061
signature: Signature,
@@ -106,36 +127,10 @@ impl ScalarUDFImpl for BTrimFunc {
106127
}
107128

108129
fn documentation(&self) -> Option<&Documentation> {
109-
Some(get_btrim_doc())
130+
self.doc()
110131
}
111132
}
112133

113-
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
114-
115-
fn get_btrim_doc() -> &'static Documentation {
116-
DOCUMENTATION.get_or_init(|| {
117-
Documentation::builder(
118-
DOC_SECTION_STRING,
119-
"Trims the specified trim string from the start and end of a string. If no trim string is provided, all whitespace is removed from the start and end of the input string.",
120-
"btrim(str[, trim_str])")
121-
.with_sql_example(r#"```sql
122-
> select btrim('__datafusion____', '_');
123-
+-------------------------------------------+
124-
| btrim(Utf8("__datafusion____"),Utf8("_")) |
125-
+-------------------------------------------+
126-
| datafusion |
127-
+-------------------------------------------+
128-
```"#)
129-
.with_standard_argument("str", Some("String"))
130-
.with_argument("trim_str", "String expression to operate on. Can be a constant, column, or function, and any combination of operators. _Default is whitespace characters._")
131-
.with_alternative_syntax("trim(BOTH trim_str FROM str)")
132-
.with_alternative_syntax("trim(trim_str FROM str)")
133-
.with_related_udf("ltrim")
134-
.with_related_udf("rtrim")
135-
.build()
136-
})
137-
}
138-
139134
#[cfg(test)]
140135
mod tests {
141136
use arrow::array::{Array, StringArray, StringViewArray};

datafusion/macros/src/user_doc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {
103103

104104
let mut description: Option<LitStr> = None;
105105
let mut syntax_example: Option<LitStr> = None;
106-
let mut alt_syntax_example: Option<LitStr> = None;
106+
let mut alt_syntax_example: Vec<Option<LitStr>> = vec![];
107107
let mut sql_example: Option<LitStr> = None;
108108
let mut standard_args: Vec<(Option<LitStr>, Option<LitStr>)> = vec![];
109109
let mut udf_args: Vec<(Option<LitStr>, Option<LitStr>)> = vec![];
@@ -131,7 +131,7 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {
131131
syntax_example = Some(meta.value()?.parse()?);
132132
Ok(())
133133
} else if meta.path.is_ident("alternative_syntax") {
134-
alt_syntax_example = Some(meta.value()?.parse()?);
134+
alt_syntax_example.push(Some(meta.value()?.parse()?));
135135
Ok(())
136136
} else if meta.path.is_ident("sql_example") {
137137
sql_example = Some(meta.value()?.parse()?);
@@ -242,7 +242,7 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {
242242
})
243243
.collect::<Vec<_>>();
244244

245-
let alt_syntax_example = alt_syntax_example.map(|syn| {
245+
let alt_syntax_example = alt_syntax_example.iter().map(|syn| {
246246
quote! {
247247
.with_alternative_syntax(#syn)
248248
}
@@ -258,7 +258,7 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {
258258
datafusion_doc::Documentation::builder(datafusion_doc::DocSection { include: #doc_section_include, label: #doc_section_lbl, description: #doc_section_description },
259259
#description.to_string(), #syntax_example.to_string())
260260
#sql_example
261-
#alt_syntax_example
261+
#(#alt_syntax_example)*
262262
#(#standard_args)*
263263
#(#udf_args)*
264264
#(#related_udfs)*

0 commit comments

Comments
 (0)