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

fix(meta) Change the view to use LEFT ARRAY JOIN #5975

Merged
merged 3 commits into from
Jun 3, 2024

Conversation

evanh
Copy link
Member

@evanh evanh commented May 27, 2024

In the current implementation if a metric has no tags associated with it, it will get excluded from the meta table since the array join only selects rows that have values.

This change will cause rows to be created with an empty tag key (0) so that the metrics can still be searched for in the meta tables.

TODO: This depends on #5974 because I still need to validate that this
actually works correctly before this can be merged.

evanh added 2 commits May 27, 2024 15:45
Change the table validation to also handle the LEFT ARRAY JOIN clause.
In the current implementation if a metric has no tags associated with it, it
will get excluded from the meta table since the array join only selects rows
that have values.

This change will cause rows to be created with an empty tag key (0) so that the
metrics can still be searched for in the meta tables.
@evanh evanh requested a review from a team as a code owner May 27, 2024 20:20
Copy link

This PR has a migration; here is the generated SQL

-- start migrations

-- forward migration generic_metrics : 0048_counters_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_counters_meta_mv;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_counters_meta_mv_v2 TO generic_metric_counters_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_counters_raw_local
                LEFT ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end forward migration generic_metrics : 0048_counters_meta_tables_support_empty_tags




-- backward migration generic_metrics : 0048_counters_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_counters_meta_mv_v2;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_counters_meta_mv TO generic_metric_counters_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_counters_raw_local
                ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end backward migration generic_metrics : 0048_counters_meta_tables_support_empty_tags
-- forward migration generic_metrics : 0049_sets_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_sets_meta_mv;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_sets_meta_mv_v2 TO generic_metric_sets_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_sets_raw_local
                LEFT ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end forward migration generic_metrics : 0049_sets_meta_tables_support_empty_tags




-- backward migration generic_metrics : 0049_sets_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_sets_meta_mv_v2;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_sets_meta_mv TO generic_metric_sets_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_sets_raw_local
                ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end backward migration generic_metrics : 0049_sets_meta_tables_support_empty_tags
-- forward migration generic_metrics : 0050_distributions_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_distributions_meta_mv;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_distributions_meta_mv_v2 TO generic_metric_distributions_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_distributions_raw_local
                LEFT ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end forward migration generic_metrics : 0050_distributions_meta_tables_support_empty_tags




-- backward migration generic_metrics : 0050_distributions_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_distributions_meta_mv_v2;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_distributions_meta_mv TO generic_metric_distributions_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_distributions_raw_local
                ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end backward migration generic_metrics : 0050_distributions_meta_tables_support_empty_tags
-- forward migration generic_metrics : 0051_gauges_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_gauges_meta_mv;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_gauges_meta_mv_v2 TO generic_metric_gauges_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_gauges_raw_local
                LEFT ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end forward migration generic_metrics : 0051_gauges_meta_tables_support_empty_tags




-- backward migration generic_metrics : 0051_gauges_meta_tables_support_empty_tags
Local op: DROP TABLE IF EXISTS generic_metric_gauges_meta_mv_v2;
Local op: CREATE MATERIALIZED VIEW IF NOT EXISTS generic_metric_gauges_meta_mv TO generic_metric_gauges_meta_local (org_id UInt64, project_id UInt64, use_case_id LowCardinality(String), metric_id UInt64, tag_key UInt64, timestamp DateTime CODEC (DoubleDelta), retention_days UInt16, count AggregateFunction(sum, Float64)) AS 
                SELECT
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    toMonday(timestamp) as timestamp,
                    retention_days,
                    sumState(count_value) as count
                FROM generic_metric_gauges_raw_local
                ARRAY JOIN tags.key AS tag_key
                WHERE record_meta = 1
                GROUP BY
                    org_id,
                    project_id,
                    use_case_id,
                    metric_id,
                    tag_key,
                    timestamp,
                    retention_days
                ;
-- end backward migration generic_metrics : 0051_gauges_meta_tables_support_empty_tags

Base automatically changed from evanh/fix/left-array-join-meta to master May 28, 2024 16:44
@evanh
Copy link
Member Author

evanh commented May 30, 2024

Validated these queries in snuba admin and they work as expected. There might need to be a follow up PR in the metrics layer to handle the tag_key = 0 case.

@evanh evanh merged commit 8a4a698 into master Jun 3, 2024
30 checks passed
@evanh evanh deleted the evanh/fix/left-array-join-migration branch June 3, 2024 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants