Repository navigation
fix(query): NULL in NULL out for BQL functions, NULL number/currency of an empty inventory, NULL #entries tags, and str() of positions - #2867
Merged
Conversation
…ncy for an empty inventory, NULL #entries tags where none, and str() of positions bean-query returns NULL from a function as soon as any argument is NULL; substr, maxwidth, date, weekday, startswith and others raised a type error instead, so one NULL payee failed the query. number() and currency() of an inventory holding nothing are NULL, as get/only read it. #entries tags/links are NULL on entries that have none, as getattr(entry, "tags", None). str() renders a position or inventory as Python does, through the renderer the conversions narration uses, and sets, metadata and entries as their cells. Closes #2820 Closes #2821 Closes #2822 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… fix the bql.md rows Review follow-up. meta, entry_meta and any_meta of a NULL key are NULL, one-argument joinstr of NULL is NULL, and three-argument getitem with a NULL key gives its default, as in bean-query. The number/currency rows land in the function table instead of above the front matter, and the NULL paragraph lists the deliberate convert/value/getprice exceptions. The CHANGELOG notes the tags/links change for JSON, WASM and FFI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-semantics # Conflicts: # CHANGELOG.md
…T str runs getitem(NULL-container, NULL, default) answered the default because the NULL-key arm came first. bean-query GetItem3 returns None for a None container, so a NULL container now gives NULL whatever the key. JOURNAL ... AT str now prints str(position) and str(balance) as bean-query does. BALANCES AT str is still refused, now with bean-query reason, no function matches "sum(str)", instead of naming str(position), which str() takes since this PR. Docs and CHANGELOG: the getitem default applies for a non-NULL container, and value refuses any NULL second argument (currency or date). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Three BQL value-semantics fixes reported against
@rustledger/wasm0.24.0, each checked against beanquery 0.2.0 / beancount 3.2.3 (therustledger-compatcontainer).#2820, NULL in, NULL out. bean-query's
@functionwrapper returnsNoneas soon as any argument isNone(beanquery/query_env.py). A single guard at the top ofevaluate_function_on_values(is_null_in_null_out) now does the same for the functions that raised a type error on NULL. Both evaluation paths go through it:substr/substring,maxwidth,date,parse_dateweekday,ymonth,date_add,date_diff,date_part,date_trunc,date_bin,intervalsplitcomp,round,root,grepn,possign(already NULL-safe, now through the guard),startswith,endswith,account_depthgetitem/get, and one-argumentjoinstr(it gave an empty string)Also:
meta/entry_meta/any_metaof a NULL key are NULL (the lazy-path arm was a type error), and three-argumentgetitemwith a NULL key gives its default, as bean-query does.The list was found by probing every function with NULL argument combinations. Only valid arities are listed, so
substr(payee)is still an arity error. Deliberately kept, where bean-query gives NULL:convertandvaluerefuse a NULL target currency (#902);convert(NULL, 'USD')is0 USD(#586); and a NULL date inconvertorgetpricemeans the latest price. The guard's rustdoc, the CONVERT date arm,docs/reference/bql.mdand the CHANGELOG all list these.#2821,
number()/currency()of an inventory that holds nothing. Both now read only the held (non-empty) positions, the same setInventory::is_emptycounts. An empty inventory, or one whose only position is the zero a balance back at zero keeps, is NULL, asgetandonlyread it. Before, they gave0andUSD.#2822,
#entriestags/links andstr().#entriestags and links are NULL for an entry kind that has none, i.e. anything but a transaction, note or document. This matches bean-query'sgetattr(entry, 'tags', None). Before, they were empty sets.Embedders see this too: JSON CLI output, the WASM
CellValueand the FFIQueryValuegivenullwhere they gave[], and the FFI column datatype, inferred from the first non-NULL cell, can readnullinstead ofsetfor atags/linkscolumn. The CHANGELOG says so.str()of a position or inventory is Python'sstr(), as bean-query prints it. The renderer is the one theCconversions narration already used, moved out ofOrderedBalance::renderinto the sharedpython_inventory_str/python_position_str, so the two cannot drift.str()of a set, metadata, anentryor an interval renders as its cell does (sort.rs::value_to_string). It no longer errors.bean-query comparison, per row
SELECT substr(payee, 0, 2)(NULL payee)type error: SUBSTR ...SELECT maxwidth(payee, 5)SELECT subst('a', 'b', payee)SELECT date(payee)SELECT weekday(close_date(account))startswith(NULL, ...)(rledger-only)WHERE payee IS NOT NULL AND substr(payee, 0, 2) = 'Ac'date_add/date_diff/date_part/date_truncof a NULL date,date_trunc(payee, date),interval(payee),getitem(meta, payee)number(balance)/currency(balance)on a balance back at zero (rledger-only overloads)0,USDSELECT count(*) FROM #entries WHERE tags IS NULL202SELECT tags IS NULL, links IS NULL FROM #entries LIMIT 1TRUE, TRUEFALSE, FALSETRUE, TRUESELECT length(tags) FROM #entries LIMIT 10SELECT str(tags) FROM #entries LIMIT 1(an open)meta(payee)on a NULL payeegetitem(meta, payee, 'd')ddjoinstr(tags)on anopen(#entries)''SELECT str(position)10 USD10 USDSELECT str(balance)(10 USD),()(10 USD),()str()of a lot2 HOOL {100 USD, 2024-01-05, "lot"}str(entry),str(meta),str(tags)of a transactionTransaction(...),{'filename': ...},frozenset({'t1'})){date: ..., ...},{filename: ..., lineno: 4},t1), deliberate deviationDeliberate deviations:
str()of a set, metadata or entry does not copy Python's repr. A repr names Python types (frozenset,datetime.date), not ledger values. The inline comment invalue_to_str,docs/reference/bql.mdand the CHANGELOG say so, andtest_entries_tags_and_links_are_null_where_the_directive_has_nonepinsstr(tags)=t1, andtest_str_of_meta_and_entry_is_the_cell_textpins the exactstr(meta)andstr(entry)text. No compat-suite query usesstr(),number(inventory)or#entriestags, so nothing goes inKNOWN_RUST_DIVERGENCES.convert(x, NULL)still raises the [Bug]: Some queries are broken #902 diagnostic, and so doesvalue(x, NULL)for any NULL second argument (a currency or a date, e.g.value(position, close_date(account))), where bean-query gives NULL. This PR does not change either.Remaining differences, pre-existing, not in scope:
str()of a running balance whose currency went through zero keeps the summed scale (-200.00 USDwhere Python, which drops the zero position, prints-200 USD). This is theInventory::addscale note already in theNUMBERarm.round(number(only(payee, balance)))is NULL where bean-query gives0.str()of an inventory orders two positions that tie on beancount's sortkey by the inventory's own order. When one of them went through zero, Python drops the emptied key and re-appends it, and the core inventory keeps it in place:+5 ZWL, +5 XYZ, -5 ZWL, +5 ZWLgives bean-query(5 XYZ, 5 ZWL)and this PR(5 ZWL, 5 XYZ). The root is the core inventory, like the-200.00item.Review round 2
Merged origin/main (#2862, #2864, #2869); the CHANGELOG conflict kept both sides. This PR bumps no parse-cache version.
getitemcontainer wins over the default. The(_, Null) if len == 3arm came before(Null, _), sogetitem(currency_meta(payee), payee, 'd')on a NULL-payee row andgetitem(NULL, NULL, 'd')gave'd'. bean-query'sGetItem3returns None for a None container; it prints a blank for the first query on every row (checked against beanquery 0.2.0). The NULL-container arm is now first.test_getitem_null_container_wins_over_the_defaultpins both. bean-query refuses the literalgetitem(NULL, NULL, 'd')at compile time (getitem(nonetype, nonetype, str)); here it is NULL, as main gave it. bql.md and the CHANGELOG now say the default applies for a non-NULL container.valueand a NULL second argument. bql.md and the CHANGELOG saidvaluerefuses a NULL target currency; it refuses any NULL second argument, a date too (VALUE: second argument evaluated to NULL). Reworded; behavior unchanged.JOURNAL ... AT strnow runs, as in bean-query.check_at_functionacceptsstrfor JOURNAL, whose cells are shown without being summed. bean-query 0.2.0 output on the test ledger, matched cell for cell bytest_journal_at_str_shows_the_position_and_balance_as_text:stris taken:currency(position)also answers text here, but bean-query refusesAT currency(andAT upper), so a text result alone does not qualify.BALANCES AT stris still refused, as bean-query refuses it, but now with bean-query's reason,no function matches "sum(str)", instead of the falsestr(position)(test_balances_at_str_refuses_summing_text). The BQL: BALANCES / JOURNAL ... AT value ignore the clause and return units #2826 CHANGELOG entry no longer listsJOURNAL ... AT stras a remaining difference. The JOURNAL column headers stayposition/balance, as they are for everyAT, where bean-query writesstr(position)/str(balance).How to test
The new
bql_null_and_str_test.rshas 9 tests. On main, 6 of the first 8 fail (the 9th, the NULL-containergetitemcase, passes on main and pins the round-2 fix). The two that pass on main pin the WHERE guard (already fixed) and the wrong-arity error. A main-vs-branch run of all 68docs/queries onexample.beancountshows no difference.Closes #2820
Closes #2821
Closes #2822
🤖 Generated with Claude Code