Skip to content

Apache DataFusion SQL Query Engine

docs: show array_distinct keeping first-seen order (#25930)

Roshan Ramani7833125a50ff
diff --git a/datafusion/functions-nested/src/set_ops.rs b/datafusion/functions-nested/src/set_ops.rs
index 66cce98b09..01093555b3 100644
--- a/datafusion/functions-nested/src/set_ops.rs
+++ b/datafusion/functions-nested/src/set_ops.rs
@@ -234,15 +234,15 @@ impl ScalarUDFImpl for ArrayIntersect {
#[user_doc(
doc_section(label = "Array Functions"),
- description = "Returns distinct values from the array after removing duplicates.",
+ description = "Returns distinct values from the array after removing duplicates, in the order they first appear.",
syntax_example = "array_distinct(array)",
sql_example = r#"```sql
> select array_distinct([1, 3, 2, 3, 1, 2, 4]);
-+---------------------------------+
-| array_distinct(List([1,2,3,4])) |
-+---------------------------------+
-| [1, 2, 3, 4] |
-+---------------------------------+
++---------------------------------------+
+| array_distinct(List([1,3,2,3,1,2,4])) |
++---------------------------------------+
+| [1, 3, 2, 4] |
++---------------------------------------+
```"#,
argument(
name = "array",
@@ -297,7 +297,7 @@ impl ScalarUDFImpl for ArrayDistinct {
}
/// array_distinct SQL function
-/// example: from list [1, 3, 2, 3, 1, 2, 4] to [1, 2, 3, 4]
+/// example: from list [1, 3, 2, 3, 1, 2, 4] to [1, 3, 2, 4]
fn array_distinct_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
let [array] = take_function_args("array_distinct", args)?;
match array.data_type() {
diff --git a/datafusion/sqllogictest/test_files/array/array_distinct.slt b/datafusion/sqllogictest/test_files/array/array_distinct.slt
index 2682413cac..f88b9d7cde 100644
--- a/datafusion/sqllogictest/test_files/array/array_distinct.slt
+++ b/datafusion/sqllogictest/test_files/array/array_distinct.slt
@@ -45,6 +45,12 @@ select array_distinct(a) from values ([1, 2, 3]), (null), ([1, 3, 1]) as X(a);
NULL
[1, 3]
+# elements keep the order they first appear in
+query ?
+select array_distinct([1, 3, 2, 3, 1, 2, 4]);
+----
+[1, 3, 2, 4]
+
query ?
select array_distinct(arrow_cast(null, 'LargeList(Int64)'));
----
diff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.md
index 1ba1e8c820..3f5c74fa39 100644
--- a/docs/source/user-guide/sql/scalar_functions.md
+++ b/docs/source/user-guide/sql/scalar_functions.md
@@ -3644,7 +3644,7 @@ array_distance(array1, array2)
### `array_distinct`
-Returns distinct values from the array after removing duplicates.
+Returns distinct values from the array after removing duplicates, in the order they first appear.
```sql
array_distinct(array)
@@ -3658,11 +3658,11 @@ array_distinct(array)
```sql
> select array_distinct([1, 3, 2, 3, 1, 2, 4]);
-+---------------------------------+
-| array_distinct(List([1,2,3,4])) |
-+---------------------------------+
-| [1, 2, 3, 4] |
-+---------------------------------+
++---------------------------------------+
+| array_distinct(List([1,3,2,3,1,2,4])) |
++---------------------------------------+
+| [1, 3, 2, 4] |
++---------------------------------------+
```
#### Aliases