Skip to content

Bug: Aggregation __bool__ returns True when empty #1234

Description

@CBroz1

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 Pre-release 0.13dev2 candidate #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?

Activity

  1. added
    bugIndicates an unexpected problem or unintended behavior
    triageIndicates issues, pull requests, or discussions need to be reviewed for the first time
    on Apr 18, 2025
  2. github-actions commented on Jun 3, 2025

    @github-actions

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

  3. added
    staleIndicates issues, pull requests, or discussions are inactive
    on Jun 3, 2025
  4. removed
    triageIndicates issues, pull requests, or discussions need to be reviewed for the first time
    staleIndicates issues, pull requests, or discussions are inactive
    on Jun 3, 2025
  5. github-actions commented on Jul 19, 2025

    @github-actions

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

  6. added
    staleIndicates issues, pull requests, or discussions are inactive
    on Jul 19, 2025
  7. added this to the DataJoint 2.0 milestone on Jan 9, 2026
  8. removed
    staleIndicates issues, pull requests, or discussions are inactive
    on Jan 9, 2026
  9. dimitri-yatsenko commented on Jan 9, 2026

    @dimitri-yatsenko
    Member

    Fixed in #1314

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

Metadata

Metadata

Labels

bugIndicates an unexpected problem or unintended behavior

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions