Hi guys, I had a method which calculated distance ...
# exposed
g
Hi guys, I had a method which calculated distance between two points, like so:
Copy code
``````fun distanceInKm(lat1: Column<Double>, long1: Column<Double>, lat2: Column<Double>, long2: Column<Double>) =
Expression.build {
ACos(
Sin(
(doubleLiteral(PI) * lat1 / doubleLiteral(180.0))
) *
Sin(doubleLiteral(PI) * lat2 / doubleLiteral(180.0))
+
Cos(doubleLiteral(PI) * lat1 / doubleLiteral(180.0)) *
Cos(doubleLiteral(PI) * lat2 / doubleLiteral(180.0)) *
Cos(doubleLiteral(PI) * (long1 - long2) / doubleLiteral(180.0))
).times(
doubleLiteral(180.0) / doubleLiteral(PI) * doubleLiteral(60.0) * doubleLiteral(1.1515) * doubleLiteral(
1.609344
)
)
}``````
But I just realized that two of the columns are nullable (
``lat2``
,
``long2``
), which breaks the method. Is there anything like
``!!``
in exposed? Given I know the values are not null because of my
``where``
.
Copy code
``````val distance = distanceInKm(
entityA.lat,
entityA.long,
entityB.lat, <--- Nullable
entityB.long <--- Nullable
)``````
Copy code
``````val distanceRounded = RoundNumeric(distance, 1).alias("distanceRounded")

....
.slice(distanceRounded, entityB.id.countDistinct())
.select {
(entityB.lat.isNotNull()) and (entityB.long.isNotNull()) and ...
}``````
Hm... I guess I could just change the
``parameter``
to
``ExpressionWithColumnType``
and then cast
``entityB.lat``
?