Skip to content

fix: SQL Server ORDER BY @@version OFFSET ... FETCH parse error (#6735) - #6736

Open
moonfruit wants to merge 1 commit into
alibaba:masterfrom
moonfruit:fix/sqlserver-order-by-variant-offset
Open

moonfruit wants to merge 1 commit into
alibaba:masterfrom
moonfruit:fix/sqlserver-order-by-variant-offset

Conversation

@moonfruit

Copy link
Copy Markdown
Contributor

Fixes #6735

Problem

Since 1.2.26, the SQL Server parser fails on an ORDER BY whose last item is a variable when it is followed by OFFSET ... FETCH:

select a from t order by @@version offset 0 rows fetch first ? rows only
-- ParserException: not supported. pos 43, line 1, column 43, token LITERAL_INT

Hibernate 6 (SQLServer2012LimitHandler) generates exactly this SQL for every paginated query that has no explicit ORDER BY. So StatFilter.mergeSql logs an error for each such query, and WallFilter cannot parse it either.

Cause

#6474 changed SQLExprParser.orderBy() to keep parsing order-by items without a comma whenever the previous item is a SQLVariantRefExpr and the next token is an IDENTIFIER. This is meant for template parameters (${...}), which may already contain the separating comma. However, the check also matches regular variables such as @@version and @var. The SQL Server lexer returns OFFSET as an IDENTIFIER, so OFFSET was parsed as a second order-by item and the row count that follows failed to parse.

Fix

Only continue without a comma when the previous item is a template parameter (SQLVariantRefExpr.isTemplateParameter()). This matches how SQLSelectParser already handles template parameters in the select list.

Tests

  • Added Issue6735, which covers:

    • @@version followed by OFFSET with a literal row count and with a ? placeholder
    • a local variable @v DESC as the last order-by item
    • the ParameterizedOutputVisitorUtils.parameterize path used by StatFilter

    All 4 tests fail without the fix and pass with it.

  • mvn -pl core test -Dtest='com/alibaba/druid/bvt/sql/**/*' passes: 2678 tests, 0 failures, checkstyle OK. This includes the ClickHouse template-parameter tests from Implement template parser. #6474 (CKResourceTest).

…aba#6735)

Since alibaba#6474, SQLExprParser.orderBy() keeps parsing order-by items without
a comma whenever the previous item is a SQLVariantRefExpr and the next
token is an IDENTIFIER. This was intended for template parameters such
as ${...}, but it also matched @@Version and @var. In SQL Server OFFSET
is an IDENTIFIER, so "ORDER BY @@Version OFFSET 0 ROWS FETCH ..." (the
pagination SQL generated by Hibernate 6) treated OFFSET as another
order-by item and then failed on the row count.

Only continue without a comma when the previous item is a template
parameter, consistent with how SQLSelectParser handles select items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] SQL Server: ORDER BY @@version OFFSET ... FETCH fails to parse since 1.2.26 (breaks Hibernate 6 pagination)

1 participant