Skip to content

Loading…

DBAL-787: [GH-512] Fix modifying limit/offset for statements with subqueries on SQL Server #2017

Closed
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

Jira issue originally created by user @doctrinebot:

This issue is created automatically through a Github pull request on behalf of deeky666:

Url: #512

Message:

SELECT statements that contains subqueries in the SELECT clause do not get properly rewritten with a limit and/or offset applied to it resulting in wrong SQL.

Example

SELECT foo.id, (SELECT COUNT(*) FROM bar) AS bar_count FROM foo

Expected with a limit of 10

SELECT ** FROM (SELECT foo.id, (SELECT COUNT(**) FROM bar) AS bar*count, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM foo) AS doctrine_tbl WHERE doctrine*rownum BETWEEN 1 AND 10

Actual with a limit of 10

SELECT ** FROM (SELECT foo.id, (SELECT COUNT(**), ROW*NUMBER() OVER (ORDER BY (SELECT 0))  AS doctrine_rownum FROM bar) AS bar_count FROM foo) AS doctrine_tbl WHERE doctrine*rownum BETWEEN 1 AND 10

The ROW_NUMBER() OVER clause is misplaced into the subselect instead of into the main FROM clause.
What this PR does is recursively matching any (nested) parentheses inside the main SELECT clause to be able to identify the main FROM clause to add the ROW_NUMBER() OVER clause to.
This of course is far from perfect for matching all kinds of possible SELECT statement syntaxes but it fixes this particular issue, which is pretty common IMO.

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-512] was closed:
#512

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added the Bug label
@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.4.3 milestone
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.