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

TopKSelector is unstable when quicksort fallback to Arrays.sort. #5692

Closed
Liulietong opened this issue Aug 19, 2021 · 3 comments · Fixed by #5696
Closed

TopKSelector is unstable when quicksort fallback to Arrays.sort. #5692

Liulietong opened this issue Aug 19, 2021 · 3 comments · Fixed by #5696
Assignees
Labels
P2 package=collect type=defect Bug, not working as expected
Milestone

Comments

@Liulietong
Copy link
Contributor

Related PR : #5691
The result of TopKSelector may be wrong when trim() is invoked and quiksort fallback to Arrasys.sort(). Because Arrasys.sort() is been used by mistake there.
Following test case can trigger this bug.

{
    int n = 10000;
    int k = 10000;
    int testIteration = 10;
    Random random = new Random(System.currentTimeMillis());
    for (int iter = 0; iter < testIteration; iter ++) {
      // target array to be sorted using TopKSelector
      List<Integer> target = new ArrayList<>();
      for (int i = 0; i < 9; i++) {
        List<Integer> sortedArray = new ArrayList();
        for (int j = 0; j < n; j++) {
          sortedArray.add(random.nextInt());
        }
        sortedArray.sort(Integer::compareTo);
        target.addAll(sortedArray);
      }

      TopKSelector<Integer> top = TopKSelector.least(k, Integer::compareTo);
      for (int value : target) {
        top.offer(value);
      }

      target.sort(Integer::compareTo);
      assertEquals(top.topK(), target.subList(0, k));
    }
  }
@shreelakshmijoshi
Copy link

Hi ! @Liulietong ,
I am a new open source contributor and I would love to fix the issue, could you please provide some more reference to help me understand better? Is this issue open for contribution ?

@Liulietong
Copy link
Contributor Author

@shreelakshmijoshi I have submit the PR #5691 to fix the issue

@shreelakshmijoshi
Copy link

oh okay

@netdpb netdpb changed the title Fix bug that result of TopKSelector is unstable when quicksort fallback to Arrays.sort. TopKSelector is unstable when quicksort fallback to Arrays.sort. Aug 23, 2021
@netdpb netdpb added P3 package=collect type=defect Bug, not working as expected labels Aug 23, 2021
@cpovirk cpovirk added this to the NEXT milestone Aug 24, 2021
@cpovirk cpovirk self-assigned this Aug 24, 2021
@cpovirk cpovirk added P2 and removed P3 labels Aug 24, 2021
copybara-service bot pushed a commit that referenced this issue Aug 26, 2021
Fixes #5691
Fixes #5692

RELNOTES=n/a
PiperOrigin-RevId: 392721126
@cpovirk cpovirk modified the milestones: NEXT, 31.0 Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 package=collect type=defect Bug, not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants