| 1 | #include <string> |
| 2 | #include <Processors/Formats/IRowOutputFormat.h> |
| 3 | #include <IO/WriteHelpers.h> |
| 4 | |
| 5 | |
| 6 | namespace DB |
| 7 | { |
| 8 | |
| 9 | void IRowOutputFormat::consume(DB::Chunk chunk) |
| 10 | { |
| 11 | writePrefixIfNot(); |
| 12 | |
| 13 | auto num_rows = chunk.getNumRows(); |
| 14 | auto & columns = chunk.getColumns(); |
| 15 | |
| 16 | for (UInt64 row = 0; row < num_rows; ++row) |
| 17 | { |
| 18 | if (!first_row) |
| 19 | writeRowBetweenDelimiter(); |
| 20 | first_row = false; |
| 21 | |
| 22 | write(columns, row); |
| 23 | |
| 24 | if (write_single_row_callback) |
| 25 | write_single_row_callback(); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void IRowOutputFormat::consumeTotals(DB::Chunk chunk) |
| 30 | { |
| 31 | writePrefixIfNot(); |
| 32 | writeSuffixIfNot(); |
| 33 | |
| 34 | auto num_rows = chunk.getNumRows(); |
| 35 | if (num_rows != 1) |
| 36 | throw Exception("Got " + toString(num_rows) + " in totals chunk, expected 1" , ErrorCodes::LOGICAL_ERROR); |
| 37 | |
| 38 | auto & columns = chunk.getColumns(); |
| 39 | |
| 40 | writeBeforeTotals(); |
| 41 | writeTotals(columns, 0); |
| 42 | writeAfterTotals(); |
| 43 | } |
| 44 | |
| 45 | void IRowOutputFormat::consumeExtremes(DB::Chunk chunk) |
| 46 | { |
| 47 | writePrefixIfNot(); |
| 48 | writeSuffixIfNot(); |
| 49 | |
| 50 | auto num_rows = chunk.getNumRows(); |
| 51 | auto & columns = chunk.getColumns(); |
| 52 | if (num_rows != 2) |
| 53 | throw Exception("Got " + toString(num_rows) + " in extremes chunk, expected 2" , ErrorCodes::LOGICAL_ERROR); |
| 54 | |
| 55 | writeBeforeExtremes(); |
| 56 | writeMinExtreme(columns, 0); |
| 57 | writeRowBetweenDelimiter(); |
| 58 | writeMaxExtreme(columns, 1); |
| 59 | writeAfterExtremes(); |
| 60 | } |
| 61 | |
| 62 | void IRowOutputFormat::finalize() |
| 63 | { |
| 64 | writePrefixIfNot(); |
| 65 | writeSuffixIfNot(); |
| 66 | writeLastSuffix(); |
| 67 | } |
| 68 | |
| 69 | void IRowOutputFormat::write(const Columns & columns, size_t row_num) |
| 70 | { |
| 71 | size_t num_columns = columns.size(); |
| 72 | |
| 73 | writeRowStartDelimiter(); |
| 74 | |
| 75 | for (size_t i = 0; i < num_columns; ++i) |
| 76 | { |
| 77 | if (i != 0) |
| 78 | writeFieldDelimiter(); |
| 79 | |
| 80 | writeField(*columns[i], *types[i], row_num); |
| 81 | } |
| 82 | |
| 83 | writeRowEndDelimiter(); |
| 84 | } |
| 85 | |
| 86 | void IRowOutputFormat::writeMinExtreme(const DB::Columns & columns, size_t row_num) |
| 87 | { |
| 88 | write(columns, row_num); |
| 89 | } |
| 90 | |
| 91 | void IRowOutputFormat::writeMaxExtreme(const DB::Columns & columns, size_t row_num) |
| 92 | { |
| 93 | write(columns, row_num); |
| 94 | } |
| 95 | |
| 96 | void IRowOutputFormat::writeTotals(const DB::Columns & columns, size_t row_num) |
| 97 | { |
| 98 | write(columns, row_num); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |