sqlalchemy / sqlalchemy/sqlalchemy
add limit() / offset() to CTE, as CTE.union() produces a CTE and not a CompoundSelect
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 12.2k
- Forks
- 1.8k
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
SQLAlchemy produces unwanted parentheses () when using UNION ALL which causes syntax errors in SQLite.
Expected behavior
UNION ALL clause produced without extra/wrapping parentheses.
To Reproduce
q = select([0]).union_all(select([0]).limit(30))
Produces:
SELECT ? UNION ALL (SELECT 0 LIMIT ? OFFSET ?)
(which leads to the error below).
The following SQL should be produced instead:
SELECT ? UNION ALL SELECT ? LIMIT ? OFFSET ?
Error
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "(": syntax error
[SQL: SELECT 0 UNION ALL (SELECT 0
LIMIT ? OFFSET ?)]
[parameters: (30, 0)]
Versions.
- OS: macOS v10.15.6
- Python: 3.8
- SQLAlchemy: v1.3.19
- Database: SQLite v3.31.1
- DBAPI: sqlite3
Additional context
- SQLAlchemy does not generate parentheses if no
LIMITclause is provided.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Python reproduction using select(), union_all(), limit(), and SQLite, then trace the SQL generation that adds parentheses around the limited SELECT. Run the reproduction against SQLite and verify that UNION ALL compiles without the unwanted wrapping parentheses while preserving LIMIT and OFFSET behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlite
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100