-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(discover) Add equation support to SnQL #29394
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| from snuba_sdk.function import CurriedFunction, Function | ||
| from snuba_sdk.orderby import Direction, OrderBy | ||
|
|
||
| from sentry.discover.arithmetic import Operation, OperationSideType, resolve_equation_list | ||
| from sentry.discover.models import TeamKeyTransaction | ||
| from sentry.exceptions import InvalidSearchQuery | ||
| from sentry.models import Project, ProjectTeam, ProjectTransactionThreshold | ||
|
|
@@ -2785,16 +2786,30 @@ def __init__( | |
| for alias, name in FUNCTION_ALIASES.items(): | ||
| self.function_converter[alias] = self.function_converter[name].alias_as(alias) | ||
|
|
||
| def resolve_select(self, selected_columns: Optional[List[str]]) -> List[SelectType]: | ||
| def resolve_select( | ||
| self, selected_columns: Optional[List[str]], equations: Optional[List[str]] | ||
| ) -> List[SelectType]: | ||
| """Given a public list of discover fields, construct the corresponding | ||
| list of Snql Columns or Functions. Duplicate columns are ignored | ||
| """ | ||
|
|
||
| if selected_columns is None: | ||
| return [] | ||
|
|
||
| resolved_columns = [] | ||
| stripped_columns = [column.strip() for column in selected_columns] | ||
|
|
||
| if equations: | ||
| _, _, parsed_equations = resolve_equation_list( | ||
| equations, stripped_columns, use_snql=True | ||
| ) | ||
| resolved_columns.extend( | ||
| [ | ||
| self.resolve_equation(equation, f"equation[{index}]") | ||
| for index, equation in enumerate(parsed_equations) | ||
| ] | ||
| ) | ||
|
|
||
| # Add threshold config alias if there's a function that depends on it | ||
| # TODO: this should be replaced with an explicit request for the project_threshold_config as a column | ||
| for column in { | ||
|
|
@@ -2860,6 +2875,27 @@ def resolve_field(self, raw_field: str, alias: bool = False) -> Column: | |
| else: | ||
| raise InvalidSearchQuery(f"Invalid characters in field {field}") | ||
|
|
||
| def resolve_equation(self, equation: Operation, alias: Optional[str] = None) -> SelectType: | ||
| """Convert this tree of Operations to the equivalent snql functions""" | ||
| lhs = self._resolve_equation_side(equation.lhs) | ||
| rhs = self._resolve_equation_side(equation.rhs) | ||
| result = Function(equation.operator, [lhs, rhs], alias) | ||
| return result | ||
|
|
||
| def _resolve_equation_side(self, side: OperationSideType) -> Union[SelectType, float]: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think this is called an operand.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😓 I'll update in a follow up PR, cause I've used |
||
| if isinstance(side, Operation): | ||
| return self.resolve_equation(side) | ||
| elif isinstance(side, float): | ||
| return side | ||
| else: | ||
| return self.resolve_column(side) | ||
|
|
||
| def is_equation_column(self, column: SelectType) -> bool: | ||
| """Equations are only ever functions, and shouldn't be literals so we | ||
| need to check that the column is a Function | ||
| """ | ||
| return isinstance(column, CurriedFunction) and column.alias.startswith("equation[") | ||
|
|
||
| def resolve_orderby(self, orderby: Optional[Union[List[str], str]]) -> List[OrderBy]: | ||
| """Given a list of public aliases, optionally prefixed by a `-` to | ||
| represent direction, construct a list of Snql Orderbys | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Move this below the
resolved_equations.append(...)because it is now splitting apart the corresponding comment.