Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix partial aggregation #10

Closed
Closed
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
9 changes: 8 additions & 1 deletion velox/core/QueryCtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class QueryCtx : public Context {
}

uint64_t maxPartialAggregationMemoryUsage() const {
return 1L << 24; // 16MB
return get<uint64_t>(
kMaxPartialAggregationMemory, kMaxPartialAggregationMemoryDefault);
}

uint64_t maxPartitionedOutputBufferSize() const {
Expand Down Expand Up @@ -195,6 +196,9 @@ class QueryCtx : public Context {
static constexpr const char* kMaxLocalExchangeBufferSize =
"max_local_exchange_buffer_size";

static constexpr const char* kMaxPartialAggregationMemory =
"max_partial_aggregation_memory";

// Overrides the previous configuration. Note that this function is NOT
// thread-safe and should probably only be used in tests.
void setConfigOverridesUnsafe(
Expand Down Expand Up @@ -222,6 +226,9 @@ class QueryCtx : public Context {

static constexpr uint64_t kMaxLocalExchangeBufferSizeDefault = 32UL << 20;

// 16MB
static constexpr uint64_t kMaxPartialAggregationMemoryDefault = 1L << 24;

CancelPoolPtr cancelPool_;
std::unique_ptr<memory::MemoryPool> pool_;
memory::MappedMemory* mappedMemory_;
Expand Down
5 changes: 5 additions & 0 deletions velox/exec/HashAggregation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ RowVectorPtr HashAggregation::getOutput() {
// Drop reference to input_ to make it singly-referenced at the producer and
// allow for memory reuse.
input_ = nullptr;

if (partialFull_) {
groupingSet_->resetPartial();
partialFull_ = false;
}
return output;
}

Expand Down
40 changes: 40 additions & 0 deletions velox/exec/tests/AggregationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,45 @@ TEST_F(AggregationTest, allKeyTypes) {
" GROUP BY c0, C1, C2, c3, C4, C5");
}

TEST_F(AggregationTest, partialAggregationMemoryLimit) {
auto vectors = {
makeRowVector({makeFlatVector<int32_t>(
100, [](auto row) { return row; }, nullEvery(5))}),
makeRowVector({makeFlatVector<int32_t>(
110, [](auto row) { return row + 29; }, nullEvery(7))}),
makeRowVector({makeFlatVector<int32_t>(
90, [](auto row) { return row - 71; }, nullEvery(7))}),
};

createDuckDbTable(vectors);

// Set an artificially low limit on the amount of data to accumulate in
// the partial aggregation.
CursorParameters params;
params.queryCtx = core::QueryCtx::create();

params.queryCtx->setConfigOverridesUnsafe({
{core::QueryCtx::kMaxPartialAggregationMemory, "100"},
});

// Distinct aggregation.
params.planNode = PlanBuilder()
.values(vectors)
.partialAggregation({0}, {})
.finalAggregation({0}, {})
.planNode();

assertQuery(params, "SELECT distinct c0 FROM tmp");

// Count aggregation.
params.planNode = PlanBuilder()
.values(vectors)
.partialAggregation({0}, {"count(1)"})
.finalAggregation({0}, {"sum(a0)"})
.planNode();

assertQuery(params, "SELECT c0, count(1) FROM tmp GROUP BY 1");
}

} // namespace
} // namespace facebook::velox::exec::test
7 changes: 7 additions & 0 deletions velox/exec/tests/OperatorTestBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class OperatorTestBase : public testing::Test {
return assertQuery(plan, splits, duckDbSql, sortingKeys);
}

std::shared_ptr<Task> assertQuery(
const CursorParameters& params,
const std::string& duckDbSql) {
return test::assertQuery(
params, [&](exec::Task* /*task*/) {}, duckDbSql, duckDbQueryRunner_);
}

std::shared_ptr<Task> assertQuery(
const std::shared_ptr<const core::PlanNode>& plan,
const std::string& duckDbSql) {
Expand Down
2 changes: 1 addition & 1 deletion velox/exec/tests/QueryAssertions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ std::shared_ptr<Task> assertQuery(
}

std::shared_ptr<Task> assertQuery(
CursorParameters& params,
const CursorParameters& params,
std::function<void(exec::Task*)> addSplits,
const std::string& duckDbSql,
DuckDbQueryRunner& duckDbQueryRunner,
Expand Down
2 changes: 1 addition & 1 deletion velox/exec/tests/QueryAssertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ std::shared_ptr<Task> assertQuery(
std::optional<std::vector<uint32_t>> sortingKeys = std::nullopt);

std::shared_ptr<Task> assertQuery(
CursorParameters& params,
const CursorParameters& params,
std::function<void(exec::Task*)> addSplits,
const std::string& duckDbSql,
DuckDbQueryRunner& duckDbQueryRunner,
Expand Down