{"record":{"id":"6cdd12cd8a00e489","repo":"prestodb/presto","slug":"invalid-function-argument-6cdd12","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Latitude must be between -90 and 90","messagePattern":"Latitude must be between -90 and 90","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-geospatial-toolkit/src/main/java/com/facebook/presto/geospatial/SphericalGeographyUtils.java","lineNumber":50,"sourceCode":"import static java.lang.String.format;\n\npublic class SphericalGeographyUtils\n{\n    public static final double EARTH_RADIUS_KM = 6371.01;\n    public static final double EARTH_RADIUS_M = EARTH_RADIUS_KM * 1000.0;\n    private static final float MIN_LATITUDE = -90;\n    private static final float MAX_LATITUDE = 90;\n    private static final float MIN_LONGITUDE = -180;\n    private static final float MAX_LONGITUDE = 180;\n    private static final Joiner OR_JOINER = Joiner.on(\" or \");\n    private static final Set<GeometryType> ALLOWED_SPHERICAL_DISTANCE_TYPES = EnumSet.of(POINT);\n\n    private SphericalGeographyUtils() {}\n\n    public static void checkLatitude(double latitude)\n    {\n        if (Double.isNaN(latitude) || Double.isInfinite(latitude) || latitude < MIN_LATITUDE || latitude > MAX_LATITUDE) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Latitude must be between -90 and 90\");\n        }\n    }\n\n    public static void checkLongitude(double longitude)\n    {\n        if (Double.isNaN(longitude) || Double.isInfinite(longitude) || longitude < MIN_LONGITUDE || longitude > MAX_LONGITUDE) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Longitude must be between -180 and 180\");\n        }\n    }\n\n    public static Double sphericalDistance(OGCGeometry leftGeometry, OGCGeometry rightGeometry)\n    {\n        if (leftGeometry.isEmpty() || rightGeometry.isEmpty()) {\n            return null;\n        }\n\n        validateSphericalType(\"ST_Distance\", leftGeometry, ALLOWED_SPHERICAL_DISTANCE_TYPES);\n        validateSphericalType(\"ST_Distance\", rightGeometry, ALLOWED_SPHERICAL_DISTANCE_TYPES);","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-geospatial-toolkit/src/main/java/com/facebook/presto/geospatial/SphericalGeographyUtils.java#L32-L68","documentation":"SphericalGeographyUtils.checkLatitude validates that a latitude value is a finite number within [-90, 90] before any spherical computation. NaN, infinity, or out-of-range latitudes are meaningless on a sphere, so the library rejects them with INVALID_FUNCTION_ARGUMENT.","triggerScenarios":"Calling spherical functions (e.g. ST_Distance on SphericalGeography, greatCircleDistance) with latitude < -90, > 90, NaN, or ±Infinity — usually from bad input columns or wrong argument order (lat/lon swapped).","commonSituations":"Swapped latitude/longitude columns (longitude up to 180 fails the ±90 check), data with missing values encoded as 999 or 0-degree artifacts, CSV imports producing NaN.","solutions":["Check argument order; ensure latitude is the first/lat column and not swapped with longitude.","Filter rows: WHERE latitude BETWEEN -90 AND 90 AND NOT is_nan(latitude).","Clamp or reject out-of-range values at ingestion time.","Cast properly — text 'NaN' parsed to double yields NaN."],"exampleFix":"// before\nSELECT ST_Distance(ST_Point(lon, lat), ST_Point(lon2, lat2)) FROM t;\n// after\nSELECT ST_Distance(ST_Point(lon, lat), ST_Point(lon2, lat2)) FROM t WHERE lat BETWEEN -90 AND 90 AND lat2 BETWEEN -90 AND 90 AND NOT is_nan(lat) AND NOT is_nan(lat2);","handlingStrategy":"validation","validationCode":"SELECT * FROM t WHERE lat IS NOT NULL AND NOT is_nan(lat) AND lat BETWEEN -90 AND 90;","typeGuard":"boolean isValidLatitude(double lat) { return !Double.isNaN(lat) && !Double.isInfinite(lat) && lat >= -90.0 && lat <= 90.0; }","tryCatchPattern":"try { distance = ST_Distance(g1, g2); } catch (PrestoException e) { if (\"INVALID_FUNCTION_ARGUMENT\".equals(e.getErrorCode().getName())) { log.error(\"bad latitude\"); return null; } throw e; }","preventionTips":["Validate lat/lon ranges in ETL before loading.","Never swap latitude and longitude column order.","Use NULL, not NaN/999, for missing coordinates.","Add CHECK-style filters in queries touching geospatial functions."],"tags":["geospatial","latitude","validation"],"backgroundTag":"coordinate-out-of-range","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}