Skip to content

Commit 6a92870

Browse files
Chen-Yuan-LaiCheng-Yuan-Lai
andauthored
doc-gen: migrate scalar functions (array) documentation 1/3 (#13928)
* doc-gen: migrate scalar functions (array) documentation 1/3 * fix: remove unsed import, fix typo and update function docs --------- Co-authored-by: Cheng-Yuan-Lai <a186235@g,ail.com>
1 parent a47729c commit 6a92870

File tree

11 files changed

+446
-605
lines changed

11 files changed

+446
-605
lines changed

datafusion-cli/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datafusion/functions-nested/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ arrow-buffer = { workspace = true }
4646
arrow-ord = { workspace = true }
4747
arrow-schema = { workspace = true }
4848
datafusion-common = { workspace = true }
49+
datafusion-doc = { workspace = true }
4950
datafusion-execution = { workspace = true }
5051
datafusion-expr = { workspace = true }
5152
datafusion-functions = { workspace = true }
5253
datafusion-functions-aggregate = { workspace = true }
54+
datafusion-macros = { workspace = true }
5355
datafusion-physical-expr-common = { workspace = true }
5456
itertools = { workspace = true, features = ["use_std"] }
5557
log = { workspace = true }

datafusion/functions-nested/src/array_has.rs

Lines changed: 67 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ use arrow_buffer::BooleanBuffer;
2525
use datafusion_common::cast::as_generic_list_array;
2626
use datafusion_common::utils::string_utils::string_array_to_vec;
2727
use datafusion_common::{exec_err, Result, ScalarValue};
28-
use datafusion_expr::scalar_doc_sections::DOC_SECTION_ARRAY;
2928
use datafusion_expr::{
3029
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
3130
};
31+
use datafusion_macros::user_doc;
3232
use datafusion_physical_expr_common::datum::compare_with_eq;
3333
use itertools::Itertools;
3434

3535
use crate::utils::make_scalar_function;
3636

3737
use std::any::Any;
38-
use std::sync::{Arc, OnceLock};
38+
use std::sync::Arc;
3939

4040
// Create static instances of ScalarUDFs for each function
4141
make_udf_expr_and_func!(ArrayHas,
@@ -57,6 +57,27 @@ make_udf_expr_and_func!(ArrayHasAny,
5757
array_has_any_udf // internal function name
5858
);
5959

60+
#[user_doc(
61+
doc_section(label = "Array Functions"),
62+
description = "Returns true if the array contains the element.",
63+
syntax_example = "array_has(array, element)",
64+
sql_example = r#"```sql
65+
> select array_has([1, 2, 3], 2);
66+
+-----------------------------+
67+
| array_has(List([1,2,3]), 2) |
68+
+-----------------------------+
69+
| true |
70+
+-----------------------------+
71+
```"#,
72+
argument(
73+
name = "array",
74+
description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
75+
),
76+
argument(
77+
name = "element",
78+
description = "Scalar or Array expression. Can be a constant, column, or function, and any combination of array operators."
79+
)
80+
)]
6081
#[derive(Debug)]
6182
pub struct ArrayHas {
6283
signature: Signature,
@@ -138,41 +159,10 @@ impl ScalarUDFImpl for ArrayHas {
138159
}
139160

140161
fn documentation(&self) -> Option<&Documentation> {
141-
Some(get_array_has_doc())
162+
self.doc()
142163
}
143164
}
144165

145-
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
146-
147-
fn get_array_has_doc() -> &'static Documentation {
148-
DOCUMENTATION.get_or_init(|| {
149-
Documentation::builder(
150-
DOC_SECTION_ARRAY,
151-
"Returns true if the array contains the element.",
152-
153-
"array_has(array, element)")
154-
.with_sql_example(
155-
r#"```sql
156-
> select array_has([1, 2, 3], 2);
157-
+-----------------------------+
158-
| array_has(List([1,2,3]), 2) |
159-
+-----------------------------+
160-
| true |
161-
+-----------------------------+
162-
```"#,
163-
)
164-
.with_argument(
165-
"array",
166-
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
167-
)
168-
.with_argument(
169-
"element",
170-
"Scalar or Array expression. Can be a constant, column, or function, and any combination of array operators.",
171-
)
172-
.build()
173-
})
174-
}
175-
176166
fn array_has_inner_for_scalar(
177167
haystack: &ArrayRef,
178168
needle: &dyn Datum,
@@ -287,6 +277,27 @@ fn array_has_any_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
287277
}
288278
}
289279

280+
#[user_doc(
281+
doc_section(label = "Array Functions"),
282+
description = "Returns true if all elements of sub-array exist in array.",
283+
syntax_example = "array_has_all(array, sub-array)",
284+
sql_example = r#"```sql
285+
> select array_has_all([1, 2, 3, 4], [2, 3]);
286+
+--------------------------------------------+
287+
| array_has_all(List([1,2,3,4]), List([2,3])) |
288+
+--------------------------------------------+
289+
| true |
290+
+--------------------------------------------+
291+
```"#,
292+
argument(
293+
name = "array",
294+
description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
295+
),
296+
argument(
297+
name = "sub-array",
298+
description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
299+
)
300+
)]
290301
#[derive(Debug)]
291302
pub struct ArrayHasAll {
292303
signature: Signature,
@@ -337,39 +348,31 @@ impl ScalarUDFImpl for ArrayHasAll {
337348
}
338349

339350
fn documentation(&self) -> Option<&Documentation> {
340-
Some(get_array_has_all_doc())
351+
self.doc()
341352
}
342353
}
343354

344-
fn get_array_has_all_doc() -> &'static Documentation {
345-
DOCUMENTATION.get_or_init(|| {
346-
Documentation::builder(
347-
DOC_SECTION_ARRAY,
348-
"Returns true if all elements of sub-array exist in array.",
349-
350-
"array_has_all(array, sub-array)")
351-
.with_sql_example(
352-
r#"```sql
353-
> select array_has_all([1, 2, 3, 4], [2, 3]);
354-
+--------------------------------------------+
355-
| array_has_all(List([1,2,3,4]), List([2,3])) |
356-
+--------------------------------------------+
357-
| true |
358-
+--------------------------------------------+
355+
#[user_doc(
356+
doc_section(label = "Array Functions"),
357+
description = "Returns true if any elements exist in both arrays.",
358+
syntax_example = "array_has_any(array, sub-array)",
359+
sql_example = r#"```sql
360+
> select array_has_any([1, 2, 3], [3, 4]);
361+
+------------------------------------------+
362+
| array_has_any(List([1,2,3]), List([3,4])) |
363+
+------------------------------------------+
364+
| true |
365+
+------------------------------------------+
359366
```"#,
360-
)
361-
.with_argument(
362-
"array",
363-
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
364-
)
365-
.with_argument(
366-
"sub-array",
367-
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
368-
)
369-
.build()
370-
})
371-
}
372-
367+
argument(
368+
name = "array",
369+
description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
370+
),
371+
argument(
372+
name = "sub-array",
373+
description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
374+
)
375+
)]
373376
#[derive(Debug)]
374377
pub struct ArrayHasAny {
375378
signature: Signature,
@@ -420,39 +423,10 @@ impl ScalarUDFImpl for ArrayHasAny {
420423
}
421424

422425
fn documentation(&self) -> Option<&Documentation> {
423-
Some(get_array_has_any_doc())
426+
self.doc()
424427
}
425428
}
426429

427-
fn get_array_has_any_doc() -> &'static Documentation {
428-
DOCUMENTATION.get_or_init(|| {
429-
Documentation::builder(
430-
DOC_SECTION_ARRAY,
431-
"Returns true if any elements exist in both arrays.",
432-
433-
"array_has_any(array, sub-array)")
434-
.with_sql_example(
435-
r#"```sql
436-
> select array_has_any([1, 2, 3], [3, 4]);
437-
+------------------------------------------+
438-
| array_has_any(List([1,2,3]), List([3,4])) |
439-
+------------------------------------------+
440-
| true |
441-
+------------------------------------------+
442-
```"#,
443-
)
444-
.with_argument(
445-
"array",
446-
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
447-
)
448-
.with_argument(
449-
"sub-array",
450-
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
451-
)
452-
.build()
453-
})
454-
}
455-
456430
/// Represents the type of comparison for array_has.
457431
#[derive(Debug, PartialEq, Clone, Copy)]
458432
enum ComparisonType {

datafusion/functions-nested/src/cardinality.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ use arrow_schema::DataType::{FixedSizeList, LargeList, List, Map, UInt64};
2626
use datafusion_common::cast::{as_large_list_array, as_list_array, as_map_array};
2727
use datafusion_common::Result;
2828
use datafusion_common::{exec_err, plan_err};
29-
use datafusion_expr::scalar_doc_sections::DOC_SECTION_ARRAY;
3029
use datafusion_expr::{
3130
ArrayFunctionSignature, ColumnarValue, Documentation, ScalarUDFImpl, Signature,
3231
TypeSignature, Volatility,
3332
};
33+
use datafusion_macros::user_doc;
3434
use std::any::Any;
35-
use std::sync::{Arc, OnceLock};
35+
use std::sync::Arc;
3636

3737
make_udf_expr_and_func!(
3838
Cardinality,
@@ -57,6 +57,23 @@ impl Cardinality {
5757
}
5858
}
5959

60+
#[user_doc(
61+
doc_section(label = "Array Functions"),
62+
description = "Returns the total number of elements in the array.",
63+
syntax_example = "cardinality(array)",
64+
sql_example = r#"```sql
65+
> select cardinality([[1, 2, 3, 4], [5, 6, 7, 8]]);
66+
+--------------------------------------+
67+
| cardinality(List([1,2,3,4,5,6,7,8])) |
68+
+--------------------------------------+
69+
| 8 |
70+
+--------------------------------------+
71+
```"#,
72+
argument(
73+
name = "array",
74+
description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
75+
)
76+
)]
6077
#[derive(Debug)]
6178
pub(super) struct Cardinality {
6279
signature: Signature,
@@ -96,37 +113,10 @@ impl ScalarUDFImpl for Cardinality {
96113
}
97114

98115
fn documentation(&self) -> Option<&Documentation> {
99-
Some(get_cardinality_doc())
116+
self.doc()
100117
}
101118
}
102119

103-
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
104-
105-
fn get_cardinality_doc() -> &'static Documentation {
106-
DOCUMENTATION.get_or_init(|| {
107-
Documentation::builder(
108-
DOC_SECTION_ARRAY,
109-
"Returns the total number of elements in the array.",
110-
111-
"cardinality(array)")
112-
.with_sql_example(
113-
r#"```sql
114-
> select cardinality([[1, 2, 3, 4], [5, 6, 7, 8]]);
115-
+--------------------------------------+
116-
| cardinality(List([1,2,3,4,5,6,7,8])) |
117-
+--------------------------------------+
118-
| 8 |
119-
+--------------------------------------+
120-
```"#,
121-
)
122-
.with_argument(
123-
"array",
124-
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
125-
)
126-
.build()
127-
})
128-
}
129-
130120
/// Cardinality SQL function
131121
pub fn cardinality_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
132122
if args.len() != 1 {

0 commit comments

Comments
 (0)