Skip to content

Commit

Permalink
fix(server/mongo): evolution step filters on all index columns
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Peschke <[email protected]>
(cherry picked from commit 1fb6f94)
  • Loading branch information
lukapeschke committed Feb 6, 2024
1 parent dcb6266 commit 3780b05
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
9 changes: 7 additions & 2 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog (weaverbird python package)

## [0.42.0] - 2024-01-31
## Unreleased

### Changed
### Fixed

- Mongo: Fixed the `evolution` step when several index columns are used. The generated pipeline now filters on all of them
rather than just the last one.

## [0.42.0] - 2024-01-31

- Remove `PipelineWithRefs`. Instead, support method `resolve_references` on types `Pipeline` and `PipelineWithVariables`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def translate_evolution(step: EvolutionStep) -> list[MongoStep]:
"cond": {
"$and": [
{"$eq": ["$_VQB_DATE_PREV", f"$$item.{step.date_col}"]},
# FIXME: this is a bug
{"$eq": [f"${col}", f"$$item.{col}"] for col in step.index_columns}, # noqa: B035
*({"$eq": [f"${col}", f"$$item.{col}"]} for col in step.index_columns),
],
},
},
Expand Down
75 changes: 75 additions & 0 deletions server/tests/backends/mongo_translator/steps/test_evolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from weaverbird.backends.mongo_translator.steps.evolution import translate_evolution
from weaverbird.pipeline.steps.evolution import EvolutionStep


def test_translate_evolution() -> None:
step = EvolutionStep(
dateCol="foo",
valueCol="bar",
evolutionFormat="abs",
evolutionType="vsLastDay",
indexColumns=["a", "b", "c"],
)

assert translate_evolution(step) == [
{"$addFields": {"_VQB_DATE_PREV": {"$dateSubtract": {"amount": 1, "startDate": "$foo", "unit": "week"}}}},
{
"$facet": {
"_VQB_COPIES_ARRAY": [{"$group": {"_VQB_ALL_DOCS": {"$push": "$$ROOT"}, "_id": None}}],
"_VQB_ORIGINALS": [{"$project": {"_id": 0}}],
}
},
{"$unwind": "$_VQB_ORIGINALS"},
{
"$project": {
"_VQB_ORIGINALS": {
"$mergeObjects": [
"$_VQB_ORIGINALS",
{"$arrayElemAt": ["$_VQB_COPIES_ARRAY", 0]},
]
}
}
},
{"$replaceRoot": {"newRoot": "$_VQB_ORIGINALS"}},
{
"$addFields": {
"_VQB_ALL_DOCS": {
"$filter": {
"as": "item",
"cond": {
"$and": [
{"$eq": ["$_VQB_DATE_PREV", "$$item.foo"]},
{"$eq": ["$a", "$$item.a"]},
{"$eq": ["$b", "$$item.b"]},
{"$eq": ["$c", "$$item.c"]},
]
},
"input": "$_VQB_ALL_DOCS",
}
}
}
},
{
"$addFields": {
"_VQB_VALUE_PREV": {
"$cond": [
{"$gt": [{"$size": "$_VQB_ALL_DOCS.bar"}, 1]},
"Error",
{"$arrayElemAt": ["$_VQB_ALL_DOCS.bar", 0]},
]
}
}
},
{
"$addFields": {
"bar_EVOL_ABS": {
"$cond": [
{"$eq": ["$_VQB_VALUE_PREV", "Error"]},
"Error: More than one previous " "date found for the specified " "index columns",
{"$subtract": ["$bar", "$_VQB_VALUE_PREV"]},
]
}
}
},
{"$project": {"_VQB_ALL_DOCS": 0, "_VQB_DATE_PREV": 0, "_VQB_VALUE_PREV": 0}},
]

0 comments on commit 3780b05

Please sign in to comment.