Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e385ddf
Initial Work
cgivre Oct 1, 2025
3357161
Fixed VARDECIMAL unit tests
cgivre Oct 1, 2025
9bdfdec
Fixed rounding errors
cgivre Oct 1, 2025
c2d0b0f
Fixed null timestamp issues
cgivre Oct 1, 2025
5b2d5b6
More test fixes
cgivre Oct 1, 2025
8d2d624
WIP
cgivre Oct 1, 2025
9b5d931
Fixed even more unit tests
cgivre Oct 3, 2025
6e963c3
Fixed conversion issues
cgivre Oct 3, 2025
4144098
Fixed flatten function parameters
cgivre Oct 3, 2025
6dcc761
Fixed COUNT(*) issues
cgivre Oct 3, 2025
00424a7
Fixed tests... again
cgivre Oct 5, 2025
7eaa271
Getting there...slowly but surely
cgivre Oct 5, 2025
2e023cf
Various fixes...hopefully the last
cgivre Oct 5, 2025
5d8d53d
Cleanup
cgivre Oct 5, 2025
c0c152b
Fingers crossed
cgivre Oct 6, 2025
51434f8
Could be...
cgivre Oct 6, 2025
8552965
Fix errors
cgivre Oct 6, 2025
d472bfa
Java-Exec Now Passing. Fixed precision errors in JDBC plugin
cgivre Oct 6, 2025
4471651
Fixed One more JDBC Unit Test
cgivre Oct 6, 2025
3f0ceed
Fix ES TestS
cgivre Oct 6, 2025
768a70b
Fixed ES and Phoenix Aggregate Tests
cgivre Oct 9, 2025
7a2560d
Bump Calcite to version 1.36
cgivre Oct 13, 2025
b0be4cc
Fixed JDBC Test Error
cgivre Oct 13, 2025
a00dbe0
Bump to Calcite 1.37
cgivre Oct 13, 2025
fb3b39e
Fix scalar subquery detection for INTERSECT/UNION queries in Calcite …
cgivre Oct 13, 2025
4e5514d
Various fixes
cgivre Oct 16, 2025
bdb59a2
WIP
cgivre Oct 16, 2025
98fdb65
Fixed large IN clause test
cgivre Oct 17, 2025
73f6178
Fixed Additional Unit Tests
cgivre Oct 17, 2025
1c3c5e3
Fixed JDBC Precision Tests
cgivre Oct 17, 2025
90bc193
Bump to 1.38
cgivre Oct 17, 2025
cc6b82a
WIP
cgivre Oct 20, 2025
17678ff
WIP: DECIMAL issues fixed
cgivre Oct 20, 2025
c7e8b55
Fix checkstyle
cgivre Oct 20, 2025
00399ec
WIP
cgivre Oct 24, 2025
4747f7b
Fix long running unit tests
cgivre Oct 24, 2025
aec0436
Fixed unit tests
cgivre Oct 26, 2025
423abf1
Fixed Tets and Added EXCLUDE support
cgivre Oct 26, 2025
ebd5f5a
Fixed infinite loop
cgivre Oct 27, 2025
0e0f243
Removed calls to deprecated methods
cgivre Oct 27, 2025
cd7de48
Fix more unit tests
cgivre Oct 27, 2025
c471175
Various fixeS
cgivre Oct 27, 2025
6cf17d5
Fixed additional unit tests
cgivre Oct 27, 2025
9fb0064
Fixed checkstyle
cgivre Oct 27, 2025
fc406e1
Fix more unit tests
cgivre Oct 27, 2025
48292aa
Fixed additional decimal tests
cgivre Oct 28, 2025
71176e8
Fixed one more test
cgivre Oct 28, 2025
0ed4de2
Fixed error message
cgivre Oct 28, 2025
2e344dd
Fixed Clickhouse tests
cgivre Oct 28, 2025
0cbe4c8
Fixed Other Clickhouse test
cgivre Oct 28, 2025
b77ad2b
Fix unit test
cgivre Aug 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed Tets and Added EXCLUDE support
  • Loading branch information
cgivre committed Aug 7, 2026
commit 423abf1695753ec785fd0381f5ed420ceb89cf8c
146 changes: 146 additions & 0 deletions docs/dev/WindowFunctionExcludeClause.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# EXCLUDE Clause in Apache Drill Window Functions

## Overview

The EXCLUDE clause allows you to exclude specific rows from window frame calculations. This feature is part of the SQL standard and was added in Calcite 1.38.

## Syntax

```sql
SELECT column_name,
aggregate_function(...) OVER (
[PARTITION BY ...]
[ORDER BY ...]
[frame_clause]
EXCLUDE exclusion_type
)
FROM table_name;
```

## Exclusion Types

### EXCLUDE NO OTHERS (default)

No rows are excluded from the frame. This is the default behavior when no EXCLUDE clause is specified.

```sql
SELECT n_regionkey,
COUNT(*) OVER (
PARTITION BY n_regionkey
ORDER BY n_regionkey
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
EXCLUDE NO OTHERS
) AS total_count
FROM cp.`tpch/nation.parquet`;
```

### EXCLUDE CURRENT ROW

Excludes only the current row from the frame calculation.

```sql
SELECT n_regionkey,
COUNT(*) OVER (
PARTITION BY n_regionkey
ORDER BY n_regionkey
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
EXCLUDE CURRENT ROW
) AS other_count
FROM cp.`tpch/nation.parquet`;
```

### EXCLUDE TIES

Excludes peer rows (rows with the same ORDER BY values) but keeps the current row.

```sql
SELECT n_regionkey,
COUNT(*) OVER (
PARTITION BY n_regionkey
ORDER BY n_regionkey
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
EXCLUDE TIES
) AS self_only_count
FROM cp.`tpch/nation.parquet`;
```

### EXCLUDE GROUP

Excludes the current row and all its peer rows from the frame calculation.

```sql
SELECT n_regionkey,
COUNT(*) OVER (
PARTITION BY n_regionkey
ORDER BY n_regionkey
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
EXCLUDE GROUP
) AS exclude_peers_count
FROM cp.`tpch/nation.parquet`;
```

## Supported Frame Types

The EXCLUDE clause is currently supported with:

- **RANGE frames**: Full support for all exclusion types
- **ROWS frames**: Supported for EXCLUDE NO OTHERS; other modes have limitations

## Common Use Cases

### Calculate differences from group average

```sql
SELECT employee_id, salary,
AVG(salary) OVER (
PARTITION BY department_id
EXCLUDE CURRENT ROW
) AS avg_other_salaries,
salary - AVG(salary) OVER (
PARTITION BY department_id
EXCLUDE CURRENT ROW
) AS diff_from_others
FROM employees;
```

### Count peer rows

```sql
SELECT order_date, order_id,
COUNT(*) OVER (
ORDER BY order_date
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
EXCLUDE CURRENT ROW
) AS other_orders_same_date
FROM orders;
```

### Running totals excluding peers

```sql
SELECT transaction_date, amount,
SUM(amount) OVER (
ORDER BY transaction_date
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
EXCLUDE TIES
) AS running_total_unique
FROM transactions;
```

## Notes

- When using EXCLUDE with RANGE frames, peer rows are determined by the ORDER BY clause. Rows with identical ORDER BY values are considered peers.
- For ROWS frames, all rows in the same partition are potential peers if they share ORDER BY values.
- EXCLUDE NO OTHERS is the default and can be omitted.
- The EXCLUDE clause must appear after the frame specification (ROWS/RANGE clause).

## Limitations

- ROWS frames with EXCLUDE CURRENT ROW, EXCLUDE TIES, or EXCLUDE GROUP may have limitations when partitioning by one column and aggregating a d
- ifferent column.
- For best results with complex EXCLUDE operations, use RANGE frames.

## Related Documentation

- [Calcite Window Functions](https://calcite.apache.org/docs/reference.html#window-functions)
- [SQL Standard Window Functions](https://www.iso.org/standard/63556.html)
24 changes: 23 additions & 1 deletion exec/java-exec/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -2699,6 +2699,7 @@ SqlWindow WindowSpecification() :
final SqlNode lowerBound, upperBound;
final Span s, s1, s2;
final SqlLiteral allowPartial;
final SqlLiteral exclude;
}
{
<LPAREN> { s = span(); }
Expand Down Expand Up @@ -2735,6 +2736,27 @@ SqlWindow WindowSpecification() :
lowerBound = upperBound = null;
}
)
(
<EXCLUDE> { final Span s3 = span(); }
(
<CURRENT> <ROW> {
exclude = SqlWindow.createExcludeCurrentRow(s3.end(this));
}
|
<GROUP> {
exclude = SqlWindow.createExcludeGroup(s3.end(this));
}
|
<TIES> {
exclude = SqlWindow.createExcludeTies(s3.end(this));
}
|
<NO> <OTHERS> {
exclude = SqlWindow.createExcludeNoOthers(s3.end(this));
}
)
| { exclude = SqlWindow.createExcludeNoOthers(s.pos()); }
)
(
<ALLOW> { s2 = span(); } <PARTIAL> {
allowPartial = SqlLiteral.createBoolean(true, s2.end(this));
Expand All @@ -2748,7 +2770,7 @@ SqlWindow WindowSpecification() :
<RPAREN>
{
return SqlWindow.create(null, id, partitionList, orderList,
isRows, lowerBound, upperBound, allowPartial, s.end(this));
isRows, lowerBound, upperBound, allowPartial, exclude, s.end(this));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public PhysicalOperator visitWindow(final Window window, final Object value) thr
input = new Sort(input, ods, false);

return new WindowPOP(input, window.getWithins(), window.getAggregations(),
window.getOrderings(), false, null, null);
window.getOrderings(), false, null, null, WindowPOP.Exclusion.EXCLUDE_NO_OTHER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.apache.calcite.rex.RexWindowBound;
import org.apache.calcite.rex.RexWindowExclusion;
import org.apache.drill.common.logical.data.NamedExpression;
import org.apache.drill.common.logical.data.Order;
import org.apache.drill.exec.physical.base.AbstractSingle;
Expand All @@ -40,26 +41,29 @@ public class WindowPOP extends AbstractSingle {
private final boolean frameUnitsRows;
private final Bound start;
private final Bound end;
private final Exclusion exclude;

public WindowPOP(@JsonProperty("child") PhysicalOperator child,
@JsonProperty("within") List<NamedExpression> withins,
@JsonProperty("aggregations") List<NamedExpression> aggregations,
@JsonProperty("orderings") List<Order.Ordering> orderings,
@JsonProperty("frameUnitsRows") boolean frameUnitsRows,
@JsonProperty("start") Bound start,
@JsonProperty("end") Bound end) {
@JsonProperty("end") Bound end,
@JsonProperty("exclude") Exclusion exclude) {
super(child);
this.withins = withins;
this.aggregations = aggregations;
this.orderings = orderings;
this.frameUnitsRows = frameUnitsRows;
this.start = start;
this.end = end;
this.exclude = exclude != null ? exclude : Exclusion.EXCLUDE_NO_OTHER;
}

@Override
protected PhysicalOperator getNewWithChild(PhysicalOperator child) {
return new WindowPOP(child, withins, aggregations, orderings, frameUnitsRows, start, end);
return new WindowPOP(child, withins, aggregations, orderings, frameUnitsRows, start, end, exclude);
}

@Override
Expand Down Expand Up @@ -96,6 +100,10 @@ public boolean isFrameUnitsRows() {
return frameUnitsRows;
}

public Exclusion getExclude() {
return exclude;
}

@Override
public String toString() {
return "WindowPOP[withins=" + withins
Expand All @@ -104,6 +112,7 @@ public String toString() {
+ ", frameUnitsRows=" + frameUnitsRows
+ ", start=" + start
+ ", end=" + end
+ ", exclude=" + exclude
+ "]";
}

Expand Down Expand Up @@ -139,4 +148,37 @@ public String toString() {
public static Bound newBound(RexWindowBound windowBound) {
return new Bound(windowBound.isUnbounded(), windowBound.isCurrentRow() ? 0 : Long.MIN_VALUE); //TODO: Get offset to work
}

/**
* Window frame exclusion mode. Corresponds to Calcite's RexWindowExclusion.
* Determines which rows to exclude from the window frame during aggregation.
*/
public enum Exclusion {
/** Do not exclude any rows from the frame (default behavior) */
EXCLUDE_NO_OTHER,
/** Exclude the current row from the frame */
EXCLUDE_CURRENT_ROW,
/** Exclude the current row and its ordering peers from the frame */
EXCLUDE_GROUP,
/** Exclude all ordering peers of the current row, but not the current row itself */
EXCLUDE_TIES;

public static Exclusion fromCalciteExclusion(RexWindowExclusion calciteExclusion) {
if (calciteExclusion == null) {
return EXCLUDE_NO_OTHER;
}
switch (calciteExclusion) {
case EXCLUDE_NO_OTHER:
return EXCLUDE_NO_OTHER;
case EXCLUDE_CURRENT_ROW:
return EXCLUDE_CURRENT_ROW;
case EXCLUDE_GROUP:
return EXCLUDE_GROUP;
case EXCLUDE_TIES:
return EXCLUDE_TIES;
default:
return EXCLUDE_NO_OTHER;
}
}
}
}
Loading