@Override
  public List<IndexSegment> prune(List<IndexSegment> segments, QueryContext query) {
    if (segments.isEmpty()) {
      return segments;
    }

    // For LIMIT 0 case, keep one segment to create the schema
    int limit = query.getLimit();
    if (limit == 0) {
      return Collections.singletonList(segments.get(0));
    }

    // Skip pruning segments for upsert table because valid doc index is equivalent to a filter
    if (segments.get(0).getValidDocIds() != null) {
      return segments;
    }

    if (query.getOrderByExpressions() == null) {
      return pruneSelectionOnly(segments, query);
    } else {
      return pruneSelectionOrderBy(segments, query);
    }
  }
@Test
  public void testSelectionOnly() {
    List<IndexSegment> indexSegments =
        Arrays.asList(getIndexSegment(null, null, 10), getIndexSegment(0L, 10L, 10), getIndexSegment(-5L, 5L, 15));

    // Should keep enough documents to fulfill the LIMIT requirement
    QueryContext queryContext = QueryContextConverterUtils.getQueryContext("SELECT * FROM testTable LIMIT 5");
    List<IndexSegment> result = _segmentPruner.prune(indexSegments, queryContext);
    assertEquals(result.size(), 1);
    assertSame(result.get(0), indexSegments.get(0));

    queryContext = QueryContextConverterUtils.getQueryContext("SELECT * FROM testTable LIMIT 10");
    result = _segmentPruner.prune(indexSegments, queryContext);
    assertEquals(result.size(), 1);
    assertSame(result.get(0), indexSegments.get(0));

    queryContext = QueryContextConverterUtils.getQueryContext("SELECT * FROM testTable LIMIT 15");
    result = _segmentPruner.prune(indexSegments, queryContext);
    assertEquals(result.size(), 2);
    assertSame(result.get(0), indexSegments.get(0));
    assertSame(result.get(1), indexSegments.get(1));

    queryContext = QueryContextConverterUtils.getQueryContext("SELECT * FROM testTable LIMIT 25");
    result = _segmentPruner.prune(indexSegments, queryContext);
    assertEquals(result.size(), 3);
    assertSame(result.get(0), indexSegments.get(0));
    assertSame(result.get(1), indexSegments.get(1));
    assertSame(result.get(2), indexSegments.get(2));

    queryContext = QueryContextConverterUtils.getQueryContext("SELECT * FROM testTable LIMIT 100");
    result = _segmentPruner.prune(indexSegments, queryContext);
    assertEquals(result.size(), 3);
    assertSame(result.get(0), indexSegments.get(0));
    assertSame(result.get(1), indexSegments.get(1));
    assertSame(result.get(2), indexSegments.get(2));
  }