#6501·paradedb

JoinScan declines DISTINCT + bare window aggregates because the DISTINCT pathkeys include the window entries

Author: barbarjCreated Sep 25, 2026Updated Sep 29, 2026
Labelsbugpriority-mediumjoins

What

sql
SELECT DISTINCT p.id, COUNT(*) OVER () AS total
FROM wj_products p JOIN wj_reviews r ON p.id = r.product_id
WHERE p.description ||| 'laptop'
ORDER BY p.id DESC
LIMIT 3;

JoinScan declines with JoinScan not used: unsupported ORDER BY expression shape; expressions must match an indexed expression or be a sum of pdb.score() calls, even though the only ORDER BY isp.id. PostgreSQL runs it fine (HashAggregate over WindowAgg over the join). The same happens with no ORDER BY at all. Without DISTINCT the query is absorbed.

Cause

PostgreSQL's standard_qp_callback sets root->query_pathkeys to the DISTINCT pathkeys whenever they outnumber the sort pathkeys, and the DISTINCT pathkeys cover every target entry, window aggregates included. JoinScan validates query_pathkeys as if they were the user's ORDER BY:

  • order_by_columns_are_fast_fields (joinscan/planning.rs:1902) reads them at line 1907. Its DISTINCT escape, expression_vars_all_fast (line 2015), returns false on any window function (line 2049), so the pathkey falls through to the decline at line 2029.
  • extract_orderby (planning.rs:2840) uses the same escape at line 2944 and returns None at line 2953, which would drop the path if the first check were lifted.

Suggested Fix

Every window aggregate the scan absorbs is a bare agg OVER (), so its value is constant across the query and a pathkey over a window-only expression orders nothing. Both sites can acknowledge such a pathkey without emitting an ORDER BY entry, the way DISTINCT-derived NullTest pathkeys are acknowledged today.