#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { Block QueryLogElement::createBlock() { auto query_status_datatype = std::make_shared( DataTypeEnum8::Values { {"QueryStart", static_cast(QUERY_START)}, {"QueryFinish", static_cast(QUERY_FINISH)}, {"ExceptionBeforeStart", static_cast(EXCEPTION_BEFORE_START)}, {"ExceptionWhileProcessing", static_cast(EXCEPTION_WHILE_PROCESSING)} }); return { {std::move(query_status_datatype), "type"}, {std::make_shared(), "event_date"}, {std::make_shared(), "event_time"}, {std::make_shared(6), "event_time_microseconds"}, {std::make_shared(), "query_start_time"}, {std::make_shared(6), "query_start_time_microseconds"}, {std::make_shared(), "query_duration_ms"}, {std::make_shared(), "read_rows"}, {std::make_shared(), "read_bytes"}, {std::make_shared(), "written_rows"}, {std::make_shared(), "written_bytes"}, {std::make_shared(), "result_rows"}, {std::make_shared(), "result_bytes"}, {std::make_shared(), "memory_usage"}, {std::make_shared(), "current_database"}, {std::make_shared(), "query"}, {std::make_shared(), "normalized_query_hash"}, {std::make_shared(std::make_shared()), "query_kind"}, {std::make_shared( std::make_shared(std::make_shared())), "databases"}, {std::make_shared( std::make_shared(std::make_shared())), "tables"}, {std::make_shared( std::make_shared(std::make_shared())), "columns"}, {std::make_shared(), "exception_code"}, {std::make_shared(), "exception"}, {std::make_shared(), "stack_trace"}, {std::make_shared(), "is_initial_query"}, {std::make_shared(), "user"}, {std::make_shared(), "query_id"}, {DataTypeFactory::instance().get("IPv6"), "address"}, {std::make_shared(), "port"}, {std::make_shared(), "initial_user"}, {std::make_shared(), "initial_query_id"}, {DataTypeFactory::instance().get("IPv6"), "initial_address"}, {std::make_shared(), "initial_port"}, {std::make_shared(), "interface"}, {std::make_shared(), "os_user"}, {std::make_shared(), "client_hostname"}, {std::make_shared(), "client_name"}, {std::make_shared(), "client_revision"}, {std::make_shared(), "client_version_major"}, {std::make_shared(), "client_version_minor"}, {std::make_shared(), "client_version_patch"}, {std::make_shared(), "http_method"}, {std::make_shared(), "http_user_agent"}, {std::make_shared(), "http_referer"}, {std::make_shared(), "forwarded_for"}, {std::make_shared(), "quota_key"}, {std::make_shared(), "revision"}, {std::make_shared(), "log_comment"}, {std::make_shared(std::make_shared()), "thread_ids"}, {std::make_shared(std::make_shared()), "ProfileEvents.Names"}, {std::make_shared(std::make_shared()), "ProfileEvents.Values"}, {std::make_shared(std::make_shared()), "Settings.Names"}, {std::make_shared(std::make_shared()), "Settings.Values"}, {std::make_shared(std::make_shared()), "used_aggregate_functions"}, {std::make_shared(std::make_shared()), "used_aggregate_function_combinators"}, {std::make_shared(std::make_shared()), "used_database_engines"}, {std::make_shared(std::make_shared()), "used_data_type_families"}, {std::make_shared(std::make_shared()), "used_dictionaries"}, {std::make_shared(std::make_shared()), "used_formats"}, {std::make_shared(std::make_shared()), "used_functions"}, {std::make_shared(std::make_shared()), "used_storages"}, {std::make_shared(std::make_shared()), "used_table_functions"} }; } void QueryLogElement::appendToBlock(MutableColumns & columns) const { size_t i = 0; columns[i++]->insert(type); 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++]->insert(normalized_query_hash); columns[i++]->insertData(query_kind.data(), query_kind.size()); { auto & column_databases = typeid_cast(*columns[i++]); auto & column_tables = typeid_cast(*columns[i++]); auto & column_columns = typeid_cast(*columns[i++]); auto fill_column = [](const std::set & 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); }; fill_column(query_databases, column_databases); fill_column(query_tables, column_tables); fill_column(query_columns, column_columns); } columns[i++]->insert(exception_code); columns[i++]->insertData(exception.data(), exception.size()); columns[i++]->insertData(stack_trace.data(), stack_trace.size()); appendClientInfo(client_info, columns, i); columns[i++]->insert(ClickHouseRevision::getVersionRevision()); columns[i++]->insertData(log_comment.data(), log_comment.size()); { Array threads_array; threads_array.reserve(thread_ids.size()); for (const UInt64 thread_id : thread_ids) threads_array.emplace_back(thread_id); columns[i++]->insert(threads_array); } if (profile_counters) { auto * column_names = columns[i++].get(); auto * column_values = columns[i++].get(); ProfileEvents::dumpToArrayColumns(*profile_counters, column_names, column_values, true); } else { columns[i++]->insertDefault(); columns[i++]->insertDefault(); } if (query_settings) { auto * column_names = columns[i++].get(); auto * column_values = columns[i++].get(); query_settings->dumpToArrayColumns(column_names, column_values, true); } else { columns[i++]->insertDefault(); columns[i++]->insertDefault(); } { auto & column_aggregate_function_factory_objects = typeid_cast(*columns[i++]); auto & column_aggregate_function_combinator_factory_objects = typeid_cast(*columns[i++]); auto & column_database_factory_objects = typeid_cast(*columns[i++]); auto & column_data_type_factory_objects = typeid_cast(*columns[i++]); auto & column_dictionary_factory_objects = typeid_cast(*columns[i++]); auto & column_format_factory_objects = typeid_cast(*columns[i++]); auto & column_function_factory_objects = typeid_cast(*columns[i++]); auto & column_storage_factory_objects = typeid_cast(*columns[i++]); auto & column_table_function_factory_objects = typeid_cast(*columns[i++]); auto fill_column = [](const std::unordered_set & 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); }; 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); 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); } } 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(UInt64(client_info.interface)); columns[i++]->insert(client_info.os_user); columns[i++]->insert(client_info.client_hostname); columns[i++]->insert(client_info.client_name); 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(UInt64(client_info.http_method)); columns[i++]->insert(client_info.http_user_agent); columns[i++]->insert(client_info.http_referer); columns[i++]->insert(client_info.forwarded_for); columns[i++]->insert(client_info.quota_key); } }