Skip to content

Make sorting of the *-bucket consistent and controllable #439

@pdobacz

Description

@pdobacz

The spot taken by the *-bucket row in the results of a query can vary from query to query (and sometimes for the same query issued before or after ANALYZE;!).

In a nutshell, the Sort plan node may or may not be pushed down by the planner, to a spot before the Agg / BucketScan nodes. If it is then the ORDER BY sorting will not impact it, meaning it will come first. In other circumstances, if the Sort node comes last, it is impacted by ORDER BY.

Example query where it didn't get pushed down:

SELECT age, count(*), diffix.is_suppress_bin(*) FROM customers GROUP BY 1 ORDER BY 1 NULLS FIRST
 Sort
   Sort Key: age
   ->  Custom Scan (BucketScan)
         ->  HashAggregate
               Group Key: age
               ->  Seq Scan on customers

And where it did (because id has many unique columns per ANALYZE;, and the HashAggregate has been changed to Sort -> GroupAggregate:

prop_test=# explain SELECT id, count(*), diffix.is_suppress_bin(*) FROM customers GROUP BY 1 ORDER BY 1 NULLS LAST;
 Custom Scan (BucketScan)
   ->  GroupAggregate
         Group Key: id
         ->  Sort
               Sort Key: id
               ->  Seq Scan on customers

Ideas from slack thread:

  1. ORDER BY volatile_func(id) where that function does nothing but is marked as VOLATILE
  2. Extract the ORDER BY clause from the query plan and insert the *-bucket conformant to this order. As in, whilst we emit the buckets, we "wait" for the first bucket which doesn't satisfy the sorting criteria, and then emit the *-bucket
  3. prepend Append an ORDER BY is_suppress_bin(*) and let the planner figure it out

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions