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

feat: optimizer rewrite #5676

Merged
merged 18 commits into from
Mar 28, 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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ jobs:
tests/sentry/event_manager \
tests/sentry/api/endpoints/test_organization_profiling_functions.py \
tests/snuba/api/endpoints/test_organization_events_stats_mep.py \
tests/sentry/sentry_metrics/querying \
tests/snuba/test_snql_snuba.py \
tests/snuba/test_metrics_layer.py \
-vv --cov . --cov-report="xml:.artifacts/snuba.coverage.xml"
Expand Down
5 changes: 4 additions & 1 deletion snuba/query/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def is_not_in_condition_pattern(lhs: Pattern[Expression]) -> FunctionCallPattern
def binary_condition(
function_name: str, lhs: Expression, rhs: Expression
) -> FunctionCall:
return FunctionCall(None, function_name, (lhs, rhs))
"""This function is deprecated please use snuba.query.dsl.binary_condition"""
from snuba.query.dsl import binary_condition as dsl_binary_condition

return dsl_binary_condition(function_name, lhs, rhs)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldnt use this function in dsl.py bc of circular import with conditions.py. I thought this belonged in dsl.py anyways so I moved it.



binary_condition_patterns = {
Expand Down
23 changes: 23 additions & 0 deletions snuba/query/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ def divide(
return FunctionCall(alias, "divide", (lhs, rhs))


# boolean functions
def binary_condition(
function_name: str, lhs: Expression, rhs: Expression
) -> FunctionCall:
return FunctionCall(None, function_name, (lhs, rhs))


def equals(lhs: Expression, rhs: Expression) -> FunctionCall:
return binary_condition("equals", lhs, rhs)


def and_cond(lhs: FunctionCall, rhs: FunctionCall) -> FunctionCall:
return binary_condition("and", lhs, rhs)


def or_cond(lhs: FunctionCall, rhs: FunctionCall) -> FunctionCall:
return binary_condition("or", lhs, rhs)


def in_cond(lhs: Expression, rhs: Expression) -> FunctionCall:
return binary_condition("in", lhs, rhs)


# aggregate functions
def count(column: Optional[Column] = None, alias: Optional[str] = None) -> FunctionCall:
return FunctionCall(alias, "count", (column,) if column else ())
Expand Down
22 changes: 13 additions & 9 deletions snuba/query/mql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from snuba.query.processors.logical.filter_in_select_optimizer import (
FilterInSelectOptimizer,
)
from snuba.query.query_settings import QuerySettings
from snuba.query.query_settings import HTTPQuerySettings, QuerySettings
from snuba.query.snql.anonymize import format_snql_anonymized
from snuba.query.snql.parser import (
MAX_LIMIT,
Expand All @@ -49,7 +49,7 @@
_replace_time_condition,
_treeify_or_and_conditions,
)
from snuba.state import explain_meta
from snuba.state import explain_meta, get_int_config
from snuba.util import parse_datetime
from snuba.utils.constants import GRANULARITIES_AVAILABLE

Expand Down Expand Up @@ -1064,19 +1064,12 @@ def transform(exp: Expression) -> Expression:
query.transform_expressions(transform)


def optimize_filter_in_select(
query: CompositeQuery[QueryEntity] | LogicalQuery,
) -> None:
FilterInSelectOptimizer().process_mql_query(query)


CustomProcessors = Sequence[
Callable[[Union[CompositeQuery[QueryEntity], LogicalQuery]], None]
]

MQL_POST_PROCESSORS: CustomProcessors = POST_PROCESSORS + [
quantiles_to_quantile,
optimize_filter_in_select,
]


Expand Down Expand Up @@ -1116,6 +1109,17 @@ def parse_mql_query(
settings,
)

# Filter in select optimizer
feat_flag = get_int_config("enable_filter_in_select_optimizer", default=1)
if feat_flag:
with sentry_sdk.start_span(
op="processor", description="filter_in_select_optimize"
):
if settings is None:
FilterInSelectOptimizer().process_query(query, HTTPQuerySettings())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to point out that in this context settings can be None, LogicalQueryProcessor interface requires settings not None, thus I had to create dummy object.

Since settings arent used by my processor it doesnt matter, but the fact that i have to create this dummy instead of None smells to me

else:
FilterInSelectOptimizer().process_query(query, settings)

# Custom processing to tweak the AST before validation
with sentry_sdk.start_span(op="processor", description="custom_processing"):
if custom_processing is not None:
Expand Down
Loading
Loading