{"record":{"id":"f86b86128ea12eab","repo":"prestodb/presto","slug":"invalid-function-argument-f86b86","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Bits specified in bit_count must be between 2 and 64, got ${bits}","messagePattern":"Bits specified in bit_count must be between 2 and 64, got (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/BitwiseFunctions.java","lineNumber":45,"sourceCode":"    private static final long TINYINT_MASK = 0b1111_1111L;\n    private static final long TINYINT_SIGNED_BIT = 0b1000_0000L;\n    private static final long SMALLINT_MASK = 0b1111_1111_1111_1111L;\n    private static final long SMALLINT_SIGNED_BIT = 0b1000_0000_0000_0000L;\n    private static final long INTEGER_MASK = 0x00_00_00_00_ff_ff_ff_ffL;\n    private static final long INTEGER_SIGNED_BIT = 0x00_00_00_00_00_80_00_00_00L;\n\n    private BitwiseFunctions() {}\n\n    @Description(\"count number of set bits in 2's complement representation\")\n    @ScalarFunction\n    @SqlType(StandardTypes.BIGINT)\n    public static long bitCount(@SqlType(StandardTypes.BIGINT) long num, @SqlType(StandardTypes.BIGINT) long bits)\n    {\n        if (bits == MAX_BITS) {\n            return Long.bitCount(num);\n        }\n        if (bits <= 1 || bits > MAX_BITS) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Bits specified in bit_count must be between 2 and 64, got \" + bits);\n        }\n        long lowBitsMask = (1L << (bits - 1)) - 1; // set the least (bits - 1) bits\n        if (num > lowBitsMask || num < ~lowBitsMask) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Number must be representable with the bits specified. \" + num + \" can not be represented with \" + bits + \" bits\");\n        }\n        long mask = (1L << bits) - 1;\n        return Long.bitCount(num & mask);\n    }\n\n    @Description(\"bitwise NOT in 2's complement arithmetic\")\n    @ScalarFunction\n    @SqlType(StandardTypes.BIGINT)\n    public static long bitwiseNot(@SqlType(StandardTypes.BIGINT) long num)\n    {\n        return ~num;\n    }\n\n    @Description(\"bitwise AND in 2's complement arithmetic\")","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/BitwiseFunctions.java#L27-L63","documentation":"bit_count(x, bits) counts set bits only when x fits in the given bit width. The bits parameter must be between 2 and 64 (64 handled by the fast path); values <= 1 or > 64 are rejected with INVALID_FUNCTION_ARGUMENT.","triggerScenarios":"Calling bit_count(num, bits) with bits <= 1 (e.g. 0 or 1) or bits > 64 (e.g. 128 from confusing it with byte widths).","commonSituations":"Passing a column of bit-widths where some rows are 0/NULL-derived; confusion between bits and bytes (8 vs 64); default parameter placeholders not filled in.","solutions":["Pass bits in the range 2..64, e.g. bit_count(num, 64) or bit_count(num, 8)","Clamp the parameter: bit_count(num, greatest(2, least(64, bits)))","Use bit_count(num) style single-arg equivalent or Long.bitCount semantics via bits=64","Coalesce invalid widths to 64: bit_count(num, COALESCE(NULLIF(bits, 0), 64))"],"exampleFix":"// before\nSELECT bit_count(x, 128);\n// after\nSELECT bit_count(x, 64);","handlingStrategy":"validation","validationCode":"SELECT CASE WHEN bits BETWEEN 2 AND 64 THEN bit_count(num, bits) ELSE NULL END FROM t;","typeGuard":"CASE WHEN bits >= 2 AND bits <= 64 THEN bits END -- guard the width param before the call","tryCatchPattern":"try(bit_count(num, bits)) -- NULL on invalid width; better: clamp with greatest(2, least(64, bits))","preventionTips":["Validate the bits parameter range (2..64) wherever it is user- or column-supplied","Don't confuse bytes with bits (64 not 8 for BIGINT)","Default to 64 for full-width popcount","Add CHECK constraints or ETL validation on width columns"],"tags":["presto","sql","bitwise","bit-count"],"backgroundTag":"bit-width-out-of-range","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"}