1#include <Processors/Formats/Impl/TabSeparatedRowOutputFormat.h>
2#include <Processors/Formats/Impl/TabSeparatedRawRowOutputFormat.h>
3#include <Formats/FormatFactory.h>
4#include <IO/WriteHelpers.h>
5
6
7namespace DB
8{
9TabSeparatedRowOutputFormat::TabSeparatedRowOutputFormat(
10 WriteBuffer & out_,
11 const Block & header_,
12 bool with_names_,
13 bool with_types_,
14 FormatFactory::WriteCallback callback,
15 const FormatSettings & format_settings_)
16 : IRowOutputFormat(header_, out_, callback), with_names(with_names_), with_types(with_types_), format_settings(format_settings_)
17{
18}
19
20
21void TabSeparatedRowOutputFormat::writePrefix()
22{
23 auto & header = getPort(PortKind::Main).getHeader();
24 size_t columns = header.columns();
25
26 if (with_names)
27 {
28 for (size_t i = 0; i < columns; ++i)
29 {
30 writeEscapedString(header.safeGetByPosition(i).name, out);
31 writeChar(i == columns - 1 ? '\n' : '\t', out);
32 }
33 }
34
35 if (with_types)
36 {
37 for (size_t i = 0; i < columns; ++i)
38 {
39 writeEscapedString(header.safeGetByPosition(i).type->getName(), out);
40 writeChar(i == columns - 1 ? '\n' : '\t', out);
41 }
42 }
43}
44
45
46void TabSeparatedRowOutputFormat::writeField(const IColumn & column, const IDataType & type, size_t row_num)
47{
48 type.serializeAsTextEscaped(column, row_num, out, format_settings);
49}
50
51
52void TabSeparatedRowOutputFormat::writeFieldDelimiter()
53{
54 writeChar('\t', out);
55}
56
57
58void TabSeparatedRowOutputFormat::writeRowEndDelimiter()
59{
60 writeChar('\n', out);
61}
62
63void TabSeparatedRowOutputFormat::writeBeforeTotals()
64{
65 writeChar('\n', out);
66}
67
68void TabSeparatedRowOutputFormat::writeBeforeExtremes()
69{
70 writeChar('\n', out);
71}
72
73
74void registerOutputFormatProcessorTabSeparated(FormatFactory & factory)
75{
76 for (auto name : {"TabSeparated", "TSV"})
77 {
78 factory.registerOutputFormatProcessor(name, [](
79 WriteBuffer & buf,
80 const Block & sample,
81 FormatFactory::WriteCallback callback,
82 const FormatSettings & settings)
83 {
84 return std::make_shared<TabSeparatedRowOutputFormat>(buf, sample, false, false, callback, settings);
85 });
86 }
87
88 for (auto name : {"TabSeparatedRaw", "TSVRaw"})
89 {
90 factory.registerOutputFormatProcessor(name, [](
91 WriteBuffer & buf,
92 const Block & sample,
93 FormatFactory::WriteCallback callback,
94 const FormatSettings & settings)
95 {
96 return std::make_shared<TabSeparatedRawRowOutputFormat>(buf, sample, false, false, callback, settings);
97 });
98 }
99
100 for (auto name : {"TabSeparatedWithNames", "TSVWithNames"})
101 {
102 factory.registerOutputFormatProcessor(name, [](
103 WriteBuffer & buf,
104 const Block & sample,
105 FormatFactory::WriteCallback callback,
106 const FormatSettings & settings)
107 {
108 return std::make_shared<TabSeparatedRowOutputFormat>(buf, sample, true, false, callback, settings);
109 });
110 }
111
112 for (auto name : {"TabSeparatedWithNamesAndTypes", "TSVWithNamesAndTypes"})
113 {
114 factory.registerOutputFormatProcessor(name, [](
115 WriteBuffer & buf,
116 const Block & sample,
117 FormatFactory::WriteCallback callback,
118 const FormatSettings & settings)
119 {
120 return std::make_shared<TabSeparatedRowOutputFormat>(buf, sample, true, true, callback, settings);
121 });
122 }
123}
124
125}
126