Skip to content

Commit

Permalink
fix: EXPOSED-623 Offset not applied in COUNT query
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichevjb committed Oct 30, 2024
1 parent 9f3396c commit 366d976
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ open class Query(override var set: FieldSet, where: Op<Boolean>?) : AbstractQuer
* @sample org.jetbrains.exposed.sql.tests.shared.dml.InsertSelectTests.testInsertSelect02
*/
override fun count(): Long {
return if (distinct || groupedByColumns.isNotEmpty() || limit != null) {
return if (distinct || groupedByColumns.isNotEmpty() || limit != null || offset > 0) {
fun Column<*>.makeAlias() =
alias(transaction.db.identifierManager.quoteIfNecessary("${table.tableNameWithoutSchemeSanitized}_$name"))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jetbrains.exposed.sql.tests.shared.dml

import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.*
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
Expand Down Expand Up @@ -66,4 +68,26 @@ class CountTests : DatabaseTestsBase() {
}
}
}

@Test
fun testCountWithOffsetWithoutLimit() {
val tester = object : IntIdTable("users") {
val value = integer("value")
}

// SQLite, MariaDB, Mysql do not support OFFSET clause without LIMIT
withTables(excludeSettings = TestDB.ALL_MYSQL_MARIADB + TestDB.SQLITE, tester) {
tester.batchInsert(listOf(1, 2, 3, 4, 5)) {
this[tester.value] = it
}

assertEquals(5, tester.selectAll().count())

assertEquals(2, tester.selectAll().offset(1).limit(2).count())

assertEquals(2, tester.selectAll().limit(2).count())

assertEquals(3, tester.selectAll().offset(2).count())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class DistinctOnTests : DatabaseTestsBase() {
}

withTables(excludeSettings = TestDB.ALL - distinctOnSupportedDb, tester) {
addLogger(StdOutSqlLogger)
// Empty list of columns should not cause exception
tester.insert {
it[value] = 1
Expand Down

0 comments on commit 366d976

Please sign in to comment.