Skip to content

distinct

relationalai.semantics
distinct(*args: Value) -> Distinct

Mark values as distinct (unique) for use in select and aggregates.

This is a convenience wrapper around Model.distinct that operates on the single active model. It returns a Distinct object. distinct is used in two common ways:

  • As a SELECT DISTINCT-style wrapper by passing it as the only argument to select, e.g. select(distinct(x, y)).
  • As an argument wrapper for aggregates to make them count/sum/etc. over unique values, e.g. count(distinct(Person.name)).

If you have more than one model, call Model.distinct on the specific model instead.

  • Distinct - An object you pass to select or to aggregate functions to request “unique values only”.
  • relationalai.util.error.RAIException - Raised in the following cases:
    • There is no active model, or multiple models exist and the active model would be ambiguous.
    • distinct is used incorrectly (for example, not applied to the entire select(...), or not applied to all aggregate arguments).

Count unique names (like SQL COUNT(DISTINCT name)):

>>> from relationalai.semantics import Model, Concept, count, distinct, select
>>> m = Model()
>>> Person = m.Concept("Person")
>>> define(
... Person.new(id=1, name="Alice"),
... Person.new(id=2, name="Bob"),
... Person.new(id=3, name="Alice"),
... )
>>> select(count(distinct(Person.name))).to_df()

Select unique rows by wrapping all selected values:

>>> counts = count(Person).per(Person.name)
>>> select(distinct(Person.name, counts)).to_df()

Incorrect vs correct usage for distinct rows:

>>> # Incorrect: only one column wrapped; this is *not* a distinct-row select.
>>> select(Person.name, distinct(Person.age))
>>> # Correct: wrap the full selected row.
>>> select(distinct(Person.name, Person.age))
RelationalAI Documentation
└──  Build With RelationalAI
    └──  Understand how PyRel works
        ├──  Build a semantic model
        │   └──  Query a model
        │       └──  Deduplicate rows with distinct
        └──  Use advanced reasoning > Rules-based reasoning
            └──  Aggregate and group data
                └──  Deduplicate aggregate inputs with distinct()