Skip to content

query: :find returns one row per binding (duplicates), with no way to ask for distinct rows #493

Closed

Description

@adityamukho

Problem

A query without aggregates returns one row per binding of the :where clause, not one row per distinct :find tuple:

(transact [[:e1 :c 1] [:e1 :c 2] [:e2 :c 1]])
(query [:find ?e :where [?e :c ?v]])
;; 3 rows: :e1 appears twice, once per ?v

Datalog, and Datomic in particular, give :find set semantics: this query returns two rows, and :with exists so a query can keep duplicates for aggregation. Minigraf has :with, but its default is the reverse of Datomic's, and nothing documents that.

Minigraf has no :distinct keyword or find option. The only "distinct" features are the aggregates count-distinct and sum-distinct. The workaround today is to add an aggregate, since aggregates group by the other find variables:

(query [:find ?e (count ?v) :where [?e :c ?v]])   ;; 2 rows

This adds a column the caller didn't ask for. Otherwise the caller deduplicates in their own code.

Found while widening the property tests (#386, PR #491). The test compares results as sets wherever a projection could repeat a row, so it passes under either behavior.

Options

  1. :find returns a set by default (Datomic semantics). :with keeps its current meaning for aggregates. This changes the results of existing queries, so it is a breaking change: v3.0.0 (which already changes query results with bug: a later assertion does not replace an earlier valid-time window of the same [e a v]; :as-of N should see exactly one window per triple #435, bug: a :find variable that is never bound returns "No results found" instead of an error #406 and bug: unknown aggregate / window function only errors once a row matches; returns empty on no data #414), or else a later major release. It needs a CHANGELOG/upgrade note and checks on window functions, :order-by and :max-results, which may rely on row multiplicity.
  2. Keep bag semantics and add an opt-in (e.g. a :distinct query option). Nothing breaks, so it fits v3.1.0 (features). The bag default stays different from Datomic and has to be documented.
  3. Keep current behavior and only document it in the Datalog reference, with the aggregate workaround.

In every case, the docs site's content/datalog-reference.md needs to say which behavior applies to each version.

Decision needed

Which option, and which release.

Activity

  1. adityamukho commented on Oct 26, 2026

    @adityamukho
    CollaboratorAuthor

    Best to stick to current behaviour, but allow a :distinct option, i.e. option 2.

  2. modified the milestones: v3.next, v3.0.0 on Oct 26, 2026
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

    enhancementNew feature or requestquestionFurther information is requested

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions