Issue Report: Runtime ClassCastException with accumulate max() on BigDecimal - Should Fail at Compile Time

2 views
Skip to first unread message

Geeta Dhamak

unread,
Dec 1, 2025, 2:04:47 AM (2 days ago) Dec 1
to Drools Usage
## Issue Report: Runtime ClassCastException with `accumulate max()` on BigDecimal - Should Fail at Compile Time

**Drools Version:** 10.0.0  
**JDK Version:** Java 21

### Problem Description

When using `accumulate` with `max()` function on a `BigDecimal` field, if the result binding type is incorrectly declared as `Double()`, the rule **compiles successfully** but **fails at runtime** with a `ClassCastException`.

### Expected Behavior
The rule compilation should fail with a type mismatch error, preventing runtime exceptions.

### Actual Behavior
- ✅ Rule compiles without errors
- ❌ Runtime failure: `java.lang.ClassCastException: class java.math.BigDecimal cannot be cast to class java.lang.Double`

### Reproducible Example : attached in discussion
Java class: TestRule.java
Sample drl : mySample.txt
Sample dom : Item.java
error logs : errorLog.txt

**Runtime Error:**
```
java.lang.ClassCastException: class java.math.BigDecimal cannot be cast to class java.lang.Double
```

### Root Cause
The `max()` accumulate function returns the same type as the input field (`BigDecimal`), but the binding declares `Double()`. The compiler does not catch this type mismatch.

### Workaround
Change the binding type to match the actual return type:
```drl
$maxSeq: BigDecimal()  // Correct
// OR
$maxSeq: Number()      // Also works (parent type)
```

### Question
Should the Drools compiler detect this type mismatch and fail at compile time rather than allowing a runtime exception? This would improve type safety and developer experience.

TestRule.java
errorLog.txt
Item.java
mySample.txt
Reply all
Reply to author
Forward
0 new messages