2017-04-16 05:40:17 +00:00
|
|
|
#include <iomanip>
|
2021-10-12 23:51:11 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
2019-05-30 20:12:44 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-08-13 16:30:28 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2020-03-02 20:23:58 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2020-11-09 16:05:40 +00:00
|
|
|
#include <IO/Operators.h>
|
2016-11-20 12:43:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-05-30 20:12:44 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2019-09-05 13:17:01 +00:00
|
|
|
extern const int INVALID_USAGE_OF_INPUT;
|
2019-05-30 20:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 23:51:11 +00:00
|
|
|
String ASTInsertQuery::getDatabase() const
|
|
|
|
{
|
|
|
|
String name;
|
|
|
|
tryGetIdentifierNameInto(database, name);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ASTInsertQuery::getTable() const
|
|
|
|
{
|
|
|
|
String name;
|
|
|
|
tryGetIdentifierNameInto(table, name);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTInsertQuery::setDatabase(const String & name)
|
|
|
|
{
|
|
|
|
if (name.empty())
|
|
|
|
database.reset();
|
|
|
|
else
|
|
|
|
database = std::make_shared<ASTIdentifier>(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTInsertQuery::setTable(const String & name)
|
|
|
|
{
|
|
|
|
if (name.empty())
|
|
|
|
table.reset();
|
|
|
|
else
|
|
|
|
table = std::make_shared<ASTIdentifier>(name);
|
|
|
|
}
|
2019-05-30 20:12:44 +00:00
|
|
|
|
2016-11-20 12:43:20 +00:00
|
|
|
void ASTInsertQuery::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
|
|
|
{
|
2018-05-17 15:17:07 +00:00
|
|
|
frame.need_parens = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-11-02 14:01:11 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << "INSERT INTO ";
|
|
|
|
if (table_function)
|
2017-12-01 13:23:10 +00:00
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << "FUNCTION ";
|
2017-11-02 14:01:11 +00:00
|
|
|
table_function->formatImpl(settings, state, frame);
|
2021-04-14 08:00:17 +00:00
|
|
|
if (partition_by)
|
|
|
|
{
|
|
|
|
settings.ostr << " PARTITION BY ";
|
|
|
|
partition_by->formatImpl(settings, state, frame);
|
|
|
|
}
|
2017-12-01 13:23:10 +00:00
|
|
|
}
|
2021-10-13 12:20:15 +00:00
|
|
|
else if (table_id)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "")
|
|
|
|
<< (!table_id.database_name.empty() ? backQuoteIfNeed(table_id.database_name) + "." : "") << backQuoteIfNeed(table_id.table_name);
|
|
|
|
}
|
2017-11-02 14:01:11 +00:00
|
|
|
else
|
2021-10-13 12:20:15 +00:00
|
|
|
{
|
2017-11-02 14:01:11 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "")
|
2021-10-15 08:41:25 +00:00
|
|
|
<< (database ? backQuoteIfNeed(getDatabase()) + "." : "") << backQuoteIfNeed(getTable());
|
2021-10-13 12:20:15 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-11-20 12:43:20 +00:00
|
|
|
if (columns)
|
|
|
|
{
|
|
|
|
settings.ostr << " (";
|
|
|
|
columns->formatImpl(settings, state, frame);
|
|
|
|
settings.ostr << ")";
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-01-12 09:59:40 +00:00
|
|
|
if (infile)
|
|
|
|
{
|
2022-04-03 20:39:59 +00:00
|
|
|
settings.ostr
|
|
|
|
<< (settings.hilite ? hilite_keyword : "")
|
|
|
|
<< " FROM INFILE "
|
|
|
|
<< (settings.hilite ? hilite_none : "")
|
|
|
|
<< quoteString(infile->as<ASTLiteral &>().value.safeGet<std::string>());
|
2022-01-12 09:59:40 +00:00
|
|
|
if (compression)
|
2022-04-03 20:39:59 +00:00
|
|
|
settings.ostr
|
|
|
|
<< (settings.hilite ? hilite_keyword : "")
|
|
|
|
<< " COMPRESSION "
|
|
|
|
<< (settings.hilite ? hilite_none : "")
|
2022-04-04 07:30:31 +00:00
|
|
|
<< quoteString(compression->as<ASTLiteral &>().value.safeGet<std::string>());
|
2022-01-12 09:59:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 07:52:44 +00:00
|
|
|
if (settings_ast)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << settings.nl_or_ws << "SETTINGS " << (settings.hilite ? hilite_none : "");
|
|
|
|
settings_ast->formatImpl(settings, state, frame);
|
|
|
|
}
|
|
|
|
|
2022-04-17 23:02:49 +00:00
|
|
|
/// Compatibility for INSERT without SETTINGS to format in oneline, i.e.:
|
2022-04-03 07:52:44 +00:00
|
|
|
///
|
|
|
|
/// INSERT INTO foo VALUES
|
|
|
|
///
|
|
|
|
/// But
|
|
|
|
///
|
|
|
|
/// INSERT INTO foo
|
|
|
|
/// SETTINGS max_threads=1
|
|
|
|
/// VALUES
|
|
|
|
///
|
|
|
|
char delim = settings_ast ? settings.nl_or_ws : ' ';
|
|
|
|
|
2016-11-20 12:43:20 +00:00
|
|
|
if (select)
|
|
|
|
{
|
2022-04-03 07:52:44 +00:00
|
|
|
settings.ostr << delim;
|
2016-11-20 12:43:20 +00:00
|
|
|
select->formatImpl(settings, state, frame);
|
|
|
|
}
|
2020-04-25 11:33:47 +00:00
|
|
|
else if (watch)
|
|
|
|
{
|
2022-04-03 07:52:44 +00:00
|
|
|
settings.ostr << delim;
|
2020-04-25 11:33:47 +00:00
|
|
|
watch->formatImpl(settings, state, frame);
|
|
|
|
}
|
2022-04-03 07:52:44 +00:00
|
|
|
|
|
|
|
if (!select && !watch)
|
2016-11-20 12:43:20 +00:00
|
|
|
{
|
|
|
|
if (!format.empty())
|
|
|
|
{
|
2022-04-03 07:52:44 +00:00
|
|
|
settings.ostr << delim
|
|
|
|
<< (settings.hilite ? hilite_keyword : "") << "FORMAT " << (settings.hilite ? hilite_none : "") << format;
|
2016-11-20 12:43:20 +00:00
|
|
|
}
|
2021-08-13 16:30:28 +00:00
|
|
|
else if (!infile)
|
2016-11-20 12:43:20 +00:00
|
|
|
{
|
2022-04-03 07:52:44 +00:00
|
|
|
settings.ostr << delim
|
|
|
|
<< (settings.hilite ? hilite_keyword : "") << "VALUES" << (settings.hilite ? hilite_none : "");
|
2016-11-20 12:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 02:16:02 +00:00
|
|
|
void ASTInsertQuery::updateTreeHashImpl(SipHash & hash_state) const
|
|
|
|
{
|
2021-09-01 23:18:09 +00:00
|
|
|
hash_state.update(table_id.database_name);
|
|
|
|
hash_state.update(table_id.table_name);
|
|
|
|
hash_state.update(table_id.uuid);
|
2021-08-31 02:16:02 +00:00
|
|
|
hash_state.update(format);
|
|
|
|
IAST::updateTreeHashImpl(hash_state);
|
|
|
|
}
|
|
|
|
|
2019-05-30 20:12:44 +00:00
|
|
|
|
2019-12-15 06:34:43 +00:00
|
|
|
static void tryFindInputFunctionImpl(const ASTPtr & ast, ASTPtr & input_function)
|
2019-05-30 20:12:44 +00:00
|
|
|
{
|
|
|
|
if (!ast)
|
|
|
|
return;
|
|
|
|
for (const auto & child : ast->children)
|
2019-05-30 21:33:06 +00:00
|
|
|
tryFindInputFunctionImpl(child, input_function);
|
2019-05-30 20:12:44 +00:00
|
|
|
|
2019-05-30 21:33:06 +00:00
|
|
|
if (const auto * table_function_ast = ast->as<ASTFunction>())
|
2019-05-30 20:12:44 +00:00
|
|
|
{
|
2019-05-30 21:33:06 +00:00
|
|
|
if (table_function_ast->name == "input")
|
2019-05-30 20:12:44 +00:00
|
|
|
{
|
|
|
|
if (input_function)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::INVALID_USAGE_OF_INPUT, "You can use 'input()' function only once per request.");
|
2019-05-30 20:12:44 +00:00
|
|
|
input_function = ast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 21:33:06 +00:00
|
|
|
|
|
|
|
void ASTInsertQuery::tryFindInputFunction(ASTPtr & input_function) const
|
|
|
|
{
|
|
|
|
tryFindInputFunctionImpl(select, input_function);
|
|
|
|
}
|
|
|
|
|
2016-11-20 12:43:20 +00:00
|
|
|
}
|