Skip to content

Commit

Permalink
remove double ifNull-wrapping on count results, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hallettj committed Dec 11, 2024
1 parent 7a599e6 commit eb89870
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
42 changes: 28 additions & 14 deletions crates/mongodb-agent-common/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ mod tests {
{
"$facet": {
"avg": [
{ "$match": { "gpa": { "$exists": true, "$ne": null } } },
{ "$match": { "gpa": { "$ne": null } } },
{ "$group": { "_id": null, "result": { "$avg": "$gpa" } } },
],
"count": [
{ "$match": { "gpa": { "$exists": true, "$ne": null } } },
{ "$match": { "gpa": { "$ne": null } } },
{ "$group": { "_id": "$gpa" } },
{ "$count": "result" },
],
Expand All @@ -123,10 +123,17 @@ mod tests {
{
"$replaceWith": {
"aggregates": {
"avg": { "$getField": {
"field": "result",
"input": { "$first": { "$getField": { "$literal": "avg" } } },
} },
"avg": {
"$ifNull": [
{
"$getField": {
"field": "result",
"input": { "$first": { "$getField": { "$literal": "avg" } } },
}
},
null
]
},
"count": {
"$ifNull": [
{
Expand Down Expand Up @@ -180,24 +187,31 @@ mod tests {
{ "$match": { "gpa": { "$lt": 4.0 } } },
{
"$facet": {
"avg": [
{ "$match": { "gpa": { "$exists": true, "$ne": null } } },
{ "$group": { "_id": null, "result": { "$avg": "$gpa" } } },
],
"__ROWS__": [{
"$replaceWith": {
"student_gpa": { "$ifNull": ["$gpa", null] },
},
}],
"avg": [
{ "$match": { "gpa": { "$ne": null } } },
{ "$group": { "_id": null, "result": { "$avg": "$gpa" } } },
],
},
},
{
"$replaceWith": {
"aggregates": {
"avg": { "$getField": {
"field": "result",
"input": { "$first": { "$getField": { "$literal": "avg" } } },
} },
"avg": {
"$ifNull": [
{
"$getField": {
"field": "result",
"input": { "$first": { "$getField": { "$literal": "avg" } } },
}
},
null
]
},
},
"rows": "$__ROWS__",
},
Expand Down
19 changes: 9 additions & 10 deletions crates/mongodb-agent-common/src/query/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,10 @@ fn facet_pipelines_for_query(
// has a field called `result`. This code selects each facet result by name, and pulls
// out the `result` value.
let value_expr = doc! {
"$ifNull": [
{
"$getField": {
"field": RESULT_FIELD, // evaluates to the value of this field
"input": { "$first": get_field(key.as_str()) }, // field is accessed from this document
},
},
null,
]
"$getField": {
"field": RESULT_FIELD, // evaluates to the value of this field
"input": { "$first": get_field(key.as_str()) }, // field is accessed from this document
},
};

// Matching SQL semantics, if a **count** aggregation does not match any rows we want
Expand All @@ -209,8 +204,12 @@ fn facet_pipelines_for_query(
doc! {
"$ifNull": [value_expr, 0],
}
// Otherwise if the aggregate value is missing because the aggregation applied to an
// empty document set then provide an explicit `null` value.
} else {
value_expr
doc! {
"$ifNull": [value_expr, null]
}
};

(key.to_string(), value_expr.into())
Expand Down

0 comments on commit eb89870

Please sign in to comment.