Details
-
Task
-
Status: In Progress (View Workflow)
-
Major
-
Resolution: Unresolved
-
None
-
Q4/2026 Server Development
Description
(Coming from Monty's observations of queries ServiceNow makes)
Consider a query:
select ... from t1 where ... having count(value)= 0;
|
select ... from t1 where ... having count(value)< 10;
|
The idea is, as soon as we get enough matching row(s), we know the query would produce an empty resultset. This means we can stop the execution.
Requirements
Implicit grouping is needed, or GROUP BY with "HAVING group_col=.." so there's only one group.
Execution
The aggregate function could have an attached "end detector". "end detection" will detect when the HAVING condition becomes false and signal to stop the execution.
Implementation considerations
We walk the HAVING and AND-parts of HAVING and find there aggregate comparison expression(s) STOPPABLE_EXPR that have the property:
If the value of aggregate is FALSE for a subset of the group, it will remain
|
false for the whole group.
|
We record the STOPPABLE_EXPR.
Then, the grouping algorithm can check the STOPPABLE_EXPR after adding a row into group.
If its value is FALSE, stop the scan.
(Note that no "end detector" is attached to aggregate functions, as was initially suggested.)
The criteria for STOPPABLE_EXPR are just pattern matching for:
COUNT(...) = 0
|
COUNT(...) < literal_const
|
COUNT(...) <= literal_const
|
(and also swap left-and-right sides, e.g. 0=COUNT() ...)
In the future we can also add
MAX(...) < const
|