当前使用版本
3.0.2 下的分页插件 PaginationInterceptor
该问题是怎么引起的?
分页查询的 select count(1) from table_name 查询总数在 sharding-jdbc 下会报错. sharding-jdbc 给出的错误是聚合函数没给别名, 导致分页插件在查询总数的时候报错.
我在重写分页插件, 把COUNT(1)简单粗暴直接给替换成了 COUNT(1) as count后就能成功分页了
报错信息
Can't find index: AggregationSelectItem(type=COUNT, innerExpression=(1), alias=Optional.absent(), derivedAggregationSelectItems=[], index=-1), please add alias for aggregate selections
Comment From: miemieYaho
sharding-jdbc版本号多少?
Comment From: zcqshine
sharding-jdbc 版本号 3.0.0.M1
Comment From: miemieYaho
https://github.com/sharding-sphere/sharding-sphere/pull/1148#event-1789641849
Comment From: zcqshine
sharding-jdbc 更新到3.0.0.M3就可以了, 他们已经修复了这个问题, 我测试了, 也 OK 了
Comment From: zhiduochang
该bug依然存在,经测试,版本:3.0.0.M1、3.0.0.M2、3.0.0.M3、3.0.0.M4、3.0.0、3.1.0.M1都存在该问题
Comment From: wumuwumu
使用postgresql数据库执行selectPage方法还是会出现错误,使用mysql数据库就没有该问题。
Comment From: latebrose
create a new class and copy the code from JsqlParserCountOptimize, change the countSelectItem method,
private static List<SelectItem> countSelectItem() {
Function function = new Function();
function.setName("COUNT");
List<Expression> expressions = new ArrayList();
LongValue longValue = new LongValue(1L);
ExpressionList expressionList = new ExpressionList();
expressions.add(longValue);
expressionList.setExpressions(expressions);
function.setParameters(expressionList);
List<SelectItem> selectItems = new ArrayList();
SelectExpressionItem selectExpressionItem = new SelectExpressionItem(function);
selectExpressionItem.setAlias(new Alias("count"));
selectItems.add(selectExpressionItem);
return selectItems;
}
and set the sqlparser
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setCountSqlParser(new ShardingSqlParserCountOptimize());
return paginationInterceptor;
}
Comment From: Leslie307
shardingshere3.1.0.tb5,mybatisplus3.1.2,分库查询selectCount()仍然在结果归并的时候报错,提示需要添加别名