ClickHouse and Presto default nulls order should be LAST, not LOW

Author: Hariomlokhande-coderCreated Sep 30, 2026Updated Sep 30, 2026

Bug Report

Which version of ShardingSphere did you use?

master @ ed68faef898

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

Both. The problem is in result merging.

Expected behavior

SELECT col1 FROM t ORDER BY col1 across two shards on Presto or ClickHouse should return NULLs at the end, the way the database itself does: 5, 10, NULL.

Actual behavior

It returns 5, NULL, 10.

Reason analyze (If you can)

ClickHouseDatabaseMetaData and PrestoDatabaseMetaData return NullsOrderType.LOW (NULLs first for ASC). Both databases actually put NULLs last by default, whatever the direction:

  • Presto: "The default null ordering is NULLS LAST, regardless of the ordering direction." (docs)
  • ClickHouse: "By default or with the NULLS LAST modifier: first the values, then NaN, then NULL." (docs). ParserOrderByElement starts with nulls_direction = direction, which is the same as NULLS LAST.

The merger resolves LOW + ASC to FIRST, so the priority queue in OrderByStreamMergedResult disagrees with the order each shard returns. DESC happens to come out right, because LOW + DESC resolves to LAST.

This also matters for #40001: once the generated ORDER BY for DISTINCT resolves the dialect default, DISTINCT on these two dialects would return the NULL row twice unless the metadata is fixed too.

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

  1. Presto or ClickHouse, table t sharded into two tables.
  2. Shard 0 has col1 = 5, NULL; shard 1 has col1 = 10.
  3. SELECT col1 FROM t ORDER BY col1;

Example codes for reproduce this issue (such as a github link).

Reproduced against ShardingDQLResultMerger with shard results [5, NULL] and [10]: the merged output is [5, null, 10] for both dialects.