Skip to content

Commit

Permalink
fix(clickhouse): Uniq count should count uniq values...
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Oct 21, 2024
1 parent 9b8c57d commit 559f091
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions app/services/events/stores/clickhouse/unique_count_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def breakdown_query
timestamp,
property,
operation_type,
#{operation_value_sql}
#{operation_value_sql},
anyOrNull(operation_type) OVER (PARTITION BY property ORDER BY timestamp ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
FROM events_data
ORDER BY timestamp ASC
SQL
Expand Down Expand Up @@ -207,8 +208,8 @@ def events_cte_sql
events(ordered: true)
.select(
"timestamp, \
#{sanitized_property_name} AS property, \
coalesce(NULLIF(events_enriched.properties['operation_type'], ''), 'add') AS operation_type"
value AS property, \
coalesce(NULLIF(events_enriched.sorted_properties['operation_type'], ''), 'add') AS operation_type"
)
.to_sql
})
Expand All @@ -227,8 +228,8 @@ def grouped_events_cte_sql
.select(
"#{groups.join(", ")}, \
timestamp, \
#{sanitized_property_name} AS property, \
coalesce(NULLIF(events_enriched.properties['operation_type'], ''), 'add') AS operation_type"
value AS property, \
coalesce(NULLIF(events_enriched.sorted_properties['operation_type'], ''), 'add') AS operation_type"
).to_sql
})
SQL
Expand All @@ -242,15 +243,15 @@ def operation_value_sql
if (
operation_type = 'add',
(if(
(lagInFrame(operation_type, 1) OVER (PARTITION BY property ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)) = 'add',
(anyOrNull(operation_type) OVER (PARTITION BY property ORDER BY timestamp ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)) = 'add',
toDecimal128(0, :decimal_scale),
toDecimal128(1, :decimal_scale)
))
,
(if(
(lagInFrame(operation_type, 1, 'remove') OVER (PARTITION BY property ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)) = 'remove',
toDecimal128(-1, :decimal_scale),
toDecimal128(0, :decimal_scale)
(anyOrNull(operation_type) OVER (PARTITION BY property ORDER BY timestamp ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)) = 'remove',
toDecimal128(0, :decimal_scale),
toDecimal128(-1, :decimal_scale)
))
)
SQL
Expand All @@ -264,15 +265,15 @@ def grouped_operation_value_sql
if (
operation_type = 'add',
(if(
(lagInFrame(operation_type, 1) OVER (PARTITION BY #{group_names}, property ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)) = 'add',
(anyOrNull(operation_type) OVER (PARTITION BY #{group_names}, property ORDER BY timestamp ASC ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)) = 'add',
toDecimal128(0, :decimal_scale),
toDecimal128(1, :decimal_scale)
))
,
(if(
(lagInFrame(operation_type, 1, 'remove') OVER (PARTITION BY #{group_names}, property ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)) = 'remove',
toDecimal128(-1, :decimal_scale),
toDecimal128(0, :decimal_scale)
(anyOrNull(operation_type) OVER (PARTITION BY #{group_names}, property ORDER BY timestamp ASC ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)) = 'remove',
toDecimal128(0, :decimal_scale),
toDecimal128(-1, :decimal_scale)
))
)
SQL
Expand Down

0 comments on commit 559f091

Please sign in to comment.