-
[OPIK-2469] [BE] Fix duplicate experiment items in dataset comparison (#3633)
发布于
2025-10-15 14:23:41 +00:00 - [OPIK-2469] [BE] Fix duplicate experiment items in dataset comparison
Added LIMIT 1 BY id clause to experiment_items_final CTE in the
SELECT_DATASET_ITEMS_WITH_EXPERIMENT_ITEMS SQL query. This ensures
that only the latest version of each experiment item is returned,
preventing duplicates from appearing in dataset comparison results.The root cause was that when multiple versions of an experiment item
existed in ClickHouse (due to updates or race conditions), all versions
were being returned. The LIMIT 1 BY id clause deduplicates based on
experiment item ID, keeping only the most recent version (based on
last_updated_at DESC ordering).- Revision 2: Add integration test for duplicate experiment items
Added DuplicateExperimentItemsTest to verify that the LIMIT 1 BY id fix
correctly prevents duplicate experiment items from being returned even when
multiple versions exist in ClickHouse.The test:
- Creates dataset, dataset items, traces, and experiment items
- Manually inserts a duplicate experiment item directly into ClickHouse
- Queries the endpoint and verifies no duplicates are returned
- Asserts that exactly 2 unique experiment items are returned (not 3)
- Revision 2: Fix root cause by removing project_id from spans aggregation
- Changed spans aggregation GROUP BY from (workspace_id, project_id, trace_id) to (workspace_id, trace_id)
- This prevents duplicate rows when traces exist in multiple projects (cross-project traces)
- Removed previous workaround (LIMIT 1 BY id on experiment_items_final)
- Updated integration test to simulate cross-project trace scenario instead of duplicate experiment items
- Test now creates spans in different projects for the same trace to verify fix
- Revision 3: Remove unhelpful integration test
Test doesn't reliably reproduce cross-project trace scenario.
Production verification via direct SQL queries is more reliable.- Revision 4: Add comprehensive cross-project trace test
Test simulates production scenario where traces have spans across multiple projects.
Successfully demonstrates bug: with GROUP BY project_id, returns 3 items (1 duplicate).
Test validates fix by inserting cross-project spans directly into ClickHouse.- Revision 2: Rewrite test to use API calls instead of direct ClickHouse access
下载附件