Skip to content
Closed
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
19 changes: 19 additions & 0 deletions core/src/main/scala/org/apache/spark/rdd/JdbcRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ class JdbcRDD[T: ClassTag](
}
}

class LimitJdbcRDD[T: ClassTag](
sc: SparkContext,
getConnection: () => Connection,
sql: String,
lowerBound: Long,
pageSize: Long,
numPartitions: Int,
mapRow: (ResultSet) => T = JdbcRDD.resultSetToObjectArray _)
extends JdbcRDD[T](sc, getConnection, sql, lowerBound, pageSize, numPartitions, mapRow) {

override def getPartitions: Array[Partition] = {
(0 until numPartitions).map { i =>
val start = lowerBound + i * pageSize
val end = pageSize
new JdbcPartition(i, start.toLong, end.toLong)
}.toArray
}
}

object JdbcRDD {
def resultSetToObjectArray(rs: ResultSet): Array[Object] = {
Array.tabulate[Object](rs.getMetaData.getColumnCount)(i => rs.getObject(i + 1))
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/scala/org/apache/spark/rdd/JdbcRDDSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ class JdbcRDDSuite extends SparkFunSuite with BeforeAndAfter with LocalSparkCont
assert(rdd.reduce(_ + _) === 10100)
}

test("limit functionality") {
sc = new SparkContext("local", "test")
val rdd = new LimitJdbcRDD(
sc,
() => { DriverManager.getConnection("jdbc:derby:target/JdbcRDDSuiteDb") },
"SELECT DATA FROM FOO ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY",
0, 10, 10,
(r: ResultSet) => { r.getInt(1) } ).cache()

assert(rdd.count === 100)
assert(rdd.reduce(_ + _) === 10100)
}

test("large id overflow") {
sc = new SparkContext("local", "test")
val rdd = new JdbcRDD(
Expand Down