Skip to content

Tabulate SUMMARISE clause - #622

Merged
teunbrand merged 41 commits into
mainfrom
tabulate-summarise
Dec 4, 2026
Merged

teunbrand merged 41 commits into
mainfrom
tabulate-summarise

Conversation

@teunbrand

Copy link
Copy Markdown
Collaborator

This PR adds a row-summarising clause to the tabulate syntax.

I think the doc pages give the best introduction, but briefly:

TABULATE *
  SUMMARISE mean OF bill_len, bill_dep

Adds a new row after every group which will the display the summarised mean for the bill_len and bill_dep columns.

Is typical syntax for a standard clause. Notably, the operation (mean in the example) can be a template with keywords and limited arithmetic, so that e.g. ({mean - se}, {mean + se}) will render to something like (0.08, 12.04).

The row labels for summary rows are placed in the same columns stub row labels (FORMAT STUB). If no suitable stub column exists, one will be created.

setup_rows renumbers a separated facet's multi-rank group keys to single
ids right after the reorder, so nothing downstream ever sees the
multi-rank keys the sort needed. collapse_group_columns sheds its rows
parameter and renumbering pass; the shared collapses() predicate decides
both sides.
compute_summaries runs between setup_columns and apply_formats, on the
raw numeric values, and its chunks flow to build_cells. End-to-end tests
cover grand and group placement, rowlabel, clause order, separated-facet
re-keying, and the error paths.
@teunbrand
teunbrand marked this pull request as ready for review November 30, 2026 12:14
@teunbrand
teunbrand requested a review from thomasp85 November 30, 2026 12:20

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this behave for 0 row tables? I agree it's not a use case that makes sense but it needs to error gracefully and in the same way for group and grand summaries.

It would be nice to have a test that ensured that the summary and aggregate functions never drifted apart

How should we deal with summaries that create un-allowed calculations, e.g. {mean/0} or geomean on negative values.. They'd return NaN or Inf but maybe we shouldn't display that?

Comment thread doc/syntax/clause/summarise.qmd Resolved
Comment thread doc/syntax/clause/summarise.qmd Resolved
Comment thread src/execute/table/summary.rs Resolved
Comment thread src/format.rs Resolved
Comment thread src/execute/table/summary.rs Resolved
Comment thread src/execute/table/summary.rs Resolved
Comment thread src/execute/table/summary.rs Resolved
@teunbrand

teunbrand commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

How does this behave for 0 row tables?

We were emitting 0 rows for groups and 1 row of null results for grand. Agreed it should just error, good catch.

They'd return NaN or Inf but maybe we shouldn't display that?

The alternative is to display null and I don't think that is better. Inf or NaN is clearly a computation gone wrong and follows SQL behaviour, whereas with null you could also doubt if you selected the columns incorrectly. I'm open to better ideas though.

It would be nice to have a test that ensured that the summary and aggregate functions never drifted apart

There is drift already, I think aggregate se is only accepted in band context in aggregate, and for tabulate I added the n_distinct and n_missing. Want me to unify?

# Conflicts:
#	doc/vendor/SKILL.md
#	src/execute/table/format.rs
#	src/execute/table/layout.rs
#	src/execute/table/mod.rs
#	src/parser/builder.rs
#	src/table/mod.rs

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand
teunbrand merged commit 923ac34 into main Dec 4, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants