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
| 513 | // generated row length to determine the target block_size. i.e - higher multiplier |
| 514 | // means fewer batches |
| 515 | static TestCase GenerateTestCase(int num_rows, double block_size_multiplier = 3.0) { |
| 516 | FieldVector data_fields = {field("s", utf8()), field("f", float64()), |
| 517 | field("b", boolean())}; |
| 518 | FieldVector fields = {field("i", int64()), field("d", struct_({data_fields}))}; |
| 519 | TestCase out; |
| 520 | out.schema = schema(fields); |
| 521 | out.num_rows = num_rows; |
| 522 | |
| 523 | constexpr int kSeed = 0x432432; |
| 524 | std::default_random_engine engine(kSeed); |
| 525 | std::vector<std::string> rows(num_rows); |
| 526 | size_t max_row_size = 1; |
| 527 | |
| 528 | auto options = GenerateOptions::Defaults(); |
| 529 | options.null_probability = 0; |
| 530 | for (int i = 0; i < num_rows; ++i) { |
| 531 | StringBuffer string_buffer; |
| 532 | Writer writer(string_buffer); |
| 533 | ABORT_NOT_OK(Generate(data_fields, engine, &writer, options)); |
| 534 | std::string json = string_buffer.GetString(); |
| 535 | rows[i] = Join({"{\"i\":", std::to_string(i), ",\"d\":", json, "}\n"}); |
| 536 | max_row_size = std::max(max_row_size, rows[i].size()); |
| 537 | } |
| 538 | |
| 539 | auto block_size = static_cast<size_t>(max_row_size * block_size_multiplier); |
| 540 | // Deduce the expected record batches from the target block size. |
| 541 | std::vector<std::string> batch_rows; |
| 542 | size_t pos = 0; |
| 543 | for (const auto& row : rows) { |
| 544 | pos += row.size(); |
| 545 | if (pos > block_size) { |
| 546 | out.batches.push_back( |
| 547 | RecordBatchFromJSON(out.schema, Join({"[", Join(batch_rows, ","), "]"}))); |
| 548 | batch_rows.clear(); |
| 549 | pos -= block_size; |
| 550 | } |
| 551 | batch_rows.push_back(row); |
| 552 | out.json += row; |
| 553 | } |
| 554 | if (!batch_rows.empty()) { |
| 555 | out.batches.push_back( |
| 556 | RecordBatchFromJSON(out.schema, Join({"[", Join(batch_rows, ","), "]"}))); |
| 557 | } |
| 558 | |
| 559 | out.json_size = static_cast<int>(out.json.size()); |
| 560 | out.block_size = static_cast<int>(block_size); |
| 561 | out.num_batches = static_cast<int>(out.batches.size()); |
| 562 | |
| 563 | return out; |
| 564 | } |
| 565 | |
| 566 | static std::string Join(const std::vector<std::string>& strings, |
| 567 | const std::string& delim = "", bool trailing_delim = false) { |
nothing calls this directly
no test coverage detected