Skip to content

when multiple aggregations are used in Query.select(String), the further ones appear in the group by #3683

@Incanus3

Description

@Incanus3

only non-aggregation clauses from the select string should be added to the group by part of the query. this is not the case though:

@Entity
@DbName(TEST_DB_NAME)
data class TestEntity(
    @Id
    var id: String = UUID.randomUUID().toString(),

    @Column
    var name: String = "",
) {
    @Aggregation("count(*)")
    val count: Int = 0

    @Aggregation("group_concat(id)")
    val ids: String = ""

    override fun hashCode() = id.hashCode()
    override fun equals(other: Any?) = other is TestEntity && other.id == id
}

class MultipleAggregationsTest {
    private val dsConfig = DataSourceConfig().also {
        it.username = "sa"
        it.password = "sa"
        it.url = "jdbc:h2:mem:test"
        it.driver = "org.h2.Driver"
    }

    private val dbConfig = DatabaseConfig().also {
        it.name = TEST_DB_NAME

        it.isDdlRun = true
        it.isDdlGenerate = true
        it.isDefaultServer = false

        it.setDataSourceConfig(dsConfig)

        it.addClass(TestEntity::class.java)
        it.addClass(TestResourceWithLobAndAggregation::class.java)
    }

    private val db = DatabaseFactory.create(dbConfig)

    @Test
    fun `multiple aggregations`() {
        db.insert(TestEntity(name = "1"))
        db.insert(TestEntity(name = "1"))
        db.insert(TestEntity(name = "2"))

        LoggedSql.start()
        val results = db.createQuery(TestEntity::class.java)
            .select("name, count(*) as count, group_concat(id) as ids")
            .findList()
        val sql = LoggedSql.stop().joinToString("\n").also { println(it) }

        println("results:")
        results.forEach {
            println("name: ${it.name} count: ${it.count} ids: ${it.ids}")
        }
    }
}

the query in this case should be:

select t0.name, count(*) count, group_concat(t0.id) ids from test_entity t0 group by t0.name;

but this is run instead:

select t0.name, count(*) count, group_concat(t0.id) ids from test_entity t0 group by t0.name, group_concat(t0.id);

when

QTestEntity(db).select(QTestEntity._alias.name, QTestEntity._alias.count, QTestEntity._alias.ids)

is used instead, it works fine (presumably because ebean knows both the count and ids properties represent aggregations), but sadly I can't do that for two reasons:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions