{"record":{"id":"9a65920fb04be9d4","repo":"prestodb/presto","slug":"invalid-function-argument-9a6592","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"invalid input length ","messagePattern":"invalid input length ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/VarbinaryFunctions.java","lineNumber":205,"sourceCode":"    {\n        String encoded;\n        if (slice.hasByteArray()) {\n            encoded = BaseEncoding.base16().encode(slice.byteArray(), slice.byteArrayOffset(), slice.length());\n        }\n        else {\n            encoded = BaseEncoding.base16().encode(slice.getBytes());\n        }\n        return Slices.utf8Slice(encoded);\n    }\n\n    @Description(\"decode hex encoded binary data\")\n    @ScalarFunction(\"from_hex\")\n    @LiteralParameters(\"x\")\n    @SqlType(StandardTypes.VARBINARY)\n    public static Slice fromHexVarchar(@SqlType(\"varchar(x)\") Slice slice)\n    {\n        if (slice.length() % 2 != 0) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"invalid input length \" + slice.length());\n        }\n\n        byte[] result = new byte[slice.length() / 2];\n        for (int i = 0; i < slice.length(); i += 2) {\n            result[i / 2] = (byte) ((hexDigitCharToInt(slice.getByte(i)) << 4) | hexDigitCharToInt(slice.getByte(i + 1)));\n        }\n        return Slices.wrappedBuffer(result);\n    }\n\n    @Description(\"encode value as a 64-bit 2's complement big endian varbinary\")\n    @ScalarFunction(\"to_big_endian_64\")\n    @SqlType(StandardTypes.VARBINARY)\n    public static Slice toBigEndian64(@SqlType(StandardTypes.BIGINT) long value)\n    {\n        Slice slice = Slices.allocate(Long.BYTES);\n        slice.setLong(0, Long.reverseBytes(value));\n        return slice;\n    }","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/VarbinaryFunctions.java#L187-L223","documentation":"from_hex(varchar) interprets a hex string as binary data, consuming two hex characters per output byte. The function requires the input length to be even; an odd-length string cannot be split into byte pairs, so Presto throws INVALID_FUNCTION_ARGUMENT with the offending length. Note the message text is literally \"invalid input length \" plus the length.","triggerScenarios":"Calling from_hex('ABC') or any hex string whose LENGTH is odd. Also triggered by data that lost a leading zero (e.g. a value serialized without zero-padding, producing 'FFF' instead of '0FFF').","commonSituations":"Hex values copied from tools that strip leading zeros; strings truncated by column width limits; manual concatenation of hex fragments where one fragment has odd length.","solutions":["Pad the string to even length with a leading '0': from_hex(lpad(h, LENGTH(h) + LENGTH(h) % 2, '0')).","Fix the source data so the hex string has an even number of digits.","Verify with LENGTH(h) % 2 = 0 before calling from_hex.","If using from_hex on varbinary input, ensure the varbinary path (fromHexVarbinary) is used and the bytes are a valid hex encoding."],"exampleFix":"// before\nSELECT from_hex('ABC');\n// after\nSELECT from_hex(lpad('ABC', 4, '0')); -- 0ABC","handlingStrategy":"validation","validationCode":"-- reject odd-length hex before decoding\nSELECT CASE WHEN LENGTH(h) % 2 = 0 THEN from_hex(h) ELSE NULL END;","typeGuard":"boolean isEvenLengthHex(String h) {\n    return h != null && h.length() % 2 == 0 && h.matches(\"[0-9A-Fa-f]+\");\n}","tryCatchPattern":"try { bytes = fromHex(h); } catch (PrestoException e) { if (e.getErrorCode().getName().equals(\"INVALID_FUNCTION_ARGUMENT\")) { bytes = fromHex(lpad(h, LENGTH(h) + 1, '0')); } else { throw e; } }","preventionTips":["Always zero-pad hex strings from sources that strip leading zeros.","Validate LENGTH(h) % 2 = 0 and hex-only characters before decoding.","Strip 0x prefixes and whitespace at ingestion time.","Keep hex values in CHAR columns of fixed even width."],"tags":["presto","sql","varbinary","hex"],"backgroundTag":"invalid-function-arguments","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"}