Skip to content

Commit

Permalink
add test for formula queries with curried aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
john-z-yang committed Jul 1, 2024
1 parent f8e1456 commit e0c4e7b
Showing 1 changed file with 93 additions and 1 deletion.
94 changes: 93 additions & 1 deletion tests/query/parser/test_formula_mql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
JoinType,
)
from snuba.query.data_source.simple import Entity as QueryEntity
from snuba.query.dsl import divide, literals_tuple, minus, plus
from snuba.query.dsl import arrayElement, divide, literals_tuple, minus, plus
from snuba.query.expressions import (
Column,
CurriedFunctionCall,
FunctionCall,
Literal,
SubscriptableReference,
Expand Down Expand Up @@ -622,3 +623,94 @@ def test_formula_with_scalar() -> None:
query = parse_mql_query_new(str(query_body), mql_context, generic_metrics)
eq, reason = query.equals(expected)
assert eq, reason


def test_curried_aggregate_formula() -> None:
query_body = "quantiles(0.5)(`d:transactions/duration@millisecond`){status_code:200} / sum(`d:transactions/duration@millisecond`)"

expected_selected = SelectedExpression(
"aggregate_value",
divide(
arrayElement(
None,
CurriedFunctionCall(
None,
FunctionCall(
None,
"quantiles",
(Literal(None, 0.5),),
),
(Column("_snuba_value", "d0", "value"),),
),
Literal(None, 1),
),
FunctionCall(
None,
"sum",
(Column("_snuba_value", "d1", "value"),),
),
"_snuba_aggregate_value",
),
)

join_clause = JoinClause(
left_node=IndividualNode(
alias="d1",
data_source=from_distributions,
),
right_node=IndividualNode(
alias="d0",
data_source=from_distributions,
),
keys=[
JoinCondition(
left=JoinConditionExpression(table_alias="d1", column="time"),
right=JoinConditionExpression(table_alias="d0", column="time"),
)
],
join_type=JoinType.INNER,
join_modifier=None,
)

tag_condition = binary_condition(
"equals", tag_column("status_code", "d0"), Literal(None, "200")
)
metric_condition1 = metric_id_condition(123456, "d0")
metric_condition2 = metric_id_condition(123456, "d1")
formula_condition = combine_and_conditions(
condition("d0")
+ condition("d1")
+ [tag_condition, metric_condition1, metric_condition2]
)

expected = CompositeQuery(
from_clause=join_clause,
selected_columns=[
expected_selected,
SelectedExpression(
"time",
time_expression("d1"),
),
SelectedExpression(
"time",
time_expression("d0"),
),
],
groupby=[time_expression("d1"), time_expression("d0")],
condition=formula_condition,
order_by=[
OrderBy(
direction=OrderByDirection.ASC,
expression=time_expression("d0"),
),
],
limit=1000,
offset=0,
)

generic_metrics = get_dataset(
"generic_metrics",
)
query = parse_mql_query_new(str(query_body), mql_context, generic_metrics)
eq, reason = query.equals(expected)
assert eq, reason

0 comments on commit e0c4e7b

Please sign in to comment.