2021-11-26 15:49:40 +00:00
|
|
|
#include <Parsers/ASTIdentifier_fwd.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTSetQuery.h>
|
2012-08-02 19:03:32 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ParserSetQuery.h>
|
2022-09-28 10:18:38 +00:00
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
2022-11-23 11:06:32 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
2022-09-30 12:12:06 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2023-02-03 20:05:18 +00:00
|
|
|
#include <Parsers/FieldFromAST.h>
|
2012-08-02 19:03:32 +00:00
|
|
|
|
2022-08-12 12:28:35 +00:00
|
|
|
#include <Core/Names.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
2022-11-08 00:19:01 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
2022-08-12 12:28:35 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2022-11-08 00:19:01 +00:00
|
|
|
#include <IO/Operators.h>
|
2022-08-12 12:28:35 +00:00
|
|
|
#include <Common/FieldVisitorToString.h>
|
2020-12-10 22:05:02 +00:00
|
|
|
#include <Common/SettingsChanges.h>
|
2022-08-12 12:28:35 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2012-08-02 19:03:32 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-08-12 12:28:35 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
class ParameterFieldVisitorToString : public StaticVisitor<String>
|
2022-08-12 12:28:35 +00:00
|
|
|
{
|
2022-11-08 00:19:01 +00:00
|
|
|
public:
|
|
|
|
template <class T>
|
|
|
|
String operator() (const T & x) const
|
|
|
|
{
|
|
|
|
FieldVisitorToString visitor;
|
|
|
|
return visitor(x);
|
|
|
|
}
|
2022-08-12 12:28:35 +00:00
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
String operator() (const Array & x) const
|
2022-08-12 12:28:35 +00:00
|
|
|
{
|
2022-11-08 00:19:01 +00:00
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
|
|
|
wb << '[';
|
|
|
|
for (Array::const_iterator it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb.write(", ", 2);
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
}
|
|
|
|
wb << ']';
|
|
|
|
|
|
|
|
return wb.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
String operator() (const Map & x) const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
|
|
|
wb << '{';
|
|
|
|
|
|
|
|
auto it = x.begin();
|
|
|
|
while (it != x.end())
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb << ", ";
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
++it;
|
|
|
|
|
|
|
|
if (it != x.end())
|
|
|
|
{
|
|
|
|
wb << ':';
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wb << '}';
|
|
|
|
|
|
|
|
return wb.str();
|
2022-08-12 12:28:35 +00:00
|
|
|
}
|
2022-11-08 00:19:01 +00:00
|
|
|
|
|
|
|
String operator() (const Tuple & x) const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
|
|
|
wb << '(';
|
|
|
|
for (auto it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb << ", ";
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
}
|
|
|
|
wb << ')';
|
|
|
|
|
|
|
|
return wb.str();
|
|
|
|
}
|
|
|
|
};
|
2022-08-12 12:28:35 +00:00
|
|
|
|
|
|
|
|
2022-07-05 13:10:09 +00:00
|
|
|
class ParserLiteralOrMap : public IParserBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
protected:
|
|
|
|
const char * getName() const override { return "literal or map"; }
|
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ParserLiteral literal;
|
|
|
|
if (literal.parse(pos, node, expected))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParserToken l_br(TokenType::OpeningCurlyBrace);
|
|
|
|
ParserToken r_br(TokenType::ClosingCurlyBrace);
|
|
|
|
ParserToken comma(TokenType::Comma);
|
|
|
|
ParserToken colon(TokenType::Colon);
|
|
|
|
ParserStringLiteral literal;
|
|
|
|
|
|
|
|
if (!l_br.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Map map;
|
|
|
|
|
|
|
|
while (!r_br.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!map.empty() && !comma.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTPtr key;
|
|
|
|
ASTPtr val;
|
|
|
|
|
|
|
|
if (!literal.parse(pos, key, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!colon.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!literal.parse(pos, val, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Tuple tuple;
|
|
|
|
tuple.push_back(std::move(key->as<ASTLiteral>()->value));
|
|
|
|
tuple.push_back(std::move(val->as<ASTLiteral>()->value));
|
|
|
|
map.push_back(std::move(tuple));
|
|
|
|
}
|
|
|
|
|
|
|
|
node = std::make_shared<ASTLiteral>(std::move(map));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2012-08-02 19:03:32 +00:00
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
/// Parse Identifier, Literal, Array/Tuple/Map of literals
|
2022-10-21 01:36:16 +00:00
|
|
|
bool parseParameterValueIntoString(IParser::Pos & pos, String & value, Expected & expected)
|
2022-09-28 10:18:38 +00:00
|
|
|
{
|
2022-10-21 01:36:16 +00:00
|
|
|
ASTPtr node;
|
2022-09-28 10:18:38 +00:00
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
/// 1. Identifier
|
2022-10-21 01:36:16 +00:00
|
|
|
ParserCompoundIdentifier identifier_p;
|
2022-09-28 10:18:38 +00:00
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
if (identifier_p.parse(pos, node, expected))
|
|
|
|
{
|
|
|
|
tryGetIdentifierNameInto(node, value);
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-28 10:18:38 +00:00
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
/// 2. Literal
|
2022-10-21 01:36:16 +00:00
|
|
|
ParserLiteral literal_p;
|
2022-11-08 00:19:01 +00:00
|
|
|
if (literal_p.parse(pos, node, expected))
|
2022-09-28 10:18:38 +00:00
|
|
|
{
|
2022-10-21 01:36:16 +00:00
|
|
|
value = applyVisitor(FieldVisitorToString(), node->as<ASTLiteral>()->value);
|
2022-09-28 10:18:38 +00:00
|
|
|
|
|
|
|
/// writeQuoted is not always quoted in line with SQL standard https://github.com/ClickHouse/ClickHouse/blob/master/src/IO/WriteHelpers.h
|
2022-10-21 01:36:16 +00:00
|
|
|
if (value.starts_with('\''))
|
2022-09-28 10:18:38 +00:00
|
|
|
{
|
2022-10-21 01:36:16 +00:00
|
|
|
ReadBufferFromOwnString buf(value);
|
|
|
|
readQuoted(value, buf);
|
2022-09-28 10:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-09-28 10:18:38 +00:00
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
/// 3. Map, Array, Tuple of literals and their combination
|
|
|
|
ParserAllCollectionsOfLiterals all_collections_p;
|
2022-09-28 10:18:38 +00:00
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
if (all_collections_p.parse(pos, node, expected))
|
2022-10-21 01:36:16 +00:00
|
|
|
{
|
2022-11-08 00:19:01 +00:00
|
|
|
value = applyVisitor(ParameterFieldVisitorToString(), node->as<ASTLiteral>()->value);
|
|
|
|
return true;
|
2022-09-28 10:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 00:19:01 +00:00
|
|
|
return false;
|
2022-09-28 10:18:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Parse `name = value`.
|
2020-04-24 16:04:54 +00:00
|
|
|
bool ParserSetQuery::parseNameValuePair(SettingChange & change, IParser::Pos & pos, Expected & expected)
|
2012-08-02 19:03:32 +00:00
|
|
|
{
|
2020-08-08 18:55:14 +00:00
|
|
|
ParserCompoundIdentifier name_p;
|
2022-09-30 12:12:06 +00:00
|
|
|
ParserLiteralOrMap literal_or_map_p;
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_eq(TokenType::Equals);
|
2022-09-30 12:12:06 +00:00
|
|
|
ParserSetQuery set_p(true);
|
2022-11-18 12:00:46 +00:00
|
|
|
ParserFunction function_p;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2012-08-02 19:03:32 +00:00
|
|
|
ASTPtr name;
|
|
|
|
ASTPtr value;
|
2022-11-18 12:00:46 +00:00
|
|
|
ASTPtr function_ast;
|
2012-08-02 19:03:32 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, name, expected))
|
2012-08-02 19:03:32 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_eq.ignore(pos, expected))
|
2012-08-02 19:03:32 +00:00
|
|
|
return false;
|
|
|
|
|
2024-03-18 15:58:57 +00:00
|
|
|
if (ParserKeyword(Keyword::TRUE_KEYWORD).ignore(pos, expected))
|
2022-04-18 08:18:31 +00:00
|
|
|
value = std::make_shared<ASTLiteral>(Field(static_cast<UInt64>(1)));
|
2024-03-18 15:58:57 +00:00
|
|
|
else if (ParserKeyword(Keyword::FALSE_KEYWORD).ignore(pos, expected))
|
2022-04-18 08:18:31 +00:00
|
|
|
value = std::make_shared<ASTLiteral>(Field(static_cast<UInt64>(0)));
|
2022-11-18 12:00:46 +00:00
|
|
|
/// for SETTINGS disk=disk(type='s3', path='', ...)
|
2023-07-31 12:14:15 +00:00
|
|
|
else if (function_p.parse(pos, function_ast, expected) && function_ast->as<ASTFunction>()->name == "disk")
|
2022-09-30 12:12:06 +00:00
|
|
|
{
|
2023-02-03 20:05:18 +00:00
|
|
|
tryGetIdentifierNameInto(name, change.name);
|
|
|
|
change.value = createFieldFromAST(function_ast);
|
2022-11-18 12:00:46 +00:00
|
|
|
|
2022-09-30 12:12:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (!literal_or_map_p.parse(pos, value, expected))
|
2012-08-02 19:03:32 +00:00
|
|
|
return false;
|
|
|
|
|
2023-02-03 20:05:18 +00:00
|
|
|
tryGetIdentifierNameInto(name, change.name);
|
|
|
|
change.value = value->as<ASTLiteral &>().value;
|
2012-08-02 19:03:32 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
bool ParserSetQuery::parseNameValuePairWithParameterOrDefault(
|
|
|
|
SettingChange & change, String & default_settings, ParserSetQuery::Parameter & parameter, IParser::Pos & pos, Expected & expected)
|
2022-10-11 18:25:28 +00:00
|
|
|
{
|
|
|
|
ParserCompoundIdentifier name_p;
|
|
|
|
ParserLiteralOrMap value_p;
|
|
|
|
ParserToken s_eq(TokenType::Equals);
|
2022-11-18 12:00:46 +00:00
|
|
|
ParserFunction function_p;
|
2022-10-11 18:25:28 +00:00
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
ASTPtr node;
|
|
|
|
String name;
|
2022-11-18 12:00:46 +00:00
|
|
|
ASTPtr function_ast;
|
2022-10-11 18:25:28 +00:00
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
if (!name_p.parse(pos, node, expected))
|
2022-10-11 18:25:28 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!s_eq.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
tryGetIdentifierNameInto(node, name);
|
|
|
|
|
|
|
|
/// Parameter
|
|
|
|
if (name.starts_with(QUERY_PARAMETER_NAME_PREFIX))
|
|
|
|
{
|
|
|
|
name = name.substr(strlen(QUERY_PARAMETER_NAME_PREFIX));
|
|
|
|
|
|
|
|
if (name.empty())
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Parameter name cannot be empty");
|
|
|
|
|
|
|
|
String value;
|
|
|
|
|
|
|
|
if (!parseParameterValueIntoString(pos, value, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
parameter = {std::move(name), std::move(value)};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Default
|
2024-03-18 15:58:57 +00:00
|
|
|
if (ParserKeyword(Keyword::DEFAULT).ignore(pos, expected))
|
2022-10-21 01:36:16 +00:00
|
|
|
{
|
|
|
|
default_settings = name;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Setting
|
2024-03-18 15:58:57 +00:00
|
|
|
if (ParserKeyword(Keyword::TRUE_KEYWORD).ignore(pos, expected))
|
2022-10-21 01:36:16 +00:00
|
|
|
node = std::make_shared<ASTLiteral>(Field(static_cast<UInt64>(1)));
|
2024-03-18 15:58:57 +00:00
|
|
|
else if (ParserKeyword(Keyword::FALSE_KEYWORD).ignore(pos, expected))
|
2022-10-21 01:36:16 +00:00
|
|
|
node = std::make_shared<ASTLiteral>(Field(static_cast<UInt64>(0)));
|
2023-07-31 12:14:15 +00:00
|
|
|
else if (function_p.parse(pos, function_ast, expected) && function_ast->as<ASTFunction>()->name == "disk")
|
2022-11-08 12:32:49 +00:00
|
|
|
{
|
2023-02-03 20:05:18 +00:00
|
|
|
change.name = name;
|
|
|
|
change.value = createFieldFromAST(function_ast);
|
2022-11-18 12:00:46 +00:00
|
|
|
|
2022-11-08 12:32:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-10-21 01:36:16 +00:00
|
|
|
else if (!value_p.parse(pos, node, expected))
|
2022-10-11 18:25:28 +00:00
|
|
|
return false;
|
|
|
|
|
2023-02-03 20:05:18 +00:00
|
|
|
change.name = name;
|
|
|
|
change.value = node->as<ASTLiteral &>().value;
|
2022-10-11 18:25:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserSetQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2012-08-02 19:03:32 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_comma(TokenType::Comma);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2015-06-05 21:28:04 +00:00
|
|
|
if (!parse_only_internals)
|
|
|
|
{
|
2024-03-18 15:58:57 +00:00
|
|
|
ParserKeyword s_set(Keyword::SET);
|
2012-08-02 19:03:32 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_set.ignore(pos, expected))
|
2015-06-05 21:28:04 +00:00
|
|
|
return false;
|
2022-03-18 13:33:59 +00:00
|
|
|
|
|
|
|
/// Parse SET TRANSACTION ... queries using ParserTransactionControl
|
2024-03-18 15:58:57 +00:00
|
|
|
if (ParserKeyword{Keyword::TRANSACTION}.check(pos, expected))
|
2022-03-18 13:33:59 +00:00
|
|
|
return false;
|
2015-06-05 21:28:04 +00:00
|
|
|
}
|
2012-08-02 19:03:32 +00:00
|
|
|
|
2019-04-18 23:29:32 +00:00
|
|
|
SettingsChanges changes;
|
2024-02-26 01:11:51 +00:00
|
|
|
NameToNameVector query_parameters;
|
2022-10-11 18:25:28 +00:00
|
|
|
std::vector<String> default_settings;
|
2012-08-02 19:03:32 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2022-10-11 18:25:28 +00:00
|
|
|
if ((!changes.empty() || !query_parameters.empty() || !default_settings.empty()) && !s_comma.ignore(pos))
|
2012-08-02 19:03:32 +00:00
|
|
|
break;
|
|
|
|
|
2022-10-21 00:25:14 +00:00
|
|
|
SettingChange setting;
|
2022-10-11 18:25:28 +00:00
|
|
|
String name_of_default_setting;
|
2022-10-21 01:36:16 +00:00
|
|
|
Parameter parameter;
|
2012-08-02 19:03:32 +00:00
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
if (!parseNameValuePairWithParameterOrDefault(setting, name_of_default_setting, parameter, pos, expected))
|
2012-08-02 19:03:32 +00:00
|
|
|
return false;
|
2022-08-12 12:28:35 +00:00
|
|
|
|
2022-10-21 01:36:16 +00:00
|
|
|
if (!parameter.first.empty())
|
2024-02-26 01:11:51 +00:00
|
|
|
query_parameters.emplace_back(std::move(parameter));
|
2022-10-11 18:25:28 +00:00
|
|
|
else if (!name_of_default_setting.empty())
|
|
|
|
default_settings.emplace_back(std::move(name_of_default_setting));
|
2022-08-12 12:28:35 +00:00
|
|
|
else
|
2022-10-21 01:36:16 +00:00
|
|
|
changes.push_back(std::move(setting));
|
2012-08-02 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto query = std::make_shared<ASTSetQuery>();
|
2012-08-02 19:03:32 +00:00
|
|
|
node = query;
|
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
query->is_standalone = !parse_only_internals;
|
2019-04-18 23:29:32 +00:00
|
|
|
query->changes = std::move(changes);
|
2022-08-12 12:28:35 +00:00
|
|
|
query->query_parameters = std::move(query_parameters);
|
2022-10-11 18:25:28 +00:00
|
|
|
query->default_settings = std::move(default_settings);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2012-08-02 19:03:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|