Skip to content

SOLR-18328 Add support for standard deviation in rollup for streaming expressions - #4691

Open
KhushJain wants to merge 2 commits into
apache:mainfrom
KhushJain:SOLR-18328
Open

SOLR-18328 Add support for standard deviation in rollup for streaming expressions#4691
KhushJain wants to merge 2 commits into
apache:mainfrom
KhushJain:SOLR-18328

Conversation

@KhushJain

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/SOLR-18328

Description

Added support for the std(col) (standard deviation) metric in rollup() streaming expressions.
The std(col) metric was non-functional; the StdMetric class was a stub copy-pasted from MeanMetricupdate() was a no-op, getValue() returned null, and newInstance() even returned a MeanMetric instead of a StdMetric.

Solution

Implemented StdMetric to compute the sample standard deviation:

StdMetric.java:

  1. update() now accumulates count, sum, and sumSq from the column value (handling Double/Float/Integer/Long, skipping nulls/other types).
  2. getValue() computes the sample standard deviation sqrt((n*sumSq - sum^2) / (n*(n-1))), returning 0.0 when count <= 1, and honoring outputLong (rounds to a long when set).
  3. Fixed newInstance() to return a StdMetric (was incorrectly returning MeanMetric), and replaced the unused doubleSum/longSum fields with sum/sumSq.

Tests

  • StreamExpressionTest.testRollupStdMetric (new): validates std(a_i) and std(a_f) alongside count(*) in a rollup(... over="a_s" ...) expression across multiple groups.
  • StreamingTest.testRollupStream: added StdMetric("a_i") / StdMetric("a_f") to the metrics array and asserted expected values per group.

Checklist

Please review the following and check all that apply:

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • I have created a Jira issue and added the issue ID to my pull request title.
  • I have given Solr maintainers access to contribute to my PR branch. (optional but recommended, not available for branches on forks living under an organisation)
  • I have developed this patch against the main branch.
  • I have run ./gradlew check.
  • I have added tests for my changes.
  • I have added documentation for the Reference Guide
  • I have added a changelog entry for my change

@github-actions github-actions Bot added documentation Improvements or additions to documentation client:solrj tests labels Aug 1, 2026
@epugh epugh self-assigned this Aug 2, 2026
@KhushJain

Copy link
Copy Markdown
Contributor Author

Hey @epugh, thanks for reviewing!!

One of the existing flaky test failed testSingleShardInFlightRequestsDuringShutDown

public void update(Tuple tuple) {
Object o = tuple.get(columnName);
double val;
if (o instanceof Double d) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could the 'if-else' structure be simplified ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there definitly are some unusla coding patterns in the streaming code... but we tend to follow them once they exist as there are so many of them!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@epugh Thanks for the clarification :) Is there a documentation about the code standard adopted by the codebase ?
@KhushJain Feel free to fix the if-else or ignore my suggestion :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we have "tidy" and errrorprone tools that we run int he builds. As far as in the streaming code, nothign formal written, just, look at other java classes ;-). Having said that, I do appreciate your reviewing this PR, and there are lots of PR's out there that need a reviewer to go through them!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed on the quirky instanceof pattern, but keeping it consistent with all its siblings.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@epugh I would be happy to review the PR's :)

@Override
public void update(Tuple tuple) {}
public void update(Tuple tuple) {
Object o = tuple.get(columnName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could the object 'o' be made immutable by declaring it as final ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it could be added but the current state is consistent with the existing style.

@@ -24,15 +24,10 @@
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;

public class StdMetric extends Metric {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you include java docs for this class ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added docstrings.

@@ -89,7 +101,13 @@ public String[] getColumns() {

@Override
public Number getValue() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you include javadocs to understand the purpose of this method ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added docstrings.

@epugh epugh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a question about docs, but otherwise sems to follow the pattern of some other ones.. It would be good to answer the other review comments, and tjhen I think we can move forward...

* `over`: (Mandatory) A list of fields to group by.
* `metrics`: (Mandatory) The list of metrics to compute.
Currently supported metrics are `sum(col)`, `avg(col)`, `min(col)`, `max(col)`, `count(*)`, `missing(col)`, `countDist(col)`, `per(col, percentile)`.
Currently supported metrics are `sum(col)`, `avg(col)`, `min(col)`, `max(col)`, `count(*)`, `missing(col)`, `countDist(col)`, `per(col, percentile)`, `std(col)`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there any more docs on these indivdual ones or is this mention what the pattern is? Just wondering if folks will know how to use it...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no dedicated per-metric docs exists, all streaming metrics are documented this same inline way.

Support for missing(col), countDist(col) and per(col, percentile) are also added by me in the past.

public void update(Tuple tuple) {
Object o = tuple.get(columnName);
double val;
if (o instanceof Double d) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there definitly are some unusla coding patterns in the streaming code... but we tend to follow them once they exist as there are so many of them!

@KhushJain

Copy link
Copy Markdown
Contributor Author

Thanks @epugh @VishnuPriyaChandraSekar for reviewing it!!

Can we please back port this to 9x as well?

@VishnuPriyaChandraSekar VishnuPriyaChandraSekar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for addressing my feedback :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

client:solrj documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants