goldmedal opened a new pull request, #16963:
URL: https://github.com/apache/datafusion/pull/16963

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and 
enhancements and this helps us generate change logs for our releases. You can 
link an issue to this PR using the GitHub syntax. For example `Closes #123` 
indicates that this PR will close issue #123.
   -->
   
   - Closes #.
   
   ## Rationale for this change
   The default null ordering is much different in different database.
   In Postgres, the null value is considered as the max value.
   ```
   psql=# select * from (values (1, 'one'), (2, 'two'), (null, 'three')) t(c1, 
c2) order by c1 asc;
    c1 |  c2   
   ----+-------
     1 | one
     2 | two
       | three
   (3 rows)
   
   psql=# select * from (values (1, 'one'), (2, 'two'), (null, 'three')) t(c1, 
c2) order by c1 desc;
    c1 |  c2   
   ----+-------
       | three
     2 | two
     1 | one
   (3 rows)
   ```
   In DuckDB, the null value is always at the last
   ```
   D select * from (values (1, 'one'), (2, 'two'), (null, 'three')) t(c1, c2) 
order by c1 asc;
   ┌───────┬─────────┐
   │  c1   │   c2    │
   │ int32 │ varchar │
   ├───────┼─────────┤
   │     1 │ one     │
   │     2 │ two     │
   │  NULL │ three   │
   └───────┴─────────┘
   D select * from (values (1, 'one'), (2, 'two'), (null, 'three')) t(c1, c2) 
order by c1 desc;
   ┌───────┬─────────┐
   │  c1   │   c2    │
   │ int32 │ varchar │
   ├───────┼─────────┤
   │     2 │ two     │
   │     1 │ one     │
   │  NULL │ three   │
   └───────┴─────────┘
   ```
   
   As a composable database engine, I think it make sense to provide a config 
to customize the default behavior of null ordering.
   <!--
    Why are you proposing this change? If this is already explained clearly in 
the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your 
changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   This PR introduces a new parser option `default_null_ordering`:
   ```rust
           /// Specifies the default null ordering for query results. There are 
4 options:
           /// - `nulls_max`: Nulls appear last in ascending order.
           /// - `nulls_min`: Nulls appear first in ascending order.
           /// - `nulls_first`: Nulls always be first in any order.
           /// - `nulls_last`: Nulls always be last in any order.
           ///
           /// By default, `null_max` is used to follow Postgres's behavior.
           /// postgres rule: 
<https://www.postgresql.org/docs/current/queries-order.html>
           pub default_null_ordering: String, default = 
"asc_reverse".to_string()
   ```
   There are 4 cases for the null ordering. `nulls_max` is used by default to 
follow the Postgres's behavior.
   
   <!--
   There is no need to duplicate the description in the issue here but it is 
sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   - add sqllogictests.
   - It's picked from the Wren AI fork 
https://github.com/Canner/datafusion/pull/3 and it's used in the production for 
a while.
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are 
they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   add a new config.
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api 
change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to