Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4474,6 +4474,21 @@ protected void validateWhereOrOn(
SqlNode condition,
String clause) {
validateNoAggs(aggOrOverOrGroupFinder, condition, clause);
// SqlSelect need to expand alias
condition.accept(new SqlShuttle() {
@Override public @Nullable SqlNode visit(SqlCall call) {
call.getOperandList()
.stream()
.filter(Objects::nonNull)
.forEach(node -> node.accept(this));
if (call.getKind() == SqlKind.SELECT) {
SqlSelect select = (SqlSelect) call;
validateHavingClause(select);
return select;
}
return call;
}
});
inferUnknownTypes(
booleanType,
scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,28 @@ public static void checkActualAndReferenceFiles() {
.withConformance(SqlConformanceEnum.LENIENT).ok();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-5486">[CALCITE-5486]
* SubQuery not support HAVING alias in where condition</a>. */
@Test void testHavingAliasInSubQuery1() {
final String sql = "select * from emp where sal >\n"
+ " (select avg(sal) as s"
+ " from emp having s > 0"
+ " )";
sql(sql).withConformance(SqlConformanceEnum.LENIENT).ok();
}

@Test void testHavingAliasInSubQuery2() {
final String sql = "select * from emp e "
+ "left join dept d "
+ "on e.deptno = d.deptno "
+ "and sal >\n"
+ " (select avg(sal) as s"
+ " from emp having s > 0"
+ " )";
sql(sql).withConformance(SqlConformanceEnum.LENIENT).ok();
}

@Test void testAliasInHaving() {
sql("select count(empno) as e from emp having e > 1")
.withConformance(SqlConformanceEnum.LENIENT).ok();
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5709,6 +5709,19 @@ public boolean isBangEqualAllowed() {
.withConformance(lenient).ok();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-5486">[CALCITE-5486]
* SubQuery not support HAVING alias in where condition</a>. */
@Test void testHavingAliasInCondition() {
sql("select * from emp where sal >\n"
+ " (select avg(sal) as s"
+ " from emp having ^s^ > 0"
+ " )")
.fails("Column 'S' not found in any table")
.withConformance(SqlConformanceEnum.LENIENT)
.ok();
}

/**
* Tests validation of the ORDER BY clause when DISTINCT is present.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,64 @@ LogicalProject(DEPTNO=[$0])
LogicalAggregate(group=[{0}], agg#0=[SUM($1)], agg#1=[SUM($2)])
LogicalProject(DEPTNO=[$7], $f1=[CASE(SEARCH($7, Sarg[1, 2]), 0, 1)], $f2=[CASE(SEARCH($7, Sarg[3, 4]), 0, 1)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testHavingAliasInSubQuery">
<Resource name="sql">
<![CDATA[select * from emp where sal >
(select avg(sal) as s from emp having s > 0 )]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
LogicalFilter(condition=[>($5, $9)])
LogicalJoin(condition=[true], joinType=[left])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
LogicalFilter(condition=[>($0, 0)])
LogicalAggregate(group=[{}], S=[AVG($0)])
LogicalProject(SAL=[$5])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testHavingAliasInSubQuery1">
<Resource name="sql">
<![CDATA[select * from emp where sal >
(select avg(sal) as s from emp having s > 0 )]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
LogicalFilter(condition=[>($5, $9)])
LogicalJoin(condition=[true], joinType=[left])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
LogicalFilter(condition=[>($0, 0)])
LogicalAggregate(group=[{}], S=[AVG($0)])
LogicalProject(SAL=[$5])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testHavingAliasInSubQuery2">
<Resource name="sql">
<![CDATA[select * from emp e left join dept d on e.deptno = d.deptno and sal >
(select avg(sal) as s from emp having s > 0 )]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], DEPTNO0=[$9], NAME=[$10])
LogicalJoin(condition=[AND(=($7, $9), >($5, $0))], joinType=[left])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalJoin(condition=[true], joinType=[left])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
LogicalAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
LogicalFilter(condition=[>($0, 0)])
LogicalAggregate(group=[{}], S=[AVG($0)])
LogicalProject(SAL=[$5])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down