Skip to content

Fix buildLimit() silently dropping OFFSET 0#1173

Merged
vjik merged 4 commits intoyiisoft:masterfrom
darkspock:fix/build-limit-offset-zero
Apr 20, 2026
Merged

Fix buildLimit() silently dropping OFFSET 0#1173
vjik merged 4 commits intoyiisoft:masterfrom
darkspock:fix/build-limit-offset-zero

Conversation

@darkspock
Copy link
Copy Markdown
Contributor

Summary

AbstractDQLQueryBuilder::buildLimit() uses !empty($offset) to decide whether to append the OFFSET clause. Since empty(0) returns true in PHP, an explicitly set OFFSET 0 is silently dropped from the generated SQL.

While OFFSET 0 has no semantic effect on query results, this is inconsistent: a value explicitly set by the caller should be reflected in the output. It can also cause confusion when debugging generated SQL.

Fix

Replace !empty($offset) with $offset !== null, which correctly distinguishes "no offset set" (null) from "offset is zero" (0).

Before

if (!empty($offset)) {

After

if ($offset !== null) {

Risk

Very low — the only change is that OFFSET 0 now appears in the SQL when explicitly set. This has no effect on query results.

!empty($offset) evaluates to false when $offset is integer 0, causing
OFFSET 0 to be silently omitted from the generated SQL. While OFFSET 0
has no semantic effect, this is inconsistent: an explicitly set offset
should appear in the output.

Replace with $offset !== null which correctly distinguishes "no offset"
(null) from "offset zero" (0).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.62%. Comparing base (b9d25ca) to head (d7a6381).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1173      +/-   ##
============================================
- Coverage     98.62%   98.62%   -0.01%     
  Complexity     1645     1645              
============================================
  Files           120      120              
  Lines          4292     4284       -8     
============================================
- Hits           4233     4225       -8     
  Misses           59       59              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/QueryBuilder/AbstractDQLQueryBuilder.php Outdated
Copy link
Copy Markdown
Member

@vjik vjik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"no offset set" (null) and "offset is zero" (0) gives equal result in SQL. I agree with @Tigrov and suggest don't change it.

But it will be useful to add an explanation to the description of methods that set offset.

Clarify parameter descriptions for limit and offset.
Comment thread src/QueryBuilder/AbstractDQLQueryBuilder.php Outdated
@Tigrov Tigrov requested a review from vjik April 20, 2026 05:26
@vjik vjik merged commit f26a751 into yiisoft:master Apr 20, 2026
77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants