Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-400 ClassCastException when using fetchBatchedResults #2113

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import org.jetbrains.exposed.sql.vendors.ForUpdateOption
import org.jetbrains.exposed.sql.vendors.currentDialect
import java.sql.ResultSet
import java.util.*

enum class SortOrder(val code: String) {
ASC(code = "ASC"),
Expand Down Expand Up @@ -39,14 +38,15 @@ open class Query(override var set: FieldSet, where: Op<Boolean>?) : AbstractQuer
var where: Op<Boolean>? = where
private set

override val queryToExecute: Statement<ResultSet> get() {
val distinctExpressions = set.fields.distinct()
return if (distinctExpressions.size < set.fields.size) {
copy().adjustSelect { select(distinctExpressions) }
} else {
this
override val queryToExecute: Statement<ResultSet>
get() {
val distinctExpressions = set.fields.distinct()
return if (distinctExpressions.size < set.fields.size) {
copy().adjustSelect { select(distinctExpressions) }
} else {
this
}
Comment on lines +41 to +48
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformatting only

}
}

/** Creates a new [Query] instance using all stored properties of this `SELECT` query. */
override fun copy(): Query = Query(set, where).also { copy ->
Expand Down Expand Up @@ -272,11 +272,29 @@ open class Query(override var set: FieldSet, where: Op<Boolean>?) : AbstractQuer
var lastOffset = if (fetchInAscendingOrder) 0L else null
while (true) {
val query = [email protected]().adjustWhere {
lastOffset?.let {
lastOffset?.let { lastOffset ->
whereOp and if (fetchInAscendingOrder) {
(autoIncColumn greater it)
when (autoIncColumn.columnType) {
is EntityIDColumnType<*> -> {
(autoIncColumn as? Column<EntityID<Long>>)?.let {
(it greater lastOffset)
} ?: (autoIncColumn as? Column<EntityID<Int>>)?.let {
(it greater lastOffset.toInt())
} ?: (autoIncColumn greater lastOffset)
}
else -> (autoIncColumn greater lastOffset)
}
} else {
(autoIncColumn less it)
when (autoIncColumn.columnType) {
is EntityIDColumnType<*> -> {
(autoIncColumn as? Column<EntityID<Long>>)?.let {
(it less lastOffset)
} ?: (autoIncColumn as? Column<EntityID<Int>>)?.let {
(it less lastOffset.toInt())
} ?: (autoIncColumn less lastOffset)
}
else -> (autoIncColumn less lastOffset)
}
}
} ?: whereOp
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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.shared.assertEqualLists
Expand Down Expand Up @@ -100,4 +101,20 @@ class FetchBatchedResultsTests : DatabaseTestsBase() {
fun `when batch size is 0 or less, should throw an exception`() {
DMLTestsData.Cities.selectAll().fetchBatchedResults(batchSize = -1)
}

@Test
fun testFetchBatchedResultsWithAutoIncrementEntityId() {
val tester1 = object : IntIdTable("table_1") {
val data = varchar("data", 100)
}

val tester2 = object : IntIdTable("table_2") {
val moreData = varchar("more_data", 100)
val prevData = reference("prev_data", tester1, onUpdate = ReferenceOption.CASCADE)
}

withTables(tester1, tester2) {
(tester2 innerJoin tester1).selectAll().fetchBatchedResults(10_000).flatten()
}
}
}
Loading