Skip to content

Commit 8ff02e2

Browse files
authored
ESQL: Small improvements in DocVector (#131691)
Small improvements in the `DocVector` class like javadoc and some tests.
1 parent ea97e17 commit 8ff02e2

File tree

4 files changed

+70
-32
lines changed

4 files changed

+70
-32
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/_nightly/esql/ValuesSourceReaderBenchmark.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,6 @@ record ItrAndOrd(PrimitiveIterator.OfInt itr, int ord) {}
579579
pages.add(
580580
new Page(
581581
new DocVector(
582-
583582
ShardRefCounted.ALWAYS_REFERENCED,
584583
blockFactory.newConstantIntBlockWith(0, size).asVector(),
585584
leafs.build().asBlock().asVector(),

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/DocVector.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,30 @@ public IntVector docs() {
110110
return docs;
111111
}
112112

113+
/**
114+
* Does this vector contain only documents from a single segment.
115+
*/
116+
public boolean singleSegment() {
117+
return shards.isConstant() && segments.isConstant();
118+
}
119+
120+
/**
121+
* Does this vector contain only documents from a single segment <strong>and</strong>
122+
* are the documents always non-decreasing? Non-decreasing here means almost monotonically
123+
* increasing.
124+
*/
113125
public boolean singleSegmentNonDecreasing() {
114126
if (singleSegmentNonDecreasing == null) {
115127
singleSegmentNonDecreasing = checkIfSingleSegmentNonDecreasing();
116128
}
117129
return singleSegmentNonDecreasing;
118130
}
119131

120-
public boolean singleSegment() {
121-
return shards.isConstant() && segments.isConstant();
122-
}
123-
124132
private boolean checkIfSingleSegmentNonDecreasing() {
125133
if (getPositionCount() < 2) {
126134
return true;
127135
}
128-
if (shards.isConstant() == false || segments.isConstant() == false) {
136+
if (singleSegment() == false) {
129137
return false;
130138
}
131139
int prev = docs.getInt(0);
@@ -167,10 +175,9 @@ private void buildShardSegmentDocMapIfMissing() {
167175
boolean success = false;
168176
long estimatedSize = sizeOfSegmentDocMap();
169177
blockFactory().adjustBreaker(estimatedSize);
170-
int[] forwards = null;
171-
int[] backwards = null;
178+
int[] forwards;
172179
try {
173-
int[] finalForwards = forwards = new int[shards.getPositionCount()];
180+
forwards = new int[shards.getPositionCount()];
174181
for (int p = 0; p < forwards.length; p++) {
175182
forwards[p] = p;
176183
}
@@ -180,19 +187,19 @@ private void buildShardSegmentDocMapIfMissing() {
180187

181188
@Override
182189
protected void setPivot(int i) {
183-
pivot = finalForwards[i];
190+
pivot = forwards[i];
184191
}
185192

186193
@Override
187194
protected int comparePivot(int j) {
188-
return Integer.compare(docs.getInt(pivot), docs.getInt(finalForwards[j]));
195+
return Integer.compare(docs.getInt(pivot), docs.getInt(forwards[j]));
189196
}
190197

191198
@Override
192199
protected void swap(int i, int j) {
193-
int tmp = finalForwards[i];
194-
finalForwards[i] = finalForwards[j];
195-
finalForwards[j] = tmp;
200+
int tmp = forwards[i];
201+
forwards[i] = forwards[j];
202+
forwards[j] = tmp;
196203
}
197204
}.sort(0, forwards.length);
198205
} else {
@@ -201,31 +208,31 @@ protected void swap(int i, int j) {
201208

202209
@Override
203210
protected void setPivot(int i) {
204-
pivot = finalForwards[i];
211+
pivot = forwards[i];
205212
}
206213

207214
@Override
208215
protected int comparePivot(int j) {
209-
int cmp = Integer.compare(shards.getInt(pivot), shards.getInt(finalForwards[j]));
216+
int cmp = Integer.compare(shards.getInt(pivot), shards.getInt(forwards[j]));
210217
if (cmp != 0) {
211218
return cmp;
212219
}
213-
cmp = Integer.compare(segments.getInt(pivot), segments.getInt(finalForwards[j]));
220+
cmp = Integer.compare(segments.getInt(pivot), segments.getInt(forwards[j]));
214221
if (cmp != 0) {
215222
return cmp;
216223
}
217-
return Integer.compare(docs.getInt(pivot), docs.getInt(finalForwards[j]));
224+
return Integer.compare(docs.getInt(pivot), docs.getInt(forwards[j]));
218225
}
219226

220227
@Override
221228
protected void swap(int i, int j) {
222-
int tmp = finalForwards[i];
223-
finalForwards[i] = finalForwards[j];
224-
finalForwards[j] = tmp;
229+
int tmp = forwards[i];
230+
forwards[i] = forwards[j];
231+
forwards[j] = tmp;
225232
}
226233
}.sort(0, forwards.length);
227234
}
228-
backwards = new int[forwards.length];
235+
int[] backwards = new int[forwards.length];
229236
for (int p = 0; p < forwards.length; p++) {
230237
backwards[forwards[p]] = p;
231238
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/read/ValuesFromSingleReader.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,9 @@ private boolean useSequentialStoredFieldsReader(BlockLoader.Docs docs, double st
205205
private record ColumnAtATimeWork(BlockLoader.ColumnAtATimeReader reader, int idx) {}
206206

207207
/**
208-
* Work for
209-
* @param reader
210-
* @param builder
211-
* @param loader
212-
* @param idx
208+
* Work for rows stride readers.
209+
* @param reader reads the values
210+
* @param idx destination in array of {@linkplain Block}s we build
213211
*/
214212
private record RowStrideReaderWork(BlockLoader.RowStrideReader reader, Block.Builder builder, BlockLoader loader, int idx)
215213
implements

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/DocVectorTests.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@
2727
import static org.hamcrest.Matchers.is;
2828

2929
public class DocVectorTests extends ComputeTestCase {
30-
public void testNonDecreasingSetTrue() {
30+
/**
31+
* Assert that {@link DocVector#singleSegmentNonDecreasing()} is true
32+
* when the vector is constructed with it set to {@code true}, regardless
33+
* of if the segment is actually a single segment. Or non-decreasing.
34+
* <p>
35+
* Note: Setting this incorrectly is rude and will break ESQL.
36+
* </p>
37+
*/
38+
public void testSingleSegmentNonDecreasingSetTrue() {
3139
int length = between(1, 100);
3240
DocVector docs = new DocVector(
3341
ShardRefCounted.ALWAYS_REFERENCED,
@@ -39,7 +47,7 @@ public void testNonDecreasingSetTrue() {
3947
assertTrue(docs.singleSegmentNonDecreasing());
4048
}
4149

42-
public void testNonDecreasingSetFalse() {
50+
public void testSingleSegmentNonDecreasingSetFalse() {
4351
BlockFactory blockFactory = blockFactory();
4452
DocVector docs = new DocVector(
4553
ShardRefCounted.ALWAYS_REFERENCED,
@@ -52,7 +60,7 @@ public void testNonDecreasingSetFalse() {
5260
docs.close();
5361
}
5462

55-
public void testNonDecreasingNonConstantShard() {
63+
public void testSingleSegmentNonDecreasingNonConstantShard() {
5664
BlockFactory blockFactory = blockFactory();
5765
DocVector docs = new DocVector(
5866
ShardRefCounted.ALWAYS_REFERENCED,
@@ -65,7 +73,7 @@ public void testNonDecreasingNonConstantShard() {
6573
docs.close();
6674
}
6775

68-
public void testNonDecreasingNonConstantSegment() {
76+
public void testSingleSegmentNonDecreasingNonConstantSegment() {
6977
BlockFactory blockFactory = blockFactory();
7078
DocVector docs = new DocVector(
7179
ShardRefCounted.ALWAYS_REFERENCED,
@@ -78,7 +86,33 @@ public void testNonDecreasingNonConstantSegment() {
7886
docs.close();
7987
}
8088

81-
public void testNonDecreasingDescendingDocs() {
89+
public void testSingleSegmentNonDecreasingAscending() {
90+
BlockFactory blockFactory = blockFactory();
91+
DocVector docs = new DocVector(
92+
ShardRefCounted.ALWAYS_REFERENCED,
93+
blockFactory.newConstantIntVector(0, 2),
94+
blockFactory.newConstantIntVector(0, 2),
95+
blockFactory.newIntArrayVector(new int[] { 0, 1 }, 2),
96+
null
97+
);
98+
assertTrue(docs.singleSegmentNonDecreasing());
99+
docs.close();
100+
}
101+
102+
public void testSingleSegmentNonDecreasingSame() {
103+
BlockFactory blockFactory = blockFactory();
104+
DocVector docs = new DocVector(
105+
ShardRefCounted.ALWAYS_REFERENCED,
106+
blockFactory.newConstantIntVector(0, 2),
107+
blockFactory.newConstantIntVector(0, 2),
108+
blockFactory.newIntArrayVector(new int[] { 2, 2 }, 2),
109+
null
110+
);
111+
assertTrue(docs.singleSegmentNonDecreasing());
112+
docs.close();
113+
}
114+
115+
public void testSingleSegmentNonDecreasingDescendingDocs() {
82116
BlockFactory blockFactory = blockFactory();
83117
DocVector docs = new DocVector(
84118
ShardRefCounted.ALWAYS_REFERENCED,

0 commit comments

Comments
 (0)