Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-22807

make NULLS LAST default when sql_mode=ORACLE

Details

    Description

      In MySQL/MariaDB NULL values are considered lower than any non-NULL
      value, therefore, NULL values appear first when the order is ASC
      (ascending), and ordered last when the order is DESC (descending).

      MariaDB [(none)]> SELECT name FROM (SELECT 'Leipzig' AS name FROM dual UNION ALL SELECT NULL FROM dual UNION ALL SELECT 'Berlin' FROM dual) t ORDER BY name;
      +---------+
      | name    |
      +---------+
      | NULL    |
      | Berlin  |
      | Leipzig |
      +---------+
      3 rows in set (0.001 sec)
      

      Can we implement something like NULLS LAST so even if with order by esc, NULLs come last and not first?

      Though for now there is some workaround like using order by with null.

      MariaDB [(none)]> SELECT name FROM (SELECT 'Leipzig' AS name FROM dual UNION ALL SELECT NULL FROM dual UNION ALL SELECT 'Berlin' FROM dual) t ORDER BY name is null, name asc;
      +---------+
      | name    |
      +---------+
      | Berlin  |
      | Leipzig |
      | NULL    |
      +---------+
      3 rows in set (0.001 sec)
      
      

      Attachments

        Issue Links

          Activity

            This is the relevant SQL standard grammar:

            <sort specification list> ::= <sort specification> [ { <comma> <sort specification> }... ]
             
            <sort specification> ::= <sort key> [ <ordering specification> ] [ <null ordering> ]
             
            <sort key> ::= <value expression> <ordering specification> ::= ASC | DESC
             
            <null ordering> ::= NULLS FIRST | NULLS LAST
            

            bar Alexander Barkov added a comment - This is the relevant SQL standard grammar: <sort specification list> ::= <sort specification> [ { <comma> <sort specification> }... ]   <sort specification> ::= <sort key> [ <ordering specification> ] [ <null ordering> ]   <sort key> ::= <value expression> <ordering specification> ::= ASC | DESC   <null ordering> ::= NULLS FIRST | NULLS LAST

            niljoshi nice to have feature request or is there any customer issue request linked to it?

            julien.fritsch Julien Fritsch added a comment - niljoshi nice to have feature request or is there any customer issue request linked to it?
            serg Sergei Golubchik added a comment - - edited

            This is just a change in filesort, with NULLS LAST the sorting won't match index order so the optimizer won't be able to use index to resolve the filesort. No changes in storage engines will be needed.

            As an additional optimization it is possible to use indexes in this case. Instead of index_first and then many times index_next it should start from the first non-null value, iterate till the end, and then start from the beginning to iterate all NULLs. Still doesn't need any changes in engines, all indexes will always stay NULLS FIRST

            serg Sergei Golubchik added a comment - - edited This is just a change in filesort, with NULLS LAST the sorting won't match index order so the optimizer won't be able to use index to resolve the filesort. No changes in storage engines will be needed. As an additional optimization it is possible to use indexes in this case. Instead of index_first and then many times index_next it should start from the first non-null value, iterate till the end, and then start from the beginning to iterate all NULLs. Still doesn't need any changes in engines, all indexes will always stay NULLS FIRST

            People

              Unassigned Unassigned
              niljoshi Nilnandan Joshi
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.