{"record":{"id":"bb1a3ad6dcaee389","repo":"prestodb/presto","slug":"invalid-varchar-length-length","errorCode":null,"errorMessage":"Invalid VARCHAR length {length}","messagePattern":"Invalid VARCHAR length (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/type/AbstractVarcharType.java","lineNumber":38,"sourceCode":"import io.airlift.slice.Slice;\nimport io.airlift.slice.Slices;\n\nimport java.util.Objects;\n\npublic class AbstractVarcharType\n        extends AbstractVariableWidthType\n{\n    public static final int UNBOUNDED_LENGTH = Integer.MAX_VALUE;\n    public static final int MAX_LENGTH = Integer.MAX_VALUE - 1;\n\n    private final int length;\n\n    AbstractVarcharType(int length, TypeSignature typeSignature)\n    {\n        super(typeSignature, Slice.class);\n\n        if (length < 0) {\n            throw new IllegalArgumentException(\"Invalid VARCHAR length \" + length);\n        }\n        this.length = length;\n    }\n\n    @Deprecated\n    public int getLength()\n    {\n        return length;\n    }\n\n    public int getLengthSafe()\n    {\n        if (isUnbounded()) {\n            throw new IllegalStateException(\"Cannot get size of unbounded VARCHAR.\");\n        }\n        return length;\n    }\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/type/AbstractVarcharType.java#L20-L56","documentation":"AbstractVarcharType's package-private constructor validates that the VARCHAR length is non-negative before creating the type, throwing IllegalArgumentException for negative lengths. VARCHAR types in Presto carry a maximum length; only createVarcharType with length >= 0 (or unbounded VARCHAR) is valid.","triggerScenarios":"Calling createVarcharType(length) with a negative length, e.g. from unbounded metadata, a length parsed from a bad DDL/type signature, or an int underflow when computing lengths.","commonSituations":"Connector metadata returning -1 or another sentinel for 'unbounded' length; parsing VARCHAR(n) from config where n is missing/negative; arithmetic on lengths that overflows to negative.","solutions":["Pass VARCHAR_LENGTH (unbounded) or a non-negative length to createVarcharType; use createUnboundedVarcharType for unbounded","Treat sentinel values like -1 from metadata as unbounded and map them to unbounded VARCHAR before constructing","Validate parsed length values (n >= 0) before type construction","Clamp/fix length arithmetic that can underflow"],"exampleFix":"// before\nVarcharType type = VarcharType.createVarcharType(metadataLength); // -1 sentinel\n// after\nVarcharType type = metadataLength < 0\n    ? VarcharType.createUnboundedVarcharType()\n    : VarcharType.createVarcharType(metadataLength);","handlingStrategy":"validation","validationCode":"if (length < 0) {\n    throw new IllegalArgumentException(\"VARCHAR length must be >= 0, got \" + length);\n}\nVarcharType t = VarcharType.createVarcharType(length);","typeGuard":"boolean isValidVarcharLength(int length) { return length >= 0; }","tryCatchPattern":"try { type = VarcharType.createVarcharType(len); } catch (IllegalArgumentException e) { type = VarcharType.createUnboundedVarcharType(); }","preventionTips":["Map metadata sentinel values (-1, 0 for unbounded conventions) to createUnboundedVarcharType","Validate lengths parsed from DDL/config before constructing types","Avoid length arithmetic that can underflow to negative"],"tags":["type","varchar","illegal-argument"],"backgroundTag":"invalid-argument-value","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"}