# Failed to execute the sum function when has window function

**URL:** <https://community.dremio.com/t/failed-to-execute-the-sum-function-when-has-window-function/13058>\
**Category:** Dremio University\
**Created:** [June 18, 2025, 1:46am UTC](https://community.dremio.com/t/failed-to-execute-the-sum-function-when-has-window-function/13058 "2025-06-18T01:46:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@chen](https://community.dremio.com/u/chen)\
**Post date:** [June 18, 2025, 1:46am UTC](https://community.dremio.com/t/failed-to-execute-the-sum-function-when-has-window-function/13058/1 "2025-06-18T01:46:00Z")

</div>

The dremio version I’m using is 25.1.1 and I have an error executing an SQL query as per the documentation.The document I refer to is: [SUM | Dremio Documentation](https://docs.dremio.com/25.x/reference/sql/sql-functions/functions/SUM/)  
This is my SQL:

```sql
SELECT name1,date_a,id,SUM(id) OVER (ORDER BY name1 ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING) from mytable;

```

The error is:

```auto
While invoking method 'public com.dremio.common.rel2sql.DremioRelToSqlConverter$Result com.dremio.common.rel2sql.DremioRelToSqlConverter.visit(org.apache.calcite.rel.core.Project)'

```

debug:

```auto
Caused by: java.lang.reflect.InvocationTargetException: null
	at jdk.internal.reflect.GeneratedMethodAccessor398.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.apache.calcite.util.ReflectUtil$2.invoke(ReflectUtil.java:524)
	... 49 common frames omitted
Caused by: java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:374)
	at java.base/java.util.ArrayList.get(ArrayList.java:459)
	at org.apache.calcite.sql.SqlNodeList.get(SqlNodeList.java:107)
	at com.dremio.exec.store.jdbc.rel2sql.JdbcDremioRelToSqlConverter$JdbcDremioSelectListContext.field(JdbcDremioRelToSqlConverter.java:740)
	at com.dremio.common.rel2sql.SqlImplementor$Context.toSql(SqlImplementor.java:461)

```

Through debugging, I found that there were only 3 selectLists and did not contain the sum field,So the index is out of bounds.  
But I don’t know how to solve this problem. Please help me. Thank you.

---

<div class="post-metadata">

**Author:** ![balaji.ramaswamy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/balaji.ramaswamy/32/843_2.png) [@balaji.ramaswamy](https://community.dremio.com/u/balaji.ramaswamy)\
**Post date:** [June 18, 2025, 3:53am UTC](https://community.dremio.com/t/failed-to-execute-the-sum-function-when-has-window-function/13058/2 "2025-06-18T03:53:27Z")

</div>

@chen Can you please clarify what “3 selectLists and did not contain the sum field,” means? Is there a chance you can filter out those rows in a view and then apply the Window function on the view?

---

<div class="post-metadata">

**Author:** ![chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@chen](https://community.dremio.com/u/chen)\
**Post date:** [June 18, 2025, 4:50am UTC](https://community.dremio.com/t/failed-to-execute-the-sum-function-when-has-window-function/13058/3 "2025-06-18T04:50:16Z")

</div>

@balaji.ramaswamy

 ![image](https://us1.discourse-cdn.com/flex020/uploads/dremio/original/2X/d/d7d5c4a633e7a3e8f90c38ab57539979c18ef0f9.png)

The cause of the error is the index out-of-bounds exception. I debug it because in’com.dremio.common.rel2sql.SqlImplementor.Context#toSql(org.apache.calcite.rex.RexProgram, org.apache.calcite.rex.RexNode)',

 ![image](https://us1.discourse-cdn.com/flex020/uploads/dremio/original/2X/2/22c74617d438a119a3a56086c98df301db2ead6c.png)  
There are only three select fields.but `((RexInputRef) rex).getIndex()` is 3, That’s why it’s wrong.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/dremio/original/2X/6/6270345468b998aa3af56236aea469481a6ce0cb.png)

I use the example in the document, and the same error is reported.
