Skip to content

Sort options for DataType::Dictionary is incorrect #4649

Closed

Description

@mingmwang

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
(This section helps Arrow developers understand the context and why for this feature, in addition to the what)

Looks like when the DataType is Dictionary, it always reset the Nulls First settings and ignore the sort option from the query engine. This will generate wrong sort result. I will add a UT to reproduce the issue.

https://github.com/apache/arrow-rs/blob/a0a5880665b1836890f6843b6b8772d81c463351/arrow-ord/src/sort.rs#L316-L332

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Activity

  1. tustvold commented on Dec 16, 2022

    @tustvold
    Contributor

    The logic you link to is fairly well tested - https://github.com/apache/arrow-rs/blob/a0a5880665b1836890f6843b6b8772d81c463351/arrow-ord/src/sort.rs#L2998

    There is, however, a known issue with lexsort's handling of nested nulls in dictionaries - apache/arrow-rs#2687 although again it should be noted that arrow-rs will not generate such arrays naturally.

  2. matthewmturner commented on Oct 27, 2026

    @matthewmturner
    Contributor

    I tried to reproduce this like so but was not able to. i dont think any tests were ever added for this - do we think adding some SLT tests for example here with these cases would be helpful to ensure no regressions and proper integration with arrow kernel? Or is it okay to just close this?

    DataFusion CLI v55.1.0
    > CREATE TABLE t AS
      SELECT arrow_cast(v, 'Dictionary(Int32, Utf8)') AS d
      FROM (VALUES ('b'), (NULL), ('a'), ('c'), (NULL)) AS x(v);
    0 row(s) fetched.
    Elapsed 0.011 seconds.
    
    > SELECT arrow_typeof(d) FROM t LIMIT 1;
    +-------------------------+
    | arrow_typeof(t.d)       |
    +-------------------------+
    | Dictionary(Int32, Utf8) |
    +-------------------------+
    1 row(s) fetched.
    Elapsed 0.033 seconds.
    
    > SELECT d FROM t ORDER BY d ASC NULLS FIRST;
    +------+
    | d    |
    +------+
    | NULL |
    | NULL |
    | a    |
    | b    |
    | c    |
    +------+
    5 row(s) fetched.
    Elapsed 0.016 seconds.
    
    > SELECT d FROM t ORDER BY d ASC NULLS LAST;
    +------+
    | d    |
    +------+
    | a    |
    | b    |
    | c    |
    | NULL |
    | NULL |
    +------+
    5 row(s) fetched.
    Elapsed 0.001 seconds.
    
    > SELECT d FROM t ORDER BY d DESC NULLS FIRST;
    +------+
    | d    |
    +------+
    | NULL |
    | NULL |
    | c    |
    | b    |
    | a    |
    +------+
    5 row(s) fetched.
    Elapsed 0.001 seconds.
    
    > SELECT d FROM t ORDER BY d DESC NULLS LAST;
    +------+
    | d    |
    +------+
    | c    |
    | b    |
    | a    |
    | NULL |
    | NULL |
    +------+
    5 row(s) fetched.
    Elapsed 0.001 seconds.
    
  3. matthewmturner commented on Oct 27, 2026

    @matthewmturner
    Contributor

    @xudong963 @zhuqi-lucas what do you think?

  4. zhuqi-lucas commented on Oct 27, 2026

    @zhuqi-lucas
    Contributor

    @matthewmturner @alamb It may have been fixed by the arrow-rs dictionary sort rewrite (apache/arrow-rs#4605). arrow-rs now tests null dictionary values with all asc/desc and nulls first/last combinations (test_sort_int8_dicts), so I think it's fine to close.

  5. alamb commented on Oct 27, 2026

    @alamb
    Contributor

    I tried to reproduce this like so but was not able to. i dont think any tests were ever added for this - do we think adding some SLT tests for example here with these cases would be helpful to ensure no regressions and proper integration with arrow kernel? Or is it okay to just close this?

    It wouldn't hurn to add SLT to be sure, but I also think closing is fine

  6. zhuqi-lucas commented on Oct 27, 2026

    @zhuqi-lucas
    Contributor

    It wouldn't hurn to add SLT to be sure, but I also think closing is fine

    +1 it's fine to adding the SLT cases, they're cheap and also cover the DataFusion integration.

  7. matthewmturner commented on Oct 27, 2026

    @matthewmturner
    Contributor

    Okay will work on that

  8. matthewmturner commented on Oct 27, 2026

    @matthewmturner
    Contributor

    Here we go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions