{"record":{"id":"7962eb7fd6e2a9c5","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-float-7962eb","errorCode":null,"errorMessage":"Cannot apply [|] operation to type [float]","messagePattern":"Cannot apply \\[\\|\\] operation to type \\[float\\]","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":963,"sourceCode":"        if (left instanceof Boolean && right instanceof Boolean) {\n            return (boolean) left ^ (boolean) right;\n        } else if (left instanceof Long || right instanceof Long) {\n            return longIntegralValue(left) ^ longIntegralValue(right);\n        } else {\n            return intIntegralValue(left) ^ intIntegralValue(right);\n        }\n    }\n\n    private static int or(int a, int b) {\n        return a | b;\n    }\n\n    private static long or(long a, long b) {\n        return a | b;\n    }\n\n    private static float or(float a, float b) {\n        throw new ClassCastException(\"Cannot apply [|] operation to type [float]\");\n    }\n\n    private static double or(double a, double b) {\n        throw new ClassCastException(\"Cannot apply [|] operation to type [float]\");\n    }\n\n    private static boolean or(boolean a, boolean b) {\n        return a | b;\n    }\n\n    private static Object or(Object left, Object right) {\n        if (left instanceof Boolean && right instanceof Boolean) {\n            return (boolean) left | (boolean) right;\n        } else if (left instanceof Long || right instanceof Long) {\n            return longIntegralValue(left) | longIntegralValue(right);\n        } else {\n            return intIntegralValue(left) | intIntegralValue(right);\n        }","sourceCodeStart":945,"sourceCodeEnd":981,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L945-L981","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Cast both float operands to int before applying |: int ia = (int) a; int ib = (int) b; int result = ia | ib;","Change the field mapping to integer if OR-mask logic is the intended use case","Use boolean conditional logic (a || b) if float values represent truthy conditions"],"exampleFix":"// before\nfloat a = doc['perm1'].value;\nfloat b = doc['perm2'].value;\nfloat combined = a | b;\n\n// after\nint ia = (int) doc['perm1'].value;\nint ib = (int) doc['perm2'].value;\nint combined = ia | ib;","handlingStrategy":"type-guard","validationCode":"// Before bitwise OR on float, cast to int\nfloat a = doc['x'].value;\nfloat b = doc['y'].value;\nint ia = (int) a;\nint ib = (int) b;\nreturn ia | ib;","typeGuard":"// Check that the value is not floating-point before OR\nboolean isIntegralType(Object v) {\n  return v instanceof Integer || v instanceof Long || \n         v instanceof Short || v instanceof Byte;\n}","tryCatchPattern":null,"preventionTips":["Never apply bitwise OR to float-typed variables — cast to int first","Change field mapping to integer if OR-mask logic is needed","Audit scripts that use | on fields mapped as float","Use explicit int declarations for variables involved in OR-mask operations"],"tags":["painless","elasticsearch","float","type-error","bitwise","runtime","or"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T11:17:21.771Z"}