Skip to content

Commit 9252516

Browse files
feat: cuDF window functionality (CudfConversion, ToCudf, CudfWindow, WindowTest)
Signed-off-by: patdevinwilson <pwilson@nvidia.com> Made-with: Cursor Signed-off-by: patdevinwilson <pwilson@nvidia.com>
1 parent 61912b2 commit 9252516

8 files changed

Lines changed: 638 additions & 14 deletions

File tree

velox/experimental/cudf/exec/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_library(
2424
CudfOrderBy.cpp
2525
CudfTopN.cpp
2626
CudfTopNRowNumber.cpp
27+
CudfWindow.cpp
2728
DebugUtil.cpp
2829
OperatorAdapters.cpp
2930
ToCudf.cpp

velox/experimental/cudf/exec/CudfConversion.cpp

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323

2424
#include "velox/exec/Driver.h"
2525
#include "velox/exec/Operator.h"
26+
#include "velox/type/Type.h"
2627
#include "velox/vector/ComplexVector.h"
28+
#include "velox/vector/DecodedVector.h"
29+
#include "velox/vector/FlatVector.h"
30+
#include "velox/vector/SelectivityVector.h"
2731

2832
#include <cudf/copying.hpp>
2933
#include <cudf/table/table.hpp>
@@ -67,6 +71,51 @@ cudf::size_type preferredGpuBatchSizeRows(
6771
"velox.cudf.gpu_batch_size_rows must be <= max(vector_size_t)");
6872
return batchSize;
6973
}
74+
75+
/// Cast a column to the plan's output type when cuDF/Arrow produced a
76+
/// different physical type (e.g. INTEGER for ROW_NUMBER/COUNT vs BIGINT).
77+
VectorPtr castColumnToPlanType(
78+
const VectorPtr& source,
79+
const TypePtr& targetType,
80+
memory::MemoryPool* pool) {
81+
if (source->type()->kindEquals(targetType)) {
82+
return source;
83+
}
84+
const auto size = source->size();
85+
if (targetType->isBigint() &&
86+
(source->type()->isInteger() || source->type()->isSmallint() ||
87+
source->type()->isTinyint())) {
88+
DecodedVector decoded(*source);
89+
auto result = BaseVector::create<FlatVector<int64_t>>(
90+
BIGINT(), size, pool);
91+
for (vector_size_t i = 0; i < size; ++i) {
92+
if (decoded.isNullAt(i)) {
93+
result->setNull(i, true);
94+
} else {
95+
int64_t v;
96+
switch (source->typeKind()) {
97+
case TypeKind::INTEGER:
98+
v = decoded.valueAt<int32_t>(i);
99+
break;
100+
case TypeKind::SMALLINT:
101+
v = decoded.valueAt<int16_t>(i);
102+
break;
103+
case TypeKind::TINYINT:
104+
v = decoded.valueAt<int8_t>(i);
105+
break;
106+
default:
107+
VELOX_UNREACHABLE();
108+
}
109+
result->set(i, v);
110+
}
111+
}
112+
return result;
113+
}
114+
VELOX_UNSUPPORTED(
115+
"CudfToVelox: cannot cast column from {} to {}",
116+
source->type()->toString(),
117+
targetType->toString());
118+
}
70119
} // namespace
71120

72121
CudfFromVelox::CudfFromVelox(
@@ -226,11 +275,26 @@ RowVectorPtr CudfToVelox::getOutput() {
226275
finished_ = noMoreInput_ && inputs_.empty();
227276
return nullptr;
228277
}
229-
RowVectorPtr output =
230-
with_arrow::toVeloxColumn(tableView, pool(), "", stream);
278+
RowVectorPtr output = with_arrow::toVeloxColumn(
279+
tableView, pool(), outputType_->names(), stream, get_temp_mr());
231280
stream.synchronize();
232281
finished_ = noMoreInput_ && inputs_.empty();
233-
output->setType(outputType_);
282+
if (!output->type()->kindEquals(outputType_)) {
283+
std::vector<VectorPtr> children;
284+
children.reserve(output->childrenSize());
285+
for (column_index_t i = 0; i < output->childrenSize(); ++i) {
286+
children.push_back(castColumnToPlanType(
287+
output->childAt(i), outputType_->childAt(i), pool()));
288+
}
289+
output = std::make_shared<RowVector>(
290+
pool(),
291+
outputType_,
292+
output->nulls(),
293+
output->size(),
294+
std::move(children));
295+
} else {
296+
output->setType(outputType_);
297+
}
234298
// cudfVector goes out of scope here, freeing the GPU memory
235299
return output;
236300
}
@@ -293,11 +357,26 @@ RowVectorPtr CudfToVelox::getOutput() {
293357
return nullptr;
294358
}
295359

296-
RowVectorPtr output =
297-
with_arrow::toVeloxColumn(resultTable->view(), pool(), "", stream);
360+
RowVectorPtr output = with_arrow::toVeloxColumn(
361+
resultTable->view(), pool(), outputType_->names(), stream, get_temp_mr());
298362
stream.synchronize();
299363
finished_ = noMoreInput_ && inputs_.empty();
300-
output->setType(outputType_);
364+
if (!output->type()->kindEquals(outputType_)) {
365+
std::vector<VectorPtr> children;
366+
children.reserve(output->childrenSize());
367+
for (column_index_t i = 0; i < output->childrenSize(); ++i) {
368+
children.push_back(castColumnToPlanType(
369+
output->childAt(i), outputType_->childAt(i), pool()));
370+
}
371+
output = std::make_shared<RowVector>(
372+
pool(),
373+
outputType_,
374+
output->nulls(),
375+
output->size(),
376+
std::move(children));
377+
} else {
378+
output->setType(outputType_);
379+
}
301380
return output;
302381
}
303382

0 commit comments

Comments
 (0)