Files
Carl Schwan 3530b52db8 feat(files-search): Support an empty in comparison as matching nothing
SearchBuilder now turns an empty `in` into an always false condition,
and its negation into an always true one. A new SimplifyEmptyIn
optimizer step folds these constants into the surrounding `and` and
`or` operators so they don't reach the database at all when possible.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
2026-09-30 11:18:25 +02:00

44 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Files\Search\QueryOptimizer;
use OCP\Files\Search\ISearchOperator;
class QueryOptimizer {
/** @var QueryOptimizerStep[] */
private $steps = [];
public function __construct() {
// note that the order here is relevant
$this->steps = [
new PushDownNegation(),
new SimplifyEmptyIn(),
new PathPrefixOptimizer(),
new MergeDistributiveOperations(),
new FlattenSingleArgumentBinaryOperation(),
new FlattenNestedBool(),
new OrEqualsToIn(),
new FlattenNestedBool(),
new SplitLargeIn(),
// SplitLargeIn can turn a `not (in (...))` into `not (or (in (...), in (...)))`, which
// needs pushing down again before it reaches the query builder.
new PushDownNegation(),
];
}
public function processOperator(ISearchOperator &$operator) {
foreach ($this->steps as $step) {
$step->inspectOperator($operator);
}
foreach ($this->steps as $step) {
$step->processOperator($operator);
}
}
}