[COMPASS-4475] Aggregation tool generating Java code that causes runtime errors Created: 27/Oct/20  Updated: 29/Oct/23  Resolved: 03/Feb/21

Status: Closed
Project: Compass
Component/s: Aggregation pipeline
Affects Version/s: 1.22.1
Fix Version/s: 1.26.0

Type: Bug Priority: Major - P3
Reporter: Michael Pratt Assignee: Michael Rose (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

node.js / npm versions: v12.12.0 / 6.13.4
OS: OS X 10.15.7
Java Driver : 4.0.5


Attachments: PNG File Screenshot 2021-01-29 at 16.17.31.png    
Issue Links:
Depends
depends on JAVA-3956 Add a helper method to the fluid API ... Backlog
Duplicate
is duplicated by COMPASS-4512 Error generating java code Closed
Related
related to JAVA-3338 $geoWithin cannot contain multiple fi... Closed
related to COMPASS-4695 Bring back builders in export to lang... Needs Triage
is related to COMPASS-4111 Export $lookup aggregate to java fails Open
is related to COMPASS-4341 export to Java doesn't like $size exp... Waiting (Blocked)
Documentation Changes: Not Needed
Sprint: Iteration Uglifruit

 Description   

I've built a pipeline in Compass that uses a project phase with $reduce:

I have a project phase that also uses $reduce:

{
  _id: 0,
  title: '$playlist.songs.title',
  artist: {
    $reduce: {
      input: '$playlist.songs.artists.name',
      initialValue: "",
      in: { $concat : ["$$value", " ", "$$this"] }
    }
  }
}

The pipeline executes fine in Compass and gives me back to documents I expect.

However, when I export to Java driver code, the code that is generated results in a runtime error in my application.

Here is the Java code that is generated is:

project(
  fields(
      excludeId(), 
      computed("title", "$playlist.songs.title"), 
      computed("artist", 
        eq("$reduce", 
          and(
            eq("input", "$playlist.songs.artists.name"),
            eq("initialValue", ""), 
            eq("in", eq("$concat", Arrays.asList("$$value", " ", "$$this"))))))))

And the error that is returned:

com.mongodb.MongoCommandException: Command failed with error 40076 (Location40076): '$reduce found an unknown argument: $and'

As some background, this was a pipeline I generated over a year ago (not sure the precise Compass version) and it worked fine with Java driver 3.x and Server 3.x. A few months ago the server was upgraded to 4.x, and the pipeline continued to run fine using Java driver 3.x.

It wasn't until I upgraded the Java driver to 4.x that this issue came up.

I have rebuilt the pipeline in the latest Compass version, and confirmed it still generates the same snippet of Java code that results in the error.



 Comments   
Comment by Githook User [ 03/Feb/21 ]

Author:

{'name': 'Michael Rose', 'email': 'michael_rose@gmx.de', 'username': 'rose-m'}

Message: fix(export-to-language): Disable builders for Java pipeline export COMPASS-4475 (#2108)
Branch: master
https://github.com/mongodb-js/compass/commit/ae438c6114c7e0a285bf468d24d723b49f2463f2

Comment by Michael Pratt [ 27/Oct/20 ]

For reference, here's some pure BSON that works in place of the generated reduce code:

eq("$reduce",
    new BsonDocument("input", new BsonString("$playlist.songs.artists.name"))
        .append("initialValue", new BsonString(""))
        .append("in", new BsonDocument("$concat", new BsonArray(Arrays.asList(
            new BsonString("$$value"),
            new BsonString(" "),
            new BsonString("$$this"))))))))),

Generated at Wed Feb 07 22:36:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.