fix: Correct ORDER BY parameter handling in count queries (#868)#871
Merged
Conversation
--在 isSimpleCount 方法中新增对 ORDER BY 条件的判断逻辑,当查询包含含占位符的 CASE WHEN 排序、布尔排序等特殊 ORDER BY 子句时,不再误判为 "简单查询",避免生成包含ORDER BY的COUNT(0)错误SQL,解决聚合查询中ORDER BY字段未参与GROUP BY的语法报错问题。
- Only skip simple count when ORDER BY contains parameter placeholders - Use existing orderByHashParameters method for consistency - Add test with parameter binding instead of string substitution - Add proper assertions to verify pagination works correctly This fixes the issue where ORDER BY clauses with parameter bindings would cause JDBC parameter mismatch if removed during count query optimization. The fix uses the existing orderByHashParameters method to detect placeholders in ORDER BY, ensuring optimization only happens when safe. Key changes: - Modified isSimpleCount to check orderByHashParameters instead of just checking if ORDER BY exists - Added selectOrderByWithParam test SQL using parameter binding - Rewrote TestOrderBy to use HSQLDB-compatible tests - Used CASE WHEN to simulate complex ORDER BY scenarios Co-authored-by: pwdLight <pwdlight@users.noreply.github.com>
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 #868:当 ORDER BY 子句中包含参数绑定(如
#{param})时,如果 PageHelper 在生成 count 查询时优化掉 ORDER BY,会导致 JDBC 参数位置不匹配,从而引发错误。原 PR #869 的问题:
isSimpleCount = false正确的解决方案:
?)时才不能优化order by id)可以安全地在 count 查询中移除修改内容
1. 核心逻辑修正
修改
isSimpleCount方法,使用已有的orderByHashParameters方法判断 ORDER BY 是否包含参数:2. 测试用例完善
关键改进:使用
#{}参数绑定而不是${}字符串替换${}是字符串替换,在 SQL 解析前就被替换,不会产生?占位符#{}是参数绑定,会生成?占位符,这才是 Issue pageHelper使用分页时查询总条数sql错误 #868 真正要测试的场景新增测试 SQL 使用 CASE WHEN 和参数绑定来模拟复杂的 ORDER BY 场景。
测试结果
✅ TestOrderBy 测试通过
✅ 完整测试套件通过(所有测试,无回归问题)
验证的场景
技术亮点
orderByHashParameters方法,保持代码一致性#{}参数绑定而不是${}字符串替换Co-authored-by: pwdLight pwdlight@users.noreply.github.com
Closes #868