questdb / questdb/questdb

SELECT DISTINCT ON

Open
#1,870 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

New feature
Dominant language
Java
Stars
17.3k
Forks
1.6k
Avg merge
5d 10h
Merged PRs (30d)
28

Description

Is your feature request related to a problem?

Let's say I have a table that logs alerts:

CREATE TABLE IF NOT EXISTS app_alerts (
        ts          TIMESTAMP,
        priority    INT,
        message     STRING
    )
    timestamp(ts)
    PARTITION BY DAY;

And I want to get the X most recent rows while de-duplicating any repeated messages.

This query:

SELECT DISTINCT message FROM app_alerts ORDER BY ts DESC LIMIT 50;

yields:

ERROR:  ORDER BY expressions must appear in select list. Invalid column: ts
LINE 1: SELECT DISTINCT message FROM app_alerts ORDER BY ts DESC LIM...
                                                         ^
Describe the solution you'd like.

I'm thinking support for something like PostgreSQL's DISTINCT ON (expression) could be employed? e.g.,

SELECT DISTINCT ON (message)
       ts, message, priority FROM app_alerts ORDER BY ts DESC LIMIT 50;
Describe alternatives you've considered.

IDK if it's semantically equivalent but I'm using:

SELECT ts, message, priority FROM app_alerts LATEST ON ts PARTITION BY message ORDER BY ts DESC LIMIT 50;
Additional context.

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing QuestDB's existing LATEST ON syntax and the SQL parsing and execution paths for DISTINCT, ORDER BY, and LIMIT. Compare the requested DISTINCT ON behavior with the PostgreSQL example and existing alternative, then define tests showing that de-duplicated messages return the selected rows in timestamp order and respect the limit.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, sql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.