Skip to content

Column disappears in query output when sum of a position for all rows is ZERO #271

@hartzell

Description

@hartzell

I ran into a problem where some of my queries were not including all of the expected columns in the output.

I think that the diagnosis in the title is accurate.

Using the test case below, using the code from Pypi I get:

$ python3 bug.py
+---------------+
| leaf(account) |
+---------------+
| Bob           |
+---------------+

What I expect (and what is generated with the change in the PR I'm about to submit) is:

$ python3 bug.py
+---------------+---------------------+
| leaf(account) | sum(position) (USD) |
+---------------+---------------------+
| Bob           | 0.00                |
+---------------+---------------------+

Rather than deleting positions that are zero, the updated code just passes them on up the chain. I'm not sure that this change isn't going to screw up something else.

To test, save this ledger as bug.beancount

option "title" "Bug demo"

2025-08-01 * "Bob bought a this expense"
Income:Bob                                   -1.00 USD
Expenses:This:Bob                             1.00 USD

Then run this query (ignore whining about unopened accounts) via
bean-query bug.beancount:

select leaf(account), sum(position) where account ~ "^(Expenses|Income).*" group by leaf(account)

or run this little python program

"""Document this..."""

import sys

from beancount import loader
from beanquery import query, query_render

entries, errors, options_map = loader.load_file("bug.beancount")

fmtopts = dict(
boxed=True, spaced=False, narrow=False, nullvalue="0.00", numberify=False
)

sql = 'select leaf(account), sum(position) where account ~ "^(Income|Expenses).*" group by leaf(account)'
rtypes, rrows = query.run_query(entries, options_map, sql, None, numberify=True)
query_render.render_text(rtypes, rrows, options_map["dcontext"], sys.stdout, **fmtopts)

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