{"record":{"id":"b0212107fdcc1335","repo":"prestodb/presto","slug":"invalid-cast-argument","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Cannot cast value to IPADDRESS: ","messagePattern":"Cannot cast value to IPADDRESS: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-elasticsearch/src/main/java/com/facebook/presto/elasticsearch/decoders/IpAddressDecoder.java","lineNumber":71,"sourceCode":"        else if (value instanceof String) {\n            String address = (String) value;\n            Slice slice = castToIpAddress(Slices.utf8Slice(address));\n            ipAddressType.writeSlice(output, slice);\n        }\n        else {\n            throw new PrestoException(ELASTICSEARCH_TYPE_MISMATCH, format(\"Expected a string value for field '%s' of type IP: %s [%s]\", path, value, value.getClass().getSimpleName()));\n        }\n    }\n\n    // This is a copy of IpAddressOperators.castFromVarcharToIpAddress method\n    private Slice castToIpAddress(Slice slice)\n    {\n        byte[] address;\n        try {\n            address = InetAddresses.forString(slice.toStringUtf8()).getAddress();\n        }\n        catch (IllegalArgumentException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"Cannot cast value to IPADDRESS: \" + slice.toStringUtf8());\n        }\n\n        byte[] bytes;\n        if (address.length == 4) {\n            bytes = new byte[16];\n            bytes[10] = (byte) 0xff;\n            bytes[11] = (byte) 0xff;\n            arraycopy(address, 0, bytes, 12, 4);\n        }\n        else if (address.length == 16) {\n            bytes = address;\n        }\n        else {\n            throw new PrestoException(GENERIC_INTERNAL_ERROR, \"Invalid InetAddress length: \" + address.length);\n        }\n\n        return wrappedBuffer(bytes);\n    }","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-elasticsearch/src/main/java/com/facebook/presto/elasticsearch/decoders/IpAddressDecoder.java#L53-L89","documentation":"After confirming the value is a string, IpAddressDecoder.castToIpAddress tries to parse it with Guava's InetAddresses.forString. If the string is not a valid IPv4/IPv6 literal, the resulting IllegalArgumentException is rethrown as INVALID_CAST_ARGUMENT. It is a data-quality error: the value has the right JSON type but wrong content.","triggerScenarios":"A document field mapped as IPADDRESS contains a malformed string such as \"999.1.1.1\", \"example.com\", \"\", or an address with an invalid IPv6 zone, which fails InetAddresses.forString.","commonSituations":"Log shippers write hostname or placeholder values (\"unknown\", \"-\") into IP fields; truncated or corrupted IPs; IPv6 with scope-id like fe80::1%eth0 which Guava rejects.","solutions":["Clean the source documents: replace invalid IP strings with valid literals or null before querying.","Map the field as VARCHAR instead of IPADDRESS and filter/validate in SQL before casting.","Fix the ingest pipeline to validate IPs (e.g. only emit values passing InetAddress.getByName/InetAddresses.isInetAddress)."],"exampleFix":"// before\n{\"src_ip\": \"unknown\"}\n// after\n{\"src_ip\": null}  // or a valid literal like \"10.0.0.5\"","handlingStrategy":"validation","validationCode":"import com.google.common.net.InetAddresses;\nboolean valid = InetAddresses.isInetAddress(rawValue); // run before relying on the IP column","typeGuard":"function isValidIpLiteral(s) { return typeof s === 'string' && /^(25[0-5]|2[0-4]\\d|1?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}$/.test(s) || /^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$/.test(s); }","tryCatchPattern":"try { SELECT CAST(src_ip AS IPADDRESS) FROM raw_view } catch (PrestoException e) { if (e.getErrorCode().getName().equals(\"INVALID_CAST_ARGUMENT\")) { return null; } throw e; }","preventionTips":["Map IP fields as Elasticsearch 'ip' type so invalid literals are rejected at indexing time.","Sanitize placeholder values like 'unknown' or '-' to null in the ingest pipeline.","Query via a VARCHAR view and use TRY_CAST to isolate bad rows."],"tags":["elasticsearch","presto","invalid-cast","ipaddress","data-quality"],"backgroundTag":"invalid-ip-cast","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"}