2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/formatReadable.h>
|
2019-10-07 18:56:03 +00:00
|
|
|
#include <Common/PODArray.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
|
|
|
#include <IO/ConcatReadBuffer.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
2018-11-30 15:36:41 +00:00
|
|
|
#include <IO/WriteBufferFromVector.h>
|
|
|
|
#include <IO/LimitReadBuffer.h>
|
|
|
|
#include <IO/copyData.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
|
|
|
#include <DataStreams/BlockIO.h>
|
|
|
|
#include <DataStreams/copyData.h>
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/IBlockInputStream.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/InputStreamFromASTInsertQuery.h>
|
|
|
|
#include <DataStreams/CountingBlockOutputStream.h>
|
|
|
|
|
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
|
|
|
#include <Parsers/ASTShowProcesslistQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ParserQuery.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
2019-05-28 18:30:10 +00:00
|
|
|
#include <Storages/StorageInput.h>
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Access/EnabledQuota.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterFactory.h>
|
|
|
|
#include <Interpreters/ProcessList.h>
|
|
|
|
#include <Interpreters/QueryLog.h>
|
2019-01-23 19:23:37 +00:00
|
|
|
#include <Interpreters/InterpreterSetQuery.h>
|
2019-05-18 21:07:23 +00:00
|
|
|
#include <Interpreters/ReplaceQueryParameterVisitor.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/executeQuery.h>
|
2019-07-19 07:44:18 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2019-07-19 13:50:11 +00:00
|
|
|
|
2019-04-29 15:01:53 +00:00
|
|
|
#include <Interpreters/DNSCacheUpdater.h>
|
2019-09-06 17:48:27 +00:00
|
|
|
#include <Common/SensitiveDataMasker.h>
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2019-03-26 18:28:37 +00:00
|
|
|
#include <Processors/Transforms/LimitsCheckingTransform.h>
|
2019-04-12 13:56:48 +00:00
|
|
|
#include <Processors/Transforms/MaterializingTransform.h>
|
2019-03-26 18:28:37 +00:00
|
|
|
#include <Processors/Formats/IOutputFormat.h>
|
2019-09-11 11:38:05 +00:00
|
|
|
#include <Parsers/ASTWatchQuery.h>
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2020-03-28 03:02:26 +00:00
|
|
|
|
2019-06-20 07:17:21 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event QueryMaskingRulesMatch;
|
|
|
|
}
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int INTO_OUTFILE_NOT_ALLOWED;
|
2018-11-01 14:56:37 +00:00
|
|
|
extern const int QUERY_WAS_CANCELLED;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2018-03-11 00:15:26 +00:00
|
|
|
static void checkASTSizeLimits(const IAST & ast, const Settings & settings)
|
2012-12-26 20:29:28 +00:00
|
|
|
{
|
2018-03-11 00:15:26 +00:00
|
|
|
if (settings.max_ast_depth)
|
|
|
|
ast.checkDepth(settings.max_ast_depth);
|
|
|
|
if (settings.max_ast_elements)
|
|
|
|
ast.checkSize(settings.max_ast_elements);
|
2012-12-26 20:29:28 +00:00
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2018-08-23 01:31:28 +00:00
|
|
|
/// NOTE This is wrong in case of single-line comments and in case of multiline string literals.
|
2016-08-16 20:33:00 +00:00
|
|
|
static String joinLines(const String & query)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
String res = query;
|
|
|
|
std::replace(res.begin(), res.end(), '\n', ' ');
|
|
|
|
return res;
|
2016-08-16 20:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-20 07:17:21 +00:00
|
|
|
static String prepareQueryForLogging(const String & query, Context & context)
|
|
|
|
{
|
|
|
|
String res = query;
|
|
|
|
|
2019-07-19 13:50:11 +00:00
|
|
|
// wiping sensitive data before cropping query by log_queries_cut_to_length,
|
|
|
|
// otherwise something like credit card without last digit can go to log
|
2019-09-06 17:48:27 +00:00
|
|
|
if (auto masker = SensitiveDataMasker::getInstance())
|
2019-06-20 07:17:21 +00:00
|
|
|
{
|
|
|
|
auto matches = masker->wipeSensitiveData(res);
|
|
|
|
if (matches > 0)
|
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::QueryMaskingRulesMatch, matches);
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 13:50:11 +00:00
|
|
|
|
|
|
|
res = res.substr(0, context.getSettingsRef().log_queries_cut_to_length);
|
|
|
|
|
2019-06-20 07:17:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-16 20:33:00 +00:00
|
|
|
/// Log query into text log (not into system table).
|
2019-03-04 18:28:42 +00:00
|
|
|
static void logQuery(const String & query, const Context & context, bool internal)
|
2015-06-17 21:34:15 +00:00
|
|
|
{
|
2019-03-04 18:28:42 +00:00
|
|
|
if (internal)
|
|
|
|
{
|
|
|
|
LOG_DEBUG(&Logger::get("executeQuery"), "(internal) " << joinLines(query));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto & current_query_id = context.getClientInfo().current_query_id;
|
|
|
|
const auto & initial_query_id = context.getClientInfo().initial_query_id;
|
|
|
|
const auto & current_user = context.getClientInfo().current_user;
|
|
|
|
|
|
|
|
LOG_DEBUG(&Logger::get("executeQuery"), "(from " << context.getClientInfo().current_address.toString()
|
|
|
|
<< (current_user != "default" ? ", user: " + context.getClientInfo().current_user : "")
|
|
|
|
<< (!initial_query_id.empty() && current_query_id != initial_query_id ? ", initial_query_id: " + initial_query_id : std::string())
|
|
|
|
<< ") "
|
|
|
|
<< joinLines(query));
|
|
|
|
}
|
2016-08-16 20:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Call this inside catch block.
|
|
|
|
static void setExceptionStackTrace(QueryLogElement & elem)
|
|
|
|
{
|
2019-10-10 16:30:33 +00:00
|
|
|
/// Disable memory tracker for stack trace.
|
|
|
|
/// Because if exception is "Memory limit (for query) exceed", then we probably can't allocate another one string.
|
2019-10-10 18:55:16 +00:00
|
|
|
auto temporarily_disable_memory_tracker = getCurrentMemoryTrackerActionLock();
|
2019-10-10 16:30:33 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2020-01-02 07:37:13 +00:00
|
|
|
catch (const std::exception & e)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-01-02 07:37:13 +00:00
|
|
|
elem.stack_trace = getExceptionStackTraceString(e);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...) {}
|
2016-08-16 20:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Log exception (with query info) into text log (not into system table).
|
|
|
|
static void logException(Context & context, QueryLogElement & elem)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_ERROR(&Logger::get("executeQuery"), elem.exception
|
|
|
|
<< " (from " << context.getClientInfo().current_address.toString() << ")"
|
|
|
|
<< " (in query: " << joinLines(elem.query) << ")"
|
2019-11-23 00:18:56 +00:00
|
|
|
<< (!elem.stack_trace.empty() ? ", Stack trace (when copying this message, always include the lines below):\n\n" + elem.stack_trace : ""));
|
2015-06-17 21:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-20 07:17:21 +00:00
|
|
|
static void onExceptionBeforeStart(const String & query_for_logging, Context & context, time_t current_time)
|
2015-07-01 05:18:54 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Exception before the query execution.
|
2020-03-07 17:37:38 +00:00
|
|
|
if (auto quota = context.getQuota())
|
|
|
|
quota->used(Quota::ERRORS, 1, /* check_exceeded = */ false);
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-08-23 01:31:28 +00:00
|
|
|
const Settings & settings = context.getSettingsRef();
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Log the start of query execution into the table if necessary.
|
2018-05-30 16:15:35 +00:00
|
|
|
QueryLogElement elem;
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-05-30 16:15:35 +00:00
|
|
|
elem.type = QueryLogElement::EXCEPTION_BEFORE_START;
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-05-30 16:15:35 +00:00
|
|
|
elem.event_time = current_time;
|
|
|
|
elem.query_start_time = current_time;
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2019-06-20 07:17:21 +00:00
|
|
|
elem.query = query_for_logging;
|
2020-01-22 12:29:30 +00:00
|
|
|
elem.exception_code = getCurrentExceptionCode();
|
2018-05-30 16:15:35 +00:00
|
|
|
elem.exception = getCurrentExceptionMessage(false);
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-05-30 16:15:35 +00:00
|
|
|
elem.client_info = context.getClientInfo();
|
2016-10-24 21:40:39 +00:00
|
|
|
|
2018-08-23 01:31:28 +00:00
|
|
|
if (settings.calculate_text_stack_trace)
|
|
|
|
setExceptionStackTrace(elem);
|
2018-05-30 16:15:35 +00:00
|
|
|
logException(context, elem);
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-06-19 20:30:35 +00:00
|
|
|
/// Update performance counters before logging to query_log
|
|
|
|
CurrentThread::finalizePerformanceCounters();
|
|
|
|
|
2018-08-23 01:31:28 +00:00
|
|
|
if (settings.log_queries)
|
2018-03-10 19:57:13 +00:00
|
|
|
if (auto query_log = context.getQueryLog())
|
|
|
|
query_log->add(elem);
|
2015-07-01 05:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
|
2017-07-10 03:41:02 +00:00
|
|
|
const char * begin,
|
|
|
|
const char * end,
|
2017-04-01 07:20:54 +00:00
|
|
|
Context & context,
|
|
|
|
bool internal,
|
2019-02-08 13:24:24 +00:00
|
|
|
QueryProcessingStage::Enum stage,
|
2019-09-02 15:19:01 +00:00
|
|
|
bool has_query_tail,
|
2019-09-09 13:20:30 +00:00
|
|
|
ReadBuffer * istr,
|
|
|
|
bool allow_processors)
|
2015-06-18 00:27:25 +00:00
|
|
|
{
|
2017-08-04 14:00:26 +00:00
|
|
|
time_t current_time = time(nullptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-10-29 10:27:19 +00:00
|
|
|
/// If we already executing query and it requires to execute internal query, than
|
|
|
|
/// don't replace thread context with given (it can be temporary). Otherwise, attach context to thread.
|
|
|
|
if (!internal)
|
|
|
|
{
|
|
|
|
context.makeQueryContext();
|
|
|
|
CurrentThread::attachQueryContext(context);
|
|
|
|
}
|
2018-03-02 05:44:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const Settings & settings = context.getSettingsRef();
|
|
|
|
|
2018-10-09 14:32:11 +00:00
|
|
|
ParserQuery parser(end, settings.enable_debug_queries);
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr ast;
|
2019-01-18 16:30:35 +00:00
|
|
|
const char * query_end;
|
2017-07-28 18:43:40 +00:00
|
|
|
|
|
|
|
/// Don't limit the size of internal queries.
|
|
|
|
size_t max_query_size = 0;
|
|
|
|
if (!internal)
|
|
|
|
max_query_size = settings.max_query_size;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-07-28 18:43:40 +00:00
|
|
|
/// TODO Parser should fail early when max_query_size limit is reached.
|
2020-01-14 11:11:01 +00:00
|
|
|
ast = parseQuery(parser, begin, end, "", max_query_size, settings.max_parser_depth);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
auto * insert_query = ast->as<ASTInsertQuery>();
|
2019-03-29 14:50:48 +00:00
|
|
|
|
|
|
|
if (insert_query && insert_query->settings_ast)
|
|
|
|
InterpreterSetQuery(insert_query->settings_ast, context).executeForCurrentContext();
|
|
|
|
|
2019-01-18 16:30:35 +00:00
|
|
|
if (insert_query && insert_query->data)
|
2019-02-08 13:24:24 +00:00
|
|
|
{
|
2019-01-18 16:30:35 +00:00
|
|
|
query_end = insert_query->data;
|
2019-02-08 13:24:24 +00:00
|
|
|
insert_query->has_tail = has_query_tail;
|
|
|
|
}
|
2019-01-18 16:30:35 +00:00
|
|
|
else
|
2019-06-15 18:22:48 +00:00
|
|
|
{
|
2019-01-18 16:30:35 +00:00
|
|
|
query_end = end;
|
2019-06-15 18:22:48 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2019-03-04 18:28:42 +00:00
|
|
|
/// Anyway log the query.
|
|
|
|
String query = String(begin, begin + std::min(end - begin, static_cast<ptrdiff_t>(max_query_size)));
|
2019-06-20 07:17:21 +00:00
|
|
|
|
|
|
|
auto query_for_logging = prepareQueryForLogging(query, context);
|
|
|
|
logQuery(query_for_logging, context, internal);
|
2019-03-04 18:28:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!internal)
|
2019-06-20 07:17:21 +00:00
|
|
|
onExceptionBeforeStart(query_for_logging, context, current_time);
|
2015-06-18 00:27:25 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
throw;
|
|
|
|
}
|
2015-06-18 00:27:25 +00:00
|
|
|
|
2019-01-18 16:30:35 +00:00
|
|
|
/// Copy query into string. It will be written to log and presented in processlist. If an INSERT query, string will not include data to insertion.
|
|
|
|
String query(begin, query_end);
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockIO res;
|
2019-03-26 18:28:37 +00:00
|
|
|
QueryPipeline & pipeline = res.pipeline;
|
2015-06-18 00:27:25 +00:00
|
|
|
|
2020-03-09 00:28:05 +00:00
|
|
|
String query_for_logging;
|
2019-06-20 07:17:21 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
2019-06-15 18:22:48 +00:00
|
|
|
/// Replace ASTQueryParameter with ASTLiteral for prepared statements.
|
2019-06-16 17:32:37 +00:00
|
|
|
if (context.hasQueryParameters())
|
|
|
|
{
|
|
|
|
ReplaceQueryParameterVisitor visitor(context.getQueryParameters());
|
|
|
|
visitor.visit(ast);
|
2019-06-15 18:22:48 +00:00
|
|
|
|
2019-07-07 23:25:15 +00:00
|
|
|
/// Get new query after substitutions.
|
2019-06-04 18:15:32 +00:00
|
|
|
query = serializeAST(*ast);
|
2019-07-07 23:25:15 +00:00
|
|
|
}
|
2019-06-04 18:15:32 +00:00
|
|
|
|
2019-06-20 07:17:21 +00:00
|
|
|
query_for_logging = prepareQueryForLogging(query, context);
|
|
|
|
|
|
|
|
logQuery(query_for_logging, context, internal);
|
2015-06-18 00:27:25 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Check the limits.
|
2018-03-11 00:15:26 +00:00
|
|
|
checkASTSizeLimits(*ast, settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Put query to process list. But don't put SHOW PROCESSLIST query itself.
|
|
|
|
ProcessList::EntryPtr process_list_entry;
|
2019-03-11 13:22:51 +00:00
|
|
|
if (!internal && !ast->as<ASTShowProcesslistQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-06-20 07:17:21 +00:00
|
|
|
/// processlist also has query masked now, to avoid secrets leaks though SHOW PROCESSLIST by other users.
|
|
|
|
process_list_entry = context.getProcessList().insert(query_for_logging, ast.get(), context);
|
2017-04-01 07:20:54 +00:00
|
|
|
context.setProcessListElement(&process_list_entry->get());
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:32:27 +00:00
|
|
|
/// Load external tables if they were provided
|
|
|
|
context.initializeExternalTablesIfSet();
|
|
|
|
|
2019-05-28 18:30:10 +00:00
|
|
|
auto * insert_query = ast->as<ASTInsertQuery>();
|
2019-05-30 20:12:44 +00:00
|
|
|
if (insert_query && insert_query->select)
|
2019-05-28 18:30:10 +00:00
|
|
|
{
|
2019-05-30 20:12:44 +00:00
|
|
|
/// Prepare Input storage before executing interpreter if we already got a buffer with data.
|
2019-05-28 18:30:10 +00:00
|
|
|
if (istr)
|
|
|
|
{
|
2019-05-30 20:12:44 +00:00
|
|
|
ASTPtr input_function;
|
2019-05-30 21:33:06 +00:00
|
|
|
insert_query->tryFindInputFunction(input_function);
|
2019-05-30 20:12:44 +00:00
|
|
|
if (input_function)
|
|
|
|
{
|
|
|
|
StoragePtr storage = context.executeTableFunction(input_function);
|
|
|
|
auto & input_storage = dynamic_cast<StorageInput &>(*storage);
|
|
|
|
BlockInputStreamPtr input_stream = std::make_shared<InputStreamFromASTInsertQuery>(ast, istr,
|
|
|
|
input_storage.getSampleBlock(), context, input_function);
|
|
|
|
input_storage.setInputStream(input_stream);
|
|
|
|
}
|
2019-05-28 18:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/// reset Input callbacks if query is not INSERT SELECT
|
|
|
|
context.resetInputCallbacks();
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto interpreter = InterpreterFactory::get(ast, context, stage);
|
2019-09-02 15:19:01 +00:00
|
|
|
bool use_processors = settings.experimental_use_processors && allow_processors && interpreter->canExecuteWithProcessors();
|
2019-03-26 18:28:37 +00:00
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::shared_ptr<const EnabledQuota> quota;
|
2019-11-11 01:11:32 +00:00
|
|
|
if (!interpreter->ignoreQuota())
|
|
|
|
{
|
|
|
|
quota = context.getQuota();
|
2020-03-07 17:37:38 +00:00
|
|
|
if (quota)
|
|
|
|
{
|
|
|
|
quota->used(Quota::QUERIES, 1);
|
|
|
|
quota->checkExceeded(Quota::ERRORS);
|
|
|
|
}
|
2019-11-11 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IBlockInputStream::LocalLimits limits;
|
|
|
|
if (!interpreter->ignoreLimits())
|
|
|
|
{
|
|
|
|
limits.mode = IBlockInputStream::LIMITS_CURRENT;
|
|
|
|
limits.size_limits = SizeLimits(settings.max_result_rows, settings.max_result_bytes, settings.result_overflow_mode);
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:28:37 +00:00
|
|
|
if (use_processors)
|
|
|
|
pipeline = interpreter->executeWithProcessors();
|
|
|
|
else
|
|
|
|
res = interpreter->execute();
|
|
|
|
|
2018-11-15 15:03:13 +00:00
|
|
|
if (auto * insert_interpreter = typeid_cast<const InterpreterInsertQuery *>(&*interpreter))
|
2019-07-17 18:30:17 +00:00
|
|
|
{
|
|
|
|
/// Save insertion table (not table function). TODO: support remote() table function.
|
2020-03-02 20:23:58 +00:00
|
|
|
auto table_id = insert_interpreter->getDatabaseTable();
|
|
|
|
if (!table_id.empty())
|
|
|
|
context.setInsertionTable(std::move(table_id));
|
2019-07-17 18:30:17 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (process_list_entry)
|
2018-11-01 14:56:37 +00:00
|
|
|
{
|
|
|
|
/// Query was killed before execution
|
|
|
|
if ((*process_list_entry)->isKilled())
|
|
|
|
throw Exception("Query '" + (*process_list_entry)->getInfo().client_info.current_query_id + "' is killed in pending state",
|
|
|
|
ErrorCodes::QUERY_WAS_CANCELLED);
|
2019-03-26 18:28:37 +00:00
|
|
|
else if (!use_processors)
|
2018-11-01 14:56:37 +00:00
|
|
|
(*process_list_entry)->setQueryStreams(res);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Hold element of process list till end of query execution.
|
|
|
|
res.process_list_entry = process_list_entry;
|
|
|
|
|
2019-03-26 18:28:37 +00:00
|
|
|
if (use_processors)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-01-23 14:48:50 +00:00
|
|
|
/// Limits on the result, the quota on the result, and also callback for progress.
|
|
|
|
/// Limits apply only to the final result.
|
2019-11-11 01:11:32 +00:00
|
|
|
pipeline.setProgressCallback(context.getProgressCallback());
|
|
|
|
pipeline.setProcessListElement(context.getProcessListElement());
|
2019-01-23 14:48:50 +00:00
|
|
|
if (stage == QueryProcessingStage::Complete)
|
|
|
|
{
|
2019-03-26 18:28:37 +00:00
|
|
|
pipeline.resize(1);
|
2019-04-05 11:34:11 +00:00
|
|
|
pipeline.addSimpleTransform([&](const Block & header)
|
|
|
|
{
|
2019-03-26 18:28:37 +00:00
|
|
|
auto transform = std::make_shared<LimitsCheckingTransform>(header, limits);
|
|
|
|
transform->setQuota(quota);
|
|
|
|
return transform;
|
|
|
|
});
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 18:28:37 +00:00
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-11-11 01:11:32 +00:00
|
|
|
/// Limits on the result, the quota on the result, and also callback for progress.
|
|
|
|
/// Limits apply only to the final result.
|
2019-03-26 18:28:37 +00:00
|
|
|
if (res.in)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-26 18:28:37 +00:00
|
|
|
res.in->setProgressCallback(context.getProgressCallback());
|
|
|
|
res.in->setProcessListElement(context.getProcessListElement());
|
|
|
|
if (stage == QueryProcessingStage::Complete)
|
|
|
|
{
|
2019-11-11 01:11:32 +00:00
|
|
|
if (!interpreter->ignoreQuota())
|
|
|
|
res.in->setQuota(quota);
|
|
|
|
if (!interpreter->ignoreLimits())
|
|
|
|
res.in->setLimits(limits);
|
2019-03-26 18:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.out)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-26 18:28:37 +00:00
|
|
|
if (auto stream = dynamic_cast<CountingBlockOutputStream *>(res.out.get()))
|
|
|
|
{
|
|
|
|
stream->setProcessListElement(context.getProcessListElement());
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Everything related to query log.
|
|
|
|
{
|
|
|
|
QueryLogElement elem;
|
|
|
|
|
|
|
|
elem.type = QueryLogElement::QUERY_START;
|
|
|
|
|
|
|
|
elem.event_time = current_time;
|
|
|
|
elem.query_start_time = current_time;
|
|
|
|
|
2019-06-20 07:17:21 +00:00
|
|
|
elem.query = query_for_logging;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
elem.client_info = context.getClientInfo();
|
|
|
|
|
|
|
|
bool log_queries = settings.log_queries && !internal;
|
|
|
|
|
|
|
|
/// Log into system table start of query execution, if need.
|
|
|
|
if (log_queries)
|
2018-03-10 19:57:13 +00:00
|
|
|
{
|
2018-05-17 16:01:41 +00:00
|
|
|
if (settings.log_query_settings)
|
|
|
|
elem.query_settings = std::make_shared<Settings>(context.getSettingsRef());
|
|
|
|
|
2018-03-10 19:57:13 +00:00
|
|
|
if (auto query_log = context.getQueryLog())
|
|
|
|
query_log->add(elem);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Also make possible for caller to log successful query finish and exception during execution.
|
2019-03-26 18:28:37 +00:00
|
|
|
auto finish_callback = [elem, &context, log_queries] (IBlockInputStream * stream_in, IBlockOutputStream * stream_out) mutable
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-02-01 17:55:08 +00:00
|
|
|
QueryStatus * process_list_elem = context.getProcessListElement();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!process_list_elem)
|
|
|
|
return;
|
|
|
|
|
2018-06-13 19:01:07 +00:00
|
|
|
/// Update performance counters before logging to query_log
|
2018-06-19 20:30:35 +00:00
|
|
|
CurrentThread::finalizePerformanceCounters();
|
2018-06-13 19:01:07 +00:00
|
|
|
|
2018-08-27 18:16:32 +00:00
|
|
|
QueryStatusInfo info = process_list_elem->getInfo(true, context.getSettingsRef().log_profile_events);
|
2018-03-09 23:04:26 +00:00
|
|
|
|
|
|
|
double elapsed_seconds = info.elapsed_seconds;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
elem.type = QueryLogElement::QUERY_FINISH;
|
|
|
|
|
2017-08-04 14:00:26 +00:00
|
|
|
elem.event_time = time(nullptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
elem.query_duration_ms = elapsed_seconds * 1000;
|
|
|
|
|
2018-03-09 23:04:26 +00:00
|
|
|
elem.read_rows = info.read_rows;
|
|
|
|
elem.read_bytes = info.read_bytes;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-03-09 23:04:26 +00:00
|
|
|
elem.written_rows = info.written_rows;
|
|
|
|
elem.written_bytes = info.written_bytes;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-04-25 11:25:49 +00:00
|
|
|
auto progress_callback = context.getProgressCallback();
|
2019-05-06 06:57:48 +00:00
|
|
|
|
2019-04-25 11:25:49 +00:00
|
|
|
if (progress_callback)
|
|
|
|
progress_callback(Progress(WriteProgress(info.written_rows, info.written_bytes)));
|
|
|
|
|
2018-03-09 23:04:26 +00:00
|
|
|
elem.memory_usage = info.peak_memory_usage > 0 ? info.peak_memory_usage : 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (stream_in)
|
|
|
|
{
|
2019-01-23 14:48:50 +00:00
|
|
|
const BlockStreamProfileInfo & stream_in_info = stream_in->getProfileInfo();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/// NOTE: INSERT SELECT query contains zero metrics
|
|
|
|
elem.result_rows = stream_in_info.rows;
|
|
|
|
elem.result_bytes = stream_in_info.bytes;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (stream_out) /// will be used only for ordinary INSERT queries
|
|
|
|
{
|
2019-04-26 03:46:52 +00:00
|
|
|
if (auto counting_stream = dynamic_cast<const CountingBlockOutputStream *>(stream_out))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-01-22 19:56:53 +00:00
|
|
|
/// NOTE: Redundancy. The same values could be extracted from process_list_elem->progress_out.query_settings = process_list_elem->progress_in
|
2019-05-20 11:37:41 +00:00
|
|
|
elem.result_rows = counting_stream->getProgress().read_rows;
|
|
|
|
elem.result_bytes = counting_stream->getProgress().read_bytes;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elem.read_rows != 0)
|
|
|
|
{
|
|
|
|
LOG_INFO(&Logger::get("executeQuery"), std::fixed << std::setprecision(3)
|
|
|
|
<< "Read " << elem.read_rows << " rows, "
|
|
|
|
<< formatReadableSizeWithBinarySuffix(elem.read_bytes) << " in " << elapsed_seconds << " sec., "
|
|
|
|
<< static_cast<size_t>(elem.read_rows / elapsed_seconds) << " rows/sec., "
|
|
|
|
<< formatReadableSizeWithBinarySuffix(elem.read_bytes / elapsed_seconds) << "/sec.");
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:27:15 +00:00
|
|
|
elem.thread_ids = std::move(info.thread_ids);
|
2018-05-17 16:01:41 +00:00
|
|
|
elem.profile_counters = std::move(info.profile_counters);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (log_queries)
|
2018-03-10 19:57:13 +00:00
|
|
|
{
|
|
|
|
if (auto query_log = context.getQueryLog())
|
|
|
|
query_log->add(elem);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
auto exception_callback = [elem, &context, log_queries, quota(quota)] () mutable
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-03-07 17:37:38 +00:00
|
|
|
if (quota)
|
|
|
|
quota->used(Quota::ERRORS, 1, /* check_exceeded = */ false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
elem.type = QueryLogElement::EXCEPTION_WHILE_PROCESSING;
|
|
|
|
|
2017-08-04 14:00:26 +00:00
|
|
|
elem.event_time = time(nullptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
elem.query_duration_ms = 1000 * (elem.event_time - elem.query_start_time);
|
2020-01-22 12:29:30 +00:00
|
|
|
elem.exception_code = getCurrentExceptionCode();
|
2017-04-01 07:20:54 +00:00
|
|
|
elem.exception = getCurrentExceptionMessage(false);
|
|
|
|
|
2018-02-01 17:55:08 +00:00
|
|
|
QueryStatus * process_list_elem = context.getProcessListElement();
|
2018-08-27 18:16:32 +00:00
|
|
|
const Settings & current_settings = context.getSettingsRef();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-06-13 19:01:07 +00:00
|
|
|
/// Update performance counters before logging to query_log
|
2018-06-19 20:30:35 +00:00
|
|
|
CurrentThread::finalizePerformanceCounters();
|
2018-06-13 19:01:07 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (process_list_elem)
|
|
|
|
{
|
2018-08-27 18:16:32 +00:00
|
|
|
QueryStatusInfo info = process_list_elem->getInfo(true, current_settings.log_profile_events, false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-03-09 23:04:26 +00:00
|
|
|
elem.query_duration_ms = info.elapsed_seconds * 1000;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-03-09 23:04:26 +00:00
|
|
|
elem.read_rows = info.read_rows;
|
|
|
|
elem.read_bytes = info.read_bytes;
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-03-09 23:04:26 +00:00
|
|
|
elem.memory_usage = info.peak_memory_usage > 0 ? info.peak_memory_usage : 0;
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2020-02-02 02:27:15 +00:00
|
|
|
elem.thread_ids = std::move(info.thread_ids);
|
2018-05-17 16:01:41 +00:00
|
|
|
elem.profile_counters = std::move(info.profile_counters);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-08-27 18:16:32 +00:00
|
|
|
if (current_settings.calculate_text_stack_trace)
|
2018-08-23 01:31:28 +00:00
|
|
|
setExceptionStackTrace(elem);
|
2017-04-01 07:20:54 +00:00
|
|
|
logException(context, elem);
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2018-05-17 16:01:41 +00:00
|
|
|
/// In case of exception we log internal queries also
|
2017-04-01 07:20:54 +00:00
|
|
|
if (log_queries)
|
2018-03-10 19:57:13 +00:00
|
|
|
{
|
|
|
|
if (auto query_log = context.getQueryLog())
|
|
|
|
query_log->add(elem);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2019-04-05 10:52:07 +00:00
|
|
|
res.finish_callback = std::move(finish_callback);
|
|
|
|
res.exception_callback = std::move(exception_callback);
|
2019-03-26 18:28:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!internal && res.in)
|
|
|
|
{
|
|
|
|
std::stringstream log_str;
|
|
|
|
log_str << "Query pipeline:\n";
|
|
|
|
res.in->dumpTree(log_str);
|
|
|
|
LOG_DEBUG(&Logger::get("executeQuery"), log_str.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (!internal)
|
2019-06-20 07:17:21 +00:00
|
|
|
{
|
|
|
|
if (query_for_logging.empty())
|
|
|
|
query_for_logging = prepareQueryForLogging(query, context);
|
|
|
|
|
|
|
|
onExceptionBeforeStart(query_for_logging, context, current_time);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2020-02-27 15:40:11 +00:00
|
|
|
return std::make_tuple(ast, std::move(res));
|
2015-06-18 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO executeQuery(
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & query,
|
|
|
|
Context & context,
|
|
|
|
bool internal,
|
2019-02-13 11:57:47 +00:00
|
|
|
QueryProcessingStage::Enum stage,
|
2019-09-02 15:19:01 +00:00
|
|
|
bool may_have_embedded_data,
|
|
|
|
bool allow_processors)
|
2015-06-18 02:11:05 +00:00
|
|
|
{
|
2019-11-04 03:53:26 +00:00
|
|
|
ASTPtr ast;
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockIO streams;
|
2019-11-04 03:53:26 +00:00
|
|
|
std::tie(ast, streams) = executeQueryImpl(query.data(), query.data() + query.size(), context,
|
2019-09-09 13:20:30 +00:00
|
|
|
internal, stage, !may_have_embedded_data, nullptr, allow_processors);
|
2020-01-23 12:53:32 +00:00
|
|
|
|
|
|
|
if (const auto * ast_query_with_output = dynamic_cast<const ASTQueryWithOutput *>(ast.get()))
|
2019-11-04 03:53:26 +00:00
|
|
|
{
|
2020-01-23 12:59:01 +00:00
|
|
|
String format_name = ast_query_with_output->format
|
|
|
|
? getIdentifierName(ast_query_with_output->format)
|
|
|
|
: context.getDefaultFormat();
|
|
|
|
|
2019-11-04 03:53:26 +00:00
|
|
|
if (format_name == "Null")
|
|
|
|
streams.null_format = true;
|
|
|
|
}
|
2020-01-23 12:53:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return streams;
|
2015-06-18 00:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
void executeQuery(
|
2017-04-01 07:20:54 +00:00
|
|
|
ReadBuffer & istr,
|
|
|
|
WriteBuffer & ostr,
|
|
|
|
bool allow_into_outfile,
|
|
|
|
Context & context,
|
2020-03-03 15:32:41 +00:00
|
|
|
std::function<void(const String &, const String &, const String &, const String &)> set_result_details)
|
2011-10-30 11:30:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
PODArray<char> parse_buf;
|
|
|
|
const char * begin;
|
|
|
|
const char * end;
|
|
|
|
|
|
|
|
/// If 'istr' is empty now, fetch next data into buffer.
|
|
|
|
if (istr.buffer().size() == 0)
|
|
|
|
istr.next();
|
|
|
|
|
|
|
|
size_t max_query_size = context.getSettingsRef().max_query_size;
|
|
|
|
|
2019-02-20 09:22:13 +00:00
|
|
|
bool may_have_tail;
|
2018-03-26 19:41:55 +00:00
|
|
|
if (istr.buffer().end() - istr.position() > static_cast<ssize_t>(max_query_size))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
/// If remaining buffer space in 'istr' is enough to parse query up to 'max_query_size' bytes, then parse inplace.
|
|
|
|
begin = istr.position();
|
|
|
|
end = istr.buffer().end();
|
|
|
|
istr.position() += end - begin;
|
2019-02-20 09:22:13 +00:00
|
|
|
/// Actually we don't know will query has additional data or not.
|
|
|
|
/// But we can't check istr.eof(), because begin and end pointers will became invalid
|
|
|
|
may_have_tail = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// If not - copy enough data into 'parse_buf'.
|
2018-11-30 15:36:41 +00:00
|
|
|
WriteBufferFromVector<PODArray<char>> out(parse_buf);
|
|
|
|
LimitReadBuffer limit(istr, max_query_size + 1, false);
|
|
|
|
copyData(limit, out);
|
2020-01-10 21:42:26 +00:00
|
|
|
out.finalize();
|
2018-11-30 15:36:41 +00:00
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
begin = parse_buf.data();
|
2017-04-01 07:20:54 +00:00
|
|
|
end = begin + parse_buf.size();
|
2019-02-20 09:22:13 +00:00
|
|
|
/// Can check stream for eof, because we have copied data
|
|
|
|
may_have_tail = !istr.eof();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ast;
|
|
|
|
BlockIO streams;
|
|
|
|
|
2019-09-09 13:20:30 +00:00
|
|
|
std::tie(ast, streams) = executeQueryImpl(begin, end, context, false, QueryProcessingStage::Complete, may_have_tail, &istr, true);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-26 18:28:37 +00:00
|
|
|
auto & pipeline = streams.pipeline;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (streams.out)
|
|
|
|
{
|
2019-05-30 20:12:44 +00:00
|
|
|
InputStreamFromASTInsertQuery in(ast, &istr, streams.out->getHeader(), context, nullptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
copyData(in, *streams.out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streams.in)
|
|
|
|
{
|
2019-03-12 12:41:57 +00:00
|
|
|
/// FIXME: try to prettify this cast using `as<>()`
|
|
|
|
const auto * ast_query_with_output = dynamic_cast<const ASTQueryWithOutput *>(ast.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
WriteBuffer * out_buf = &ostr;
|
2017-11-20 04:15:43 +00:00
|
|
|
std::optional<WriteBufferFromFile> out_file_buf;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (ast_query_with_output && ast_query_with_output->out_file)
|
|
|
|
{
|
|
|
|
if (!allow_into_outfile)
|
|
|
|
throw Exception("INTO OUTFILE is not allowed", ErrorCodes::INTO_OUTFILE_NOT_ALLOWED);
|
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & out_file = ast_query_with_output->out_file->as<ASTLiteral &>().value.safeGet<std::string>();
|
2017-04-01 07:20:54 +00:00
|
|
|
out_file_buf.emplace(out_file, DBMS_DEFAULT_BUFFER_SIZE, O_WRONLY | O_EXCL | O_CREAT);
|
2017-10-04 00:22:00 +00:00
|
|
|
out_buf = &*out_file_buf;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String format_name = ast_query_with_output && (ast_query_with_output->format != nullptr)
|
2019-08-08 20:02:30 +00:00
|
|
|
? getIdentifierName(ast_query_with_output->format)
|
2017-04-01 07:20:54 +00:00
|
|
|
: context.getDefaultFormat();
|
|
|
|
|
2019-01-24 19:22:26 +00:00
|
|
|
if (ast_query_with_output && ast_query_with_output->settings_ast)
|
|
|
|
InterpreterSetQuery(ast_query_with_output->settings_ast, context).executeForCurrentContext();
|
2019-01-23 19:23:37 +00:00
|
|
|
|
2018-01-07 00:35:44 +00:00
|
|
|
BlockOutputStreamPtr out = context.getOutputFormat(format_name, *out_buf, streams.in->getHeader());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/// Save previous progress callback if any. TODO Do it more conveniently.
|
|
|
|
auto previous_progress_callback = context.getProgressCallback();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/// NOTE Progress callback takes shared ownership of 'out'.
|
|
|
|
streams.in->setProgressCallback([out, previous_progress_callback] (const Progress & progress)
|
|
|
|
{
|
|
|
|
if (previous_progress_callback)
|
|
|
|
previous_progress_callback(progress);
|
|
|
|
out->onProgress(progress);
|
|
|
|
});
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-03-03 15:32:41 +00:00
|
|
|
if (set_result_details)
|
|
|
|
set_result_details(context.getClientInfo().current_query_id, out->getContentType(), format_name, DateLUT::instance().getTimeZone());
|
2019-02-02 12:24:26 +00:00
|
|
|
|
2019-09-11 15:37:12 +00:00
|
|
|
if (ast->as<ASTWatchQuery>())
|
2019-09-11 11:38:05 +00:00
|
|
|
{
|
|
|
|
/// For Watch query, flush data if block is empty (to send data to client).
|
|
|
|
auto flush_callback = [&out](const Block & block)
|
|
|
|
{
|
|
|
|
if (block.rows() == 0)
|
|
|
|
out->flush();
|
|
|
|
};
|
|
|
|
|
|
|
|
copyData(*streams.in, *out, [](){ return false; }, std::move(flush_callback));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
copyData(*streams.in, *out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-03-26 18:28:37 +00:00
|
|
|
|
|
|
|
if (pipeline.initialized())
|
|
|
|
{
|
|
|
|
const ASTQueryWithOutput * ast_query_with_output = dynamic_cast<const ASTQueryWithOutput *>(ast.get());
|
|
|
|
|
|
|
|
WriteBuffer * out_buf = &ostr;
|
|
|
|
std::optional<WriteBufferFromFile> out_file_buf;
|
|
|
|
if (ast_query_with_output && ast_query_with_output->out_file)
|
|
|
|
{
|
|
|
|
if (!allow_into_outfile)
|
|
|
|
throw Exception("INTO OUTFILE is not allowed", ErrorCodes::INTO_OUTFILE_NOT_ALLOWED);
|
|
|
|
|
|
|
|
const auto & out_file = typeid_cast<const ASTLiteral &>(*ast_query_with_output->out_file).value.safeGet<std::string>();
|
|
|
|
out_file_buf.emplace(out_file, DBMS_DEFAULT_BUFFER_SIZE, O_WRONLY | O_EXCL | O_CREAT);
|
|
|
|
out_buf = &*out_file_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
String format_name = ast_query_with_output && (ast_query_with_output->format != nullptr)
|
2019-08-08 20:02:30 +00:00
|
|
|
? getIdentifierName(ast_query_with_output->format)
|
2019-03-26 18:28:37 +00:00
|
|
|
: context.getDefaultFormat();
|
|
|
|
|
|
|
|
if (ast_query_with_output && ast_query_with_output->settings_ast)
|
|
|
|
InterpreterSetQuery(ast_query_with_output->settings_ast, context).executeForCurrentContext();
|
|
|
|
|
2019-04-12 15:32:53 +00:00
|
|
|
pipeline.addSimpleTransform([](const Block & header)
|
|
|
|
{
|
|
|
|
return std::make_shared<MaterializingTransform>(header);
|
|
|
|
});
|
|
|
|
|
2019-04-09 11:00:38 +00:00
|
|
|
auto out = context.getOutputFormatProcessor(format_name, *out_buf, pipeline.getHeader());
|
2019-03-26 18:28:37 +00:00
|
|
|
|
|
|
|
/// Save previous progress callback if any. TODO Do it more conveniently.
|
|
|
|
auto previous_progress_callback = context.getProgressCallback();
|
|
|
|
|
|
|
|
/// NOTE Progress callback takes shared ownership of 'out'.
|
|
|
|
pipeline.setProgressCallback([out, previous_progress_callback] (const Progress & progress)
|
|
|
|
{
|
|
|
|
if (previous_progress_callback)
|
|
|
|
previous_progress_callback(progress);
|
|
|
|
out->onProgress(progress);
|
|
|
|
});
|
|
|
|
|
2020-03-03 15:32:41 +00:00
|
|
|
if (set_result_details)
|
|
|
|
set_result_details(context.getClientInfo().current_query_id, out->getContentType(), format_name, DateLUT::instance().getTimeZone());
|
2019-03-26 18:28:37 +00:00
|
|
|
|
|
|
|
pipeline.setOutput(std::move(out));
|
2019-04-12 13:56:48 +00:00
|
|
|
|
2019-04-29 13:57:29 +00:00
|
|
|
{
|
2019-05-16 14:57:27 +00:00
|
|
|
auto executor = pipeline.execute();
|
2020-03-15 21:22:55 +00:00
|
|
|
executor->execute(pipeline.getNumThreads());
|
2019-04-29 13:57:29 +00:00
|
|
|
}
|
2019-03-26 18:28:37 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
streams.onException();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
streams.onFinish();
|
2015-06-18 02:11:05 +00:00
|
|
|
}
|
2012-03-11 08:52:56 +00:00
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
}
|