Casting expression as it errors out in postgres