Skip to content

Commit

Permalink
Merge pull request #54 from target/sql-var-double
Browse files Browse the repository at this point in the history
extend JsonUtils for doubles
  • Loading branch information
colindean authored Jan 14, 2021
2 parents 01d7f7d + 44c9863 commit 532ab96
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/scala/com/target/data_validator/JsonUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ object JsonUtils extends LazyLogging {
case IntegerType => Json.fromInt(row.getInt(col))
case NullType => Json.Null
case BooleanType => Json.fromBoolean(row.getBoolean(col))
case DoubleType => Json.fromDoubleOrNull(row.getDouble(col))
case _: StructType => row2Json(row.getStruct(col))
case _ =>
logger.error(s"Unimplemented dataType '${dataType.typeName}' in column: ${row.schema(col).name} " +
Expand Down
14 changes: 11 additions & 3 deletions src/test/scala/com/target/data_validator/JsonUtilsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@ class JsonUtilsSpec extends FunSpec with Matchers with TestingSparkSession {
val TEST_LONG = Random.nextLong
val TEST_INT = Random.nextInt
val TEST_BOOLEAN = Random.nextBoolean
val TEST_DOUBLE = Random.nextDouble

val schema = StructType(
List(
StructField("string", StringType),
StructField("long", LongType),
StructField("int", IntegerType),
StructField("null", NullType),
StructField("bool", BooleanType)
StructField("bool", BooleanType),
StructField("double", DoubleType)
)
)

val sampleData = List(Row(TEST_STRING, TEST_LONG, TEST_INT, null, TEST_BOOLEAN)) // scalastyle:ignore
val sampleData = List(Row(TEST_STRING, TEST_LONG, TEST_INT, null, TEST_BOOLEAN, TEST_DOUBLE)) // scalastyle:ignore

def mkRow: Row = spark.createDataFrame(sc.parallelize(sampleData), schema).head()

Expand Down Expand Up @@ -113,14 +115,20 @@ class JsonUtilsSpec extends FunSpec with Matchers with TestingSparkSession {
assert(row2Json(sut, 4) == Json.fromBoolean(TEST_BOOLEAN)) // scalastyle:ignore
}

it("Row with double") {
val sut = mkRow
assert(row2Json(sut, 5) == Json.fromDoubleOrNull(TEST_DOUBLE)) // scalastyle:ignore
}

it("Full Row") {
val sut = mkRow
assert(row2Json(sut) == Json.obj(
("string", Json.fromString(TEST_STRING)),
("long", Json.fromLong(TEST_LONG)),
("int", Json.fromInt(TEST_INT)),
("null", Json.Null),
("bool", Json.fromBoolean(TEST_BOOLEAN))
("bool", Json.fromBoolean(TEST_BOOLEAN)),
("double", Json.fromDoubleOrNull(TEST_DOUBLE))
))
}

Expand Down

0 comments on commit 532ab96

Please sign in to comment.