|
23 | 23 |
|
24 | 24 | #include "velox/exec/Driver.h" |
25 | 25 | #include "velox/exec/Operator.h" |
| 26 | +#include "velox/type/Type.h" |
26 | 27 | #include "velox/vector/ComplexVector.h" |
| 28 | +#include "velox/vector/DecodedVector.h" |
| 29 | +#include "velox/vector/FlatVector.h" |
| 30 | +#include "velox/vector/SelectivityVector.h" |
27 | 31 |
|
28 | 32 | #include <cudf/copying.hpp> |
29 | 33 | #include <cudf/table/table.hpp> |
@@ -67,6 +71,51 @@ cudf::size_type preferredGpuBatchSizeRows( |
67 | 71 | "velox.cudf.gpu_batch_size_rows must be <= max(vector_size_t)"); |
68 | 72 | return batchSize; |
69 | 73 | } |
| 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 | +} |
70 | 119 | } // namespace |
71 | 120 |
|
72 | 121 | CudfFromVelox::CudfFromVelox( |
@@ -226,11 +275,26 @@ RowVectorPtr CudfToVelox::getOutput() { |
226 | 275 | finished_ = noMoreInput_ && inputs_.empty(); |
227 | 276 | return nullptr; |
228 | 277 | } |
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()); |
231 | 280 | stream.synchronize(); |
232 | 281 | 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 | + } |
234 | 298 | // cudfVector goes out of scope here, freeing the GPU memory |
235 | 299 | return output; |
236 | 300 | } |
@@ -293,11 +357,26 @@ RowVectorPtr CudfToVelox::getOutput() { |
293 | 357 | return nullptr; |
294 | 358 | } |
295 | 359 |
|
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()); |
298 | 362 | stream.synchronize(); |
299 | 363 | 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 | + } |
301 | 380 | return output; |
302 | 381 | } |
303 | 382 |
|
|
0 commit comments