Skip to content

Commit 7833125

Browse files
authored
docs: show array_distinct keeping first-seen order (#25930)
## Which issue does this PR close? - None; documentation fix. ## Rationale for this change The `array_distinct` example says `select array_distinct([1, 3, 2, 3, 1, 2, 4])` returns `[1, 2, 3, 4]`. It returns `[1, 3, 2, 4]`: #20364 changed it to keep elements in the order they first appear, matching `array_union` and `array_intersect`, and the example was not updated. ## What changes are included in this PR? The `#[user_doc]` example and description in `set_ops.rs`, the matching code comment, and `scalar_functions.md` regenerated with `./dev/update_function_docs.sh`. Also a sqllogictest case for the order, since every existing `array_distinct` case used input whose sorted and first-seen orders coincide. ## What is the testing strategy for this PR? Ran the example against `datafusion-cli` built from `main`. The new case in `array_distinct.slt` passes; `cargo fmt --check`, `cargo clippy -p datafusion-functions-nested --all-targets -- -D warnings` and `./ci/scripts/doc_prettier_check.sh` pass. ## Are there any user-facing changes? Documentation and a test only. Generated with Claude Code (claude-opus-5-5); I checked the output above.
1 parent d9d7be8 commit 7833125

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

‎datafusion/functions-nested/src/set_ops.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,15 @@ impl ScalarUDFImpl for ArrayIntersect {
234234

235235
#[user_doc(
236236
doc_section(label = "Array Functions"),
237-
description = "Returns distinct values from the array after removing duplicates.",
237+
description = "Returns distinct values from the array after removing duplicates, in the order they first appear.",
238238
syntax_example = "array_distinct(array)",
239239
sql_example = r#"```sql
240240
> select array_distinct([1, 3, 2, 3, 1, 2, 4]);
241-
+---------------------------------+
242-
| array_distinct(List([1,2,3,4])) |
243-
+---------------------------------+
244-
| [1, 2, 3, 4] |
245-
+---------------------------------+
241+
+---------------------------------------+
242+
| array_distinct(List([1,3,2,3,1,2,4])) |
243+
+---------------------------------------+
244+
| [1, 3, 2, 4] |
245+
+---------------------------------------+
246246
```"#,
247247
argument(
248248
name = "array",
@@ -297,7 +297,7 @@ impl ScalarUDFImpl for ArrayDistinct {
297297
}
298298

299299
/// array_distinct SQL function
300-
/// example: from list [1, 3, 2, 3, 1, 2, 4] to [1, 2, 3, 4]
300+
/// example: from list [1, 3, 2, 3, 1, 2, 4] to [1, 3, 2, 4]
301301
fn array_distinct_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
302302
let [array] = take_function_args("array_distinct", args)?;
303303
match array.data_type() {

‎datafusion/sqllogictest/test_files/array/array_distinct.slt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ select array_distinct(a) from values ([1, 2, 3]), (null), ([1, 3, 1]) as X(a);
4545
NULL
4646
[1, 3]
4747

48+
# elements keep the order they first appear in
49+
query ?
50+
select array_distinct([1, 3, 2, 3, 1, 2, 4]);
51+
----
52+
[1, 3, 2, 4]
53+
4854
query ?
4955
select array_distinct(arrow_cast(null, 'LargeList(Int64)'));
5056
----

‎docs/source/user-guide/sql/scalar_functions.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,7 +3644,7 @@ array_distance(array1, array2)
36443644

36453645
### `array_distinct`
36463646

3647-
Returns distinct values from the array after removing duplicates.
3647+
Returns distinct values from the array after removing duplicates, in the order they first appear.
36483648

36493649
```sql
36503650
array_distinct(array)
@@ -3658,11 +3658,11 @@ array_distinct(array)
36583658

36593659
```sql
36603660
> select array_distinct([1, 3, 2, 3, 1, 2, 4]);
3661-
+---------------------------------+
3662-
| array_distinct(List([1,2,3,4])) |
3663-
+---------------------------------+
3664-
| [1, 2, 3, 4] |
3665-
+---------------------------------+
3661+
+---------------------------------------+
3662+
| array_distinct(List([1,3,2,3,1,2,4])) |
3663+
+---------------------------------------+
3664+
| [1, 3, 2, 4] |
3665+
+---------------------------------------+
36663666
```
36673667

36683668
#### Aliases

0 commit comments

Comments
 (0)