@@ -3796,21 +3796,14 @@ void putItems__whenCreatingBatchRepeatsStableId__thenCountedOnce() {
37963796 var datasetId = createDataset (UUID .randomUUID ().toString ());
37973797
37983798 // The SDK's parallel upload sends every batch under one batch_group_id. The batch that
3799- // arrives first CREATES the version, and that path derives itemsTotal from insertItems,
3800- // which returns items.size() -- the raw list length, deliberately not a DB row count
3801- // (ClickHouse async inserts report 0 before commit). A stable id repeated inside that
3802- // first batch is therefore counted twice, while ClickHouse collapses it to one row.
3799+ // arrives first CREATES the version, and that path derives itemsTotal from insertItems.
3800+ // That used to return items.size() -- the raw list length, deliberately not a DB row count
3801+ // (ClickHouse async inserts report 0 before commit) -- so a stable id repeated inside the
3802+ // first batch was counted twice while ClickHouse collapsed it to one row. It now counts
3803+ // distinct dataset_item_ids, which is what this test pins.
38033804 var duplicatedId = TestIdGeneratorFactory .create ().generateId ();
38043805 var distinctId = TestIdGeneratorFactory .create ().generateId ();
38053806
3806- // The repeated id wins with its LAST submitted content: ClickHouse keeps one row per
3807- // dataset_item_id and reads take the newest, so "second" survives and "first" does not.
3808- var winningDuplicate = DatasetItem .builder ()
3809- .id (duplicatedId )
3810- .datasetItemId (duplicatedId )
3811- .source (DatasetItemSource .SDK )
3812- .data (Map .of ("value" , JsonUtils .getJsonNodeFromString ("\" second\" " )))
3813- .build ();
38143807 var distinctItem = DatasetItem .builder ()
38153808 .id (distinctId )
38163809 .datasetItemId (distinctId )
@@ -3824,7 +3817,11 @@ void putItems__whenCreatingBatchRepeatsStableId__thenCountedOnce() {
38243817 .source (DatasetItemSource .SDK )
38253818 .data (Map .of ("value" , JsonUtils .getJsonNodeFromString ("\" first\" " )))
38263819 .build (),
3827- winningDuplicate ,
3820+ DatasetItem .builder ()
3821+ .id (duplicatedId )
3822+ .source (DatasetItemSource .SDK )
3823+ .data (Map .of ("value" , JsonUtils .getJsonNodeFromString ("\" second\" " )))
3824+ .build (),
38283825 distinctItem );
38293826
38303827 datasetResourceClient .createDatasetItems (DatasetItemBatch .builder ()
@@ -3834,15 +3831,23 @@ void putItems__whenCreatingBatchRepeatsStableId__thenCountedOnce() {
38343831 .build (), TEST_WORKSPACE , API_KEY );
38353832
38363833 var version = getLatestVersion (datasetId );
3837-
3838- // Assert on the rows themselves, not just how many: a bug that kept the wrong revision
3839- // of the duplicate, or dropped the distinct item and kept both duplicates, would leave
3840- // the count at 2 and slip through a size-only check.
38413834 var stored = datasetResourceClient .getDatasetItems (
38423835 datasetId , 1 , 100 , version .versionHash (), API_KEY , TEST_WORKSPACE ).content ();
3836+
3837+ // Which revision of the repeated id survives is deliberately NOT asserted: every row in a
3838+ // batch is written with the same now64(9), and the read dedupes with
3839+ // `ORDER BY dataset_item_id DESC, last_updated_at DESC LIMIT 1 BY dataset_item_id` -- no
3840+ // tie-breaker, so either payload may win. Pinning "second" would be asserting an accident.
3841+ // What is guaranteed, and what the fix is about: exactly one row per distinct id, and a
3842+ // counter that agrees with it.
3843+ assertThat (stored ).extracting (DatasetItem ::id )
3844+ .containsExactlyInAnyOrder (duplicatedId , distinctId );
38433845 assertThat (stored )
3844- .usingRecursiveFieldByFieldElementComparatorIgnoringFields (IGNORED_FIELDS_DATA_ITEM )
3845- .containsExactlyInAnyOrder (winningDuplicate , distinctItem );
3846+ .filteredOn (item -> distinctId .equals (item .id ()))
3847+ .singleElement ()
3848+ .usingRecursiveComparison ()
3849+ .ignoringFields (IGNORED_FIELDS_DATA_ITEM )
3850+ .isEqualTo (distinctItem );
38463851
38473852 // items_total must agree with what is actually stored.
38483853 assertThat (version .itemsTotal ()).isEqualTo (stored .size ());
0 commit comments