2014-01-03 08:20:13 +00:00
|
|
|
#include <DB/Common/ProfileEvents.h>
|
2015-06-29 21:35:35 +00:00
|
|
|
#include <DB/Common/formatReadable.h>
|
2014-01-03 08:20:13 +00:00
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
#include <DB/IO/ConcatReadBuffer.h>
|
2011-11-06 20:47:07 +00:00
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
#include <DB/DataStreams/BlockIO.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
#include <DB/DataStreams/copyData.h>
|
2015-06-29 21:35:35 +00:00
|
|
|
#include <DB/DataStreams/IProfilingBlockInputStream.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2015-03-02 01:10:58 +00:00
|
|
|
#include <DB/Parsers/ASTInsertQuery.h>
|
|
|
|
#include <DB/Parsers/ASTShowProcesslistQuery.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
2015-04-14 02:45:30 +00:00
|
|
|
#include <DB/Parsers/ParserQuery.h>
|
2015-04-11 03:10:23 +00:00
|
|
|
#include <DB/Parsers/parseQuery.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <DB/Interpreters/Quota.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
#include <DB/Interpreters/InterpreterFactory.h>
|
2015-06-21 06:06:04 +00:00
|
|
|
#include <DB/Interpreters/ProcessList.h>
|
2015-06-26 20:48:10 +00:00
|
|
|
#include <DB/Interpreters/QueryLog.h>
|
2011-10-30 11:30:52 +00:00
|
|
|
#include <DB/Interpreters/executeQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int QUERY_IS_TOO_LARGE;
|
|
|
|
}
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2012-12-26 20:29:28 +00:00
|
|
|
static void checkLimits(const IAST & ast, const Limits & limits)
|
|
|
|
{
|
|
|
|
if (limits.max_ast_depth)
|
|
|
|
ast.checkDepth(limits.max_ast_depth);
|
|
|
|
if (limits.max_ast_elements)
|
|
|
|
ast.checkSize(limits.max_ast_elements);
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2012-12-26 20:29:28 +00:00
|
|
|
|
2016-08-16 20:33:00 +00:00
|
|
|
static String joinLines(const String & query)
|
|
|
|
{
|
|
|
|
String res = query;
|
|
|
|
std::replace(res.begin(), res.end(), '\n', ' ');
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Log query into text log (not into system table).
|
2015-06-17 21:34:15 +00:00
|
|
|
static void logQuery(const String & query, const Context & context)
|
|
|
|
{
|
2016-08-16 20:33:00 +00:00
|
|
|
LOG_DEBUG(&Logger::get("executeQuery"), "(from " << context.getIPAddress().toString() << ") " << joinLines(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Call this inside catch block.
|
|
|
|
static void setExceptionStackTrace(QueryLogElement & elem)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
elem.stack_trace = e.getStackTrace().toString();
|
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Log exception (with query info) into text log (not into system table).
|
|
|
|
static void logException(Context & context, QueryLogElement & elem)
|
|
|
|
{
|
|
|
|
LOG_ERROR(&Logger::get("executeQuery"), elem.exception
|
|
|
|
<< " (from " << context.getIPAddress().toString() << ")"
|
|
|
|
<< " (in query: " << joinLines(elem.query) << ")"
|
|
|
|
<< (!elem.stack_trace.empty() ? ", Stack trace:\n\n" + elem.stack_trace : ""));
|
2015-06-17 21:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
static void setClientInfo(QueryLogElement & elem, Context & context)
|
|
|
|
{
|
|
|
|
elem.interface = context.getInterface();
|
|
|
|
elem.http_method = context.getHTTPMethod();
|
|
|
|
elem.ip_address = context.getIPAddress();
|
|
|
|
elem.user = context.getUser();
|
|
|
|
elem.query_id = context.getCurrentQueryId();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void onExceptionBeforeStart(const String & query, Context & context, time_t current_time)
|
|
|
|
{
|
|
|
|
/// Эксепшен до начала выполнения запроса.
|
|
|
|
context.getQuota().addError(current_time);
|
|
|
|
|
|
|
|
bool log_queries = context.getSettingsRef().log_queries;
|
|
|
|
|
|
|
|
/// Логгируем в таблицу начало выполнения запроса, если нужно.
|
|
|
|
if (log_queries)
|
|
|
|
{
|
|
|
|
QueryLogElement elem;
|
|
|
|
|
|
|
|
elem.type = QueryLogElement::EXCEPTION_BEFORE_START;
|
|
|
|
|
|
|
|
elem.event_time = current_time;
|
|
|
|
elem.query_start_time = current_time;
|
|
|
|
|
2016-06-25 03:21:01 +00:00
|
|
|
elem.query = query.substr(0, context.getSettingsRef().log_queries_cut_to_length);
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.exception = getCurrentExceptionMessage(false);
|
|
|
|
|
|
|
|
setClientInfo(elem, context);
|
2016-08-16 20:33:00 +00:00
|
|
|
setExceptionStackTrace(elem);
|
|
|
|
logException(context, elem);
|
2015-07-01 05:18:54 +00:00
|
|
|
|
|
|
|
context.getQueryLog().add(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
|
|
|
|
IParser::Pos begin,
|
|
|
|
IParser::Pos end,
|
|
|
|
Context & context,
|
|
|
|
bool internal,
|
|
|
|
QueryProcessingStage::Enum stage)
|
2015-06-18 00:27:25 +00:00
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::Query);
|
2015-07-01 05:18:54 +00:00
|
|
|
time_t current_time = time(0);
|
2015-06-18 00:27:25 +00:00
|
|
|
|
2016-06-25 03:21:01 +00:00
|
|
|
const Settings & settings = context.getSettingsRef();
|
|
|
|
|
2015-06-18 00:27:25 +00:00
|
|
|
ParserQuery parser;
|
|
|
|
ASTPtr ast;
|
|
|
|
size_t query_size;
|
2016-06-25 03:21:01 +00:00
|
|
|
size_t max_query_size = settings.max_query_size;
|
2015-06-18 00:27:25 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ast = parseQuery(parser, begin, end, "");
|
|
|
|
|
2016-08-17 04:53:14 +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.
|
2015-06-18 00:27:25 +00:00
|
|
|
query_size = ast->range.second - ast->range.first;
|
|
|
|
|
|
|
|
if (max_query_size && query_size > max_query_size)
|
|
|
|
throw Exception("Query is too large (" + toString(query_size) + ")."
|
|
|
|
" max_query_size = " + toString(max_query_size), ErrorCodes::QUERY_IS_TOO_LARGE);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Anyway log query.
|
2015-07-01 05:18:54 +00:00
|
|
|
if (!internal)
|
|
|
|
{
|
|
|
|
String query = String(begin, begin + std::min(end - begin, static_cast<ptrdiff_t>(max_query_size)));
|
2016-06-25 03:21:01 +00:00
|
|
|
logQuery(query.substr(0, settings.log_queries_cut_to_length), context);
|
2015-07-01 05:18:54 +00:00
|
|
|
onExceptionBeforeStart(query, context, current_time);
|
|
|
|
}
|
2015-06-18 00:27:25 +00:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
String query(begin, query_size);
|
2015-07-01 05:18:54 +00:00
|
|
|
BlockIO res;
|
2015-06-18 00:27:25 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!internal)
|
2016-06-25 03:21:01 +00:00
|
|
|
logQuery(query.substr(0, settings.log_queries_cut_to_length), context);
|
2015-06-18 00:27:25 +00:00
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Check the limits.
|
2016-06-25 03:21:01 +00:00
|
|
|
checkLimits(*ast, settings.limits);
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
QuotaForIntervals & quota = context.getQuota();
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2016-03-21 23:20:00 +00:00
|
|
|
quota.addQuery(current_time);
|
2015-07-01 05:18:54 +00:00
|
|
|
quota.checkExceeded(current_time);
|
2015-06-26 20:48:10 +00:00
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Put query to process list. But don't put SHOW PROCESSLIST query itself.
|
2015-07-01 05:18:54 +00:00
|
|
|
ProcessList::EntryPtr process_list_entry;
|
|
|
|
if (!internal && nullptr == typeid_cast<const ASTShowProcesslistQuery *>(&*ast))
|
|
|
|
{
|
|
|
|
process_list_entry = context.getProcessList().insert(
|
2015-09-08 21:01:43 +00:00
|
|
|
query,
|
|
|
|
context.getUser(),
|
|
|
|
context.getCurrentQueryId(),
|
|
|
|
context.getIPAddress(),
|
2016-10-11 18:54:33 +00:00
|
|
|
context.getPort(),
|
2015-09-08 21:01:43 +00:00
|
|
|
settings);
|
2015-07-01 05:18:54 +00:00
|
|
|
|
|
|
|
context.setProcessListElement(&process_list_entry->get());
|
|
|
|
}
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-06-29 23:54:33 +00:00
|
|
|
auto interpreter = InterpreterFactory::get(ast, context, stage);
|
|
|
|
res = interpreter->execute();
|
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Hold element of process list till end of query execution.
|
2015-06-29 23:54:33 +00:00
|
|
|
res.process_list_entry = process_list_entry;
|
|
|
|
|
2015-07-25 10:38:52 +00:00
|
|
|
if (res.in)
|
|
|
|
{
|
|
|
|
if (IProfilingBlockInputStream * stream = dynamic_cast<IProfilingBlockInputStream *>(res.in.get()))
|
|
|
|
{
|
|
|
|
stream->setProgressCallback(context.getProgressCallback());
|
|
|
|
stream->setProcessListElement(context.getProcessListElement());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Everything related to query log.
|
2015-07-01 05:18:54 +00:00
|
|
|
{
|
|
|
|
QueryLogElement elem;
|
2015-06-26 20:48:10 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.type = QueryLogElement::QUERY_START;
|
2015-06-26 20:48:10 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.event_time = current_time;
|
|
|
|
elem.query_start_time = current_time;
|
2015-06-26 20:48:10 +00:00
|
|
|
|
2016-06-25 03:21:01 +00:00
|
|
|
elem.query = query.substr(0, settings.log_queries_cut_to_length);
|
2015-06-26 20:48:10 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
setClientInfo(elem, context);
|
2015-06-26 20:48:10 +00:00
|
|
|
|
2015-09-24 19:25:18 +00:00
|
|
|
bool log_queries = settings.log_queries && !internal;
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Log into system table start of query execution, if need.
|
2015-07-01 05:18:54 +00:00
|
|
|
if (log_queries)
|
|
|
|
context.getQueryLog().add(elem);
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Also make possible for caller to log successful query finish and exception during execution.
|
2015-07-14 03:05:10 +00:00
|
|
|
res.finish_callback = [elem, &context, log_queries] (IBlockInputStream * stream) mutable
|
2015-07-01 05:18:54 +00:00
|
|
|
{
|
|
|
|
ProcessListElement * process_list_elem = context.getProcessListElement();
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
if (!process_list_elem)
|
|
|
|
return;
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
double elapsed_seconds = process_list_elem->watch.elapsedSeconds();
|
|
|
|
|
|
|
|
elem.type = QueryLogElement::QUERY_FINISH;
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.event_time = time(0);
|
2015-06-29 23:54:33 +00:00
|
|
|
elem.query_duration_ms = elapsed_seconds * 1000;
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.read_rows = process_list_elem->progress.rows;
|
|
|
|
elem.read_bytes = process_list_elem->progress.bytes;
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
auto memory_usage = process_list_elem->memory_tracker.getPeak();
|
|
|
|
elem.memory_usage = memory_usage > 0 ? memory_usage : 0;
|
|
|
|
|
2015-07-14 03:05:10 +00:00
|
|
|
if (stream)
|
2015-07-01 05:18:54 +00:00
|
|
|
{
|
2015-07-14 03:05:10 +00:00
|
|
|
if (IProfilingBlockInputStream * profiling_stream = dynamic_cast<IProfilingBlockInputStream *>(stream))
|
|
|
|
{
|
2016-08-13 02:21:17 +00:00
|
|
|
const BlockStreamProfileInfo & info = profiling_stream->getProfileInfo();
|
2015-07-01 05:18:54 +00:00
|
|
|
|
2015-07-14 03:05:10 +00:00
|
|
|
elem.result_rows = info.rows;
|
|
|
|
elem.result_bytes = info.bytes;
|
|
|
|
}
|
2015-07-01 05:18:54 +00:00
|
|
|
}
|
2015-06-29 21:35:35 +00:00
|
|
|
|
|
|
|
if (elem.read_rows != 0)
|
|
|
|
{
|
|
|
|
LOG_INFO(&Logger::get("executeQuery"), std::fixed << std::setprecision(3)
|
|
|
|
<< "Read " << elem.read_rows << " rows, "
|
2015-06-29 23:54:33 +00:00
|
|
|
<< 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.");
|
2015-06-29 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
if (log_queries)
|
|
|
|
context.getQueryLog().add(elem);
|
|
|
|
};
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
res.exception_callback = [elem, &context, log_queries, current_time] () mutable
|
|
|
|
{
|
|
|
|
context.getQuota().addError(current_time);
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.type = QueryLogElement::EXCEPTION_WHILE_PROCESSING;
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.event_time = time(0);
|
|
|
|
elem.query_duration_ms = 1000 * (elem.event_time - elem.query_start_time);
|
|
|
|
elem.exception = getCurrentExceptionMessage(false);
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
ProcessListElement * process_list_elem = context.getProcessListElement();
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
if (process_list_elem)
|
|
|
|
{
|
|
|
|
double elapsed_seconds = process_list_elem->watch.elapsedSeconds();
|
2015-06-26 20:48:10 +00:00
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
elem.query_duration_ms = elapsed_seconds * 1000;
|
|
|
|
|
|
|
|
elem.read_rows = process_list_elem->progress.rows;
|
|
|
|
elem.read_bytes = process_list_elem->progress.bytes;
|
|
|
|
|
|
|
|
auto memory_usage = process_list_elem->memory_tracker.getPeak();
|
|
|
|
elem.memory_usage = memory_usage > 0 ? memory_usage : 0;
|
|
|
|
}
|
|
|
|
|
2016-08-16 20:33:00 +00:00
|
|
|
setExceptionStackTrace(elem);
|
|
|
|
logException(context, elem);
|
2015-07-01 05:18:54 +00:00
|
|
|
|
|
|
|
if (log_queries)
|
|
|
|
context.getQueryLog().add(elem);
|
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2015-06-29 23:54:33 +00:00
|
|
|
}
|
2015-06-29 21:35:35 +00:00
|
|
|
}
|
2015-07-01 05:18:54 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
2015-11-12 02:14:28 +00:00
|
|
|
if (!internal)
|
|
|
|
onExceptionBeforeStart(query, context, current_time);
|
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
throw;
|
|
|
|
}
|
2015-06-29 21:35:35 +00:00
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
return std::make_tuple(ast, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO executeQuery(
|
|
|
|
const String & query,
|
|
|
|
Context & context,
|
|
|
|
bool internal,
|
|
|
|
QueryProcessingStage::Enum stage)
|
|
|
|
{
|
|
|
|
BlockIO streams;
|
|
|
|
std::tie(std::ignore, streams) = executeQueryImpl(query.data(), query.data() + query.size(), context, internal, stage);
|
|
|
|
return streams;
|
2015-06-18 00:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
void executeQuery(
|
|
|
|
ReadBuffer & istr,
|
|
|
|
WriteBuffer & ostr,
|
|
|
|
Context & context,
|
2012-05-22 18:32:45 +00:00
|
|
|
BlockInputStreamPtr & query_plan,
|
2015-10-30 21:19:54 +00:00
|
|
|
std::function<void(const String &)> set_content_type)
|
2011-10-30 11:30:52 +00:00
|
|
|
{
|
2014-04-02 18:38:17 +00:00
|
|
|
PODArray<char> parse_buf;
|
2011-10-30 11:30:52 +00:00
|
|
|
const char * begin;
|
|
|
|
const char * end;
|
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// If 'istr' is empty now, fetch next data into buffer.
|
2011-10-30 11:30:52 +00:00
|
|
|
if (istr.buffer().size() == 0)
|
|
|
|
istr.next();
|
|
|
|
|
2015-06-18 00:27:25 +00:00
|
|
|
size_t max_query_size = context.getSettingsRef().max_query_size;
|
2012-08-02 17:33:31 +00:00
|
|
|
|
|
|
|
if (istr.buffer().end() - istr.position() >= static_cast<ssize_t>(max_query_size))
|
2011-10-30 11:30:52 +00:00
|
|
|
{
|
2016-08-17 04:53:14 +00:00
|
|
|
/// If remaining buffer space in 'istr' is enough to parse query up to 'max_query_size' bytes, then parse inplace.
|
2011-10-30 11:30:52 +00:00
|
|
|
begin = istr.position();
|
|
|
|
end = istr.buffer().end();
|
|
|
|
istr.position() += end - begin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-17 04:53:14 +00:00
|
|
|
/// If not - copy enough data into 'parse_buf'.
|
2012-08-02 17:33:31 +00:00
|
|
|
parse_buf.resize(max_query_size);
|
|
|
|
parse_buf.resize(istr.read(&parse_buf[0], max_query_size));
|
2011-10-30 11:30:52 +00:00
|
|
|
begin = &parse_buf[0];
|
|
|
|
end = begin + parse_buf.size();
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:27:25 +00:00
|
|
|
ASTPtr ast;
|
2015-06-18 02:11:05 +00:00
|
|
|
BlockIO streams;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2015-10-30 21:19:54 +00:00
|
|
|
std::tie(ast, streams) = executeQueryImpl(begin, end, context, false, QueryProcessingStage::Complete);
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
try
|
2013-08-12 00:36:18 +00:00
|
|
|
{
|
2015-06-29 21:35:35 +00:00
|
|
|
if (streams.out)
|
|
|
|
{
|
|
|
|
const ASTInsertQuery * ast_insert_query = dynamic_cast<const ASTInsertQuery *>(ast.get());
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
if (!ast_insert_query)
|
|
|
|
throw Exception("Logical error: query requires data to insert, but it is not INSERT query", ErrorCodes::LOGICAL_ERROR);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
String format = ast_insert_query->format;
|
|
|
|
if (format.empty())
|
|
|
|
format = "Values";
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/// Data could be in parsed (ast_insert_query.data) and in not parsed yet (istr) part of query.
|
2012-03-11 08:52:56 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
ConcatReadBuffer::ReadBuffers buffers;
|
|
|
|
ReadBuffer buf1(const_cast<char *>(ast_insert_query->data), ast_insert_query->data ? ast_insert_query->end - ast_insert_query->data : 0, 0);
|
2012-12-26 20:29:28 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
if (ast_insert_query->data)
|
|
|
|
buffers.push_back(&buf1);
|
|
|
|
buffers.push_back(&istr);
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
/** NOTE Must not read from 'istr' before read all between 'ast_insert_query.data' and 'ast_insert_query.end'.
|
|
|
|
* - because 'query.data' could refer to memory piece, used as buffer for 'istr'.
|
|
|
|
*/
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
ConcatReadBuffer data_istr(buffers);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
BlockInputStreamPtr in{
|
2016-02-13 06:37:19 +00:00
|
|
|
context.getInputFormat(
|
2015-06-29 21:35:35 +00:00
|
|
|
format, data_istr, streams.out_sample, context.getSettings().max_insert_block_size)};
|
2013-09-07 04:54:59 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
copyData(*in, *streams.out);
|
|
|
|
}
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
if (streams.in)
|
|
|
|
{
|
|
|
|
const ASTQueryWithOutput * ast_query_with_output = dynamic_cast<const ASTQueryWithOutput *>(ast.get());
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
String format_name = ast_query_with_output && (ast_query_with_output->getFormat() != nullptr)
|
|
|
|
? typeid_cast<const ASTIdentifier &>(*ast_query_with_output->getFormat()).name
|
|
|
|
: context.getDefaultFormat();
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
BlockOutputStreamPtr out = context.getOutputFormat(format_name, ostr, streams.in_sample);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
2016-08-17 04:53:14 +00:00
|
|
|
if (IProfilingBlockInputStream * stream = dynamic_cast<IProfilingBlockInputStream *>(streams.in.get()))
|
|
|
|
{
|
|
|
|
/// NOTE Progress callback takes shared ownership of 'out'.
|
|
|
|
stream->setProgressCallback([out] (const Progress & progress) { out->onProgress(progress); });
|
|
|
|
}
|
|
|
|
|
2015-10-30 21:19:54 +00:00
|
|
|
if (set_content_type)
|
|
|
|
set_content_type(out->getContentType());
|
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
copyData(*streams.in, *out);
|
|
|
|
}
|
2015-06-18 02:11:05 +00:00
|
|
|
}
|
2015-06-29 21:35:35 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
streams.onException();
|
2015-07-01 05:18:54 +00:00
|
|
|
throw;
|
2015-06-29 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 05:18:54 +00:00
|
|
|
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
|
|
|
}
|