Firebird OFFSET rewrite issue
Bug Report
For English only, other languages will not accept.
Before report a bug, make sure you have:
- Searched open and closed GitHub issues.
- Read documentation: ShardingSphere Doc.
Please pay attention on issues you submitted, because we maybe need more details. If no response anymore and we cannot reproduce it on current information, we will close it.
Please answer these questions before submitting your issue. Thanks!
Which version of ShardingSphere did you use?
latest (tested on commit 0496849)
Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
Both ShardingSphere-JDBC and ShardingSphere-Proxy
Expected behavior
When executing SQL query SELECT * FROM T_ORDER ORDER BY ID OFFSET 100000 ROWS FETCH FIRST 10 ROWS ONLY it should be rewritten as SELECT * FROM T_ORDER ORDER BY ID OFFSET 0 ROWS FETCH FIRST 100010 ROWS ONLY according to documentation
Actual behavior
The SQL query was not rewritten as expected and is executed as SELECT * FROM T_ORDER ORDER BY ID OFFSET 100000 ROWS FETCH FIRST 10 ROWS ONLY
Reason analyze (If you can)
The limitClause rule in Firebird BaseRule.g4 lacks a visit function in FirebirdStatementVisitor that creates a LimitSegment (similar to visitFirstSkipClause in FirebirdDMLStatementVisitor).
Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
SQL: SELECT * FROM T_ORDER ORDER BY ID OFFSET 100000 ROWS FETCH FIRST 10 ROWS ONLY
Example codes for reproduce this issue (such as a github link).
I would like to work on this issue.
Root cause is two-fold, and I confirmed both locally against master:
FirebirdDMLStatementVisitor.visitSelectClauseonly handlesctx.firstSkipClause(); thelimitClause?thatselectClausecarries (DMLStatement.g4:80) has no visit, soOFFSET ... ROWS FETCH FIRST ... ROWS ONLYnever produces aLimitSegmentand the rewrite rule sees no offset.orderByClausealso carries a trailinglimitClause?(BaseRule.g4:393). ANTLR's greedy prediction consumes the clause insideorderByClause, so for the SQL in the report (... ORDER BY ID OFFSET 100000 ROWS FETCH FIRST 10 ROWS ONLY)ctx.limitClause()is null at theselectClauselevel. SinceorderByClauseis reused by window specifications (OVER (...)),GROUP_CONCAT(...)and friends, accepting a LIMIT there is wrong anyway, and every statement that wants one already declares its ownlimitClause?(DML/DAL/DDL). So the fix is to drop it fromorderByClause.
Plan, if that direction is acceptable:
- add
visitLimitClausetoFirebirdDMLStatementVisitorand wire it intovisitSelectClause - remove the nested
limitClause?fromorderByClausein the FirebirdBaseRule.g4 - add the two missing IT cases:
SELECT * FROM t_order OFFSET 100000 ROWS FETCH FIRST 10 ROWS ONLYand theORDER BYvariant from the report, with assertions onoffset/rowCount - note that the existing
select_pagination_with_offset_fetchcase isdb-types="SQLServer,PostgreSQL,openGauss"and never included Firebird, which is why this was not caught
Status of local verification: I applied both parts and ran the Firebird parser IT locally - InternalFirebirdParserIT passes 238/238, and with only one of the two parts applied the ORDER BY case fails, so both are needed. I also checked SELECT * FROM t_order ROWS 100000 TO 100009 and found the rowsClause branch of visitLimitClause does not resolve its operands (both come out null) and that Firebird's ROWS m TO n is an inclusive range, so the conversion would be offset=m, rowCount=n-m+1. That grammar is not covered by any existing case; I will keep it out of this PR to keep the change focused and file it separately.
I have the patch ready and can open the PR.