grist-core icon indicating copy to clipboard operation
grist-core copied to clipboard

Improving joining of `RecordSet`s

Open bowdi opened this issue 9 months ago • 3 comments
trafficstars

Describe the problem to be solved

Refs:

I've got a table similar to what is described in community #1495 , although in my scenario, I have the following schema:

Objectives

  • type - choice e.g. project, task
  • title - text field
  • parent - a self reference to the objectives table as formula reference lists aren't supported as two way references these are included:
  • links_from - a two way reference list
  • links_to - the other two way reference list

Links

  • from - reference field to objectives
  • link - text explaining the link
  • to - reference field to objectives
  • links - a formula reference list joining the $from and $to fields as described in the community post

I have one use case that requires two joins, I'd like a reference list on Objectives that includes links of itself AND links of it's parent (chain). I would need to join links_from and links_to into a single reference list $links on Objectives first then join $parent.links and $links into $all_links together.

This is possible with nested list comprehension but it's not intuitive and could be simpler!

Describe the solution you would like

Overriding the __add__ operator in python record.RecordSet might work and it would result in formula like this:

  • $links: $links_to + $links_from
  • $all_links: $parent.links + $links

rather than this:

  • $links: { el for sublist in [$links_to, $links_from] for el in sublist }
  • $all_links: { el for sublist in [$parent.all_links, $links] for el in sublist }

bowdi avatar Feb 14 '25 12:02 bowdi

A couple of notes here:

One is that in addition to the list-comprehension approach, you can also use set($links_to) | set($links_from) (when you want to remove any duplicates and order doesn't natter) or list($links_to) + list($links_from (when you want to keep duplicates and order).

The other note is that a there was an improvement last year to a related issue reported here: https://github.com/gristlabs/grist-core/issues/1130. If adding an __add__ operator, it would make sense to match that behavior (which takes care of preserving order, but removing duplicates, see here).

dsagal avatar Feb 16 '25 16:02 dsagal

Oh nice! Thanks for those tips.

bowdi avatar Feb 17 '25 11:02 bowdi

Thanks for the suggestions @dsagal. Might be worth adding to the Help Center's reference or cheat sheet.

Mentioning that, because of the name RecordSet, I though it inherited Python's set, but that isn't the case.

raph-topo avatar Mar 29 '25 18:03 raph-topo