-
-
Notifications
You must be signed in to change notification settings - Fork 64
feat(eap): Add v2 co-occurring attributes storage with count, last_seen, and per-type attribute keys #7801
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
Open
phacops
wants to merge
10
commits into
master
Choose a base branch
from
phacops/eap-co-occurring-attrs-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−4
Open
feat(eap): Add v2 co-occurring attributes storage with count, last_seen, and per-type attribute keys #7801
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
64b94fc
feat(eap): Add v2 co-occurring attributes storage with count column
phacops 75193f8
Merge branch 'master' into phacops/eap-co-occurring-attrs-v2
phacops 39cbfd4
fix(eap): Renumber co-occurring attrs migration to 0055
phacops ee84da6
Merge remote-tracking branch 'origin/master' into phacops/eap-co-occu…
claude c85c925
ref(eap): Scope co-occurring v2 to table only and renumber migration
claude 76c05a5
Merge remote-tracking branch 'origin/master' into phacops/eap-co-occu…
claude 4386319
feat(eap): Add last_seen field to co-occurring attrs v2 + fix migrati…
claude 9afbed5
feat(eap): Represent int and array attribute types in co-occurring v2
claude 6f1c111
Merge remote-tracking branch 'origin/master' into phacops/eap-co-occu…
claude dbe7258
feat(eap): Split co-occurring v2 array attrs by element type; bump se…
claude 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
34 changes: 34 additions & 0 deletions
34
...sets/configuration/events_analytics_platform/storages/eap_item_co_occurring_attrs_v2.yaml
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,34 @@ | ||
| version: v1 | ||
| kind: readable_storage | ||
| name: eap_item_co_occurring_attrs_v2 | ||
|
|
||
| storage: | ||
| key: eap_item_co_occurring_attrs_v2 | ||
| set_key: events_analytics_platform | ||
|
|
||
| readiness_state: complete | ||
|
|
||
| schema: | ||
| columns: | ||
| [ | ||
| { name: organization_id, type: UInt, args: { size: 64 } }, | ||
| { name: project_id, type: UInt, args: { size: 64 } }, | ||
| { name: item_type, type: UInt, args: { size: 8 } }, | ||
| { name: date, type: Date }, | ||
| { name: retention_days, type: UInt, args: { size: 16 } }, | ||
| { name: attribute_keys_hash, type: Array, args: { inner_type: { type: UInt, args: { size: 64 } } } }, | ||
| { name: attributes_string, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: attributes_float, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: attributes_int, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: attributes_bool, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: attributes_array_string, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: attributes_array_int, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: attributes_array_float, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: attributes_array_bool, type: Array, args: { inner_type: { type: String } } }, | ||
| { name: count, type: UInt, args: { size: 64 } }, | ||
| { name: last_seen, type: SimpleAggregateFunction, args: { func: max, arg_types: [{ type: DateTime }] } }, | ||
| ] | ||
| local_table_name: eap_item_co_occurring_attrs_2_local | ||
| dist_table_name: eap_item_co_occurring_attrs_2_dist | ||
| partition_format: [retention_days, date] | ||
| allocation_policies: [] |
177 changes: 177 additions & 0 deletions
177
snuba/snuba_migrations/events_analytics_platform/0062_add_count_to_co_occurring_attrs.py
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,177 @@ | ||
| from collections.abc import Sequence | ||
|
|
||
| from snuba.clusters.storage_sets import StorageSetKey | ||
| from snuba.datasets.storages.tags_hash_map import get_array_vals_hash | ||
| from snuba.migrations import migration, operations, table_engines | ||
| from snuba.migrations.columns import MigrationModifiers as Modifiers | ||
| from snuba.migrations.operations import OperationTarget, SqlOperation | ||
| from snuba.utils.schemas import ( | ||
| Array, | ||
| Column, | ||
| Date, | ||
| DateTime, | ||
| SimpleAggregateFunction, | ||
| String, | ||
| UInt, | ||
| ) | ||
|
|
||
| num_attr_buckets = 40 | ||
|
|
||
| # Every attribute-key array in the table, across all attribute types (the scalar | ||
| # string/float/int/bool maps and the four array-valued maps). The dedup `key_hash` | ||
| # and the bloom-filter `attribute_keys_hash` are both derived from this so they | ||
| # cover every attribute key regardless of its type. | ||
| _all_attribute_keys = ( | ||
| "arrayConcat(" | ||
| "attributes_string, attributes_float, attributes_int, attributes_bool, " | ||
| "attributes_array_string, attributes_array_int, " | ||
| "attributes_array_float, attributes_array_bool)" | ||
| ) | ||
|
|
||
| columns: list[Column[Modifiers]] = [ | ||
| Column("organization_id", UInt(64)), | ||
| Column("project_id", UInt(64)), | ||
| Column("item_type", UInt(8)), | ||
| Column("date", Date(Modifiers(codecs=["DoubleDelta", "ZSTD(1)"]))), | ||
| Column( | ||
| "retention_days", | ||
| UInt(16), | ||
| ), | ||
| Column( | ||
| "attribute_keys_hash", | ||
| Array( | ||
| UInt(64), | ||
| Modifiers(materialized=get_array_vals_hash(f"arrayDistinct({_all_attribute_keys})")), | ||
| ), | ||
| ), | ||
| # one array of attribute keys per attribute type, mirroring the typed maps on | ||
| # eap_items so every attribute can be surfaced with its type. | ||
| Column("attributes_string", Array(String())), | ||
| Column("attributes_float", Array(String())), | ||
| Column("attributes_int", Array(String())), | ||
| Column("attributes_bool", Array(String())), | ||
| # keys of the array-valued attributes, split by element type to match the | ||
| # AttributeKey TYPE_ARRAY_STRING / TYPE_ARRAY_INT / TYPE_ARRAY_DOUBLE / | ||
| # TYPE_ARRAY_BOOL types (float arrays map to TYPE_ARRAY_DOUBLE). | ||
| Column("attributes_array_string", Array(String())), | ||
| Column("attributes_array_int", Array(String())), | ||
| Column("attributes_array_float", Array(String())), | ||
| Column("attributes_array_bool", Array(String())), | ||
| # a hash of all the attribute keys of the item in sorted order | ||
| # this lets us deduplicate rows with merges | ||
| Column( | ||
| "key_hash", | ||
| UInt( | ||
| 64, | ||
| Modifiers(materialized=f"cityHash64(arraySort(arrayDistinct({_all_attribute_keys})))"), | ||
| ), | ||
| ), | ||
| Column("count", UInt(64)), | ||
| # the most recent timestamp at which this set of attributes was seen. | ||
| # SummingMergeTree applies the `max` aggregate function on merge, so this | ||
| # keeps the latest timestamp across all the rows that get collapsed. | ||
| Column("last_seen", SimpleAggregateFunction("max", [DateTime()])), | ||
| ] | ||
|
|
||
| _attr_num_names = ", ".join([f"mapKeys(attributes_float_{i})" for i in range(num_attr_buckets)]) | ||
| _attr_str_names = ", ".join([f"mapKeys(attributes_string_{i})" for i in range(num_attr_buckets)]) | ||
|
|
||
| MV_QUERY = f""" | ||
| SELECT | ||
| organization_id AS organization_id, | ||
| project_id AS project_id, | ||
| item_type as item_type, | ||
| toMonday(timestamp) AS date, | ||
| retention_days as retention_days, | ||
| arrayConcat({_attr_str_names}) AS attributes_string, | ||
| arrayConcat({_attr_num_names}) AS attributes_float, | ||
| mapKeys(attributes_int) AS attributes_int, | ||
| mapKeys(attributes_bool) AS attributes_bool, | ||
| mapKeys(attributes_array_string) AS attributes_array_string, | ||
| mapKeys(attributes_array_int) AS attributes_array_int, | ||
| mapKeys(attributes_array_float) AS attributes_array_float, | ||
| mapKeys(attributes_array_bool) AS attributes_array_bool, | ||
| 1 AS count, | ||
| timestamp AS last_seen | ||
| FROM eap_items_1_local | ||
| """ | ||
|
|
||
|
|
||
| class Migration(migration.ClickhouseNodeMigration): | ||
| blocking = False | ||
| storage_set_key = StorageSetKey.EVENTS_ANALYTICS_PLATFORM | ||
|
|
||
| local_table_name = "eap_item_co_occurring_attrs_2_local" | ||
| dist_table_name = "eap_item_co_occurring_attrs_2_dist" | ||
| mv_name = "eap_item_co_occurring_attrs_3_mv" | ||
|
|
||
| def forwards_ops(self) -> Sequence[SqlOperation]: | ||
| create_table_ops = [ | ||
| operations.CreateTable( | ||
| storage_set=self.storage_set_key, | ||
| table_name=self.local_table_name, | ||
| engine=table_engines.SummingMergeTree( | ||
| storage_set=self.storage_set_key, | ||
| primary_key="(organization_id, project_id, date, item_type, key_hash)", | ||
| order_by="(organization_id, project_id, date, item_type, key_hash, retention_days)", | ||
| partition_by="(retention_days, toMonday(date))", | ||
| ttl="date + toIntervalDay(retention_days)", | ||
| ), | ||
| columns=columns, | ||
| target=OperationTarget.LOCAL, | ||
| ), | ||
| operations.CreateTable( | ||
| storage_set=self.storage_set_key, | ||
| table_name=self.dist_table_name, | ||
| engine=table_engines.Distributed( | ||
| local_table_name=self.local_table_name, | ||
| sharding_key=None, | ||
| ), | ||
| columns=columns, | ||
| target=OperationTarget.DISTRIBUTED, | ||
| ), | ||
| ] | ||
|
|
||
| index_ops = [ | ||
| operations.AddIndex( | ||
| storage_set=self.storage_set_key, | ||
| table_name=self.local_table_name, | ||
| index_name="bf_attribute_keys_hash", | ||
| index_expression="attribute_keys_hash", | ||
| index_type="bloom_filter", | ||
| granularity=1, | ||
| target=operations.OperationTarget.LOCAL, | ||
| ), | ||
| ] | ||
|
|
||
| materialized_view_ops: list[SqlOperation] = [ | ||
| operations.CreateMaterializedView( | ||
| storage_set=self.storage_set_key, | ||
| view_name=self.mv_name, | ||
| columns=columns, | ||
| destination_table_name=self.local_table_name, | ||
| target=OperationTarget.LOCAL, | ||
| query=MV_QUERY, | ||
| ), | ||
| ] | ||
|
|
||
| return create_table_ops + index_ops + materialized_view_ops | ||
|
|
||
| def backwards_ops(self) -> Sequence[SqlOperation]: | ||
| return [ | ||
| operations.DropTable( | ||
| storage_set=self.storage_set_key, | ||
| table_name=self.mv_name, | ||
| target=OperationTarget.LOCAL, | ||
| ), | ||
| operations.DropTable( | ||
| storage_set=self.storage_set_key, | ||
| table_name=self.dist_table_name, | ||
| target=OperationTarget.DISTRIBUTED, | ||
| ), | ||
| operations.DropTable( | ||
| storage_set=self.storage_set_key, | ||
| table_name=self.local_table_name, | ||
| target=OperationTarget.LOCAL, | ||
| ), | ||
| ] | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.