Skip to content
Merged
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 @@ -504,11 +504,9 @@ private Plan processWithSortHavingProject(Plan plan, Optional<Scope> outerScope)
}
result = oldSort.get().withOrderKeysAndChild(newOrderKeys, result);
}
if (!hasAggregateFunc.get()) {
// handle for miss slots case, add a top project
result = new LogicalProject<>(ImmutableList.copyOf(oldProject.getOutput()), result);
}
return result;
// The outputs appended for HAVING and ORDER BY are implementation details. Restore the
// original projection contract after those operators have consumed their helper slots.
return new LogicalProject<>(ImmutableList.copyOf(oldProject.getOutput()), result);
}

private void collectNotExistsSlotAndAggFunc(Expression expression, Set<Slot> oldProjectSlots,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@ public void testHavingAggregateFunction() {
).when(FieldChecker.check("projects", Lists.newArrayList(a1.toSlot()))));
}

@Test
void testHavingAggregateFunctionDoesNotLeakHelperOutput() {
Plan plan = PlanChecker.from(connectContext)
.analyze("SELECT 1 FROM t1 HAVING SUM(a1) > 0")
.getPlan();
Assertions.assertEquals(1, plan.getOutput().size());

PlanChecker.from(connectContext)
.analyze("SELECT (SELECT 1 FROM t1 HAVING SUM(a1) > 0)");

ExceptionChecker.expectThrowsWithMsg(
AnalysisException.class,
"Multiple columns returned by subquery are not yet supported. Found 2",
() -> PlanChecker.from(connectContext).analyze(
"SELECT (SELECT 1, 2 FROM t1 HAVING SUM(a1) > 0)"
));
}

@Test
void testJoinWithHaving() {
String sql = "SELECT a1, sum(a2) FROM t1, t2 WHERE t1.pk = t2.pk GROUP BY a1 HAVING a1 > sum(b1)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,31 +492,36 @@ void testAggFunctionNullabe2() {
.analyze("select 1 from t1 having sum(id) > 10")
.matchesFromRoot(
logicalResultSink(
logicalFilter(
logicalProject(
logicalProject(
logicalFilter(
logicalProject(
logicalAggregate().when(agg -> {
List<Slot> output = agg.getOutput();
checkExprsToSql(output, "sum(id)");
Assertions.assertTrue(output.get(0).nullable());
logicalProject(
logicalAggregate().when(agg -> {
List<Slot> output = agg.getOutput();
checkExprsToSql(output, "sum(id)");
Assertions.assertTrue(output.get(0).nullable());
return true;
})
).when(project -> {
List<NamedExpression> projects = project.getProjects();
checkExprsToSql(projects, "sum(id)");
Assertions.assertTrue(projects.get(0).nullable());
return true;
})
).when(project -> {
List<NamedExpression> projects = project.getProjects();
checkExprsToSql(projects, "sum(id)");
Assertions.assertTrue(projects.get(0).nullable());
checkExprsToSql(projects, "1 AS `1`", "sum(id)");
Assertions.assertTrue(projects.get(1).nullable());
return true;
})
).when(project -> {
List<NamedExpression> projects = project.getProjects();
checkExprsToSql(projects, "1 AS `1`", "sum(id)");
Assertions.assertTrue(projects.get(1).nullable());
).when(filter -> {
List<Expression> conjuncts = filter.getExpressions();
checkExprsToSql(conjuncts, "(sum(id) > 10)");
Assertions.assertTrue(conjuncts.get(0).child(0).nullable());
return true;
})
).when(filter -> {
List<Expression> conjuncts = filter.getExpressions();
checkExprsToSql(conjuncts, "(sum(id) > 10)");
Assertions.assertTrue(conjuncts.get(0).child(0).nullable());
).when(project -> {
checkExprsToSql(project.getProjects(), "1");
return true;
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
-- !having_array_lambda_local_slots --
1 2

-- !scalar_subquery_having_true --
1

-- !scalar_subquery_having_false --

Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,19 @@ suite("test_having_project") {
sql "SELECT 1 AS c1 FROM t HAVING count(1) > 0 OR c1 IS NOT NULL"
exception "HAVING expression 'c1' must appear in the GROUP BY clause or be used in an aggregate function"
}

sql "INSERT INTO t VALUES (1)"

qt_scalar_subquery_having_true """
SELECT (SELECT 1 FROM t HAVING SUM(id) > 0) AS scalar_value
"""

qt_scalar_subquery_having_false """
SELECT (SELECT 1 FROM t HAVING SUM(id) < 0) AS scalar_value
"""

test {
sql "SELECT (SELECT 1, 2 FROM t HAVING SUM(id) > 0)"
exception "Multiple columns returned by subquery are not yet supported. Found 2"
}
}
Loading