opensearch-project / opensearch-project/sql

Optimize type casting workaround in addtotals/addcoltotals aggregation logic

Open
#4,952 0 comments 0 reactions 1 assignee View on GitHub

@LantaoJin is already working on this.

Since Dec 12, 2025.

PPL
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:

  1. This is a Calcite bug that should be reported upstream
  2. There's a better way to handle type compatibility in UNION operations
  3. The type conversion can be handled more elegantly at a different layer

Context

Related Code

core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java:

  • getAggregateDataTypeFieldRef method (line 2529)
  • buildAddRowTotalAggregate method usage

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.