2018-05-24 01:02:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-20 11:17:57 +00:00
|
|
|
#include <Formats/FormatFactory.h>
|
2018-05-24 01:02:16 +00:00
|
|
|
#include <Processors/Formats/IOutputFormat.h>
|
|
|
|
|
2019-08-20 11:17:57 +00:00
|
|
|
#include <string>
|
|
|
|
|
2018-05-24 01:02:16 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-10-06 12:47:52 +00:00
|
|
|
struct RowOutputFormatParams
|
|
|
|
{
|
|
|
|
using WriteCallback = std::function<void(const Columns & columns,size_t row)>;
|
|
|
|
|
|
|
|
// Callback used to indicate that another row is written.
|
|
|
|
WriteCallback callback;
|
|
|
|
};
|
|
|
|
|
2018-05-24 01:02:16 +00:00
|
|
|
class WriteBuffer;
|
|
|
|
|
|
|
|
/** Output format that writes data row by row.
|
|
|
|
*/
|
|
|
|
class IRowOutputFormat : public IOutputFormat
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
DataTypes types;
|
2020-10-06 14:32:01 +00:00
|
|
|
bool first_row = true;
|
2018-05-24 01:02:16 +00:00
|
|
|
|
2019-02-18 16:36:07 +00:00
|
|
|
void consume(Chunk chunk) override;
|
|
|
|
void consumeTotals(Chunk chunk) override;
|
|
|
|
void consumeExtremes(Chunk chunk) override;
|
2019-02-19 18:41:18 +00:00
|
|
|
void finalize() override;
|
2018-05-24 01:02:16 +00:00
|
|
|
|
|
|
|
public:
|
2020-10-06 12:47:52 +00:00
|
|
|
using Params = RowOutputFormatParams;
|
|
|
|
|
|
|
|
IRowOutputFormat(const Block & header, WriteBuffer & out_, const Params & params_)
|
|
|
|
: IOutputFormat(header, out_), types(header.getDataTypes()), params(params_)
|
2018-05-24 01:02:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Write a row.
|
|
|
|
* Default implementation calls methods to write single values and delimiters
|
|
|
|
* (except delimiter between rows (writeRowBetweenDelimiter())).
|
|
|
|
*/
|
|
|
|
virtual void write(const Columns & columns, size_t row_num);
|
2019-02-19 18:41:18 +00:00
|
|
|
virtual void writeMinExtreme(const Columns & columns, size_t row_num);
|
|
|
|
virtual void writeMaxExtreme(const Columns & columns, size_t row_num);
|
|
|
|
virtual void writeTotals(const Columns & columns, size_t row_num);
|
2018-05-24 01:02:16 +00:00
|
|
|
|
|
|
|
/** Write single value. */
|
|
|
|
virtual void writeField(const IColumn & column, const IDataType & type, size_t row_num) = 0;
|
|
|
|
|
|
|
|
/** Write delimiter. */
|
2019-02-19 18:41:18 +00:00
|
|
|
virtual void writeFieldDelimiter() {} /// delimiter between values
|
|
|
|
virtual void writeRowStartDelimiter() {} /// delimiter before each row
|
|
|
|
virtual void writeRowEndDelimiter() {} /// delimiter after each row
|
|
|
|
virtual void writeRowBetweenDelimiter() {} /// delimiter between rows
|
|
|
|
virtual void writePrefix() {} /// delimiter before resultset
|
|
|
|
virtual void writeSuffix() {} /// delimiter after resultset
|
|
|
|
virtual void writeBeforeTotals() {}
|
|
|
|
virtual void writeAfterTotals() {}
|
|
|
|
virtual void writeBeforeExtremes() {}
|
|
|
|
virtual void writeAfterExtremes() {}
|
|
|
|
virtual void writeLastSuffix() {} /// Write something after resultset, totals end extremes.
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool prefix_written = false;
|
|
|
|
bool suffix_written = false;
|
|
|
|
|
2020-10-06 12:47:52 +00:00
|
|
|
Params params;
|
2019-08-20 11:17:57 +00:00
|
|
|
|
2019-02-19 18:41:18 +00:00
|
|
|
void writePrefixIfNot()
|
|
|
|
{
|
|
|
|
if (!prefix_written)
|
|
|
|
writePrefix();
|
|
|
|
|
|
|
|
prefix_written = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeSuffixIfNot()
|
|
|
|
{
|
|
|
|
if (!suffix_written)
|
|
|
|
writeSuffix();
|
|
|
|
|
|
|
|
suffix_written = true;
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:02:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|