in connector/src/main/scala/com/microsoft/kusto/spark/utils/KustoDataSourceUtils.scala [865:902]
private[kusto] def estimateRowsCount(
client: Client,
query: String,
database: String,
crp: ClientRequestProperties): Int = {
val estimationResult: util.List[AnyRef] = Await.result(
Future {
val res =
client.execute(database, generateEstimateRowsCountQuery(query), crp).getPrimaryResults
res.next()
res.getCurrentRow
},
KustoConstants.TimeoutForCountCheck)
val maybeEstimatedCount = Option(estimationResult.get(1))
/*
Check if the result is null or an empty string return a 0 , else return the numeric value
*/
val estimatedCount = maybeEstimatedCount match {
case Some(ecStr: String) =>
if (StringUtils.isBlank(ecStr) || !StringUtils.isNumeric(ecStr)) /* Empty estimate */ 0
else ecStr.toInt
case Some(ecInt: java.lang.Number) =>
ecInt.intValue() // Is a numeric , get the int value back
case None => 0 // No value
}
// We cannot be finitely determine the count , or have a 0 count. Recheck using a 'query | count()'
if (estimatedCount == 0) {
Await.result(
Future {
val res = client.execute(database, generateCountQuery(query), crp).getPrimaryResults
res.next()
res.getInt(0)
},
KustoConstants.TimeoutForCountCheck)
} else {
estimatedCount
}
}