Skip to content

Commit 1e5d0a8

Browse files
authored
Add tests for repeated read with limit from SQL table and ResultSet (#500)
1 parent c29c6f4 commit 1e5d0a8

File tree

2 files changed

+50
-1
lines changed
  • dataframe-jdbc/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/io
    • test/kotlin/org/jetbrains/kotlinx/dataframe/io

2 files changed

+50
-1
lines changed

dataframe-jdbc/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/readJdbc.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ private fun fetchAndConvertDataFromResultSet(
564564
var counter = 0
565565

566566
if (limit > 0) {
567-
while (rs.next() && counter < limit) {
567+
while (counter < limit && rs.next()) {
568568
handleRow(tableColumns, data, dbType, rs)
569569
counter++
570570
// if (counter % 1000 == 0) logger.debug { "Loaded $counter rows." } // TODO: https://github.com/Kotlin/dataframe/issues/455

dataframe-jdbc/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/h2Test.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,27 @@ class JdbcTest {
254254
dataSchema.columns["name"]!!.type shouldBe typeOf<String>()
255255
}
256256

257+
// to cover a reported case from https://github.com/Kotlin/dataframe/issues/494
258+
@Test
259+
fun `repeated read from table with limit`() {
260+
val tableName = "Customer"
261+
262+
for(i in 1 .. 10) {
263+
val df1 = DataFrame.readSqlTable(connection, tableName, 2).cast<Customer>()
264+
265+
df1.rowsCount() shouldBe 2
266+
df1.filter { it[Customer::age] > 30 }.rowsCount() shouldBe 1
267+
df1[0][1] shouldBe "John"
268+
269+
val dbConfig = DatabaseConfiguration(url = URL)
270+
val df2 = DataFrame.readSqlTable(dbConfig, tableName, 2).cast<Customer>()
271+
272+
df2.rowsCount() shouldBe 2
273+
df2.filter { it[Customer::age] > 30 }.rowsCount() shouldBe 1
274+
df2[0][1] shouldBe "John"
275+
}
276+
}
277+
257278
@Test
258279
fun `read from ResultSet`() {
259280
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).use { st ->
@@ -306,6 +327,34 @@ class JdbcTest {
306327
}
307328
}
308329

330+
@Test
331+
fun `repeated read from ResultSet with limit`() {
332+
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).use { st ->
333+
@Language("SQL")
334+
val selectStatement = "SELECT * FROM Customer"
335+
336+
st.executeQuery(selectStatement).use { rs ->
337+
for (i in 1 .. 10) {
338+
rs.beforeFirst()
339+
340+
val df1 = DataFrame.readResultSet(rs, H2, 2).cast<Customer>()
341+
342+
df1.rowsCount() shouldBe 2
343+
df1.filter { it[Customer::age] > 30 }.rowsCount() shouldBe 1
344+
df1[0][1] shouldBe "John"
345+
346+
rs.beforeFirst()
347+
348+
val df2 = DataFrame.readResultSet(rs, connection, 2).cast<Customer>()
349+
350+
df2.rowsCount() shouldBe 2
351+
df2.filter { it[Customer::age] > 30 }.rowsCount() shouldBe 1
352+
df2[0][1] shouldBe "John"
353+
}
354+
}
355+
}
356+
}
357+
309358
@Test
310359
fun `read from non-existing table`() {
311360
shouldThrow<JdbcSQLSyntaxErrorException> {

0 commit comments

Comments
 (0)