Skip to content

Commit

Permalink
Fortified parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard Wolters committed Dec 11, 2023
1 parent 2e879a8 commit a26bfb1
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ object QueryProgress extends LazyLogging {
Some(ClickhouseQueryProgress(queryId, QueryAccepted))
case queryId :: progressJson :: Nil =>
Try {
val fields = progressJson.parseJson.asJsObject.fields
ClickhouseQueryProgress(
queryId,
Progress(
fields("read_rows").convertTo[String].toLong,
fields("read_bytes").convertTo[String].toLong,
fields.get("written_rows").map(_.convertTo[String].toLong).getOrElse(0),
fields.get("written_bytes").map(_.convertTo[String].toLong).getOrElse(0),
Iterable("total_rows_to_read", "total_rows")
.flatMap(fields.get)
.map(_.convertTo[String].toLong)
.headOption
.getOrElse(0L)
)
)
progressJson.parseJson match {
case JsObject(fields) =>
ClickhouseQueryProgress(
queryId,
Progress(
fields("read_rows").convertTo[String].toLong,
fields("read_bytes").convertTo[String].toLong,
fields.get("written_rows").map(_.convertTo[String].toLong).getOrElse(0),
fields.get("written_bytes").map(_.convertTo[String].toLong).getOrElse(0),
Iterable("total_rows_to_read", "total_rows")
.flatMap(fields.get)
.map(_.convertTo[String].toLong)
.headOption
.getOrElse(0L)
)
)
case unknown => throw new IllegalArgumentException(s"Cannot extract progress from $unknown")
}
} match {
case Success(value) => Some(value)
case Failure(exception) =>
Expand Down

0 comments on commit a26bfb1

Please sign in to comment.