Skip to content

Commit 54567d4

Browse files
safe guard prepare with min max
1 parent 7b669b8 commit 54567d4

File tree

1 file changed

+5
-1
lines changed
  • x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/date

1 file changed

+5
-1
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/date/DateTrunc.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ private static Rounding.Prepared createRounding(final Period period, final ZoneI
201201
}
202202

203203
final Rounding.Builder rounding;
204+
boolean tryPrepareWithMinMax = true;
204205
if (period.getDays() == 1) {
205206
rounding = new Rounding.Builder(Rounding.DateTimeUnit.DAY_OF_MONTH);
206207
} else if (period.getDays() == 7) {
@@ -209,6 +210,7 @@ private static Rounding.Prepared createRounding(final Period period, final ZoneI
209210
rounding = new Rounding.Builder(Rounding.DateTimeUnit.WEEK_OF_WEEKYEAR);
210211
} else if (period.getDays() > 1) {
211212
rounding = new Rounding.Builder(new TimeValue(period.getDays(), TimeUnit.DAYS));
213+
tryPrepareWithMinMax = false;
212214
} else if (period.getMonths() == 3) {
213215
// java.time.Period does not have a QUARTERLY period, so a period of 3 months
214216
// returns a quarterly rounding
@@ -217,16 +219,18 @@ private static Rounding.Prepared createRounding(final Period period, final ZoneI
217219
rounding = new Rounding.Builder(Rounding.DateTimeUnit.MONTH_OF_YEAR);
218220
} else if (period.getMonths() > 0) {
219221
rounding = new Rounding.Builder(Rounding.DateTimeUnit.MONTHS_OF_YEAR, period.getMonths());
222+
tryPrepareWithMinMax = false;
220223
} else if (period.getYears() == 1) {
221224
rounding = new Rounding.Builder(Rounding.DateTimeUnit.YEAR_OF_CENTURY);
222225
} else if (period.getYears() > 0) {
223226
rounding = new Rounding.Builder(Rounding.DateTimeUnit.YEARS_OF_CENTURY, period.getYears());
227+
tryPrepareWithMinMax = false;
224228
} else {
225229
throw new IllegalArgumentException("Time interval is not supported");
226230
}
227231

228232
rounding.timeZone(timeZone);
229-
if (min != null && max != null && period.getMonths() <= 1 && period.getYears() <= 1) {
233+
if (min != null && max != null && tryPrepareWithMinMax) {
230234
// Multiple quantities of month, quarter or year is not supported by PreparedRounding.maybeUseArray, which is called by
231235
// prepare(min, max), as it may hit an assert in it.
232236
return rounding.build().prepare(min, max);

0 commit comments

Comments
 (0)