Creates a test case from valid JSON objects with a human-readable index field and a struct field of random data. `block_size_multiplier` is applied to the largest generated row length to determine the target block_size. i.e - higher multiplier means fewer batches
| 535 | // generated row length to determine the target block_size. i.e - higher multiplier |
| 536 | // means fewer batches |
| 537 | static TestCase GenerateTestCase(int num_rows, double block_size_multiplier = 3.0) { |
| 538 | FieldVector data_fields = {field("s", utf8()), field("f", float64()), |
| 539 | field("b", boolean())}; |
| 540 | FieldVector fields = {field("i", int64()), field("d", struct_({data_fields}))}; |
| 541 | TestCase out; |
| 542 | out.schema = schema(fields); |
| 543 | out.num_rows = num_rows; |
| 544 | |
| 545 | constexpr int kSeed = 0x432432; |
| 546 | std::default_random_engine engine(kSeed); |
| 547 | std::vector<std::string> rows(num_rows); |
| 548 | size_t max_row_size = 1; |
| 549 | |
| 550 | auto options = GenerateOptions::Defaults(); |
| 551 | options.null_probability = 0; |
| 552 | for (int i = 0; i < num_rows; ++i) { |
| 553 | StringBuffer string_buffer; |
| 554 | Writer writer(string_buffer); |
| 555 | ABORT_NOT_OK(Generate(data_fields, engine, &writer, options)); |
| 556 | std::string json = string_buffer.GetString(); |
| 557 | rows[i] = Join({"{\"i\":", std::to_string(i), ",\"d\":", json, "}\n"}); |
| 558 | max_row_size = std::max(max_row_size, rows[i].size()); |
| 559 | } |
| 560 | |
| 561 | auto block_size = static_cast<size_t>(max_row_size * block_size_multiplier); |
| 562 | // Deduce the expected record batches from the target block size. |
| 563 | std::vector<std::string> batch_rows; |
| 564 | size_t pos = 0; |
| 565 | for (const auto& row : rows) { |
| 566 | pos += row.size(); |
| 567 | if (pos > block_size) { |
| 568 | out.batches.push_back( |
| 569 | RecordBatchFromJSON(out.schema, Join({"[", Join(batch_rows, ","), "]"}))); |
| 570 | batch_rows.clear(); |
| 571 | pos -= block_size; |
| 572 | } |
| 573 | batch_rows.push_back(row); |
| 574 | out.json += row; |
| 575 | } |
| 576 | if (!batch_rows.empty()) { |
| 577 | out.batches.push_back( |
| 578 | RecordBatchFromJSON(out.schema, Join({"[", Join(batch_rows, ","), "]"}))); |
| 579 | } |
| 580 | |
| 581 | out.json_size = static_cast<int>(out.json.size()); |
| 582 | out.block_size = static_cast<int>(block_size); |
| 583 | out.num_batches = static_cast<int>(out.batches.size()); |
| 584 | |
| 585 | return out; |
| 586 | } |
| 587 | |
| 588 | static std::string Join(const std::vector<std::string>& strings, |
| 589 | const std::string& delim = "", bool trailing_delim = false) { |
nothing calls this directly
no test coverage detected