2018-01-10 00:04:08 +00:00
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2019-02-19 19:26:41 +00:00
|
|
|
#include <Interpreters/InterpreterSetQuery.h>
|
2018-01-10 00:04:08 +00:00
|
|
|
#include <IO/ConcatReadBuffer.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromMemory.h>
|
2018-01-10 00:04:08 +00:00
|
|
|
#include <DataStreams/BlockIO.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/InputStreamFromASTInsertQuery.h>
|
2018-07-10 17:20:55 +00:00
|
|
|
#include <DataStreams/AddingDefaultsBlockInputStream.h>
|
2018-12-04 20:03:04 +00:00
|
|
|
#include <Storages/ColumnsDescription.h>
|
2019-05-01 21:43:05 +00:00
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
|
2017-01-03 01:42:17 +00:00
|
|
|
|
2016-12-09 10:10:12 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-01-10 00:04:08 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
2019-09-05 13:17:01 +00:00
|
|
|
extern const int INVALID_USAGE_OF_INPUT;
|
2018-01-10 00:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-09 10:10:12 +00:00
|
|
|
InputStreamFromASTInsertQuery::InputStreamFromASTInsertQuery(
|
2019-05-30 20:12:44 +00:00
|
|
|
const ASTPtr & ast, ReadBuffer * input_buffer_tail_part, const Block & header, const Context & context, const ASTPtr & input_function)
|
2016-12-09 10:10:12 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * ast_insert_query = ast->as<ASTInsertQuery>();
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!ast_insert_query)
|
|
|
|
throw Exception("Logical error: query requires data to insert, but it is not INSERT query", ErrorCodes::LOGICAL_ERROR);
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String format = ast_insert_query->format;
|
|
|
|
if (format.empty())
|
2019-05-30 20:12:44 +00:00
|
|
|
{
|
|
|
|
if (input_function)
|
2019-09-05 13:17:01 +00:00
|
|
|
throw Exception("FORMAT must be specified for function input()", ErrorCodes::INVALID_USAGE_OF_INPUT);
|
2017-04-01 07:20:54 +00:00
|
|
|
format = "Values";
|
2019-05-30 20:12:44 +00:00
|
|
|
}
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Data could be in parsed (ast_insert_query.data) and in not parsed yet (input_buffer_tail_part) part of query.
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
input_buffer_ast_part = std::make_unique<ReadBufferFromMemory>(
|
|
|
|
ast_insert_query->data, ast_insert_query->data ? ast_insert_query->end - ast_insert_query->data : 0);
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ConcatReadBuffer::ReadBuffers buffers;
|
|
|
|
if (ast_insert_query->data)
|
|
|
|
buffers.push_back(input_buffer_ast_part.get());
|
2019-02-08 13:24:24 +00:00
|
|
|
|
|
|
|
if (input_buffer_tail_part)
|
|
|
|
buffers.push_back(input_buffer_tail_part);
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** NOTE Must not read from 'input_buffer_tail_part' 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 'input_buffer_tail_part'.
|
|
|
|
*/
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
input_buffer_contacenated = std::make_unique<ConcatReadBuffer>(buffers);
|
2016-12-09 10:10:12 +00:00
|
|
|
|
2019-02-08 13:24:24 +00:00
|
|
|
res_stream = context.getInputFormat(format, *input_buffer_contacenated, header, context.getSettings().max_insert_block_size);
|
2018-07-13 12:46:29 +00:00
|
|
|
|
2019-09-04 12:25:20 +00:00
|
|
|
if (context.getSettingsRef().input_format_defaults_for_omitted_fields && !ast_insert_query->table.empty() && !input_function)
|
2019-03-15 17:40:16 +00:00
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
StoragePtr storage = context.getTable(ast_insert_query->database, ast_insert_query->table);
|
|
|
|
auto column_defaults = storage->getColumns().getDefaults();
|
2019-03-15 17:40:16 +00:00
|
|
|
if (!column_defaults.empty())
|
|
|
|
res_stream = std::make_shared<AddingDefaultsBlockInputStream>(res_stream, column_defaults, context);
|
|
|
|
}
|
2016-12-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|