[SERVER-15585] Group command does not support code with scope for the key and finalize functions Created: 09/Oct/14  Updated: 06/Dec/22  Resolved: 18/Mar/16

Status: Closed
Project: Core Server
Component/s: MapReduce
Affects Version/s: 2.6.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Robert Stam Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-14193 deprecate (JS) group command Closed
Assigned Teams:
Query
Operating System: ALL
Participants:

 Description   

The group command DOES support code with scope for the reduce function, but does NOT support code with scope for the key and finalize functions.

Here's a C# test to reproduce the issue with the key function:

[Test]
public void TestKeyFunctionWithScope()
{
    var reduceFunction = new BsonJavaScript("function(document, result) { result.z = 2 }");
    var keyFunction = new BsonJavaScriptWithScope(
        "function(document) { return { x : document[key] }; }",
        new BsonDocument("key", "x"));
    var result = _collection.Group(new GroupArgs
    {
        KeyFunction  = keyFunction,
        ReduceFunction = reduceFunction,
        Initial = new BsonDocument("z", 0)
    })
    .ToList();
    Assert.That(result.Count, Is.EqualTo(1));
    Assert.That(result[0]["z"].ToInt32(), Is.EqualTo(2));
}

The result is:

Command 'group' failed: exception: ReferenceError: key is not defined

Here's a C# test to reproduce the issue with the finalize function:

[Test]
public void TestFinalizeFunctionWithScope()
{
    var reduceFunction = new BsonJavaScript("function(document, result) { result.z = 1; }");
    var finalizeFunction = new BsonJavaScriptWithScope(
        "function(result) { result.z = z }",
        new BsonDocument("z", 2));
    var result = _collection.Group(new GroupArgs
    {
        KeyFields = GroupBy.Keys("x"),
        ReduceFunction = reduceFunction,
        FinalizeFunction = finalizeFunction,
        Initial = new BsonDocument("z", 0)
    })
    .ToList();
    Assert.That(result.Count, Is.EqualTo(1));
    Assert.That(result[0]["z"].ToInt32(), Is.EqualTo(2));
}

The result is:

Command 'group' failed: exception: ReferenceError: z is not defined

The ASSERTs in the above tests assume a test collection with the following documents:

{ _id : ..., x : 1 }
{ _id : ..., x : 1 }



 Comments   
Comment by J Rassi [ 18/Mar/16 ]

We have tentatively scheduled the group command for deprecation in 3.4, and are not considering improvements to it at this time. Please watch SERVER-14193 for related updates.

Closing this ticket as "Won't Fix".

Generated at Thu Feb 08 03:38:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.