1#include <Common/ProfileEvents.h>
2#include <Common/IPv6ToBinary.h>
3#include <Common/ClickHouseRevision.h>
4#include <Columns/ColumnsNumber.h>
5#include <Columns/ColumnString.h>
6#include <Columns/ColumnFixedString.h>
7#include <Columns/ColumnArray.h>
8#include <DataTypes/DataTypesNumber.h>
9#include <DataTypes/DataTypeDateTime.h>
10#include <DataTypes/DataTypeDate.h>
11#include <DataTypes/DataTypeString.h>
12#include <DataTypes/DataTypeArray.h>
13#include <DataTypes/DataTypeFactory.h>
14#include <DataTypes/DataTypeEnum.h>
15#include <Interpreters/QueryLog.h>
16#include <Interpreters/ProfileEventsExt.h>
17#include <Poco/Net/IPAddress.h>
18#include <array>
19
20
21namespace DB
22{
23
24Block QueryLogElement::createBlock()
25{
26 auto query_status_datatype = std::make_shared<DataTypeEnum8>(
27 DataTypeEnum8::Values
28 {
29 {"QueryStart", static_cast<Int8>(QUERY_START)},
30 {"QueryFinish", static_cast<Int8>(QUERY_FINISH)},
31 {"ExceptionBeforeStart", static_cast<Int8>(EXCEPTION_BEFORE_START)},
32 {"ExceptionWhileProcessing", static_cast<Int8>(EXCEPTION_WHILE_PROCESSING)}
33 });
34
35 return
36 {
37 {std::move(query_status_datatype), "type"},
38 {std::make_shared<DataTypeDate>(), "event_date"},
39 {std::make_shared<DataTypeDateTime>(), "event_time"},
40 {std::make_shared<DataTypeDateTime>(), "query_start_time"},
41 {std::make_shared<DataTypeUInt64>(), "query_duration_ms"},
42
43 {std::make_shared<DataTypeUInt64>(), "read_rows"},
44 {std::make_shared<DataTypeUInt64>(), "read_bytes"},
45 {std::make_shared<DataTypeUInt64>(), "written_rows"},
46 {std::make_shared<DataTypeUInt64>(), "written_bytes"},
47 {std::make_shared<DataTypeUInt64>(), "result_rows"},
48 {std::make_shared<DataTypeUInt64>(), "result_bytes"},
49 {std::make_shared<DataTypeUInt64>(), "memory_usage"},
50
51 {std::make_shared<DataTypeString>(), "query"},
52 {std::make_shared<DataTypeString>(), "exception"},
53 {std::make_shared<DataTypeString>(), "stack_trace"},
54
55 {std::make_shared<DataTypeUInt8>(), "is_initial_query"},
56 {std::make_shared<DataTypeString>(), "user"},
57 {std::make_shared<DataTypeString>(), "query_id"},
58 {DataTypeFactory::instance().get("IPv6"), "address"},
59 {std::make_shared<DataTypeUInt16>(), "port"},
60 {std::make_shared<DataTypeString>(), "initial_user"},
61 {std::make_shared<DataTypeString>(), "initial_query_id"},
62 {DataTypeFactory::instance().get("IPv6"), "initial_address"},
63 {std::make_shared<DataTypeUInt16>(), "initial_port"},
64 {std::make_shared<DataTypeUInt8>(), "interface"},
65 {std::make_shared<DataTypeString>(), "os_user"},
66 {std::make_shared<DataTypeString>(), "client_hostname"},
67 {std::make_shared<DataTypeString>(), "client_name"},
68 {std::make_shared<DataTypeUInt32>(), "client_revision"},
69 {std::make_shared<DataTypeUInt32>(), "client_version_major"},
70 {std::make_shared<DataTypeUInt32>(), "client_version_minor"},
71 {std::make_shared<DataTypeUInt32>(), "client_version_patch"},
72 {std::make_shared<DataTypeUInt8>(), "http_method"},
73 {std::make_shared<DataTypeString>(), "http_user_agent"},
74 {std::make_shared<DataTypeString>(), "quota_key"},
75
76 {std::make_shared<DataTypeUInt32>(), "revision"},
77
78 {std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt32>()), "thread_numbers"},
79 {std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt32>()), "os_thread_ids"},
80 {std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()), "ProfileEvents.Names"},
81 {std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt64>()), "ProfileEvents.Values"},
82 {std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()), "Settings.Names"},
83 {std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()), "Settings.Values"}
84 };
85}
86
87
88void QueryLogElement::appendToBlock(Block & block) const
89{
90 MutableColumns columns = block.mutateColumns();
91
92 size_t i = 0;
93
94 columns[i++]->insert(type);
95 columns[i++]->insert(DateLUT::instance().toDayNum(event_time));
96 columns[i++]->insert(event_time);
97 columns[i++]->insert(query_start_time);
98 columns[i++]->insert(query_duration_ms);
99
100 columns[i++]->insert(read_rows);
101 columns[i++]->insert(read_bytes);
102 columns[i++]->insert(written_rows);
103 columns[i++]->insert(written_bytes);
104 columns[i++]->insert(result_rows);
105 columns[i++]->insert(result_bytes);
106
107 columns[i++]->insert(memory_usage);
108
109 columns[i++]->insertData(query.data(), query.size());
110 columns[i++]->insertData(exception.data(), exception.size());
111 columns[i++]->insertData(stack_trace.data(), stack_trace.size());
112
113 appendClientInfo(client_info, columns, i);
114
115 columns[i++]->insert(ClickHouseRevision::get());
116
117 {
118 Array threads_array;
119 threads_array.reserve(thread_numbers.size());
120 for (const UInt32 thread_number : thread_numbers)
121 threads_array.emplace_back(UInt64(thread_number));
122 columns[i++]->insert(threads_array);
123 }
124
125 {
126 Array threads_array;
127 threads_array.reserve(os_thread_ids.size());
128 for (const UInt32 thread_number : os_thread_ids)
129 threads_array.emplace_back(UInt64(thread_number));
130 columns[i++]->insert(threads_array);
131 }
132
133 if (profile_counters)
134 {
135 auto column_names = columns[i++].get();
136 auto column_values = columns[i++].get();
137 ProfileEvents::dumpToArrayColumns(*profile_counters, column_names, column_values, true);
138 }
139 else
140 {
141 columns[i++]->insertDefault();
142 columns[i++]->insertDefault();
143 }
144
145 if (query_settings)
146 {
147 auto column_names = columns[i++].get();
148 auto column_values = columns[i++].get();
149 query_settings->dumpToArrayColumns(column_names, column_values, true);
150 }
151 else
152 {
153 columns[i++]->insertDefault();
154 columns[i++]->insertDefault();
155 }
156
157 block.setColumns(std::move(columns));
158}
159
160void QueryLogElement::appendClientInfo(const ClientInfo & client_info, MutableColumns & columns, size_t & i)
161{
162 columns[i++]->insert(client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY);
163
164 columns[i++]->insert(client_info.current_user);
165 columns[i++]->insert(client_info.current_query_id);
166 columns[i++]->insertData(IPv6ToBinary(client_info.current_address.host()).data(), 16);
167 columns[i++]->insert(client_info.current_address.port());
168
169 columns[i++]->insert(client_info.initial_user);
170 columns[i++]->insert(client_info.initial_query_id);
171 columns[i++]->insertData(IPv6ToBinary(client_info.initial_address.host()).data(), 16);
172 columns[i++]->insert(client_info.initial_address.port());
173
174 columns[i++]->insert(UInt64(client_info.interface));
175
176 columns[i++]->insert(client_info.os_user);
177 columns[i++]->insert(client_info.client_hostname);
178 columns[i++]->insert(client_info.client_name);
179 columns[i++]->insert(client_info.client_revision);
180 columns[i++]->insert(client_info.client_version_major);
181 columns[i++]->insert(client_info.client_version_minor);
182 columns[i++]->insert(client_info.client_version_patch);
183
184 columns[i++]->insert(UInt64(client_info.http_method));
185 columns[i++]->insert(client_info.http_user_agent);
186
187 columns[i++]->insert(client_info.quota_key);
188}
189}
190