Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle platform ignores OFFSET in case if LIMIT is not specified #2509

Merged
merged 1 commit into from
Jan 16, 2017

Conversation

morozov
Copy link
Member

@morozov morozov commented Sep 16, 2016

There's a non-empty table users. The following script should return all existing record IDs except the 1st one.

$builder = $conn->createQueryBuilder();
$builder->select('id')
    ->from('users')
    ->setFirstResult(1);
$data = $builder->execute()->fetchAll();
var_dump($data);

On Oracle, it returns all records ignoring the value set by calling setFirstResult().

@@ -975,24 +975,30 @@ public function getName()
*/
protected function doModifyLimitQuery($query, $limit, $offset = null)
{
$limit = (int) $limit;
Copy link
Member

Choose a reason for hiding this comment

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

Can you please remove the integer casting entirely and just rely on the method signature integer|null for both $limit and $offset? Then just check everywhere against null. Also please remove the $has* variables as they are unnecessary.

Copy link
Member Author

@morozov morozov Jan 14, 2017

Choose a reason for hiding this comment

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

@deeky666 The confusing part here is how is $offset = null different from $offset = 0 from the SQL semantics standpoint? Should the DBAL produce additional sub-query in the latter case? Right now it doesn't which makes more sense to me.

UPD: comparing $offset to null will break unit test:

1) Doctrine\Tests\DBAL\Platforms\OraclePlatformTest::testModifyLimitQuery
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'SELECT a.* FROM (SELECT * FROM user) a WHERE ROWNUM <= 10'
+'SELECT * FROM (SELECT a.*, ROWNUM AS doctrine_rownum FROM (SELECT * FROM user) a WHERE ROWNUM <= 10) WHERE doctrine_rownum >= 1'

Copy link
Member

Choose a reason for hiding this comment

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

@morozov yeah good point. But then leave it as before casting both parameters to int and checking for 0 instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

@deeky666 but unlike OFFSET, for LIMIT 0 and null have different meanings — "0 records" and "all records" respectively. There's no complete consistency between existing adapters but all of them except SQL Anywhere only omit $limit when it's null. Technically, it is possible to execute a query and retrieve 0 records from it, so the DBAL shouldn't be a limiting factor here. Also from the API consumer standpoint, if there's a bug in the calculation of $limit on the application end and it's calculated as 0, I'd better expect 0 records to be returned instead of all of them.

Copy link
Member

Choose a reason for hiding this comment

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

@morozov sounds reasonable. Thanks for clearing that up. I'll accept it as is now.

$query = 'SELECT a.' . $column . ' FROM (' . $query . ') a WHERE ROWNUM <= ' . $max;
}

$query = 'SELECT a.*'
Copy link
Member

Choose a reason for hiding this comment

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

Can you please use sprintf() here? Guess that makes it more readable.

@@ -975,24 +975,30 @@ public function getName()
*/
protected function doModifyLimitQuery($query, $limit, $offset = null)
{
$limit = (int) $limit;
Copy link
Member

Choose a reason for hiding this comment

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

@morozov sounds reasonable. Thanks for clearing that up. I'll accept it as is now.

@deeky666 deeky666 added this to the 2.5.8 milestone Jan 16, 2017
@deeky666 deeky666 merged commit e80626f into doctrine:master Jan 16, 2017
@deeky666
Copy link
Member

@morozov thank you very much!

@deeky666
Copy link
Member

Backported to 2.5 via b9a3b08

@morozov
Copy link
Member Author

morozov commented Jan 16, 2017

@deeky666 thank you for your patience. Can you also review #2463 which is a similar one?

@morozov morozov deleted the oracle-offset-no-limit branch January 16, 2017 23:36
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants