vitessio/vitess · error

unexpected bit operation

Error message

unexpected bit operation

What it means

Panic in the runtime evaluation of bitwise expressions (`BitwiseExpr.eval`). The operator enum is expected to contain only opBitBinary and opBitShift variants; any other value reaches `default: panic("unexpected bit operation")`. It indicates a BitwiseExpr was constructed with an operator the evaluator does not know.

Source

Thrown at go/vt/vtgate/evalengine/expr_bit.go:243

		return newEvalUint64(op.numeric(uint64(lu.i), uint64(ru.i))), nil

	case opBitShift:
		/*
			The result type depends on whether the bit argument is evaluated as a binary string or number:
			Binary-string evaluation occurs when the bit argument has a binary string type, and is not a hexadecimal
			literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to an
			unsigned 64-bit integer as necessary.
		*/
		if l, ok := l.(*evalBytes); ok && l.isBinary() && !l.isHexOrBitLiteral() {
			ru := evalToInt64(r)
			return newEvalBinary(op.binary(l.bytes, uint64(ru.i))), nil
		}
		lu := evalToInt64(l)
		ru := evalToInt64(r)
		return newEvalUint64(op.numeric(uint64(lu.i), uint64(ru.i))), nil

	default:
		panic("unexpected bit operation")
	}
}

func (expr *BitwiseExpr) compileBinary(c *compiler, asm_ins_bb, asm_ins_uu func()) (ctype, error) {
	lt, err := expr.Left.compile(c)
	if err != nil {
		return ctype{}, err
	}

	skip1 := c.compileNullCheck1(lt)

	rt, err := expr.Right.compile(c)
	if err != nil {
		return ctype{}, err
	}

	skip2 := c.compileNullCheck1r(rt)

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Add the missing operator case to BitwiseExpr.eval with its binary/numeric implementation
  2. Ensure the parser cannot produce BitwiseExpr with unknown operators
  3. Return a vterrors internal error instead of panicking
  4. Upgrade to a Vitess release containing the missing operator support

Example fix

// before
default:
    panic("unexpected bit operation")
// after
default:
    return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected bit operation %v", bit.Op)
Defensive patterns

Strategy: type-guard

Validate before calling

switch bit.Op.(type) {
case opBitBinary, opBitShift:
    // ok
default:
    return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unsupported bit operator %v", bit.Op)
}

Type guard

func isKnownBitOp(op BitOp) bool {
    switch op.(type) {
    case opBitBinary, opBitShift:
        return true
    }
    return false
}

Try / catch

defer func() {
    if r := recover(); r != nil {
        err = vterrors.Errorf(vtrpcpb.Code_INTERNAL, "bitwise eval panic: %v", r)
    }
}()

Prevention

When it happens

Trigger: Evaluating a bitwise expression (&, |, ^, <<, >>) whose `bit.Op` is neither opBitBinary nor opBitShift — e.g. a new bitwise operator added to the parser but not to the evaluator's switch.

Common situations: Development-time issue after adding new bitwise operators or refactoring the op enum; on a released binary it would crash vtgate on a specific query.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/c359dfe219b4462b. Report an issue: GitHub.