{"record":{"id":"15e846baea815ec1","repo":"prestodb/presto","slug":"invalid-cast-argument-15e846","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Invalid JSON string for KDB tree","messagePattern":"Invalid JSON string for KDB tree","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/geospatial/KdbTreeCasts.java","lineNumber":39,"sourceCode":"import io.airlift.slice.Slice;\n\nimport static com.facebook.presto.common.function.OperatorType.CAST;\nimport static com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT;\n\npublic final class KdbTreeCasts\n{\n    private KdbTreeCasts() {}\n\n    @LiteralParameters(\"x\")\n    @ScalarOperator(CAST)\n    @SqlType(KdbTreeType.NAME)\n    public static Object castVarcharToKdbTree(@SqlType(\"varchar(x)\") Slice json)\n    {\n        try {\n            return KdbTreeUtils.fromJson(json.toStringUtf8());\n        }\n        catch (IllegalArgumentException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"Invalid JSON string for KDB tree\", e);\n        }\n    }\n}\n","sourceCodeStart":21,"sourceCodeEnd":43,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/geospatial/KdbTreeCasts.java#L21-L43","documentation":"castVarcharToKdbTree converts a VARCHAR containing JSON into a KDB tree object used by spatial partitioning functions (e.g. spatial_partitions). KdbTreeUtils.fromJson parses the JSON and validates its structure; any malformed JSON or wrong shape raises IllegalArgumentException, which is rethrown as INVALID_CAST_ARGUMENT. This is a cast, so the error signals the input string is not a valid KDB-tree serialization.","triggerScenarios":"Calling CAST(varchar AS KdbTree) or passing a varchar literal/column to functions expecting a KdbTree where the string is not valid JSON, was truncated, was produced by a different tool, or uses an outdated schema.","commonSituations":"Hand-editing a tree JSON, storing trees in a table that got corrupted or truncated, version mismatch where an older Presto wrote a tree format the current parser rejects, quotes/escaping mangled by the SQL client.","solutions":["Obtain the KdbTree string from spatial_partitioning(...) output instead of hand-writing JSON.","Validate the string is well-formed JSON with the expected structure before casting.","Re-generate the tree if it came from an older cluster/version with an incompatible format.","Check for client-side escaping/truncation of the varchar value."],"exampleFix":"// before\nCAST('{\"nodes\": [invalid' AS KdbTree)\n// after\nCAST((SELECT tree_json FROM spatial_partitioning(geom, 100)) AS KdbTree)","handlingStrategy":"validation","validationCode":"// Java: validate JSON before cast\ntry { new ObjectMapper().readTree(kdbTreeJson); } catch (Exception e) { throw new IllegalArgumentException(\"not valid JSON\"); }\n-- SQL: sanity check the string looks like a tree\nSELECT tree_json FROM trees WHERE tree_json LIKE '{%' AND json_format(CAST(json_parse(tree_json) AS JSON)) IS NOT NULL;","typeGuard":"boolean isProbablyKdbTree(String s) { return s != null && s.trim().startsWith(\"{\") && s.contains(\"nodes\"); }","tryCatchPattern":"try {\n    tree = castToKdbTree(json);\n} catch (PrestoException e) {\n    if (e.getErrorCode().getName().equals(\"INVALID_CAST_ARGUMENT\")) {\n        // regenerate the tree via spatial_partitioning and retry\n    }\n}","preventionTips":["Always source tree JSON from spatial_partitioning output, never hand-written","Persist trees without truncation and verify round-trip parse after writes","Pin cluster versions so tree serialization format matches"],"tags":["presto","geospatial","json","cast"],"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"}