Implement Spark-compatible `array_sort`

オープン
#269 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
64/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
backend

調査の方向性

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 の本文から書いたものです。

説明

enhancement

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.defaultComparator is 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.0 equals 0.0.
  • It sorts with java.util.Arrays.sort(Object[], Comparator), which is stable, so equal elements keep their input order.
    An array of NullType elements 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 type VOID).
  • 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), ±Infinity and 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 ArraySort between 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_sort uses 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/. Add crates/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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/datafusion-spark のほかの issue

apache/datafusion-spark の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。