datajoint-python icon indicating copy to clipboard operation
datajoint-python copied to clipboard

Bug: Aggregation `__bool__` returns True when empty

Open CBroz1 opened this issue 7 months ago • 1 comments
trafficstars

Bug Report

Description

My intuition regarding projection and aggregation is that they return objects that will behave the same as tables. For an ordinary table, bool(Table) == len(Table) > 0, but this isn't the case for aggregations

Reproducibility

Include:

  • OS Linux
  • Python Version: 3.10
  • MySQL Version: 8 latest
  • MySQL Deployment Strategy local-docker
  • DataJoint Version: 0.14.3
  • Minimum number of steps to reliably reproduce the issue: see below
  • Complete error stack as a result of evaluating the above steps: n/a
MWE
import datajoint as dj

schema = dj.schema("cbroz_temp")


@schema
class A(dj.Lookup):
    definition = """
    a_id: int
    """

    contents = [[i] for i in range(1, 3)]


@schema
class B(dj.Lookup):
    definition = """
    -> A
    b_id: int
    """

    contents = [(i, j) for i in range(1, 2) for j in range(4, 8)]


if __name__ == "__main__":
    has_entry = A & "a_id=1"
    no_entry = A & "a_id=3"
    print("Table with contents as bool: ", bool(has_entry)) # True
    print("Table without contents as bool: ", bool(no_entry)) # False
    aggr_has_entry = has_entry.aggr(B, b_id="COUNT(b_id)")
    aggr_no_entry = no_entry.aggr(B, b_id="COUNT(b_id)")
    print("Aggr table with contents as bool: ", bool(aggr_has_entry)) # True 
    print("Aggr table without contents as bool: ", bool(aggr_no_entry)) # True

Expected Behavior

I expected bool(Aggr) == len(Aggr) > 0

Screenshots

n/a

Additional Research and Context

  • Checked existing issues
  • Looked at git blame for the relevant code. PR #850 featured this commit, which chose to make the QueryExpression bool based on length and the Aggregation bool based on it being a valid selection - Is this related to evaluations during cascading delete?

CBroz1 avatar Apr 18 '25 14:04 CBroz1

This issue is stale because it has been open for 45 days with no activity.

github-actions[bot] avatar Jun 03 '25 02:06 github-actions[bot]

This issue is stale because it has been open for 45 days with no activity.

github-actions[bot] avatar Jul 19 '25 03:07 github-actions[bot]