Repository navigation
feat(flags): warn when SQL reads $feature_flag_called from events #114059
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
+1,086
−702
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fec4176
feat(flags): warn when SQL reads $feature_flag_called from events
haacked 6436081
Skip saved expression bodies and field type graphs in the flag called…
haacked d077358
Gate the flag called SQL warning on flag-called-move-notices
haacked c1e66cc
Link the flag called SQL warning to the announcement from the flag pa…
haacked a425e90
Cover the flag-called-move-notices flag at 0% rollout
haacked ac75cf4
chore(visual): update storybook baselines
posthog[bot] 3eddfc7
test(backend): update query snapshots
tests-posthog[bot] 6d704ec
Expect the url field in metadata notice assertions
haacked 9dede36
chore(visual): update storybook baselines
posthog[bot] 36b59a5
Remove the announcement link from the flag called SQL warning
haacked 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| from posthog.schema import HogQLNotice | ||
|
|
||
| from posthog.hogql import ast | ||
| from posthog.hogql.context import HogQLContext | ||
| from posthog.hogql.database.schema.events import EventsTable | ||
| from posthog.hogql.resolver import resolve_types | ||
| from posthog.hogql.visitor import TraversingVisitor, clone_expr | ||
|
|
||
| # posthog/models/flag_evaluations/sql.py defines this name too. It imports django.conf, which this module keeps | ||
| # off its import path, so the name is spelled out here. | ||
| FLAG_CALLED_EVENT = "$feature_flag_called" | ||
|
|
||
| # Orgs that can't query posthog.flag_evaluations yet get this warning too, so the copy speaks of the move in the future. | ||
| FLAG_CALLED_ON_EVENTS_WARNING = ( | ||
| f"{FLAG_CALLED_EVENT} is moving from events to posthog.flag_evaluations. Once the move finishes " | ||
| "for your organization, this query will stop returning these events and you can query " | ||
| "posthog.flag_evaluations instead." | ||
| ) | ||
|
|
||
|
|
||
| class _FlagCalledOnEventsFinder(TraversingVisitor): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.literals: list[ast.Constant] = [] | ||
|
|
||
| def visit_select_query(self, node: ast.SelectQuery) -> None: | ||
| # The resolver inlines a saved view's body, parsed from the view's own text. Its offsets do not | ||
| # point into the user's query. | ||
| if node.view_name is not None: | ||
| return | ||
| super().visit_select_query(node) | ||
|
|
||
| def visit_alias(self, node: ast.Alias) -> None: | ||
| # The resolver inlines a saved expression's body in a hidden alias. The body's offsets point into its own text. | ||
| if node.hidden: | ||
| return | ||
| super().visit_alias(node) | ||
|
|
||
| def visit_field(self, node: ast.Field) -> None: | ||
| # The inherited visit follows the field's type into shared CTE types, which takes exponential time. | ||
| pass | ||
|
|
||
| def visit_compare_operation(self, node: ast.CompareOperation) -> None: | ||
| if node.op in (ast.CompareOperationOp.Eq, ast.CompareOperationOp.In, ast.CompareOperationOp.GlobalIn): | ||
| for field_side, value_side in ((node.left, node.right), (node.right, node.left)): | ||
| if _is_events_event_field(field_side): | ||
| self.literals.extend(_flag_called_constants(value_side)) | ||
| super().visit_compare_operation(node) | ||
|
|
||
|
|
||
| def _is_events_event_field(expr: ast.Expr) -> bool: | ||
| while isinstance(expr, ast.Alias): | ||
| expr = expr.expr | ||
| if not isinstance(expr, ast.Field): | ||
| return False | ||
| # A select alias used in WHERE wraps the column in one FieldAliasType per alias. | ||
| field_type = expr.type | ||
| while isinstance(field_type, ast.FieldAliasType): | ||
| field_type = field_type.type | ||
| if not isinstance(field_type, ast.FieldType) or field_type.name != "event": | ||
| return False | ||
| table_type = field_type.table_type | ||
| while isinstance(table_type, (ast.TableAliasType, ast.ColumnAliasedTableType)): | ||
| table_type = table_type.table_type | ||
| return isinstance(table_type, ast.TableType) and isinstance(table_type.table, EventsTable) | ||
|
|
||
|
|
||
| def _flag_called_constants(expr: ast.Expr) -> list[ast.Constant]: | ||
| if isinstance(expr, ast.Constant): | ||
| return [expr] if expr.value == FLAG_CALLED_EVENT else [] | ||
| if isinstance(expr, (ast.Tuple, ast.Array)): | ||
| return [constant for sub in expr.exprs for constant in _flag_called_constants(sub)] | ||
| return [] | ||
|
|
||
|
|
||
| def flag_called_on_events_warnings( | ||
| node: ast.SelectQuery | ast.SelectSetQuery, context: HogQLContext | ||
| ) -> list[HogQLNotice]: | ||
| """A warning on each `$feature_flag_called` literal that a query compares to the events table's `event` column. | ||
|
|
||
| The query resolves on a clone, because the printed AST has been rewritten and its offsets no longer match | ||
| what the user typed. | ||
| """ | ||
| # A caller that passes an already printed AST never builds the database, and its AST may be rewritten. | ||
| if context.database is None: | ||
| return [] | ||
| finder = _FlagCalledOnEventsFinder() | ||
| with context.timings.measure("flag_called_warnings"): | ||
| finder.visit(resolve_types(clone_expr(node), context, dialect="clickhouse")) | ||
| # A literal that a variable supplies has no span, and a notice without one marks the whole query. | ||
| return [ | ||
| HogQLNotice(message=FLAG_CALLED_ON_EVENTS_WARNING, start=literal.start, end=literal.end) | ||
| for literal in finder.literals | ||
| if literal.start is not None and literal.end is not None | ||
|
haacked marked this conversation as resolved.
|
||
| ] | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.