Skip to content

Commit bfff504

Browse files
jiangtianjiangtian
authored andcommitted
add unit test
1 parent 7ecefee commit bfff504

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

velox/exec/Window.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ void Window::callApplyForPartitionRows(
642642
result->childAt(numInputColumns_ + i));
643643
}
644644

645+
velox::common::testutil::TestValue::adjust(
646+
"facebook::velox::exec::Window::callApplyForPartitionRows", this);
645647
const vector_size_t numRows = endRow - startRow;
646648
numProcessedRows_ += numRows;
647649
partitionOffset_ += numRows;

velox/exec/Window.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class Window : public Operator {
6767
void reclaim(uint64_t targetBytes, memory::MemoryReclaimer::Stats& stats)
6868
override;
6969

70+
const HashStringAllocator* testingGetHashStringAllocator() const {
71+
return &stringAllocator_;
72+
}
73+
7074
/// Runtime statistics holding total number of batches read from spilled data.
7175
/// 0 if no spilling occurred.
7276
static inline const std::string kWindowSpillReadNumBatches{

velox/functions/sparksql/window/tests/SparkWindowTest.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
* limitations under the License.
1515
*/
1616
#include "velox/common/base/tests/GTestUtils.h"
17+
#include "velox/exec/tests/utils/AssertQueryBuilder.h"
18+
#include "velox/exec/tests/utils/PlanBuilder.h"
19+
#include "velox/exec/Window.h"
1720
#include "velox/functions/lib/window/tests/WindowTestBase.h"
21+
#include "velox/functions/sparksql/aggregates/Register.h"
1822
#include "velox/functions/sparksql/window/WindowFunctionsRegistration.h"
1923

2024
using namespace facebook::velox::exec::test;
@@ -106,5 +110,59 @@ VELOX_INSTANTIATE_TEST_SUITE_P(
106110
SparkWindowTest,
107111
testing::ValuesIn(getSparkWindowTestParams()));
108112

113+
class SparkAggregateWindowTest : public WindowTestBase {
114+
public:
115+
void SetUp() override {
116+
WindowTestBase::SetUp();
117+
WindowTestBase::options_.parseIntegerAsBigint = false;
118+
velox::functions::aggregate::sparksql::registerAggregateFunctions("");
119+
}
120+
};
121+
122+
DEBUG_ONLY_TEST_F(SparkAggregateWindowTest, destroyPreviousAccumulator) {
123+
const auto size = 100;
124+
auto input = makeRowVector(
125+
{"d", "p0", "s"},
126+
{
127+
// Payload Data.
128+
makeFlatVector<std::string>(size, [](auto row){ return std::string(1024, 'a'); }),
129+
// Partition key.
130+
makeFlatVector<int64_t>(size, [](auto row) { return row % 11; }),
131+
// Sorting key.
132+
makeFlatVector<int32_t>(size, [](auto row) { return row; }),
133+
});
134+
135+
createDuckDbTable({input});
136+
137+
auto plan = PlanBuilder()
138+
.values(split(input, 10))
139+
.window({"last(d) over (partition by p0 order by s)"})
140+
.planNode();
141+
142+
const HashStringAllocator* stringAllocator = nullptr;
143+
uint64_t usedBytes = 0;
144+
SCOPED_TESTVALUE_SET(
145+
"facebook::velox::exec::Window::callApplyForPartitionRows",
146+
std::function<void(exec::Window*)>([&](exec::Window* windowOp) {
147+
if (stringAllocator == nullptr) {
148+
stringAllocator = windowOp->testingGetHashStringAllocator();
149+
// Record how many bytes have been used.
150+
usedBytes = stringAllocator->currentBytes();
151+
} else {
152+
// Because we will destroy previous created accumulator and every string in input
153+
// is of the same length, so here we check if the `usedBytes` is not changed.
154+
ASSERT_EQ(usedBytes, stringAllocator->currentBytes());
155+
}
156+
}));
157+
158+
auto task =
159+
AssertQueryBuilder(plan, duckDbQueryRunner_)
160+
.config(core::QueryConfig::kPreferredOutputBatchBytes, "1024")
161+
.serialExecution(true)
162+
.assertResults(
163+
"SELECT *, last(d) over (partition by p0 order by s) "
164+
"FROM tmp ");
165+
}
166+
109167
} // namespace
110168
} // namespace facebook::velox::window::test

0 commit comments

Comments
 (0)