Implement Spark-compatible `array_sort`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Start by reading native/spark-expr/src/array_funcs/sort_array.rs, which already holds the float-ordering and stable-sort logic to share. Then add a SparkArraySort expression in crates/spark/src/function/array/, register it as spark_array_sort, and document it with #[user_doc]. Write crates/sqllogictest/test_files/array/array_sort.slt covering the NULL-last, NaN, signed-zero and stability cases listed above; done means those cases match Spark 3.4-4.1 output. Leave the lambda form for later.
索引モデルが issue の本文から書いたものです。
説明
Part of #178. Split out of #145, which was closed as a duplicate of #218 (sort_array).
What the function does
array_sort(array[, func]) sorts an array. Without func it uses Spark's default comparator, which sorts ascending and
puts NULL elements last. This differs from sort_array, which puts NULLs first when ascending. The result type is the
input type. With func, the lambda (left, right) -> int is the comparator. It must return INT, or analysis fails with
DATATYPE_MISMATCH.UNEXPECTED_RETURN_TYPE. If it returns NULL, the query fails unless
spark.sql.legacy.allowNullComparisonResultInArraySort (internal, default false) is set, in which case NULL counts as 0.
Spark implements this as ArraySort in sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala:
- It is a higher-order function even without
func.ArraySort.defaultComparatoris the lambda
if(left IS NULL AND right IS NULL, 0, if(left IS NULL, 1, if(right IS NULL, -1, if(left < right, -1, if(left > right, 1, 0))))). - Only top-level NULL elements are placed last. Inside struct and array elements,
<and>order NULLs below non-null values. - Floats compare with
<and>, so NaN is greater than every non-NaN value, all NaNs are equal, and-0.0equals0.0. - It sorts with
java.util.Arrays.sort(Object[], Comparator), which is stable, so equal elements keep their input order.
An array ofNullTypeelements is returned unchanged.
Examples
SELECT array_sort(array('b', 'd', null, 'c', 'a'));
-- ["a","b","c","d",null]
SELECT array_sort(array(5, 6, 1), (left, right) -> case when left < right then -1 when left > right then 1 else 0 end);
-- [1,5,6]
SELECT array_sort(array(3, 1, 2), 1);
-- [3,1,2] (a constant comparator; the stable sort keeps the input order)
SELECT array_sort(array(3, 1, 2), map('compare', 0));
-- error DATATYPE_MISMATCH.UNEXPECTED_RETURN_TYPE
Comet implementation
Comet implements the default-comparator form (SparkArraySort, registered as spark_array_sort). It shares the
float-ordering and stable-sort code with sort_array:
Edge cases to test
- NULL array, empty array, an array of only NULLs, and
array(NULL, NULL)(element typeVOID). - NULLs at the start, middle and end of the input. All of them go last.
- Struct and array elements that hold NULLs. Those NULLs sort first within the element ordering.
- NaN (with different bit patterns),
±Infinityand signed zeros.[0.0, -0.0]and[-0.0, 0.0]keep their order. - Stability for equal elements, for example structs whose fields compare equal.
Differences across Spark versions
- 3.4 and 3.5 report
paramIndex"1"for a non-array argument; 4.0 and 4.1 report it as an ordinal ("first"). - No other differences in
ArraySortbetween 3.4.3, 3.5.8, 4.0.1 and 4.1.1.
Implementation notes
- The lambda form depends on lambda support. The higher-order functions in #176 have the same dependency, so start with
the default comparator. - DataFusion core's
array_sortuses an unstable sort and IEEE 754 total order for floats, as #218 describes.
It can be used with'ASC', 'NULLS LAST'only for non-float element types with no nested NULLs, if the stability
difference is acceptable there. - The function belongs in
crates/spark/src/function/array/. Addcrates/sqllogictest/test_files/array/array_sort.slt
and document it with#[user_doc].
This issue was generated by an LLM (Claude Code) from the Apache Spark source code, Spark's .sql.out golden files and
Comet's source. Please verify the details against Spark before relying on them.
- 主要言語
- Rust
- スター
- 7
- フォーク
- 15
- 平均マージ
- 16時間 43分
- マージ済み PR(30日)
- 26
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/datafusion-spark のほかの issue
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
apache/datafusion-spark#287 ·
メンテナーはふだん 1 日以内に返信
-
enhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
apache/datafusion-spark#266 ·
メンテナーはふだん 1 日以内に返信
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the interval対応中かも @Subodh-17 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/datafusion-spark#109 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 18/100
apache/datafusion-spark#299 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
apache/datafusion-spark#298 ·
メンテナーはふだん 1 日以内に返信
apache/datafusion-spark の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
storytold/photocraft#15232 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
suhina-daw/suhina-daw#1477 ·
メンテナーはふだん 1 日以内に返信
-
parity priority: low ui
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
storytold/pdfcraft#7483 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
IngvarConsulting/unica#3097 ·
メンテナーはふだん 1 日以内に返信
-
enhancement in-progress
難易度 1/5 1時間未満 初心者へのやさしさ 64/100