ClickHouse/src/Interpreters/QueryLog.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

341 lines
20 KiB
C++
Raw Normal View History

#include <Interpreters/QueryLog.h>
#include <base/getFQDNOrHostName.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeDateTime.h>
2019-08-01 11:11:08 +00:00
#include <DataTypes/DataTypeEnum.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/DataTypeMap.h>
2020-12-12 05:57:27 +00:00
#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeString.h>
2022-08-13 13:03:16 +00:00
#include <DataTypes/DataTypeUUID.h>
#include <DataTypes/DataTypesNumber.h>
#include <Interpreters/ProfileEventsExt.h>
#include <Common/ClickHouseRevision.h>
#include <Common/IPv6ToBinary.h>
#include <Common/ProfileEvents.h>
#include <Common/typeid_cast.h>
#include <Poco/Net/IPAddress.h>
#include <array>
namespace DB
{
ColumnsDescription QueryLogElement::getColumnsDescription()
{
2020-09-04 16:10:29 +00:00
auto query_status_datatype = std::make_shared<DataTypeEnum8>(
DataTypeEnum8::Values
{
{"QueryStart", static_cast<Int8>(QUERY_START)},
{"QueryFinish", static_cast<Int8>(QUERY_FINISH)},
{"ExceptionBeforeStart", static_cast<Int8>(EXCEPTION_BEFORE_START)},
{"ExceptionWhileProcessing", static_cast<Int8>(EXCEPTION_WHILE_PROCESSING)}
});
auto query_cache_usage_datatype = std::make_shared<DataTypeEnum8>(
DataTypeEnum8::Values
{
2023-07-27 08:55:19 +00:00
{"Unknown", static_cast<Int8>(QueryCache::Usage::Unknown)},
{"None", static_cast<Int8>(QueryCache::Usage::None)},
{"Write", static_cast<Int8>(QueryCache::Usage::Write)},
2023-07-27 08:55:19 +00:00
{"Read", static_cast<Int8>(QueryCache::Usage::Read)}
});
auto low_cardinality_string = std::make_shared<DataTypeLowCardinality>(std::make_shared<DataTypeString>());
auto array_low_cardinality_string = std::make_shared<DataTypeArray>(low_cardinality_string);
return ColumnsDescription
2020-09-04 16:10:29 +00:00
{
{"hostname", low_cardinality_string, "Hostname of the server executing the query."},
{"type", std::move(query_status_datatype), "Type of an event that occurred when executing the query."},
{"event_date", std::make_shared<DataTypeDate>(), "Query starting date."},
{"event_time", std::make_shared<DataTypeDateTime>(), "Query starting time."},
{"event_time_microseconds", std::make_shared<DataTypeDateTime64>(6), "Query starting time with microseconds precision."},
{"query_start_time", std::make_shared<DataTypeDateTime>(), "Start time of query execution."},
{"query_start_time_microseconds", std::make_shared<DataTypeDateTime64>(6), "Start time of query execution with microsecond precision."},
{"query_duration_ms", std::make_shared<DataTypeUInt64>(), "Duration of query execution in milliseconds."},
{"read_rows", std::make_shared<DataTypeUInt64>(), "Total number of rows read from all tables and table functions participated in query. It includes usual subqueries, subqueries for IN and JOIN. For distributed queries read_rows includes the total number of rows read at all replicas. Each replica sends its read_rows value, and the server-initiator of the query summarizes all received and local values. The cache volumes do not affect this value."},
{"read_bytes", std::make_shared<DataTypeUInt64>(), "Total number of bytes read from all tables and table functions participated in query. It includes usual subqueries, subqueries for IN and JOIN. For distributed queries read_bytes includes the total number of rows read at all replicas. Each replica sends its read_bytes value, and the server-initiator of the query summarizes all received and local values. The cache volumes do not affect this value."},
{"written_rows", std::make_shared<DataTypeUInt64>(), "For INSERT queries, the number of written rows. For other queries, the column value is 0."},
{"written_bytes", std::make_shared<DataTypeUInt64>(), "For INSERT queries, the number of written bytes (uncompressed). For other queries, the column value is 0."},
{"result_rows", std::make_shared<DataTypeUInt64>(), "Number of rows in a result of the SELECT query, or a number of rows in the INSERT query."},
{"result_bytes", std::make_shared<DataTypeUInt64>(), "RAM volume in bytes used to store a query result."},
{"memory_usage", std::make_shared<DataTypeUInt64>(), "Memory consumption by the query."},
{"current_database", low_cardinality_string, "Name of the current database."},
{"query", std::make_shared<DataTypeString>(), " Query string."},
{"formatted_query", std::make_shared<DataTypeString>(), "Formatted query string."},
{"normalized_query_hash", std::make_shared<DataTypeUInt64>(), "Identical hash value without the values of literals for similar queries."},
{"query_kind", low_cardinality_string, "Type of the query."},
{"databases", array_low_cardinality_string, "Names of the databases present in the query."},
{"tables", array_low_cardinality_string, "Names of the tables present in the query."},
{"columns", array_low_cardinality_string, "Names of the columns present in the query."},
{"partitions", array_low_cardinality_string, "Names of the partitions present in the query."},
{"projections", array_low_cardinality_string, "Names of the projections used during the query execution."},
{"views", array_low_cardinality_string, "Names of the (materialized or live) views present in the query."},
{"exception_code", std::make_shared<DataTypeInt32>(), "Code of an exception."},
{"exception", std::make_shared<DataTypeString>(), "Exception message."},
{"stack_trace", std::make_shared<DataTypeString>(), "Stack trace. An empty string, if the query was completed successfully."},
{"is_initial_query", std::make_shared<DataTypeUInt8>(), "Query type. Possible values: 1 — query was initiated by the client, 0 — query was initiated by another query as part of distributed query execution."},
{"user", low_cardinality_string, "Name of the user who initiated the current query."},
{"query_id", std::make_shared<DataTypeString>(), "ID of the query."},
{"address", DataTypeFactory::instance().get("IPv6"), "IP address that was used to make the query."},
{"port", std::make_shared<DataTypeUInt16>(), "The client port that was used to make the query."},
{"initial_user", low_cardinality_string, "Name of the user who ran the initial query (for distributed query execution)."},
{"initial_query_id", std::make_shared<DataTypeString>(), "ID of the initial query (for distributed query execution)."},
{"initial_address", DataTypeFactory::instance().get("IPv6"), "IP address that the parent query was launched from."},
{"initial_port", std::make_shared<DataTypeUInt16>(), "The client port that was used to make the parent query."},
{"initial_query_start_time", std::make_shared<DataTypeDateTime>(), "Initial query starting time (for distributed query execution)."},
{"initial_query_start_time_microseconds", std::make_shared<DataTypeDateTime64>(6), "Initial query starting time with microseconds precision (for distributed query execution)."},
{"interface", std::make_shared<DataTypeUInt8>(), "Interface that the query was initiated from. Possible values: 1 — TCP, 2 — HTTP."},
{"is_secure", std::make_shared<DataTypeUInt8>(), "The flag whether a query was executed over a secure interface"},
{"os_user", low_cardinality_string, "Operating system username who runs clickhouse-client."},
{"client_hostname", low_cardinality_string, "Hostname of the client machine where the clickhouse-client or another TCP client is run."},
{"client_name", low_cardinality_string, "The clickhouse-client or another TCP client name."},
{"client_revision", std::make_shared<DataTypeUInt32>(), "Revision of the clickhouse-client or another TCP client."},
{"client_version_major", std::make_shared<DataTypeUInt32>(), "Major version of the clickhouse-client or another TCP client."},
{"client_version_minor", std::make_shared<DataTypeUInt32>(), "Minor version of the clickhouse-client or another TCP client."},
{"client_version_patch", std::make_shared<DataTypeUInt32>(), "Patch component of the clickhouse-client or another TCP client version."},
{"http_method", std::make_shared<DataTypeUInt8>(), "HTTP method that initiated the query. Possible values: 0 — The query was launched from the TCP interface, 1 — GET method was used, 2 — POST method was used."},
{"http_user_agent", low_cardinality_string, "HTTP header UserAgent passed in the HTTP query."},
{"http_referer", std::make_shared<DataTypeString>(), "HTTP header Referer passed in the HTTP query (contains an absolute or partial address of the page making the query)."},
{"forwarded_for", std::make_shared<DataTypeString>(), "HTTP header X-Forwarded-For passed in the HTTP query."},
{"quota_key", std::make_shared<DataTypeString>(), "The quota key specified in the quotas setting (see keyed)."},
{"distributed_depth", std::make_shared<DataTypeUInt64>(), "How many times a query was forwarded between servers."},
{"revision", std::make_shared<DataTypeUInt32>(), "ClickHouse revision."},
{"log_comment", std::make_shared<DataTypeString>(), "Log comment. It can be set to arbitrary string no longer than max_query_size. An empty string if it is not defined."},
{"thread_ids", std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt64>()), "Thread ids that are participating in query execution. These threads may not have run simultaneously."},
{"peak_threads_usage", std::make_shared<DataTypeUInt64>(), "Maximum count of simultaneous threads executing the query."},
{"ProfileEvents", std::make_shared<DataTypeMap>(low_cardinality_string, std::make_shared<DataTypeUInt64>()), "ProfileEvents that measure different metrics. The description of them could be found in the table system.events"},
{"Settings", std::make_shared<DataTypeMap>(low_cardinality_string, low_cardinality_string), "Settings that were changed when the client ran the query. To enable logging changes to settings, set the log_query_settings parameter to 1."},
{"used_aggregate_functions", array_low_cardinality_string, "Canonical names of aggregate functions, which were used during query execution."},
{"used_aggregate_function_combinators", array_low_cardinality_string, "Canonical names of aggregate functions combinators, which were used during query execution."},
{"used_database_engines", array_low_cardinality_string, "Canonical names of database engines, which were used during query execution."},
{"used_data_type_families", array_low_cardinality_string, "Canonical names of data type families, which were used during query execution."},
{"used_dictionaries", array_low_cardinality_string, "Canonical names of dictionaries, which were used during query execution."},
{"used_formats", array_low_cardinality_string, "Canonical names of formats, which were used during query execution."},
{"used_functions", array_low_cardinality_string, "Canonical names of functions, which were used during query execution."},
{"used_storages", array_low_cardinality_string, "Canonical names of storages, which were used during query execution."},
{"used_table_functions", array_low_cardinality_string, "Canonical names of table functions, which were used during query execution."},
2022-10-20 07:46:16 +00:00
{"used_row_policies", array_low_cardinality_string, "The list of row policies names that were used during query execution."},
{"transaction_id", getTransactionIDDataType(), "The identifier of the transaction in scope of which this query was executed."},
2023-02-07 17:50:31 +00:00
{"query_cache_usage", std::move(query_cache_usage_datatype), "Usage of the query cache during query execution. Values: 'Unknown' = Status unknown, 'None' = The query result was neither written into nor read from the query cache, 'Write' = The query result was written into the query cache, 'Read' = The query result was read from the query cache."},
{"asynchronous_read_counters", std::make_shared<DataTypeMap>(low_cardinality_string, std::make_shared<DataTypeUInt64>()), "Metrics for asynchronous reading."},
2020-09-04 16:10:29 +00:00
};
}
NamesAndAliases QueryLogElement::getNamesAndAliases()
{
auto low_cardinality_string = std::make_shared<DataTypeLowCardinality>(std::make_shared<DataTypeString>());
auto array_low_cardinality_string = std::make_shared<DataTypeArray>(low_cardinality_string);
return
{
{"ProfileEvents.Names", array_low_cardinality_string, "mapKeys(ProfileEvents)"},
{"ProfileEvents.Values", {std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt64>())}, "mapValues(ProfileEvents)"},
{"Settings.Names", array_low_cardinality_string, "mapKeys(Settings)" },
{"Settings.Values", array_low_cardinality_string, "mapValues(Settings)"}
};
}
void QueryLogElement::appendToBlock(MutableColumns & columns) const
{
2016-10-27 17:48:12 +00:00
size_t i = 0;
2023-10-21 05:40:45 +00:00
columns[i++]->insert(getFQDNOrHostName());
2019-08-01 11:11:08 +00:00
columns[i++]->insert(type);
Extended range of DateTime64 to years 1925 - 2238 The Year 1925 is a starting point because most of the timezones switched to saner (mostly 15-minutes based) offsets somewhere during 1924 or before. And that significantly simplifies implementation. 2238 is to simplify arithmetics for sanitizing LUT index access; there are less than 0x1ffff days from 1925. * Extended DateLUTImpl internal LUT to 0x1ffff items, some of which represent negative (pre-1970) time values. As a collateral benefit, Date now correctly supports dates up to 2149 (instead of 2106). * Added a new strong typedef ExtendedDayNum, which represents dates pre-1970 and post 2149. * Functions that used to return DayNum now return ExtendedDayNum. * Refactored DateLUTImpl to untie DayNum from the dual role of being a value and an index (due to negative time). Index is now a different type LUTIndex with explicit conversion functions from DatNum, time_t, and ExtendedDayNum. * Updated DateLUTImpl to properly support values close to epoch start (1970-01-01 00:00), including negative ones. * Reduced resolution of DateLUTImpl::Values::time_at_offset_change to multiple of 15-minutes to allow storing 64-bits of time_t in DateLUTImpl::Value while keeping same size. * Minor performance updates to DateLUTImpl when building month LUT by skipping non-start-of-month days. * Fixed extractTimeZoneFromFunctionArguments to work correctly with DateTime64. * New unit-tests and stateless integration tests for both DateTime and DateTime64.
2020-04-17 13:26:44 +00:00
columns[i++]->insert(DateLUT::instance().toDayNum(event_time).toUnderType());
columns[i++]->insert(event_time);
columns[i++]->insert(event_time_microseconds);
columns[i++]->insert(query_start_time);
columns[i++]->insert(query_start_time_microseconds);
columns[i++]->insert(query_duration_ms);
columns[i++]->insert(read_rows);
columns[i++]->insert(read_bytes);
columns[i++]->insert(written_rows);
columns[i++]->insert(written_bytes);
columns[i++]->insert(result_rows);
columns[i++]->insert(result_bytes);
columns[i++]->insert(memory_usage);
columns[i++]->insertData(current_database.data(), current_database.size());
columns[i++]->insertData(query.data(), query.size());
columns[i++]->insertData(formatted_query.data(), formatted_query.size());
columns[i++]->insert(normalized_query_hash);
2023-02-01 02:31:31 +00:00
const std::string_view query_kind_str = magic_enum::enum_name(query_kind);
columns[i++]->insertData(query_kind_str.data(), query_kind_str.size());
{
auto & column_databases = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_tables = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_columns = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_partitions = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_projections = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_views = typeid_cast<ColumnArray &>(*columns[i++]);
2021-01-22 09:02:58 +00:00
auto fill_column = [](const std::set<String> & data, ColumnArray & column)
{
size_t size = 0;
for (const auto & name : data)
{
column.getData().insertData(name.data(), name.size());
++size;
}
auto & offsets = column.getOffsets();
offsets.push_back(offsets.back() + size);
};
2021-01-21 21:15:11 +00:00
fill_column(query_databases, column_databases);
fill_column(query_tables, column_tables);
fill_column(query_columns, column_columns);
fill_column(query_partitions, column_partitions);
fill_column(query_projections, column_projections);
fill_column(query_views, column_views);
}
columns[i++]->insert(exception_code);
2020-01-22 12:29:30 +00:00
columns[i++]->insertData(exception.data(), exception.size());
columns[i++]->insertData(stack_trace.data(), stack_trace.size());
appendClientInfo(client_info, columns, i);
2020-09-17 12:15:05 +00:00
columns[i++]->insert(ClickHouseRevision::getVersionRevision());
2021-01-25 19:18:23 +00:00
columns[i++]->insertData(log_comment.data(), log_comment.size());
{
Array threads_array;
2020-02-02 02:27:15 +00:00
threads_array.reserve(thread_ids.size());
for (const UInt64 thread_id : thread_ids)
threads_array.emplace_back(thread_id);
columns[i++]->insert(threads_array);
}
2023-09-07 01:32:39 +00:00
columns[i++]->insert(peak_threads_usage);
if (profile_counters)
{
auto * column = columns[i++].get();
ProfileEvents::dumpToMapColumn(*profile_counters, column, true);
}
else
{
columns[i++]->insertDefault();
}
if (query_settings)
{
auto * column = columns[i++].get();
query_settings->dumpToMapColumn(column, true);
}
else
{
columns[i++]->insertDefault();
}
2021-01-21 21:15:11 +00:00
{
2021-01-22 09:02:58 +00:00
auto & column_aggregate_function_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_aggregate_function_combinator_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
2021-01-22 09:02:58 +00:00
auto & column_database_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_data_type_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_dictionary_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_format_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_function_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_storage_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_table_function_factory_objects = typeid_cast<ColumnArray &>(*columns[i++]);
auto & column_row_policies_names = typeid_cast<ColumnArray &>(*columns[i++]);
2021-01-22 09:02:58 +00:00
auto fill_column = [](const auto & data, ColumnArray & column)
2021-01-21 21:15:11 +00:00
{
size_t size = 0;
for (const auto & value : data)
2021-01-21 21:15:11 +00:00
{
column.getData().insert(value);
2021-01-21 21:15:11 +00:00
++size;
}
auto & offsets = column.getOffsets();
offsets.push_back(offsets.back() + size);
};
fill_column(used_aggregate_functions, column_aggregate_function_factory_objects);
fill_column(used_aggregate_function_combinators, column_aggregate_function_combinator_factory_objects);
fill_column(used_database_engines, column_database_factory_objects);
fill_column(used_data_type_families, column_data_type_factory_objects);
2021-01-21 21:15:11 +00:00
fill_column(used_dictionaries, column_dictionary_factory_objects);
fill_column(used_formats, column_format_factory_objects);
fill_column(used_functions, column_function_factory_objects);
fill_column(used_storages, column_storage_factory_objects);
fill_column(used_table_functions, column_table_function_factory_objects);
2022-10-21 16:13:36 +00:00
fill_column(used_row_policies, column_row_policies_names);
2021-01-21 21:15:11 +00:00
}
2021-03-31 17:55:04 +00:00
columns[i++]->insert(Tuple{tid.start_csn, tid.local_tid, tid.host_id});
2023-02-07 17:50:31 +00:00
columns[i++]->insert(query_cache_usage);
2023-02-07 17:50:31 +00:00
if (async_read_counters)
async_read_counters->dumpToMapColumn(columns[i++].get());
else
columns[i++]->insertDefault();
}
void QueryLogElement::appendClientInfo(const ClientInfo & client_info, MutableColumns & columns, size_t & i)
{
columns[i++]->insert(client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY);
columns[i++]->insert(client_info.current_user);
columns[i++]->insert(client_info.current_query_id);
columns[i++]->insertData(IPv6ToBinary(client_info.current_address.host()).data(), 16);
columns[i++]->insert(client_info.current_address.port());
columns[i++]->insert(client_info.initial_user);
columns[i++]->insert(client_info.initial_query_id);
columns[i++]->insertData(IPv6ToBinary(client_info.initial_address.host()).data(), 16);
columns[i++]->insert(client_info.initial_address.port());
columns[i++]->insert(client_info.initial_query_start_time);
columns[i++]->insert(client_info.initial_query_start_time_microseconds);
columns[i++]->insert(static_cast<UInt64>(client_info.interface));
2022-03-28 08:30:34 +00:00
columns[i++]->insert(static_cast<UInt64>(client_info.is_secure));
columns[i++]->insert(client_info.os_user);
columns[i++]->insert(client_info.client_hostname);
columns[i++]->insert(client_info.client_name);
2020-09-17 12:15:05 +00:00
columns[i++]->insert(client_info.client_tcp_protocol_version);
columns[i++]->insert(client_info.client_version_major);
columns[i++]->insert(client_info.client_version_minor);
columns[i++]->insert(client_info.client_version_patch);
columns[i++]->insert(static_cast<UInt64>(client_info.http_method));
columns[i++]->insert(client_info.http_user_agent);
2021-01-21 22:55:45 +00:00
columns[i++]->insert(client_info.http_referer);
columns[i++]->insert(client_info.forwarded_for);
columns[i++]->insert(client_info.quota_key);
columns[i++]->insert(client_info.distributed_depth);
}
}