Skip to content

Fixes #1

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ object TableStatsSinglePathMain {
val schema = df.schema

//Part B
val columnValueCounts = df.flatMap(r => {
val columnValueCounts = df.flatMap{ r =>
(0 until schema.length).map { idx =>
//((columnIdx, cellValue), count)
((idx, r.get(idx)), 1l)
}
}).reduceByKey(_ + _) //This is like word count
}.reduceByKey(_ + _) //This is like word count

//Part C
val firstPassStats = columnValueCounts.mapPartitions[FirstPassStatsModel](it => {
val firstPassStats = columnValueCounts.mapPartitions[FirstPassStatsModel]{it =>
val firstPassStatsModel = new FirstPassStatsModel()
it.foreach{ case ((columnIdx, columnVal), count) => {
firstPassStatsModel.+=(columnIdx, columnVal, count)
}}
it.foreach{ case ((columnIdx, columnVal), count) =>
firstPassStatsModel += (columnIdx, columnVal, count)
}
Iterator(firstPassStatsModel)
}).reduce((a, b) => { //Part D
a.+=(b)
}.reduce{ (a, b) => //Part D
a += b
a
})
}

firstPassStats
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@ class ColumnStats(var nulls:Long = 0l,
var sumLong:Long = 0l,
val topNValues:TopNList = new TopNList(10)) extends Serializable {

def avgLong: Long = sumLong/totalCount
def avg: Double = sumLong / totalCount.toDouble

//Part C.B
def +=(colValue: Any, colCount: Long): Unit = {
totalCount += colCount
uniqueValues += 1

if (colValue == null) {
nulls += 1
} else if (colValue.isInstanceOf[String]) {
val colStringValue = colValue.asInstanceOf[String]
if (colStringValue.isEmpty) {
empties += 1
}
} else if (colValue.isInstanceOf[Long]) {
val colLongValue = colValue.asInstanceOf[Long]
if (maxLong < colLongValue) maxLong = colLongValue
if (minLong > colLongValue) minLong = colLongValue
sumLong += colLongValue
colValue match {
case null =>
nulls += 1
case s: String =>
if (s.isEmpty)
empties += 1
case l: Long =>
if (maxLong < l) maxLong = l
if (minLong > l) minLong = l
sumLong += l
}

topNValues.add(colValue, colCount)
Expand All @@ -46,11 +44,11 @@ class ColumnStats(var nulls:Long = 0l,
maxLong = maxLong.max(columnStats.maxLong)
minLong = minLong.max(columnStats.minLong)

columnStats.topNValues.topNCountsForColumnArray.foreach( r => {
columnStats.topNValues.topNCountsForColumnArray.foreach{ r =>
topNValues.add(r._1, r._2)
})
}
}

override def toString = s"ColumnStats(nulls=$nulls, empties=$empties, totalCount=$totalCount, uniqueValues=$uniqueValues, maxLong=$maxLong, minLong=$minLong, sumLong=$sumLong, topNValues=$topNValues, avgLong=$avgLong)"
override def toString = s"ColumnStats(nulls=$nulls, empties=$empties, totalCount=$totalCount, uniqueValues=$uniqueValues, maxLong=$maxLong, minLong=$minLong, sumLong=$sumLong, topNValues=$topNValues, avgLong=$avg)"
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ class FirstPassStatsModel extends Serializable {
var columnStatsMap = new mutable.HashMap[Integer, ColumnStats]

def +=(colIndex: Int, colValue: Any, colCount: Long): Unit = {
columnStatsMap.getOrElseUpdate(colIndex, {new ColumnStats}).+=(colValue, colCount)
columnStatsMap.getOrElseUpdate(colIndex, new ColumnStats) += (colValue, colCount)
}

def +=(firstPassStatsModel: FirstPassStatsModel): Unit = {
firstPassStatsModel.columnStatsMap.foreach( e => {
val columnStats = columnStatsMap.getOrElse(e._1, null)
if (columnStats != null) {
columnStats.+=(e._2)
} else {
columnStatsMap += ((e._1, e._2))
firstPassStatsModel.columnStatsMap.foreach{ case(idx, stats) =>
columnStatsMap.get(idx) match {
case Some(prevStats) =>
prevStats += stats
case None =>
columnStatsMap(idx) = stats
}
})
}
}

override def toString = s"FirstPassStatsModel(columnStatsMap=$columnStatsMap)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TopNList(val maxSize:Int) extends Serializable {

def add(newValue:Any, newCount:Long): Unit = {
if (topNCountsForColumnArray.length < maxSize -1) {
topNCountsForColumnArray.+=((newValue, newCount))
topNCountsForColumnArray += ((newValue, newCount))
} else if (topNCountsForColumnArray.length == maxSize) {
updateLowestValue
} else {
Expand All @@ -26,13 +26,13 @@ class TopNList(val maxSize:Int) extends Serializable {
def updateLowestValue: Unit = {
var index = 0

topNCountsForColumnArray.foreach( r => {
topNCountsForColumnArray.foreach{ r =>
if (r._2 < lowestValue) {
lowestValue = r._2
lowestColumnCountIndex = index
}
index+=1
})
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,54 @@ class TestTableStatsSinglePathMain extends FunSuite with BeforeAndAfterEach with
sparkConfig.set("spark.broadcast.compress", "false")
sparkConfig.set("spark.shuffle.compress", "false")
sparkConfig.set("spark.shuffle.spill.compress", "false")
var sc = new SparkContext("local", "test", sparkConfig)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)

val schema =
StructType(
Array(
StructField("id", LongType, true),
StructField("name", StringType, true),
StructField("age", LongType, true),
StructField("gender", StringType, true),
StructField("height", LongType, true),
StructField("job_title", StringType, true)
val sc = new SparkContext("local", "test", sparkConfig)
try {
val sqlContext = new org.apache.spark.sql.SQLContext(sc)

val schema =
StructType(
Array(
StructField("id", LongType, true),
StructField("name", StringType, true),
StructField("age", LongType, true),
StructField("gender", StringType, true),
StructField("height", LongType, true),
StructField("job_title", StringType, true)
)
)
)

val rowRDD = sc.parallelize(Array(
Row(1l, "Name.1", 20l, "M", 6l, "dad"),
Row(2l, "Name.2", 20l, "F", 5l, "mom"),
Row(3l, "Name.3", 20l, "F", 5l, "mom"),
Row(4l, "Name.4", 20l, "M", 5l, "mom"),
Row(5l, "Name.5", 10l, "M", 4l, "kid"),
Row(6l, "Name.6", 8l, "M", 3l, "kid")))

val df = sqlContext.createDataFrame(rowRDD, schema)

val firstPassStats = TableStatsSinglePathMain.getFirstPassStat(df)

assertResult(6l)(firstPassStats.columnStatsMap(0).maxLong)
assertResult(1l)(firstPassStats.columnStatsMap(0).minLong)
assertResult(21l)(firstPassStats.columnStatsMap(0).sumLong)
assertResult(3l)(firstPassStats.columnStatsMap(0).avgLong)

assertResult(2)(firstPassStats.columnStatsMap(3).topNValues.topNCountsForColumnArray.length)

firstPassStats.columnStatsMap(3).topNValues.topNCountsForColumnArray.foreach( r => {
if (r._1.equals("M")) {
assertResult(4l)(r._2)
} else if (r._1.equals("F")) {
assertResult(2l)(r._2)
} else {
throw new RuntimeException("Unknown gender: " + r._1)

val rowRDD = sc.parallelize(Array(
Row(1l, "Name.1", 20l, "M", 6l, "dad"),
Row(2l, "Name.2", 20l, "F", 5l, "mom"),
Row(3l, "Name.3", 20l, "F", 5l, "mom"),
Row(4l, "Name.4", 20l, "M", 5l, "mom"),
Row(5l, "Name.5", 10l, "M", 4l, "kid"),
Row(6l, "Name.6", 8l, "M", 3l, "kid")))

val df = sqlContext.createDataFrame(rowRDD, schema)

val firstPassStats = TableStatsSinglePathMain.getFirstPassStat(df)

assertResult(6l)(firstPassStats.columnStatsMap(0).maxLong)
assertResult(1l)(firstPassStats.columnStatsMap(0).minLong)
assertResult(21l)(firstPassStats.columnStatsMap(0).sumLong)
assertResult(3.5)(firstPassStats.columnStatsMap(0).avg)

assertResult(2)(firstPassStats.columnStatsMap(3).topNValues.topNCountsForColumnArray.length)

firstPassStats.columnStatsMap(3).topNValues.topNCountsForColumnArray.foreach{ r =>
if (r._1.equals("M")) {
assertResult(4l)(r._2)
} else if (r._1.equals("F")) {
assertResult(2l)(r._2)
} else {
throw new RuntimeException("Unknown gender: " + r._1)
}
}
})
} finally {
// or use https://github.com/holdenk/spark-testing-base/blob/master/src/main/scala/com/holdenkarau/spark/testing/LocalSparkContext.scala
sc.stop()
}

}
}