Skip to content

Commit

Permalink
Check for Aggregation inside a window clause when syntax used as - WI…
Browse files Browse the repository at this point in the history
…NDOW W AS DEF (apache#16801)
  • Loading branch information
sreemanamala authored Jul 26, 2024
1 parent 725d442 commit 9b76d13
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,22 @@ public void validateCall(SqlCall call, SqlValidatorScope scope)
super.validateCall(call, scope);
}

@Override
protected void validateWindowClause(SqlSelect select)
{
SqlNodeList windows = select.getWindowList();
for (SqlNode sqlNode : windows) {
if (SqlUtil.containsAgg(sqlNode)) {
throw buildCalciteContextException(
"Aggregation inside window is currently not supported with syntax WINDOW W AS <DEF>. "
+ "Try providing window definition directly without alias",
sqlNode
);
}
}
super.validateWindowClause(select);
}

@Override
protected SqlNode performUnconditionalRewrites(SqlNode node, final boolean underFrom)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.runtime.CalciteContextException;
import org.apache.druid.common.config.NullHandling;
import org.apache.druid.error.DruidException;
Expand Down Expand Up @@ -149,6 +150,7 @@
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
Expand Down Expand Up @@ -15592,6 +15594,23 @@ public void testDistinctNotSupportedWithWindow()
assertThat(e, invalidSqlContains("DISTINCT is not supported for window functions"));
}

@Test
public void testUnSupportedAggInSelectWindow()
{
assertEquals(
"1.37.0",
RelNode.class.getPackage().getImplementationVersion(),
"Calcite version changed; check if CALCITE-6500 is fixed and update:\n * method DruidSqlValidator#validateWindowClause"
);

DruidException e = assertThrows(DruidException.class, () -> testBuilder()
.queryContext(ImmutableMap.of(PlannerContext.CTX_ENABLE_WINDOW_FNS, true))
.sql("SELECT dim1, ROW_NUMBER() OVER W from druid.foo WINDOW W as (ORDER BY max(length(dim1)))")
.run());

assertThat(e, invalidSqlContains("not supported with syntax WINDOW W AS <DEF>"));
}

@Test
public void testInGroupByLimitOutGroupByOrderBy()
{
Expand Down

0 comments on commit 9b76d13

Please sign in to comment.