Skip to content

Commit 8124d3a

Browse files
committed
address comments
1 parent 8ea19ef commit 8124d3a

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/CompositeAggsRowSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CompositeAggsRowSet extends ResultRowSet<BucketExtractor> {
4949

5050
// If the limit is -1 then we have a local sorting (sort on aggregate function) that requires all the buckets
5151
// to be processed so we stop only when all data is exhausted.
52-
int remainingLimit = limit == -1 ? limit : limit - size >= 0 ? limit - size : 0;
52+
int remainingLimit = (limit == -1) ? limit : ((limit - size) >= 0 ? (limit - size) : 0);
5353

5454
// if the computed limit is zero, or the size is zero it means either there's nothing left or the limit has been reached
5555
// note that a composite agg might be valid but return zero groups (since these can be filtered with HAVING/bucket selector)

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/Querier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ private void consumeRowSet(RowSet rowSet) {
210210
for (boolean hasRows = rrs.hasCurrentRow(); hasRows; hasRows = rrs.advanceRow()) {
211211
List<Object> row = new ArrayList<>(rrs.columnCount());
212212
rrs.forEachResultColumn(row::add);
213-
// if the queue overflows and no limit was specified, bail out
213+
// if the queue overflows and no limit was specified, throw an error
214214
if (data.insertWithOverflow(new Tuple<>(row, counter.getAndIncrement())) != null && noLimit) {
215215
onFailure(new SqlIllegalArgumentException(
216-
"The default limit [{}] for aggregate sorting has been reached; please specify a LIMIT", data.size()));
216+
"The default limit [{}] for aggregate sorting has been reached; please specify a LIMIT", MAXIMUM_SIZE));
217217
}
218218
}
219219
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/QuerierTests.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,53 @@ public void testAggSorting_TwoFields() {
6565
assertEquals(49 - (i / 2) + ((i % 2) * 50), results.get(i).get(1));
6666
}
6767
}
68+
69+
@SuppressWarnings("rawtypes")
70+
public void testAggSorting_ThreeFieldsWithRandomization() {
71+
// Initialize comparators for fields (columns)
72+
int noColumns = randomIntBetween(3, 10);
73+
List<Tuple<Integer, Comparator>> tuples = new ArrayList<>(noColumns);
74+
boolean[] ordering = new boolean[noColumns];
75+
for (int j = 0; j < noColumns; j++) {
76+
boolean order = randomBoolean();
77+
ordering[j] = order;
78+
tuples.add(new Tuple<>(j, order ? Comparator.naturalOrder() : Comparator.reverseOrder()));
79+
}
80+
81+
// Insert random no of documents (rows) with random 0/1 values for each field
82+
int noDocs = randomIntBetween(10, 50);
83+
int queueSize = randomIntBetween(4, noDocs / 2);
84+
List<List<Integer>> expected = new ArrayList<>(noDocs);
85+
Querier.AggSortingQueue queue = new AggSortingQueue(queueSize, tuples);
86+
for (int i = 0; i < noDocs; i++) {
87+
List<Integer> values = new ArrayList<>(noColumns);
88+
for (int j = 0; j < noColumns; j++) {
89+
values.add(randomBoolean() ? 1 : 0);
90+
}
91+
queue.insertWithOverflow(new Tuple<>(values, i));
92+
expected.add(values);
93+
}
94+
95+
List<List<?>> results = queue.asList();
96+
assertEquals(queueSize, results.size());
97+
expected.sort((o1, o2) -> {
98+
for (int j = 0; j < noColumns; j++) {
99+
if (ordering[j]) { // asc
100+
if (o1.get(j) < o2.get(j)) {
101+
return -1;
102+
} else if (o1.get(j) > o2.get(j)) {
103+
return 1;
104+
}
105+
} else { // desc
106+
if (o1.get(j) < o2.get(j)) {
107+
return 1;
108+
} else if (o1.get(j) > o2.get(j)) {
109+
return -1;
110+
}
111+
}
112+
}
113+
return 0;
114+
});
115+
assertEquals(expected.subList(0, queueSize), results);
116+
}
68117
}

0 commit comments

Comments
 (0)