Skip to content

Commit 91899d4

Browse files
jonathanc-nalamb
andauthored
Migrate Array function Documentation to code (#12948)
* Migrate Array * p fix * Update datafusion/functions-nested/src/make_array.rs Co-authored-by: Andrew Lamb <[email protected]> * Update docs --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent caeabc1 commit 91899d4

23 files changed

+2996
-1499
lines changed

datafusion/functions-nested/src/array_has.rs

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +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::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};
28+
use datafusion_expr::scalar_doc_sections::DOC_SECTION_ARRAY;
29+
use datafusion_expr::{
30+
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
31+
};
2932
use datafusion_physical_expr_common::datum::compare_with_eq;
3033
use itertools::Itertools;
3134

3235
use crate::utils::make_scalar_function;
3336

3437
use std::any::Any;
35-
use std::sync::Arc;
38+
use std::sync::{Arc, OnceLock};
3639

3740
// Create static instances of ScalarUDFs for each function
3841
make_udf_expr_and_func!(ArrayHas,
@@ -129,6 +132,43 @@ impl ScalarUDFImpl for ArrayHas {
129132
fn aliases(&self) -> &[String] {
130133
&self.aliases
131134
}
135+
136+
fn documentation(&self) -> Option<&Documentation> {
137+
Some(get_array_has_doc())
138+
}
139+
}
140+
141+
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
142+
143+
fn get_array_has_doc() -> &'static Documentation {
144+
DOCUMENTATION.get_or_init(|| {
145+
Documentation::builder()
146+
.with_doc_section(DOC_SECTION_ARRAY)
147+
.with_description(
148+
"Returns true if the array contains the element.",
149+
)
150+
.with_syntax_example("array_has(array, element)")
151+
.with_sql_example(
152+
r#"```sql
153+
> select array_has([1, 2, 3], 2);
154+
+-----------------------------+
155+
| array_has(List([1,2,3]), 2) |
156+
+-----------------------------+
157+
| true |
158+
+-----------------------------+
159+
```"#,
160+
)
161+
.with_argument(
162+
"array",
163+
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
164+
)
165+
.with_argument(
166+
"element",
167+
"Scalar or Array expression. Can be a constant, column, or function, and any combination of array operators.",
168+
)
169+
.build()
170+
.unwrap()
171+
})
132172
}
133173

134174
fn array_has_inner_for_scalar(
@@ -289,6 +329,41 @@ impl ScalarUDFImpl for ArrayHasAll {
289329
fn aliases(&self) -> &[String] {
290330
&self.aliases
291331
}
332+
333+
fn documentation(&self) -> Option<&Documentation> {
334+
Some(get_array_has_all_doc())
335+
}
336+
}
337+
338+
fn get_array_has_all_doc() -> &'static Documentation {
339+
DOCUMENTATION.get_or_init(|| {
340+
Documentation::builder()
341+
.with_doc_section(DOC_SECTION_ARRAY)
342+
.with_description(
343+
"Returns true if all elements of sub-array exist in array.",
344+
)
345+
.with_syntax_example("array_has_all(array, sub-array)")
346+
.with_sql_example(
347+
r#"```sql
348+
> select array_has_all([1, 2, 3, 4], [2, 3]);
349+
+--------------------------------------------+
350+
| array_has_all(List([1,2,3,4]), List([2,3])) |
351+
+--------------------------------------------+
352+
| true |
353+
+--------------------------------------------+
354+
```"#,
355+
)
356+
.with_argument(
357+
"array",
358+
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
359+
)
360+
.with_argument(
361+
"sub-array",
362+
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
363+
)
364+
.build()
365+
.unwrap()
366+
})
292367
}
293368

294369
#[derive(Debug)]
@@ -335,6 +410,41 @@ impl ScalarUDFImpl for ArrayHasAny {
335410
fn aliases(&self) -> &[String] {
336411
&self.aliases
337412
}
413+
414+
fn documentation(&self) -> Option<&Documentation> {
415+
Some(get_array_has_any_doc())
416+
}
417+
}
418+
419+
fn get_array_has_any_doc() -> &'static Documentation {
420+
DOCUMENTATION.get_or_init(|| {
421+
Documentation::builder()
422+
.with_doc_section(DOC_SECTION_ARRAY)
423+
.with_description(
424+
"Returns true if any elements exist in both arrays.",
425+
)
426+
.with_syntax_example("array_has_any(array, sub-array)")
427+
.with_sql_example(
428+
r#"```sql
429+
> select array_has_any([1, 2, 3], [3, 4]);
430+
+------------------------------------------+
431+
| array_has_any(List([1,2,3]), List([3,4])) |
432+
+------------------------------------------+
433+
| true |
434+
+------------------------------------------+
435+
```"#,
436+
)
437+
.with_argument(
438+
"array",
439+
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
440+
)
441+
.with_argument(
442+
"sub-array",
443+
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
444+
)
445+
.build()
446+
.unwrap()
447+
})
338448
}
339449

340450
/// Represents the type of comparison for array_has.

datafusion/functions-nested/src/cardinality.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +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;
2930
use datafusion_expr::{
30-
ArrayFunctionSignature, ColumnarValue, ScalarUDFImpl, Signature, TypeSignature,
31-
Volatility,
31+
ArrayFunctionSignature, ColumnarValue, Documentation, ScalarUDFImpl, Signature,
32+
TypeSignature, Volatility,
3233
};
3334
use std::any::Any;
34-
use std::sync::Arc;
35+
use std::sync::{Arc, OnceLock};
3536

3637
make_udf_expr_and_func!(
3738
Cardinality,
@@ -89,6 +90,39 @@ impl ScalarUDFImpl for Cardinality {
8990
fn aliases(&self) -> &[String] {
9091
&self.aliases
9192
}
93+
94+
fn documentation(&self) -> Option<&Documentation> {
95+
Some(get_cardinality_doc())
96+
}
97+
}
98+
99+
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
100+
101+
fn get_cardinality_doc() -> &'static Documentation {
102+
DOCUMENTATION.get_or_init(|| {
103+
Documentation::builder()
104+
.with_doc_section(DOC_SECTION_ARRAY)
105+
.with_description(
106+
"Returns the total number of elements in the array.",
107+
)
108+
.with_syntax_example("cardinality(array)")
109+
.with_sql_example(
110+
r#"```sql
111+
> select cardinality([[1, 2, 3, 4], [5, 6, 7, 8]]);
112+
+--------------------------------------+
113+
| cardinality(List([1,2,3,4,5,6,7,8])) |
114+
+--------------------------------------+
115+
| 8 |
116+
+--------------------------------------+
117+
```"#,
118+
)
119+
.with_argument(
120+
"array",
121+
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
122+
)
123+
.build()
124+
.unwrap()
125+
})
92126
}
93127

94128
/// Cardinality SQL function

datafusion/functions-nested/src/concat.rs

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
//! [`ScalarUDFImpl`] definitions for `array_append`, `array_prepend` and `array_concat` functions.
1919
20-
use std::{any::Any, cmp::Ordering, sync::Arc};
20+
use std::sync::{Arc, OnceLock};
21+
use std::{any::Any, cmp::Ordering};
2122

2223
use arrow::array::{Capacities, MutableArrayData};
2324
use arrow_array::{Array, ArrayRef, GenericListArray, OffsetSizeTrait};
@@ -27,9 +28,10 @@ use datafusion_common::Result;
2728
use datafusion_common::{
2829
cast::as_generic_list_array, exec_err, not_impl_err, plan_err, utils::list_ndims,
2930
};
31+
use datafusion_expr::scalar_doc_sections::DOC_SECTION_ARRAY;
3032
use datafusion_expr::{
31-
type_coercion::binary::get_wider_type, ColumnarValue, ScalarUDFImpl, Signature,
32-
Volatility,
33+
type_coercion::binary::get_wider_type, ColumnarValue, Documentation, ScalarUDFImpl,
34+
Signature, Volatility,
3335
};
3436

3537
use crate::utils::{align_array_dimensions, check_datatypes, make_scalar_function};
@@ -91,6 +93,43 @@ impl ScalarUDFImpl for ArrayAppend {
9193
fn aliases(&self) -> &[String] {
9294
&self.aliases
9395
}
96+
97+
fn documentation(&self) -> Option<&Documentation> {
98+
Some(get_array_append_doc())
99+
}
100+
}
101+
102+
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
103+
104+
fn get_array_append_doc() -> &'static Documentation {
105+
DOCUMENTATION.get_or_init(|| {
106+
Documentation::builder()
107+
.with_doc_section(DOC_SECTION_ARRAY)
108+
.with_description(
109+
"Appends an element to the end of an array.",
110+
)
111+
.with_syntax_example("array_append(array, element)")
112+
.with_sql_example(
113+
r#"```sql
114+
> select array_append([1, 2, 3], 4);
115+
+--------------------------------------+
116+
| array_append(List([1,2,3]),Int64(4)) |
117+
+--------------------------------------+
118+
| [1, 2, 3, 4] |
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+
.with_argument(
127+
"element",
128+
"Element to append to the array.",
129+
)
130+
.build()
131+
.unwrap()
132+
})
94133
}
95134

96135
make_udf_expr_and_func!(
@@ -150,6 +189,41 @@ impl ScalarUDFImpl for ArrayPrepend {
150189
fn aliases(&self) -> &[String] {
151190
&self.aliases
152191
}
192+
193+
fn documentation(&self) -> Option<&Documentation> {
194+
Some(get_array_prepend_doc())
195+
}
196+
}
197+
198+
fn get_array_prepend_doc() -> &'static Documentation {
199+
DOCUMENTATION.get_or_init(|| {
200+
Documentation::builder()
201+
.with_doc_section(DOC_SECTION_ARRAY)
202+
.with_description(
203+
"Prepends an element to the beginning of an array.",
204+
)
205+
.with_syntax_example("array_prepend(element, array)")
206+
.with_sql_example(
207+
r#"```sql
208+
> select array_prepend(1, [2, 3, 4]);
209+
+---------------------------------------+
210+
| array_prepend(Int64(1),List([2,3,4])) |
211+
+---------------------------------------+
212+
| [1, 2, 3, 4] |
213+
+---------------------------------------+
214+
```"#,
215+
)
216+
.with_argument(
217+
"element",
218+
"Element to prepend to the array.",
219+
)
220+
.with_argument(
221+
"array",
222+
"Array expression. Can be a constant, column, or function, and any combination of array operators.",
223+
)
224+
.build()
225+
.unwrap()
226+
})
153227
}
154228

155229
make_udf_expr_and_func!(
@@ -233,6 +307,41 @@ impl ScalarUDFImpl for ArrayConcat {
233307
fn aliases(&self) -> &[String] {
234308
&self.aliases
235309
}
310+
311+
fn documentation(&self) -> Option<&Documentation> {
312+
Some(get_array_concat_doc())
313+
}
314+
}
315+
316+
fn get_array_concat_doc() -> &'static Documentation {
317+
DOCUMENTATION.get_or_init(|| {
318+
Documentation::builder()
319+
.with_doc_section(DOC_SECTION_ARRAY)
320+
.with_description(
321+
"Concatenates arrays.",
322+
)
323+
.with_syntax_example("array_concat(array[, ..., array_n])")
324+
.with_sql_example(
325+
r#"```sql
326+
> select array_concat([1, 2], [3, 4], [5, 6]);
327+
+---------------------------------------------------+
328+
| array_concat(List([1,2]),List([3,4]),List([5,6])) |
329+
+---------------------------------------------------+
330+
| [1, 2, 3, 4, 5, 6] |
331+
+---------------------------------------------------+
332+
```"#,
333+
)
334+
.with_argument(
335+
"array",
336+
"Array expression to concatenate. Can be a constant, column, or function, and any combination of array operators.",
337+
)
338+
.with_argument(
339+
"array_n",
340+
"Subsequent array column or literal array to concatenate.",
341+
)
342+
.build()
343+
.unwrap()
344+
})
236345
}
237346

238347
/// Array_concat/Array_cat SQL function

0 commit comments

Comments
 (0)