{"record":{"id":"b002f34812fcfc08","repo":"elastic/elasticsearch","slug":"cannot-convert-to-an-integral-value","errorCode":null,"errorMessage":"Cannot convert [{}] to an integral value.","messagePattern":"Cannot convert \\[(.+?)\\] to an integral value\\.","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":878,"sourceCode":"                + left.getClass().getCanonicalName()\n                + \"] and [\"\n                + right.getClass().getCanonicalName()\n                + \"].\"\n        );\n    }\n\n    // helper methods to convert an integral according to numeric promotion\n    // this is used by the generic code for bitwise and shift operators\n\n    private static long longIntegralValue(Object o) {\n        if (o instanceof Long) {\n            return (long) o;\n        } else if (o instanceof Integer || o instanceof Short || o instanceof Byte) {\n            return ((Number) o).longValue();\n        } else if (o instanceof Character) {\n            return (char) o;\n        } else {\n            throw new ClassCastException(\"Cannot convert [\" + o.getClass().getCanonicalName() + \"] to an integral value.\");\n        }\n    }\n\n    private static int intIntegralValue(Object o) {\n        if (o instanceof Integer || o instanceof Short || o instanceof Byte) {\n            return ((Number) o).intValue();\n        } else if (o instanceof Character) {\n            return (char) o;\n        } else {\n            throw new ClassCastException(\"Cannot convert [\" + o.getClass().getCanonicalName() + \"] to an integral value.\");\n        }\n    }\n\n    // bitwise operators: valid only for integral types\n\n    private static int and(int a, int b) {\n        return a & b;\n    }","sourceCodeStart":860,"sourceCodeEnd":896,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L860-L896","documentation":"Thrown by DefMath.longIntegralValue(Object) when the object is not one of Long, Integer, Short, Byte, or Character. This helper is called by the Object overload of bitwise operators (&, ^, |) when at least one operand is a Long, requiring both operands to be converted to long. Float, Double, Boolean, String, and other types are rejected because bitwise operations are only defined for integral types in Java semantics.","triggerScenarios":"A Painless script applies a bitwise operator (&, ^, |) to two def-typed values where one operand evaluates to Long at runtime and the other evaluates to Float, Double, Boolean, String, or another non-integral type. The and/xor/or(Object, Object) overload enters the 'left instanceof Long || right instanceof Long' branch and calls longIntegralValue on both operands.","commonSituations":"def variable holding a float/double value used in a bitwise mask operation; mixing a Long bitmask constant with a floating-point def field; def variable from params that was passed as a Double from JSON; bitwise logic on a field that was dynamically typed as float in the mapping.","solutions":["Ensure both operands are integral types (int, long) before applying bitwise operators","Cast explicitly: longIntegralValue-compatible types only — use ((Number)x).longValue() after checking the type is not Float/Double","Use explicit long types instead of def for both operands","Separate the logic: extract integral values into typed variables before bitwise operations"],"exampleFix":"// before\ndef flags = doc['bitmask'].value;  // Long\ndef factor = params['factor'];      // Double\ndef result = flags & factor;\n\n// after\nlong flags = doc['bitmask'].value;\nlong factor = (long)(double) params['factor'];\nlong result = flags & factor;","handlingStrategy":"validation","validationCode":"// Before bitwise on def values, verify both are integral\ndef a = doc['mask'].value;\ndef b = params['bits'];\nif (a instanceof Long && b instanceof Long) {\n  return (long) a & (long) b;\n} else if ((a instanceof Integer || a instanceof Long) && \n           (b instanceof Integer || b instanceof Long)) {\n  return ((Number) a).intValue() & ((Number) b).intValue();\n}\nthrow new IllegalArgumentException('Bitwise operands must be integral');","typeGuard":"// Check if a def value is safe for long bitwise operations\nboolean isLongIntegral(def v) {\n  return v instanceof Long || v instanceof Integer || \n         v instanceof Short || v instanceof Byte ||\n         v instanceof Character;\n}","tryCatchPattern":null,"preventionTips":["Never apply bitwise operators to def variables holding Float or Double values","Use explicit long or int types for variables involved in bitwise operations","Verify that params passed from JSON are integers, not doubles (JSON numbers default to double in some parsers)","Cast floating-point values to long/int explicitly before bitwise operations"],"tags":["painless","elasticsearch","def","type-error","bitwise","runtime","integral-conversion"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}