Problem/Motivation

The Select query builder allows setting a range using:

$query->range($start, $length);

However, there is currently no public API to retrieve the configured range values from the query object after they have been set.

This makes it difficult for modules and integrations to inspect an existing query and determine:

  • Whether a range has already been applied
  • What the start (offset) value is
  • What the length (limit) value is

This limitation affects use cases such as:

  • hook_query_alter() implementations that need to adjust or respect existing pagination
  • Query decorators or wrappers that must preserve previously defined ranges
  • Contrib modules extending or modifying queries before execution
  • Debugging or logging tools requiring full visibility into query configuration

Proposed resolution

Introduce a public getter method on Select query objects to expose range information.

For example:

$range = $query->getRange();

Suggested behavior:

  • Return NULL if no range is set
  • Return an associative array if defined:
[ 'start' => $start, 'length' => $length, ]

Alternatively, the API could provide:

  • hasRange()
  • getRange()

This maintains encapsulation while allowing safe and supported introspection of query state.

Alternatives considered

  • Accessing internal properties directly (not acceptable; violates encapsulation)
  • Requiring modules to track range values separately (adds unnecessary complexity)
  • Leaving the API unchanged and discouraging introspection (limits extensibility)

Remaining tasks

  • Decide on final method naming and return structure
  • Add unit test coverage
  • Update inline documentation
  • Verify no backward compatibility concerns

API changes

Yes — adds a new public method to Select.

The change is backward compatible.

Issue fork drupal-2986699

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

drunken monkey created an issue. See original summary.

drunken monkey’s picture

Status: Active » Needs review
StatusFileSize
new1.47 KB

This would add the getter.

Status: Needs review » Needs work

The last submitted patch, 2: 2986699-2--getRange.patch, failed testing. View results

drunken monkey’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new527 bytes
new1.99 KB

Ah, right!

borisson_’s picture

Good catch, probably this will need some kind of test to ensure that this behavior works? A kernel-test would be the perfect place for this. I can't see any kind of test for ranges in core/tests/Drupal/Tests/Core/Database which is where this should probably end up.

Since there is no test for ranges currently, we could probably get away with not adding a test and doing that in a followup instead.

daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs testing

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

luke.stewart’s picture

Issue tags: +Bug Smash Initiative

This is the Bug Smash Initiatives target today.

I've verified that this feature is not present in latest drupal codebase

In https://git.drupalcode.org/project/drupal/-/blob/main/core/lib/Drupal/Co... or
https://git.drupalcode.org/project/drupal/-/blob/main/core/lib/Drupal/Co...

I'm not sure if this should perhaps be reclassified as a Feature request.

Looks like all that is required here would be some testing.

Note based on the documentation of the other getters the comment could probably do with updating to match the same style.

neptune-dc made their first commit to this issue’s fork.

sivaji_ganesh_jojodae’s picture

Title: No way to read SELECT query range? » Add missing getter method to retrieve range (limit/offset) from Select query objects
Issue summary: View changes

neptune-dc’s picture

Status: Needs work » Needs review

Built the fork and added two Kernal tests. Everything is running green. I tested locally with these commands:

PHPSTAN:
ddev exec vendor/bin/phpstan analyse \
-c core/phpstan.neon.dist \
core/tests/Drupal/KernelTests/Core/Database/SelectGetRangeTest.php

PHPCS:
ddev exec vendor/bin/phpcs --standard=Drupal core/tests/Drupal/KernelTests/Core/Database/SelectGetRangeTest.php

PHPCBF:
ddev exec vendor/bin/phpcbf --standard=Drupal core/tests/Drupal/KernelTests/Core/Database/SelectGetRangeTest.php

Run test:
ddev exec bash -c '
export SIMPLETEST_DB="mysql://db:db@db/db"
export SIMPLETEST_BASE_URL="http://web"
export BROWSERTEST_OUTPUT_DIRECTORY="sites/simpletest/browser_output"
mkdir -p sites/simpletest/browser_output
chmod -R a+w sites/simpletest
vendor/bin/phpunit -c core/phpunit.xml.dist core/tests/Drupal/KernelTests/Core/Database/SelectGetRangeTest.php
'

luke.stewart’s picture

Status: Needs review » Needs work

Thanks for your work on this. Great work updating issue summary.

I'm flipping back to Needs Work as I think this will probably need a change record?

>Yes — adds a new public method to Select.
https://www.drupal.org/about/core/policies/core-change-policies/change-r... -> An API change

I also wonder if we need to ensure the documentation is consistent across the methods in the Select Interface.
namely:

compare these lines:

   * Returns a reference to the tables array for this query.
   * Because this method returns by reference, alter hooks may edit the tables ...
   * Note that this method must be called by reference as well:"
neptune-dc’s picture

Status: Needs work » Needs review

I have added a change record. It is my first time, so I would appreciate someone to review it.

https://www.drupal.org/node/3574425

borisson_’s picture

Issue tags: -Needs testing

The change record looks great

drunken monkey’s picture

I had one minor comment, but otherwise looks good to me. Could be RTBC if someone agrees with my change.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

I agree.

quietone’s picture

While checking change records I saw that the one here was published although the issue has not been committed. So, I have returned it to a draft state. Also, the version and branch are incorrect and should be updated.

drunken monkey’s picture

Fixed the typo pointed out by @sivaji_ganesh_jojodae.
I also think that even though the rest of the methods in the interface don’t have type hints the new method should get one? Added that, too, please feel free to revert.

drunken monkey’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record updates

@quietone What is the correct version for the change record, 12.0.0?

In any case, I also found an error in the text, it claims the method returns NULL (instead of []) if there is no range set. However, we might also want to adapt the code to match the change record instead? To me personally, NULL makes much more sense than [].

neptune-dc’s picture

Status: Needs work » Needs review

> Return NULL if no range is set

Updated the function to return null when no range is set. This is following instructions in the summary.

smustgrave made their first commit to this issue’s fork.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record updates

Believe CR matches the solution. Did update branch for main as this is an API change that not sure which 11.x branch will land in

Test coverage can be seen here https://git.drupalcode.org/issue/drupal-2986699/-/jobs/9031886
Did rebase to trigger a new pipeline that I can rerun as a Reporter role.

Took a crack at credit while I was at it. Believe this one is good to go.

catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • catch committed db6e00de on 11.x
    task: #2986699 Add missing getter method to retrieve range (limit/offset...

  • catch committed b1241452 on main
    task: #2986699 Add missing getter method to retrieve range (limit/offset...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.