【问题标题】:SQL query on H2 database throws ArrayIndexOutOfBoundsException with having count clauseH2 数据库上的 SQL 查询抛出带有 count 子句的 ArrayIndexOutOfBoundsException
【发布时间】:2021-06-04 09:08:15
【问题描述】:

我想按 count(favorite.favorite_id) 显示结果顺序并使用基于光标的分页。

但是,当我在查询中添加 'have count()' 子句时,我发现了 ArrayIndexOutOfBoundsException。

select
        album0_.album_id as col_0_0_,
        count(favoriteli2_.favorite_id) as cnt,
        user1_.user_id as user_id1_8_1_,
        album0_.album_id as album_id1_0_0_,
        album0_.create_date as create_d2_0_0_,
        album0_.modify_date as modify_d3_0_0_,
        album0_.description as descript4_0_0_,
        album0_.thumbnail as thumbnai5_0_0_,
        album0_.title as title6_0_0_,
        album0_.user_id as user_id8_0_0_,
        album0_.version as version7_0_0_,
        user1_.code as code2_8_1_,
        user1_.introduction as introduc3_8_1_,
        user1_.name as name4_8_1_,
        user1_.pic as pic5_8_1_
    from
        album album0_
    inner join
        user user1_
            on album0_.user_id=user1_.user_id
    left outer join
        favorite favoriteli2_
            on album0_.album_id=favoriteli2_.album_id
    group by
        album0_.album_id
    order by
        cnt desc, album0_.album_id desc

上面的查询工作正常。

select
        album0_.album_id as col_0_0_,
        count(favoriteli2_.favorite_id) as cnt,
        user1_.user_id as user_id1_8_1_,
        album0_.album_id as album_id1_0_0_,
        album0_.create_date as create_d2_0_0_,
        album0_.modify_date as modify_d3_0_0_,
        album0_.description as descript4_0_0_,
        album0_.thumbnail as thumbnai5_0_0_,
        album0_.title as title6_0_0_,
        album0_.user_id as user_id8_0_0_,
        album0_.version as version7_0_0_,
        user1_.code as code2_8_1_,
        user1_.introduction as introduc3_8_1_,
        user1_.name as name4_8_1_,
        user1_.pic as pic5_8_1_
    from
        album album0_
    inner join
        user user1_
            on album0_.user_id=user1_.user_id
    left outer join
        favorite favoriteli2_
            on album0_.album_id=favoriteli2_.album_id
    group by
        album0_.album_id
    having 
        count(favoriteli2_.favorite_id) < 2  --this causes the problem
    order by
        cnt desc, album0_.album_id desc

但是这个有 count() 子句的行不通。

下面是发生ArrayIndexOutOfBoundsException的方法。 [org.h2.command.dml.Select.updateAgg(Select.java:542)]

/**
 * Update any aggregate expressions with the query stage.
 * @param columnCount number of columns
 * @param stage see STAGE_RESET/STAGE_GROUP/STAGE_WINDOW in DataAnalysisOperation
 */
void updateAgg(int columnCount, int stage) {
    for (int i = 0; i < columnCount; i++) {
        if ((groupByExpression == null || !groupByExpression[i])
                && (groupByCopies == null || groupByCopies[i] < 0)) {
            Expression expr = expressions.get(i);
            expr.updateAggregate(session, stage);
        }
    }
}

columnCount = 16

groupByExpression:大小 16

groupByCopies:大小 15

表达式:大小 16(包含 15 个选定列,1 个比较(=count(favoriteli2_.favorite_id)

我认为“groupByCopies”是错误的原因。我该如何解决?

【问题讨论】:

标签: sql spring jpa h2 indexoutofboundsexception


【解决方案1】:

虽然 MySQL/MariaDB 允许这样做,但我认为 H2(与许多其他 SQL RDBMS 一样)希望 group by 具有所有非聚合列(使用 album_id 将它们全部添加到 group by)。所以我会尝试。您看到 Group By 逻辑引发的异常这一事实就是我倾向于这种方式的原因。

【讨论】:

  • H2 和其他一些 DBMS 支持可选功能 T301,来自 SQL 标准的“功能依赖关系”,并且不需要聚合或分组所有列。但是这些列在每个组中必须具有相同的值,因此有效性取决于数据。
猜你喜欢
  • 2014-10-15
  • 2018-04-27
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多