[Bug] Firebird rowsClause (ROWS m TO n) does not resolve its operands and maps the inclusive end row as a row count

Author: lzcGeekCreated Sep 25, 2026Updated Sep 25, 2026

Bug Report

For English only, other languages will not accept.

Before report a bug, make sure you have:

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?

master @ 22137b6, with the ROWS support for limitClause in place (see the note at the end — the branch this is based on is the one proposed in #39955, without which the clause is not visited at all and this path is unreachable).

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

Both, though this is a parser-level defect and is visible without a database.

Expected behaviour

Firebird's ROWS m TO n form selects the inclusive row range m .. n, so it should parse to a LimitSegment with offset = m and rowCount = n - m + 1. For SELECT * FROM t_order ROWS 100000 TO 100009 that is offset = 100000, rowCount = 10.

Actual behaviour

The rowsClause branch of FirebirdDMLStatementVisitor.visitLimitClause does not resolve its operands — both come out null:

SELECT * FROM t_order ROWS 100000 TO 100009
  -> limit=[22,42] offset=null rowCount=null

SELECT * FROM t_order ROWS 100000
  -> offset=null rowCount=null

SELECT * FROM t_order ORDER BY order_id ROWS 5
  -> offset=null rowCount=null

Reason analyse (If you can)

The branch passes ctx.rowsClause().expr(i) to the visitLimitValue helper:

java
if (null != ctx.rowsClause()) {
    offset = (PaginationValueSegment) visitLimitValue(ctx.rowsClause().expr(0));
    if (ctx.rowsClause().expr().size() > 1) {
        rowCount = (PaginationValueSegment) visitLimitValue(ctx.rowsClause().expr(1));
    }
}

visitLimitValue only recognises contexts whose single child is a NumberLiteralsContext or a ParameterMarkerContext — that is limitOffset and limitRowCount, the sub-rule contexts used by the OFFSET / FETCH path. The single child of an expr is a booleanPrimary, so neither branch matches and the helper falls through to null, discarding both values while still building a LimitSegment with anchors.

Independently of the lookup, the mapping is wrong: rowsClause is ROWS expr (TO expr)? and Firebird's range is inclusive, so the end value is a row number, not a row count. offset = m, rowCount = n - m + 1 is the conversion; the current code would need to compute that once the operands are readable.

Steps to reproduce the behaviour, such as: SQL to execute, sharding rule configuration, when exception occur etc.

Parse any of the three statements above with the Firebird dialect. No sharding rule is needed — the failure is visible in the parsed statement: LimitSegment is present but getOffset() and getRowCount() are both empty.

Example codes for reproduce this issue

The standalone probe used to produce the output above parses the statement with FirebirdParserFacade and prints the LimitSegment anchors and values; happy to attach it if useful.


Note on scope: this is filed separately on purpose. #39926 / #39955 cover the OFFSET ... ROWS FETCH FIRST ... ROWS ONLY path, which is what the rewrite rule needed and what was reported; no existing sql-case covers the ROWS m TO n grammar, so that change was kept out of the PR. I am willing to submit a PR for this issue (add the operand lookup for expr, apply the inclusive-range conversion, and add sql-case entries with expectations for both ROWS m and ROWS m TO n).

I have searched the open and closed issues for ROWS TO / rowsClause and found no existing report.

Disclosure: found while preparing #39955 with an AI coding assistant (DeepSeek Harness), which was used to read the Firebird grammar and visitor and to run the local parser probe.