@@ -207,9 +207,20 @@ def run_top_events_timeseries_query(
207
207
change this"""
208
208
"""Make a table query first to get what we need to filter by"""
209
209
rpc_dataset_common .validate_granularity (params )
210
- search_resolver = get_resolver (params = params , config = config )
210
+
211
+ # Virtual context columns (VCCs) are currently only supported in TraceItemTable.
212
+ # For TopN queries, we want table and timeseries data to match.
213
+ # Here, we want to run the table request the the VCCs. SnubaParams has
214
+ # a property `is_timeseries_request` which resolves to true if granularity_secs is set.
215
+ # `is_timeseries_request` is used to evaluate if VCCs should be used.
216
+ # Unset granularity_secs, so this gets treated as a table request with
217
+ # the correct VCC.
218
+ table_query_params = params .copy ()
219
+ table_query_params .granularity_secs = None
220
+ table_search_resolver = get_resolver (params = table_query_params , config = config )
221
+
211
222
top_events = run_table_query (
212
- params ,
223
+ table_query_params ,
213
224
query_string ,
214
225
raw_groupby + y_axes ,
215
226
orderby ,
@@ -218,10 +229,12 @@ def run_top_events_timeseries_query(
218
229
referrer ,
219
230
config ,
220
231
sampling_mode ,
221
- search_resolver ,
232
+ table_search_resolver ,
222
233
)
223
234
if len (top_events ["data" ]) == 0 :
224
235
return {}
236
+
237
+ search_resolver = get_resolver (params = params , config = config )
225
238
# Need to change the project slug columns to project.id because timeseries requests don't take virtual_column_contexts
226
239
groupby_columns = [col for col in raw_groupby if not is_function (col )]
227
240
groupby_columns_without_project = [
@@ -275,8 +288,22 @@ def run_top_events_timeseries_query(
275
288
int (groupby_attributes [resolved_groupby .internal_name ])
276
289
]
277
290
else :
278
- resolved_groupby , _ = search_resolver .resolve_attribute (col )
279
- remapped_groupby [col ] = groupby_attributes [resolved_groupby .internal_name ]
291
+ resolved_groupby , context = search_resolver .resolve_attribute (col )
292
+
293
+ # Virtual context columns (VCCs) are currently only supported in TraceItemTable.
294
+ # Since timeseries run the query with the original column, we need to map
295
+ # them correctly so they map the table result. We need to map both the column name
296
+ # and the values.
297
+ if context is not None :
298
+ resolved_groupby = search_resolver .map_context_to_original_column (context )
299
+
300
+ groupby_value = groupby_attributes [resolved_groupby .internal_name ]
301
+ if context is not None :
302
+ groupby_value = context .constructor (params ).value_map [groupby_value ]
303
+ groupby_attributes [resolved_groupby .internal_name ] = groupby_value
304
+
305
+ remapped_groupby [col ] = groupby_value
306
+
280
307
result_key = create_result_key (remapped_groupby , groupby_columns , {})
281
308
map_result_key_to_timeseries [result_key ].append (timeseries )
282
309
final_result = {}
0 commit comments