Title: fix(sql): count groups for HEAD requests with @group by lnuxe · Pull Request #884 · APIJSON/APIJSON · GitHub
Open Graph Title: fix(sql): count groups for HEAD requests with @group by lnuxe · Pull Request #884 · APIJSON/APIJSON
X Title: fix(sql): count groups for HEAD requests with @group by lnuxe · Pull Request #884 · APIJSON/APIJSON
Description: 背景 issue #624 反馈:[] 里带 @group 时,total 拿到的是组内数量而不是分组后的总条数。 复现请求(issue 里的原始 JSON): { "[]": { "VehicleList_count": { "vehicleid$": "鄂QD24S8_0%", "sumdate%": "2023-10-15,2023-10-17", "@group": "vehicleid,sumdate", "@column": "vehicleid;sumdate;max(cnt):maxcnt", "@order": "maxcnt-" }, "page": 0, "count": 5 }, "total@": "/[]/total" } 原因 gainColumnString 在 HEAD 请求下把 SELECT 换成 count(*),但 gainConditionString 对 HEAD 仍然拼接 GROUP BY,于是生成: SELECT count(*) AS `count` FROM `sys`.`Vehicle` GROUP BY `vid`,`sdate` 这条 SQL 返回的是每一组的行数(多行),不是分组后的组数,所以 total 语义错位。Oracle 分支(isOracle() || isDameng() || isKingBaseOracle())早就用外层 count(*) 包了一层子查询,其它库没有对应处理。 修复 在 gainSelectSQL 里对齐 Oracle 的写法:@group 非空且是 HEAD 方法时,把原语句包成子查询再 count(*): if (StringUtil.isNotEmpty(getGroup(), true) && RequestMethod.isHeadMethod(getMethod(), true)) { String sql = "SELECT " + (getCache() == JSONMap.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + gainConditionString(tablePath, this); return explain + "SELECT count(*)" + gainAs() + getQuote() + JSONResponse.KEY_COUNT + getQuote() + " FROM (" + sql + ")" + gainAs() + gainSQLAliasWithQuote(); } MySQL / SQL Server 要求 FROM 子查询有别名,用已有的 gainSQLAliasWithQuote() 补上,各库引号风格自动适配。 修复后: -- MySQL SELECT count(*) AS `count` FROM (SELECT count(*) AS `count` FROM `sys`.`Vehicle` GROUP BY `vid`,`sdate`) AS `Vehicle` -- PostgreSQL SELECT count(*) AS "count" FROM (SELECT count(*) AS "count" FROM "sys"."Vehicle" GROUP BY "vid","sdate") AS "Vehicle" 测试 新增 AbstractSQLConfigGroupCountTest,覆盖: MySQL / PostgreSQL / SQL Server / DB2 分组 HEAD 都断言外层 count(*) + 子查询结构,且外层不再出现 GROUP BY 没有 @group 时保持原来的单层 count(*),不包子查询 GET + @group 保持原样,不被改成 count(*) 双通道验证 修复前(git checkout 还原 AbstractSQLConfig.java): [ERROR] AbstractSQLConfigGroupCountTest.countsGroupsInsteadOfRowsInEachGroupForMySQL:87 HEAD with @group on MYSQL: SELECT count(*) AS `count` FROM `sys`.`Vehicle` GROUP BY `vid`,`sdate` [ERROR] Tests run: 6, Failures: 4, Errors: 0, Skipped: 0 修复后: [INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 -- in apijson.orm.AbstractSQLConfigGroupCountTest [INFO] Tests run: 44, Failures: 0, Errors: 0, Skipped: 0 [INFO] BUILD SUCCESS 全量 mvn test 44 个用例全绿(原有 38 + 新增 6),无回归。 fix #624
Open Graph Description: 背景 issue #624 反馈:[] 里带 @group 时,total 拿到的是组内数量而不是分组后的总条数。 复现请求(issue 里的原始 JSON): { "[]": { "VehicleList_count": { "vehicleid$": "鄂QD24S8_0%", ...
X Description: 背景 issue #624 反馈:[] 里带 @group 时,total 拿到的是组内数量而不是分组后的总条数。 复现请求(issue 里的原始 JSON): { "[]": { "VehicleList_count": { "vehicleid$": &am...
Opengraph URL: https://github.com/APIJSON/APIJSON/pull/884
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:d859e57b-6441-60ed-5e53-abe0e9951753 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 97B2:21B98A:FD6A4F:16B4E79:6AFCB1C0 |
| html-safe-nonce | af9057df7894f901b58e731f4b442308dab72bf728ca9ff77abdd4963831c5f3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5N0IyOjIxQjk4QTpGRDZBNEY6MTZCNEU3OTo2QUZDQjFDMCIsInZpc2l0b3JfaWQiOiIzMzQ1Mzk3MTkxNjg1MjIyODQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 34632772cf8b6e8f8102f6841d5255e59ec4a67b965ef5551d911ff42705c182 |
| hovercard-subject-tag | pull_request:5259074163 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/APIJSON/APIJSON/pull/884/files |
| twitter:image | https://avatars.githubusercontent.com/u/144079529?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/144079529?s=400&v=4 |
| og:image:alt | 背景 issue #624 反馈:[] 里带 @group 时,total 拿到的是组内数量而不是分组后的总条数。 复现请求(issue 里的原始 JSON): { "[]": { "VehicleList_count": { "vehicleid$": "鄂QD24S8_0%", ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | e3084aead77cbcb4f4904fba53851fa1a0546b0dec3619bd250815e72588dc6d |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/APIJSON/APIJSON git https://github.com/APIJSON/APIJSON.git |
| octolytics-dimension-user_id | 41146037 |
| octolytics-dimension-user_login | APIJSON |
| octolytics-dimension-repository_id | 74359442 |
| octolytics-dimension-repository_nwo | APIJSON/APIJSON |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 74359442 |
| octolytics-dimension-repository_network_root_nwo | APIJSON/APIJSON |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5410e4bebd3e45b138c853ecef3992279d86d5af |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width