[SERVER-77503] $percentile accumulator doesn't accept variables from the "let" option Created: 25/May/23  Updated: 29/Oct/23  Resolved: 15/Jun/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.1.0-rc0, 7.0.0-rc5

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

Issue Links:
Backports
Related
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v7.0
Sprint: QI 2023-06-12, QI 2023-06-26
Participants:

 Description   

Run:

db.test.aggregate([{
    $group: {_id: null, median: {$percentile: {p: ["$$foo"], method: "approximate", input: "$a"}}}
  }],
  {let: {foo: 0.5}
})

Result: 'MongoServerError: BSON field '$percentile.p.0' is the wrong type 'string', expected types '[long, int, decimal, double]'

It looks like the idl validation kicks in before the expression is substituted from the "let" option.



 Comments   
Comment by Githook User [ 15/Jun/23 ]

Author:

{'name': 'Irina Yatsenko', 'email': 'irina.yatsenko@mongodb.com', 'username': 'IrinaYatsenko'}

Message: SERVER-77503 $percentile should accept variables and expressions for "p" arg
Branch: v7.0
https://github.com/mongodb/mongo/commit/a3e1081bed2af047710d61ca2e1b1ca3a084b0d3

Comment by Githook User [ 15/Jun/23 ]

Author:

{'name': 'Irina Yatsenko', 'email': 'irina.yatsenko@mongodb.com', 'username': 'IrinaYatsenko'}

Message: SERVER-77503 $percentile should accept variables and expressions for "p" arg
Branch: master
https://github.com/mongodb/mongo/commit/2a4b3b6d7250a8870c3facf26cc7df1be8163f53

Comment by Slav Babanin [ 26/May/23 ]

Based on my understanding, once the issue is fixed, it should resolve the use case below as well, which is referencing the whole array. Currently it results in error: MongoServerError: BSON field '$percentile.p' is the wrong type 'string', expected type 'array.

irina.yatsenko@mongodb.com Could you please confirm if "referencing array from let"  in p field is allowed operation?

This is based on examples from Syntax: Discrete, Continuous and Approximate Percentile and Median Accumulator

db.test.aggregate([
  {
    $group: {
      _id: "$t",
      Percentile: {
        $percentile: {
          p: "$$percentiles_array",
          method: "approximate",
          input: "$a"
        }
      }
    }
  }
], {
  let: {
    percentiles_array: [0.5, 0.9, 0.95]
  }
}) 

Generated at Thu Feb 08 06:35:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.