Index < 0 means appending null values to all columns.
| 444 | // Index < 0 means appending null values to all columns. |
| 445 | // |
| 446 | void TakeUsingVector(ExecContext* ctx, const std::vector<std::shared_ptr<Array>>& input, |
| 447 | const std::vector<int32_t> indices, |
| 448 | std::vector<std::shared_ptr<Array>>* result) { |
| 449 | ASSERT_OK_AND_ASSIGN( |
| 450 | std::shared_ptr<Buffer> buf, |
| 451 | AllocateBuffer(indices.size() * sizeof(int32_t), ctx->memory_pool())); |
| 452 | int32_t* buf_indices = reinterpret_cast<int32_t*>(buf->mutable_data()); |
| 453 | bool has_null_rows = false; |
| 454 | for (size_t i = 0; i < indices.size(); ++i) { |
| 455 | if (indices[i] < 0) { |
| 456 | buf_indices[i] = 0; |
| 457 | has_null_rows = true; |
| 458 | } else { |
| 459 | buf_indices[i] = indices[i]; |
| 460 | } |
| 461 | } |
| 462 | std::shared_ptr<Array> indices_array = MakeArray(ArrayData::Make( |
| 463 | int32(), indices.size(), {nullptr, std::move(buf)}, /*null_count=*/0)); |
| 464 | |
| 465 | result->resize(input.size()); |
| 466 | for (size_t i = 0; i < result->size(); ++i) { |
| 467 | ASSERT_OK_AND_ASSIGN(Datum new_array, Take(input[i], indices_array)); |
| 468 | (*result)[i] = new_array.make_array(); |
| 469 | } |
| 470 | if (has_null_rows) { |
| 471 | for (size_t i = 0; i < result->size(); ++i) { |
| 472 | if ((*result)[i]->data()->buffers[0] == NULLPTR) { |
| 473 | ASSERT_OK_AND_ASSIGN(std::shared_ptr<Buffer> null_buf, |
| 474 | AllocateBitmap(indices.size(), ctx->memory_pool())); |
| 475 | uint8_t* non_nulls = null_buf->mutable_data(); |
| 476 | memset(non_nulls, 0xFF, bit_util::BytesForBits(indices.size())); |
| 477 | if ((*result)[i]->data()->buffers.size() == 2) { |
| 478 | (*result)[i] = MakeArray( |
| 479 | ArrayData::Make((*result)[i]->type(), indices.size(), |
| 480 | {std::move(null_buf), (*result)[i]->data()->buffers[1]})); |
| 481 | } else { |
| 482 | (*result)[i] = MakeArray( |
| 483 | ArrayData::Make((*result)[i]->type(), indices.size(), |
| 484 | {std::move(null_buf), (*result)[i]->data()->buffers[1], |
| 485 | (*result)[i]->data()->buffers[2]})); |
| 486 | } |
| 487 | } |
| 488 | (*result)[i]->data()->SetNullCount(kUnknownNullCount); |
| 489 | } |
| 490 | for (size_t i = 0; i < indices.size(); ++i) { |
| 491 | if (indices[i] < 0) { |
| 492 | for (size_t col = 0; col < result->size(); ++col) { |
| 493 | uint8_t* non_nulls = (*result)[col]->data()->buffers[0]->mutable_data(); |
| 494 | bit_util::ClearBit(non_nulls, i); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Generate random arrays given list of data types and null probabilities. |
| 502 | // Make sure that all generated records are unique. |
no test coverage detected