Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server/mongo): evolution step filters on all index columns #2015

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

## [0.26.10] - 2024-02-06

- 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.26.9] - 2023-10-05

- Pypika: Fixed the `todate` step by ensuring the right formatting is used for every backend
Expand Down
2 changes: 1 addition & 1 deletion server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "weaverbird"
version = "0.26.9"
version = "0.26.10"
description = "A visual data pipeline builder with various backends"
authors = ["Toucan Toco <[email protected]>"]
keywords = ["mongodb", "pandas", "sql", "data", "dataviz", "pipeline", "query", "builder"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def translate_evolution(step: EvolutionStep) -> list[MongoStep]:
"cond": {
"$and": [
{"$eq": ["$_VQB_DATE_PREV", f"$$item.{step.date_col}"]},
{"$eq": [f"${col}", f"$$item.{col}"] for col in step.index_columns},
*(
{"$eq": [f"${col}", f"$$item.{col}"]}
for col in step.index_columns
),
],
},
},
Expand Down
85 changes: 85 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,85 @@
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}},
]
Loading