{"record":{"id":"c08e91045033f576","repo":"prestodb/presto","slug":"invalid-cast-argument-c08e91","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Invalid bigint tile encoding: %s","messagePattern":"Invalid bigint tile encoding: (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/geospatial/BingTileFunctions.java","lineNumber":109,"sourceCode":"\n    @Description(\"Encodes a Bing tile into a bigint\")\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.BIGINT)\n    public static long castToBigint(@SqlType(BingTileType.NAME) long tile)\n    {\n        return tile;\n    }\n\n    @Description(\"Decodes a Bing tile from a bigint\")\n    @ScalarOperator(CAST)\n    @SqlType(BingTileType.NAME)\n    public static long castFromBigint(@SqlType(StandardTypes.BIGINT) long tile)\n    {\n        try {\n            BingTile.decode(tile);\n        }\n        catch (IllegalArgumentException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT,\n                    format(\"Invalid bigint tile encoding: %s\", tile));\n        }\n        return tile;\n    }\n\n    @Description(\"Creates a Bing tile from XY coordinates and zoom level\")\n    @ScalarFunction(\"bing_tile\")\n    @SqlType(BingTileType.NAME)\n    public static long toBingTile(@SqlType(StandardTypes.INTEGER) long tileX, @SqlType(StandardTypes.INTEGER) long tileY, @SqlType(StandardTypes.INTEGER) long zoomLevel)\n    {\n        checkZoomLevel(zoomLevel);\n        checkCoordinate(tileX, zoomLevel);\n        checkCoordinate(tileY, zoomLevel);\n\n        return BingTile.fromCoordinates(toIntExact(tileX), toIntExact(tileY), toIntExact(zoomLevel)).encode();\n    }\n\n    @Description(\"Given a Bing tile, returns its QuadKey\")","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/geospatial/BingTileFunctions.java#L91-L127","documentation":"castFromBigint implements the cast from BIGINT to BingTile. It validates the bigint by decoding it via BingTile.decode(tile), which enforces the quadkey/tile encoding invariants (zoom level 0-31, valid x/y bounds for the zoom). If decoding fails, Presto throws INVALID_CAST_ARGUMENT because the bigint does not represent a valid Bing tile. This is the library's way of rejecting malformed tile encodings during a SQL cast.","triggerScenarios":"Executing CAST(bigint_value AS BingTile) or bing_tile(bigint) where the bigint is negative, exceeds the maximum encoding, has zoom level > 31, or has x/y coordinates out of range for the encoded zoom level, so BingTile.decode(tile) throws IllegalArgumentException.","commonSituations":"Hand-crafted or corrupted tile IDs passed to a cast; computing tile encodings in external code with wrong bit layouts; column data from another system where tile IDs were encoded differently; arithmetic on valid tile IDs producing invalid values.","solutions":["Validate the tile value before casting: ensure it is non-negative and decodable, or build it with bing_tile(x, y, zoom_level) instead of a raw bigint cast.","Check the zoom level encoded in the bigint (top bits) is between 0 and 31 and that x/y fit within 2^zoom.","If data came from external tooling, re-encode tile IDs using the documented Bing tile quadkey/encoding scheme.","Wrap the cast in a TRY clause in SQL to get NULL instead of a query failure for dirty data."],"exampleFix":"// before (SQL)\nSELECT CAST(tile_id AS BingTile) FROM raw_tiles;\n// after\nSELECT TRY(CAST(tile_id AS BingTile)) FROM raw_tiles;\n-- or construct explicitly:\nSELECT bing_tile(x, y, zoom_level) FROM raw_tiles;","handlingStrategy":"validation","validationCode":"-- SQL: validate before casting\nSELECT tile_id FROM raw_tiles\nWHERE tile_id >= 0\n  AND bing_tile_zoom_level(TRY(CAST(tile_id AS BingTile))) IS NOT NULL;","typeGuard":null,"tryCatchPattern":"-- SQL\nSELECT TRY(CAST(tile_id AS BingTile)) AS tile FROM raw_tiles;","preventionTips":["Prefer bing_tile(x, y, zoom) construction over raw bigint casts","Validate tile ids are non-negative and within encoding limits upstream","Use TRY() for dirty external data"],"tags":["presto","geospatial","bing-tile","invalid-cast","sql"],"backgroundTag":"invalid-cast-argument","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"}