prestodb/presto · error · PrestoException
INVALID_CAST_ARGUMENT
INVALID_CAST_ARGUMENT
Error message
Invalid JSON string for KDB tree
What it means
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.
Source
Thrown at presto-main-base/src/main/java/com/facebook/presto/geospatial/KdbTreeCasts.java:39
import io.airlift.slice.Slice;
import static com.facebook.presto.common.function.OperatorType.CAST;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT;
public final class KdbTreeCasts
{
private KdbTreeCasts() {}
@LiteralParameters("x")
@ScalarOperator(CAST)
@SqlType(KdbTreeType.NAME)
public static Object castVarcharToKdbTree(@SqlType("varchar(x)") Slice json)
{
try {
return KdbTreeUtils.fromJson(json.toStringUtf8());
}
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Invalid JSON string for KDB tree", e);
}
}
}
View on GitHub (pinned to 55bb57d202)
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.
Example fix
// before
CAST('{"nodes": [invalid' AS KdbTree)
// after
CAST((SELECT tree_json FROM spatial_partitioning(geom, 100)) AS KdbTree) Defensive patterns
Strategy: validation
Validate before calling
// Java: validate JSON before cast
try { new ObjectMapper().readTree(kdbTreeJson); } catch (Exception e) { throw new IllegalArgumentException("not valid JSON"); }
-- SQL: sanity check the string looks like a tree
SELECT tree_json FROM trees WHERE tree_json LIKE '{%' AND json_format(CAST(json_parse(tree_json) AS JSON)) IS NOT NULL; Type guard
boolean isProbablyKdbTree(String s) { return s != null && s.trim().startsWith("{") && s.contains("nodes"); } Try / catch
try {
tree = castToKdbTree(json);
} catch (PrestoException e) {
if (e.getErrorCode().getName().equals("INVALID_CAST_ARGUMENT")) {
// regenerate the tree via spatial_partitioning and retry
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/15e846baea815ec1.
Report an issue: GitHub.