elastic/elasticsearch · error · ClassCastException

Cannot apply [|] operation to type [float]

Error message

Cannot apply [|] operation to type [float]

What it means

Thrown by DefMath.or(float, float) which unconditionally throws. When both operands are statically typed float, binary promotion yields float.class and the compiler dispatches to this overload. Bitwise OR is only defined for integral and boolean types; the float overload exists for method-table completeness but always raises this exception.

Source

Thrown at modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java:963

        if (left instanceof Boolean && right instanceof Boolean) {
            return (boolean) left ^ (boolean) right;
        } else if (left instanceof Long || right instanceof Long) {
            return longIntegralValue(left) ^ longIntegralValue(right);
        } else {
            return intIntegralValue(left) ^ intIntegralValue(right);
        }
    }

    private static int or(int a, int b) {
        return a | b;
    }

    private static long or(long a, long b) {
        return a | b;
    }

    private static float or(float a, float b) {
        throw new ClassCastException("Cannot apply [|] operation to type [float]");
    }

    private static double or(double a, double b) {
        throw new ClassCastException("Cannot apply [|] operation to type [float]");
    }

    private static boolean or(boolean a, boolean b) {
        return a | b;
    }

    private static Object or(Object left, Object right) {
        if (left instanceof Boolean && right instanceof Boolean) {
            return (boolean) left | (boolean) right;
        } else if (left instanceof Long || right instanceof Long) {
            return longIntegralValue(left) | longIntegralValue(right);
        } else {
            return intIntegralValue(left) | intIntegralValue(right);
        }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Cast both float operands to int before applying |: int ia = (int) a; int ib = (int) b; int result = ia | ib;
  2. Change the field mapping to integer if OR-mask logic is the intended use case
  3. Use boolean conditional logic (a || b) if float values represent truthy conditions

Example fix

// before
float a = doc['perm1'].value;
float b = doc['perm2'].value;
float combined = a | b;

// after
int ia = (int) doc['perm1'].value;
int ib = (int) doc['perm2'].value;
int combined = ia | ib;
Defensive patterns

Strategy: type-guard

Validate before calling

// Before bitwise OR on float, cast to int
float a = doc['x'].value;
float b = doc['y'].value;
int ia = (int) a;
int ib = (int) b;
return ia | ib;

Type guard

// Check that the value is not floating-point before OR
boolean isIntegralType(Object v) {
  return v instanceof Integer || v instanceof Long || 
         v instanceof Short || v instanceof Byte;
}

Prevention

When it happens

Trigger: A Painless script applies the | operator to two operands both statically typed as float. The promote() method returns float.class for (float, float), selecting the or(float, float) overload.

Common situations: Bitwise OR mask on float-typed fields; float variables used in a flag-combination expression; refactoring integer OR code to operate on float fields; float-mapped runtime field used in permission/flag merging.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/7962eb7fd6e2a9c5. Report an issue: GitHub.