gristlabs_grist-core/sandbox/grist/functions
Dmitry S f0d0a07295 (core) Implement PREVIOUS/NEXT/RANK and lookupRecords().find.* methods.
Summary:
- `lookupRecords()` now allows efficient search in sorted results, with
  the syntax  `lookupRecords(..., order_by="-Date").find.le($Date)`. This will find the record with the nearest date that's <= `$Date`.
- The `find.*` methods are `le`, `lt`, `ge`, `gt`, and `eq`. All have O(log N) performance.
- `PREVIOUS(rec, group_by=..., order_by=...)` finds the previous record to rec, according to `group_by` / `order_by`, in amortized O(log N) time. For example, `PREVIOUS(rec, group_by="Account", order_by="Date")`.
- `PREVIOUS(rec, order_by=None)` finds the previous record in the full table, sorted by the `manualSort` column, to match the order visible in the unsorted table.
- `NEXT(...)` is just like `PREVIOUS(...)` but finds the next record.
- `RANK(rec, group_by=..., order_by=..., order="asc")` returns the rank of the record within the group, starting with 1. Order can be `"asc"` (default) or `"desc"`.
- The `order_by` argument in `lookupRecords`, and the new functions now supports tuples, as well as the "-" prefix to reverse order, e.g. `("Category", "-Date")`.
- New functions are only available in Python3, for a minor reason (to support keyword-only arguments for `group_by` and `order_by`) and also as a nudge to Python2 users to update.

- Includes fixes for several situations related to lookups that used to cause quadratic complexity.

Test Plan:
- New performance check that sorted lookups don't add quadratic complexity.
- Tests added for lookup find.* methods, and for PREVIOUS/NEXT/RANK.
- Tests added that renaming columns updates `order_by` and `group_by` arguments, and attributes on results (e.g. `PREVIOUS(...).ColId`) appropriately.
- Python3 tests can now produce verbose output when VERBOSE=1 and -v are given.

Reviewers: jarek, georgegevoian

Reviewed By: jarek, georgegevoian

Subscribers: paulfitz, jarek

Differential Revision: https://phab.getgrist.com/D4265
2024-07-17 12:00:55 -04:00
..
__init__.py (core) Implement PREVIOUS/NEXT/RANK and lookupRecords().find.* methods. 2024-07-17 12:00:55 -04:00
date.py (core) Fix MOONPHASE doctests (python2!) 2023-03-30 12:28:56 -04:00
info.py Make ISEMAIL and ISURL more flexible for longer TLD (#834) 2024-01-31 13:58:50 -05:00
logical.py (core) Simple Python 3 compatibility changes 2021-06-22 17:13:17 +02:00
lookup.py (core) Add a row to summary tables grouped by list column(s) corresponding to empty lists 2022-06-09 23:38:14 +02:00
math.py (core) Fix imports into reference columns, and support two ways to import Numeric as a reference. 2023-05-02 10:28:14 -04:00
prevnext.py (core) Implement PREVIOUS/NEXT/RANK and lookupRecords().find.* methods. 2024-07-17 12:00:55 -04:00
schedule.py (core) New type conversion in the backend 2022-02-04 20:28:13 +02:00
stats.py (core) Implement PREVIOUS/NEXT/RANK and lookupRecords().find.* methods. 2024-07-17 12:00:55 -04:00
test_schedule.py (core) Simple Python 3 compatibility changes 2021-06-22 17:13:17 +02:00
text.py (core) Update documentation of certain functions 2024-06-14 09:52:23 -04:00
unimplemented.py (core) Improve suggestions for formula autocomplete 2020-09-12 00:21:29 -04:00