Skip to content

Allow Mongo group-by on an aliased function expression - #327

Open
bhubam wants to merge 3 commits into
mainfrom
function-group-by
Open

bhubam wants to merge 3 commits into
mainfrom
function-group-by

Conversation

@bhubam

@bhubam bhubam commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add an optional alias on FunctionExpression so a function can be used as a Mongo group key.
  • When the alias is set, $addFields materializes the function and $group groups by that field. A blank alias still fails with the existing unsupported-operation error.
  • Covers the actor interval bucket (INTERVAL_START_TIME), which is floor(lastActivity / interval) * interval.

Merge order

Merge this before the entity-service interval change. Entity-service will pick up FunctionExpression.alias through a BOM upgrade; this PR does not publish a version.

Test plan

  • MongoFunctionGroupByTest asserts $addFields for INTERVAL_START_TIME, $group._id references that field, and the projection reads $_id.INTERVAL_START_TIME.
  • CI on this PR.

Interval buckets are computed in $addFields and grouped by that field instead of the function itself.
@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.65517% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.23%. Comparing base (db61d59) to head (3952cbd).

Files with missing lines Patch % Lines
...o/query/parser/MongoGroupTypeExpressionParser.java 85.00% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #327      +/-   ##
============================================
+ Coverage     81.16%   81.23%   +0.06%     
- Complexity     1679     1698      +19     
============================================
  Files           243      243              
  Lines          7762     7780      +18     
  Branches        769      771       +2     
============================================
+ Hits           6300     6320      +20     
+ Misses          967      962       -5     
- Partials        495      498       +3     
Flag Coverage Δ
integration 81.23% <89.65%> (+0.06%) ⬆️
unit 58.84% <89.65%> (+0.34%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Test Results

  128 files  +1    128 suites  +1   44s ⏱️ +11s
  894 tests +5    893 ✅ +5  1 💤 ±0  0 ❌ ±0 
1 264 runs  +5  1 263 ✅ +5  1 💤 ±0  0 ❌ ±0 

Results for commit 3952cbd. ± Comparison against base commit db61d59.

♻️ This comment has been updated with latest results.

@suddendust

suddendust commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Two cases:

  1. What happens today if the function alias is null? Can you add a test case for this?
  2. What happens when the function alias and the selection alias are different? Smth like:
IdentifierExpression timestamp =
    IdentifierExpression.of("attributes.last_activity_timestamp.value.long");
ConstantExpression interval = ConstantExpression.of(86_400_000L);

FunctionExpression bucket =
    FunctionExpression.builder()
        .alias("bar")                     
        .operator(MULTIPLY)
        .operand(
            FunctionExpression.builder()
                .operator(FLOOR)
                .operand(
                    FunctionExpression.builder()
                        .operator(DIVIDE)
                        .operand(timestamp)
                        .operand(interval)
                        .build())
                .build())
        .operand(interval)
        .build();

Query query =
    Query.builder()
        .addSelection(bucket, "foo")      
        .addSelection(AggregateExpression.of(COUNT, IdentifierExpression.of("id")), "count")
        .addAggregation(bucket)
        .build();

A null or blank function alias still fails. When the selection alias differs, group by the function alias and project that _id field under the selection name.

Co-authored-by: Cursor <cursoragent@cursor.com>
@bhubam

bhubam commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Null or blank function alias still throws UnsupportedOperationException (Grouping by a function ... is not yet supported). No $addFields stage. Covered by rejectsFunctionGroupByWhenAliasIsMissing (null, "", and " ").

If the function alias and the selection alias differ (bar vs foo):

$addFields writes the bucket as bar
$group._id is { bar: "$bar" }
the projection is { foo: "$_id.bar" }
When both aliases are the same (INTERVAL_START_TIME), the pipeline is unchanged. See 3952cbd.

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