prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

Bits specified in bit_count must be between 2 and 64, got ${bits}

What it means

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.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/BitwiseFunctions.java:45

    private static final long TINYINT_MASK = 0b1111_1111L;
    private static final long TINYINT_SIGNED_BIT = 0b1000_0000L;
    private static final long SMALLINT_MASK = 0b1111_1111_1111_1111L;
    private static final long SMALLINT_SIGNED_BIT = 0b1000_0000_0000_0000L;
    private static final long INTEGER_MASK = 0x00_00_00_00_ff_ff_ff_ffL;
    private static final long INTEGER_SIGNED_BIT = 0x00_00_00_00_00_80_00_00_00L;

    private BitwiseFunctions() {}

    @Description("count number of set bits in 2's complement representation")
    @ScalarFunction
    @SqlType(StandardTypes.BIGINT)
    public static long bitCount(@SqlType(StandardTypes.BIGINT) long num, @SqlType(StandardTypes.BIGINT) long bits)
    {
        if (bits == MAX_BITS) {
            return Long.bitCount(num);
        }
        if (bits <= 1 || bits > MAX_BITS) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bits specified in bit_count must be between 2 and 64, got " + bits);
        }
        long lowBitsMask = (1L << (bits - 1)) - 1; // set the least (bits - 1) bits
        if (num > lowBitsMask || num < ~lowBitsMask) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Number must be representable with the bits specified. " + num + " can not be represented with " + bits + " bits");
        }
        long mask = (1L << bits) - 1;
        return Long.bitCount(num & mask);
    }

    @Description("bitwise NOT in 2's complement arithmetic")
    @ScalarFunction
    @SqlType(StandardTypes.BIGINT)
    public static long bitwiseNot(@SqlType(StandardTypes.BIGINT) long num)
    {
        return ~num;
    }

    @Description("bitwise AND in 2's complement arithmetic")

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Pass bits in the range 2..64, e.g. bit_count(num, 64) or bit_count(num, 8)
  2. Clamp the parameter: bit_count(num, greatest(2, least(64, bits)))
  3. Use bit_count(num) style single-arg equivalent or Long.bitCount semantics via bits=64
  4. Coalesce invalid widths to 64: bit_count(num, COALESCE(NULLIF(bits, 0), 64))

Example fix

// before
SELECT bit_count(x, 128);
// after
SELECT bit_count(x, 64);
Defensive patterns

Strategy: validation

Validate before calling

SELECT CASE WHEN bits BETWEEN 2 AND 64 THEN bit_count(num, bits) ELSE NULL END FROM t;

Type guard

CASE WHEN bits >= 2 AND bits <= 64 THEN bits END -- guard the width param before the call

Try / catch

try(bit_count(num, bits)) -- NULL on invalid width; better: clamp with greatest(2, least(64, bits))

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/f86b86128ea12eab. Report an issue: GitHub.