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 query to get most recent actor catalog fetch event for some sources #21726

Merged
merged 5 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1355,12 +1355,13 @@ public Map<UUID, ActorCatalogFetchEvent> getMostRecentActorCatalogFetchEventForS
// noinspection SqlResolve
return database.query(ctx -> ctx.fetch(
"""
select actor_catalog_id, actor_id, created_at from
(select actor_catalog_id, actor_id, created_at, rank() over (partition by actor_id order by created_at desc) as creation_order_rank
select distinct actor_catalog_id, actor_id, created_at from
(select actor_catalog_id, actor_id, created_at, row_number() over (partition by actor_id order by created_at desc) as creation_order_row_number
from public.actor_catalog_fetch_event
where actor_id in ({0})
) table_with_rank
where creation_order_rank = 1;
"""))
where creation_order_row_number = 1;
""", DSL.list(sourceIds.stream().map(DSL::value).collect(Collectors.toList()))))
.stream().map(DbConverter::buildActorCatalogFetchEvent)
.collect(Collectors.toMap(ActorCatalogFetchEvent::getActorId, record -> record));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,39 @@ void testGetMostRecentActorCatalogFetchEventForSources() throws SQLException, IO
assertEquals(MockData.ACTOR_CATALOG_ID_3, result.get(MockData.SOURCE_ID_2).getActorCatalogId());
}

@Test
void testGetMostRecentActorCatalogFetchEventWithDuplicates() throws SQLException, IOException {
// Tests that we can handle two fetch events in the db with the same actor id, actor catalog id, and
// timestamp e.g., from duplicate discoveries.
for (final ActorCatalog actorCatalog : MockData.actorCatalogs()) {
writeActorCatalog(database, Collections.singletonList(actorCatalog));
}

database.transaction(ctx -> {
// Insert the fetch events twice.
MockData.actorCatalogFetchEventsForAggregationTest().forEach(actorCatalogFetchEvent -> {
insertCatalogFetchEvent(
ctx,
actorCatalogFetchEvent.getActorCatalogFetchEvent().getActorId(),
actorCatalogFetchEvent.getActorCatalogFetchEvent().getActorCatalogId(),
actorCatalogFetchEvent.getCreatedAt());
insertCatalogFetchEvent(
ctx,
actorCatalogFetchEvent.getActorCatalogFetchEvent().getActorId(),
actorCatalogFetchEvent.getActorCatalogFetchEvent().getActorCatalogId(),
actorCatalogFetchEvent.getCreatedAt());
});
return null;
});

final Map<UUID, ActorCatalogFetchEvent> result =
configRepository.getMostRecentActorCatalogFetchEventForSources(List.of(MockData.SOURCE_ID_1,
MockData.SOURCE_ID_2));

assertEquals(MockData.ACTOR_CATALOG_ID_1, result.get(MockData.SOURCE_ID_1).getActorCatalogId());
assertEquals(MockData.ACTOR_CATALOG_ID_3, result.get(MockData.SOURCE_ID_2).getActorCatalogId());
}

@Test
void testGetActorDefinitionsInUseToProtocolVersion() throws IOException {
final Set<UUID> actorDefinitionIds = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,17 @@ public static List<ActorCatalogFetchEventWithCreationDate> actorCatalogFetchEven
.withActorId(SOURCE_ID_2)
.withConfigHash("1394")
.withConnectorVersion("1.2.0");
final ActorCatalogFetchEvent actorCatalogFetchEvent4 = new ActorCatalogFetchEvent()
.withId(ACTOR_CATALOG_FETCH_EVENT_ID_3)
.withActorCatalogId(ACTOR_CATALOG_ID_3)
.withActorId(SOURCE_ID_3)
.withConfigHash("1394")
.withConnectorVersion("1.2.0");
return Arrays.asList(
new ActorCatalogFetchEventWithCreationDate(actorCatalogFetchEvent1, now),
new ActorCatalogFetchEventWithCreationDate(actorCatalogFetchEvent2, yesterday),
new ActorCatalogFetchEventWithCreationDate(actorCatalogFetchEvent3, now));
new ActorCatalogFetchEventWithCreationDate(actorCatalogFetchEvent3, now),
new ActorCatalogFetchEventWithCreationDate(actorCatalogFetchEvent4, now));
}

public static List<WorkspaceServiceAccount> workspaceServiceAccounts() {
Expand Down