Merge pull request #19838 from MoonE/count-sql-no-cache

Fix #19822 - Fix incorrect usage/placement of 'SQL_NO_CACHE'
This commit is contained in:
Maurício Meneghini Fauth 2025-09-08 13:34:51 -03:00 committed by GitHub
commit d2fdad042c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 1 deletions

View File

@ -79,6 +79,7 @@
"symfony/polyfill-iconv": "^1.33",
"symfony/polyfill-mbstring": "^1.17.0",
"symfony/polyfill-php80": "^1.16",
"symfony/polyfill-php84": "^1.33",
"twig/twig": "^3.3.5",
"webmozart/assert": "^1.10",
"williamdes/mariadb-mysql-kbs": "^1.2"

View File

@ -21,9 +21,11 @@ use PhpMyAdmin\SqlParser\Utils\Query;
use PhpMyAdmin\Utils\ForeignKey;
use function __;
use function array_find_key;
use function array_key_exists;
use function array_keys;
use function array_map;
use function assert;
use function bin2hex;
use function ceil;
use function count;
@ -34,11 +36,13 @@ use function in_array;
use function is_array;
use function is_bool;
use function is_object;
use function is_string;
use function session_start;
use function session_write_close;
use function sprintf;
use function str_contains;
use function str_replace;
use function strtoupper;
use function ucwords;
/**
@ -745,6 +749,15 @@ class Sql
/** @var SelectStatement $statement */
$statement = $analyzedSqlResults['statement'];
assert($statement->options !== null);
/** @var int|null $noCacheIndex */
$noCacheIndex = array_find_key(
$statement->options->options,
/** @param mixed $value */
static function ($value): bool {
return is_string($value) && strtoupper($value) === 'SQL_NO_CACHE';
}
);
$changeOrder = $analyzedSqlResults['order'] !== false;
$changeLimit = $analyzedSqlResults['limit'] !== false;
$changeExpression = $analyzedSqlResults['is_group'] === false
@ -752,8 +765,14 @@ class Sql
&& $analyzedSqlResults['union'] === false
&& count($statement->expr) === 1;
if ($changeOrder || $changeLimit || $changeExpression) {
if ($changeOrder || $changeLimit || $changeExpression || $noCacheIndex !== null) {
$statement = clone $statement;
// Remove SQL_NO_CACHE from subquery because it is not valid sql
if ($noCacheIndex !== null) {
assert($statement->options !== null);
$statement->options = clone $statement->options;
unset($statement->options->options[$noCacheIndex]);
}
}
// Remove ORDER BY to decrease unnecessary sorting time

View File

@ -538,6 +538,14 @@ class SqlTest extends AbstractTestCase
false,
'SELECT COUNT(*) FROM (SELECT * FROM t1 UNION SELECT * FROM t2 ) as cnt',
],
[
'SELECT SQL_NO_CACHE * FROM t1 WHERE id <> 0',
['max_rows' => -1, 'pos' => 0],
25,
100,
false,
'SELECT COUNT(*) FROM (SELECT 1 FROM t1 WHERE id <> 0 ) as cnt',
],
];
}