Skip to content

Feature (low priority): support COUNT(DISTINCT col) aggregate #4

Closed

Description

@david-anderson-parsolvo

Summary

Low-priority ergonomics request: support COUNT(DISTINCT col) as an aggregate.

Motivation

Today, getting a distinct-value count takes a query plus an external line count:

xql csv apps.csv --exec "SELECT DISTINCT application_name WHERE application_name IS NOT NULL" --mode=csv --no-output-header | grep -c .

COUNT(DISTINCT col) would collapse that to one self-contained query:

xql csv apps.csv --exec "SELECT COUNT(DISTINCT application_name) WHERE application_name IS NOT NULL"

It would also sidestep an easy off-by-one: an unfiltered SELECT DISTINCT counts the NULL/blank group as one distinct value, so the piped count reads one higher than the number of real values unless you remember to add IS NOT NULL.

Priority / scope

Low priority, pure convenience — the SELECT DISTINCT + count workaround works fine. Just COUNT(DISTINCT col); no need for multi-column COUNT(DISTINCT a, b) unless it's trivial to include.

Version / environment

  • xql 1.5.0
  • Linux (x86_64), bash

Activity

  1. anderix commented on Aug 14, 2026

    @anderix
    Member

    Done in the commit above.

    Verified against your own example. With a fixture matching the one in the issue — six rows, three real applications, one blank:

    $ xql csv apps.csv --exec "SELECT COUNT(*), COUNT(application_name), COUNT(DISTINCT application_name)"
    COUNT(*)  COUNT(application_name)  COUNT(DISTINCT application_name)
    6         5                        3
    

    And the off-by-one you described, reproduced: the workaround piped through grep -c . reads 3 with IS NOT NULL and 4 without it, because the blank group is a line. COUNT(DISTINCT ...) reads 3 either way, since it skips NULLs exactly as COUNT(col) already does.

    Both backends, no translation needed. The SharePoint executor aggregates client-side over fetched rows through the same AggSlot, so there is no OData push-down to bypass and nothing to express in $apply.

    Multi-column: not implemented, per your "unless it's trivial" — it isn't. COUNT(DISTINCT a, b) needs a tuple comparison the grammar has nowhere else. It is refused by name rather than failing as a missing ). SUM(DISTINCT x) likewise reports that it is legal SQL and not implemented, rather than an unexpected-keyword parse error.

    One implementation note worth recording. Distinct values are keyed by cell.FormatCell, which is injective within a type — ints render in full, floats with the precision that round-trips exactly — and an argument expression carries one static type for the whole scan, so two different values cannot collide on a shared key. The set is allocated only for a distinct count, so every other aggregate costs what it did before.

    And a note on the tests, because the first pass was wrong in an instructive way. Three of my five passed with the feature disabled. The shared fixture's columns are either all-unique or NULL-free, and against those a distinct count and a plain count agree — so the tests were asserting nothing about DISTINCT. They now run against a table with both a repeat and a NULL, and with a repeat inside a group so the GROUP BY case discriminates too. Four of the five now fail with the distinct set removed; the fifth asserts an empty distinct set counts 0, which cannot discriminate by nature and stands as a guard on the separate return path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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