Skip to content

Commit 73e163b

Browse files
committed
Avoid creating exceptions for warnings
1 parent 12022ad commit 73e163b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/Warnings.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,23 @@ private Warnings(int lineNumber, int columnNumber, String sourceText, String fir
8989
}
9090

9191
public void registerException(Exception exception) {
92+
registerException(exception.getClass(), exception.getMessage());
93+
}
94+
95+
/**
96+
* Register an exception to be included in the warnings.
97+
* <p>
98+
* This overload avoids the need to instantiate the exception, which can be expensive.
99+
* Instead, it asks only the required pieces to build the warning.
100+
* </p>
101+
*/
102+
public void registerException(Class<? extends Exception> exceptionClass, String message) {
92103
if (addedWarnings < MAX_ADDED_WARNINGS) {
93104
if (addedWarnings == 0) {
94105
addWarning(first);
95106
}
96107
// location needs to be added to the exception too, since the headers are deduplicated
97-
addWarning(location + exception.getClass().getName() + ": " + exception.getMessage());
108+
addWarning(location + exceptionClass.getName() + ": " + message);
98109
addedWarnings++;
99110
}
100111
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/querydsl/query/SingleValueMatchQuery.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public final class SingleValueMatchQuery extends Query {
4848
private static final int MULTI_VALUE_MATCH_COST = 1000;
4949
private final IndexFieldData<?> fieldData;
5050
private final Warnings warnings;
51-
private final IllegalArgumentException multiValueException;
51+
private final String multiValueExceptionMessage;
5252

5353
public SingleValueMatchQuery(IndexFieldData<?> fieldData, Warnings warnings, String multiValueExceptionMessage) {
5454
this.fieldData = fieldData;
5555
this.warnings = warnings;
56-
this.multiValueException = new IllegalArgumentException(multiValueExceptionMessage);
56+
this.multiValueExceptionMessage = multiValueExceptionMessage;
5757
}
5858

5959
@Override
@@ -122,7 +122,7 @@ private ScorerSupplier scorerSupplier(
122122
return false;
123123
}
124124
if (sortedNumerics.docValueCount() != 1) {
125-
warnings.registerException(multiValueException);
125+
registerMultiValueException();
126126
return false;
127127
}
128128
return true;
@@ -157,7 +157,7 @@ private ScorerSupplier scorerSupplier(
157157
return false;
158158
}
159159
if (sortedSetDocValues.docValueCount() != 1) {
160-
warnings.registerException(multiValueException);
160+
registerMultiValueException();
161161
return false;
162162
}
163163
return true;
@@ -186,7 +186,7 @@ private ScorerSupplier scorerSupplier(
186186
return false;
187187
}
188188
if (sortedBinaryDocValues.docValueCount() != 1) {
189-
warnings.registerException(multiValueException);
189+
registerMultiValueException();
190190
return false;
191191
}
192192
return true;
@@ -266,6 +266,10 @@ public long cost() {
266266
}
267267
}
268268

269+
private void registerMultiValueException() {
270+
warnings.registerException(IllegalArgumentException.class, multiValueExceptionMessage);
271+
}
272+
269273
private static class PredicateScorerSupplier extends ScorerSupplier {
270274
private final float score;
271275
private final ScoreMode scoreMode;

0 commit comments

Comments
 (0)