GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(sql): skip TSQL paging for aggregates without @group by lnuxe · Pull Request #885 · APIJSON/APIJSON

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 11, 2026

Copy link
Copy Markdown

背景

issue #648 反馈:SQL Server 下 @column 用聚合函数会报错。

复现请求:

{
  "Moment": {
    "@column": "max(date):date"
  }
}

报错:

order by 子句列 id 无效,因为该列没有包含在聚合函数或 group by 子句中

原因

SQL Server / DB2 / Kingbase SQL Server 模式下,OFFSET ... FETCH ... 必须带 ORDER BY,所以 gainOrderString 在 getCount() > 0 时自动补上 ORDER BY "id":

if (getCount() > 0 && (isSQLServer() || isKingBaseSQLServer() || isDb2())) {
    ...
    order = idKey;  // 没传 @order 时默认用 id
}

但 max(date) 这种不带 GROUP BY 的单聚合永远只返回一行,它没有 id 列可以排序,于是生成非法 SQL:

SELECT max("date") AS "date" FROM "sys"."Moment" ORDER BY "id" OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY

MySQL / PostgreSQL 走 LIMIT,不强制 ORDER BY,所以一直是好的——这解释了为什么只有 SQL Server 报。

修复

新增 isAggregateWithoutGroup():@group 为空、@column 只有一项、且该项是 SQL_AGGREGATE_FUNCTION_MAP 里的聚合函数(max/min/avg/count/sum)时返回 true。

两处使用:

  1. gainOrderString:这种查询不再补 ORDER BY。
  2. gainLimitString:直接返回空字符串——结果本来就只有一行,OFFSET/FETCH 没有意义。

修复后:

SELECT max("date") AS "date" FROM "sys"."Moment"

不受影响的场景

  • 普通字段分页:仍保留 ORDER BY "id" OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY
  • 带 @group 的聚合:仍保留 ORDER BY(可以按分组字段排序)
  • MySQL / PostgreSQL:LIMIT 分支未改动

测试

新增 AbstractSQLConfigAggregatePagingTest(6 个用例):

  • SQL Server / DB2 / Kingbase SQL Server 的聚合查询断言无 ORDER BY、无 OFFSET
  • SQL Server 普通字段分页断言仍带 ORDER BY + OFFSET
  • SQL Server 带 @group 的聚合断言仍带 ORDER BY

双通道验证

修复前(git checkout origin/main -- src/main/java/apijson/orm/AbstractSQLConfig.java):

aggregate paging: SELECT max("date") AS "date" FROM "sys"."Moment" ORDER BY "id" OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY
[ERROR] AbstractSQLConfigAggregatePagingTest.skipsOrderByForSQLServerAggregate:127
    聚合函数不带 GROUP BY 时不应补 ORDER BY,否则列不在聚合函数内会报错
[ERROR] Tests run: 6, Failures: 4, Errors: 0, Skipped: 0

修复后:

[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 -- in apijson.orm.AbstractSQLConfigAggregatePagingTest
[INFO] Tests run: 44, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

全量 mvn test 44 个用例全绿(原有 38 + 新增 6),无回归。

fix #648

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lnuxe
fix(sql): skip TSQL paging for aggregates without @group
c4da9e9
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[Bug] sqlserver查询聚合函数出错

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@lnuxe
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.