Fix - Use an indexable range for date search criteria - #25096
Open
RomainLvr wants to merge 1 commit into
Open
Conversation
trasher
approved these changes
Aug 6, 2026
Rom1-B
reviewed
Aug 6, 2026
Comment on lines
+5499
to
+5500
| // `!` resets unspecified fields to their "zero" value | ||
| $lower_bound = DateTimeImmutable::createFromFormat('!' . $format, $val); |
Contributor
There was a problem hiding this comment.
On a DST spring-forward gap hour (e.g. 2024-03-31 02 in Europe/Paris), the parsed hour silently shifts with no warning, so the search range misses a row whose naive datetime column holds that literal value.
diff --git a/src/Glpi/Search/Provider/SQLProvider.php b/src/Glpi/Search/Provider/SQLProvider.php
index 50fddf4df6..390fd6a714 100644
--- a/src/Glpi/Search/Provider/SQLProvider.php
+++ b/src/Glpi/Search/Provider/SQLProvider.php
@@ -50,6 +50,7 @@ use Consumable;
use CronTask;
use DateInterval;
use DateTimeImmutable;
+use DateTimeZone;
use DBConnection;
use DBmysql;
use DBmysqlIterator;
@@ -5496,8 +5497,8 @@ final class SQLProvider implements SearchProviderInterface
return null;
}
- // `!` resets unspecified fields to their "zero" value
- $lower_bound = DateTimeImmutable::createFromFormat('!' . $format, $val);
+ // `!` resets unspecified fields to their "zero" value; force UTC as the value has no time offset.
+ $lower_bound = DateTimeImmutable::createFromFormat('!' . $format, $val, new DateTimeZone('UTC'));
$errors = DateTimeImmutable::getLastErrors();
if ($lower_bound === false || ($errors !== false && ($errors['warning_count'] + $errors['error_count']) > 0)) {
// Out of range value, e.g. `2024-02-30`
diff --git a/tests/functional/SearchTest.php b/tests/functional/SearchTest.php
index 08dc26c77a..005c5a4b29 100644
--- a/tests/functional/SearchTest.php
+++ b/tests/functional/SearchTest.php
@@ -4725,6 +4725,43 @@ class SearchTest extends DbTestCase
}
}
+ /**
+ * The range boundaries must not be shifted by a DST transition of the server timezone,
+ * as the compared `datetime` column holds a naive value with no time offset.
+ */
+ public function testDateTimeEqualsCriterionOnDstTransition(): void
+ {
+ global $DB;
+
+ $original_tz = date_default_timezone_get();
+ // Hack to prevent the script tz from being changed by the DB access layer
+ $DB->use_timezones = true;
+ // Clocks jump from 02:00 to 03:00 in `Europe/Paris` on this date
+ date_default_timezone_set('Europe/Paris');
+
+ try {
+ $data = $this->doSearch(Ticket::class, [
+ 'is_deleted' => 0,
+ 'start' => 0,
+ 'criteria' => [
+ [
+ 'link' => 'AND',
+ 'field' => 15, // date
+ 'searchtype' => 'equals',
+ 'value' => '2024-03-31 02',
+ ],
+ ],
+ ]);
+ } finally {
+ date_default_timezone_set($original_tz);
+ }
+
+ $this->assertStringContainsString(
+ "(`glpi_tickets`.`date` >= '2024-03-31 02:00:00') AND (`glpi_tickets`.`date` < '2024-03-31 03:00:00')",
+ $this->cleanSQL($data['sql']['search'])
+ );
+ }
+
protected function customAssetsProvider(): iterable
{
$root_entity_id = getItemByTypeName('Entity', '_test_root_entity', true);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist before requesting a review
Please delete options that are not relevant.
Description
Searching on a date field does not use the index defined on the column. On a large
glpi_ticketstable, filtering on a single day reads the whole table instead of a few hundred rows.Cause
Both the "is" and the "contains" criteria on a date/datetime field were built as a
LIKEpattern:Comparing a date/time column with a
LIKEpattern forces MySQL to cast it into a string, which makes the index unusable. EvenFORCE INDEXcannot help.Fix
Both criteria now search the range matching the precision of the searched value:
EXPLAINgoes fromtype=ALL(full table scan) totype=rangeusing the column index.Results are unchanged. The previous
LIKEform is still used when the searched value cannot be interpreted as a date prefix (-07-,2026-02-30,NULL), on computed fields, and ondate_delayfields.Tests
SearchTestcovers both criteria for every precision, in the positive and negated form, plus the values that must keep theLIKEform.