opensearch-project / opensearch-project/sql
Optimize type casting workaround in addtotals/addcoltotals aggregation logic
@LantaoJin is already working on this.
Since Dec 12, 2025.
- Dominant language
- Java
- Stars
- 176
- Forks
- 229
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 43
Description
Description
A workaround was implemented in PR #4754 to handle type casting issues when performing UNION operations between original data and aggregated totals in the addtotals/addcoltotals commands.
Current Implementation
The method getAggregateDataTypeFieldRef (line 2529 in CalciteRelNodeVisitor.java) explicitly casts:
- INTEGER → BIGINT
- FLOAT/REAL → DOUBLE
This workaround prevents ClassCastException that occurs during UNION operations when Calcite automatically converts aggregation result types.
Problem
When combining original data (with int/float types) and aggregated totals (Calcite converts to long/double), a ClassCastException occurs in multi-node scenarios:
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long
at org.apache.calcite.avatica.util.AbstractCursor$LongAccessor.getLong
at org.opensearch.sql.opensearch.util.JdbcOpenSearchDataTypeConvertor.getExprValueFromSqlType
This issue only manifests in CrossClusterSearchIT tests when multi-node results are combined.
Expected Optimization
Investigate whether:
- This is a Calcite bug that should be reported upstream
- There's a better way to handle type compatibility in UNION operations
- The type conversion can be handled more elegantly at a different layer
Context
- Introduced in: PR #4754
- Discussion: https://github.com/opensearch-project/sql/pull/4754#discussion_r2612169694
- Test case demonstrating the issue: https://github.com/asifabashar/sql/pull/8/files
Related Code
core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java:
getAggregateDataTypeFieldRefmethod (line 2529)buildAddRowTotalAggregatemethod usage
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.