Repository navigation
Conversation
SQL Server and DB2 require ORDER BY for OFFSET FETCH, so gainOrderString injected ORDER BY id for any paginated query. A query whose @column is a single aggregate without @group, such as max(date):date, returns exactly one row and cannot order by a non aggregated column: SELECT max("date") AS "date" FROM "sys"."Moment" ORDER BY "id" OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY The database rejects it because id is not contained in an aggregate function or a GROUP BY clause, as reported in issue 648. Add isAggregateWithoutGroup, skip the injected ORDER BY for such queries, and return an empty limit string from gainLimitString because the result is a single row anyway. Queries with @group and queries on plain columns keep paging. fix APIJSON#648
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
issue #648 反馈:SQL Server 下
@column用聚合函数会报错。复现请求:
{ "Moment": { "@column": "max(date):date" } }报错:
原因
SQL Server / DB2 / Kingbase SQL Server 模式下,
OFFSET ... FETCH ...必须带ORDER BY,所以gainOrderString在getCount() > 0时自动补上ORDER BY "id":但
max(date)这种不带 GROUP BY 的单聚合永远只返回一行,它没有id列可以排序,于是生成非法 SQL:MySQL / PostgreSQL 走
LIMIT,不强制ORDER BY,所以一直是好的——这解释了为什么只有 SQL Server 报。修复
新增
isAggregateWithoutGroup():@group为空、@column只有一项、且该项是SQL_AGGREGATE_FUNCTION_MAP里的聚合函数(max/min/avg/count/sum)时返回 true。两处使用:
gainOrderString:这种查询不再补ORDER BY。gainLimitString:直接返回空字符串——结果本来就只有一行,OFFSET/FETCH没有意义。修复后:
不受影响的场景
ORDER BY "id" OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY@group的聚合:仍保留ORDER BY(可以按分组字段排序)LIMIT分支未改动测试
新增
AbstractSQLConfigAggregatePagingTest(6 个用例):ORDER BY、无OFFSETORDER BY+OFFSET@group的聚合断言仍带ORDER BY双通道验证
修复前(
git checkout origin/main -- src/main/java/apijson/orm/AbstractSQLConfig.java):修复后:
全量
mvn test44 个用例全绿(原有 38 + 新增 6),无回归。fix #648