ClickHouse/src/Parsers/IAST.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

271 lines
7.3 KiB
C++
Raw Normal View History

#include <Parsers/IAST.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
#include <IO/Operators.h>
#include <Common/SensitiveDataMasker.h>
#include <Common/SipHash.h>
namespace DB
{
2017-01-04 03:35:05 +00:00
namespace ErrorCodes
{
extern const int TOO_BIG_AST;
extern const int TOO_DEEP_AST;
extern const int BAD_ARGUMENTS;
extern const int UNKNOWN_ELEMENT_IN_AST;
2017-01-04 03:35:05 +00:00
}
2019-06-15 17:52:53 +00:00
const char * IAST::hilite_keyword = "\033[1m";
const char * IAST::hilite_identifier = "\033[0;36m";
const char * IAST::hilite_function = "\033[0;33m";
const char * IAST::hilite_operator = "\033[1;33m";
const char * IAST::hilite_alias = "\033[0;32m";
const char * IAST::hilite_substitution = "\033[1;36m";
const char * IAST::hilite_none = "\033[0m";
2022-07-29 11:00:33 +00:00
IAST::~IAST()
{
2022-08-07 14:29:23 +00:00
/** Create intrusive linked list of children to delete.
* Each ASTPtr child contains pointer to next child to delete.
*/
ASTPtr delete_list_head_holder = nullptr;
2022-08-08 12:54:11 +00:00
const bool delete_directly = next_to_delete_list_head == nullptr;
2022-08-07 14:29:23 +00:00
ASTPtr & delete_list_head_reference = next_to_delete_list_head ? *next_to_delete_list_head : delete_list_head_holder;
/// Move children into intrusive list
for (auto & child : children)
2022-07-29 11:00:33 +00:00
{
2022-08-07 14:29:23 +00:00
/** If two threads remove ASTPtr concurrently,
* it is possible that neither thead will see use_count == 1.
* It is ok. Will need one more extra stack frame in this case.
*/
if (child.use_count() != 1)
continue;
2022-07-29 11:00:33 +00:00
2022-08-07 14:29:23 +00:00
ASTPtr child_to_delete;
child_to_delete.swap(child);
if (!delete_list_head_reference)
2022-07-29 11:00:33 +00:00
{
2022-08-07 14:29:23 +00:00
/// Initialize list first time
delete_list_head_reference = std::move(child_to_delete);
continue;
2022-07-29 11:00:33 +00:00
}
2022-08-07 14:29:23 +00:00
ASTPtr previous_head = std::move(delete_list_head_reference);
delete_list_head_reference = std::move(child_to_delete);
delete_list_head_reference->next_to_delete = std::move(previous_head);
2022-07-29 11:00:33 +00:00
}
2022-08-07 14:29:23 +00:00
if (!delete_directly)
return;
while (delete_list_head_reference)
2022-07-29 11:00:33 +00:00
{
2022-08-08 12:54:11 +00:00
/** Extract child to delete from current list head.
* Child will be destroyed at the end of scope.
*/
ASTPtr child_to_delete;
child_to_delete.swap(delete_list_head_reference);
/// Update list head
delete_list_head_reference = std::move(child_to_delete->next_to_delete);
2022-08-07 14:29:23 +00:00
/** Pass list head into child before destruction.
* It is important to properly handle cases where subclass has member same as one of its children.
*
* class ASTSubclass : IAST
* {
* ASTPtr first_child; /// Same as first child
* }
*
* In such case we must move children into list only in IAST destructor.
* If we try to move child to delete children into list before subclasses desruction,
* first child use count will be 2.
*/
2022-08-08 12:54:11 +00:00
child_to_delete->next_to_delete_list_head = &delete_list_head_reference;
2022-07-29 11:00:33 +00:00
}
}
size_t IAST::size() const
{
size_t res = 1;
for (const auto & child : children)
res += child->size();
return res;
}
2016-11-20 12:43:20 +00:00
size_t IAST::checkSize(size_t max_size) const
{
size_t res = 1;
for (const auto & child : children)
res += child->checkSize(max_size);
if (res > max_size)
throw Exception(ErrorCodes::TOO_BIG_AST, "AST is too big. Maximum: {}", max_size);
2016-11-20 12:43:20 +00:00
return res;
}
2023-11-10 12:15:23 +00:00
IAST::Hash IAST::getTreeHash(bool ignore_aliases) const
{
SipHash hash_state;
2023-11-10 12:15:23 +00:00
updateTreeHash(hash_state, ignore_aliases);
2023-07-31 13:48:50 +00:00
return getSipHash128AsPair(hash_state);
}
2023-11-10 12:15:23 +00:00
void IAST::updateTreeHash(SipHash & hash_state, bool ignore_aliases) const
{
2023-11-10 12:15:23 +00:00
updateTreeHashImpl(hash_state, ignore_aliases);
2018-03-03 15:36:20 +00:00
hash_state.update(children.size());
for (const auto & child : children)
2023-11-10 12:15:23 +00:00
child->updateTreeHash(hash_state, ignore_aliases);
}
2023-11-10 12:15:23 +00:00
void IAST::updateTreeHashImpl(SipHash & hash_state, bool /*ignore_aliases*/) const
{
auto id = getID();
hash_state.update(id.data(), id.size());
2016-11-20 12:43:20 +00:00
}
size_t IAST::checkDepthImpl(size_t max_depth) const
2016-11-20 12:43:20 +00:00
{
std::vector<std::pair<ASTPtr, size_t>> stack;
stack.reserve(children.size());
for (const auto & i: children)
stack.push_back({i, 1});
size_t res = 0;
while (!stack.empty())
2016-11-20 12:43:20 +00:00
{
auto top = stack.back();
stack.pop_back();
if (top.second >= max_depth)
throw Exception(ErrorCodes::TOO_DEEP_AST, "AST is too deep. Maximum: {}", max_depth);
res = std::max(res, top.second);
for (const auto & i: top.first->children)
stack.push_back({i, top.second + 1});
2016-11-20 12:43:20 +00:00
}
2016-11-20 12:43:20 +00:00
return res;
}
2023-02-17 10:36:58 +00:00
String IAST::formatWithPossiblyHidingSensitiveData(size_t max_length, bool one_line, bool show_secrets) const
2020-05-20 18:57:20 +00:00
{
2020-11-09 16:05:40 +00:00
WriteBufferFromOwnString buf;
2023-07-19 17:03:04 +00:00
FormatSettings settings(buf, one_line);
settings.show_secrets = show_secrets;
format(settings);
return wipeSensitiveDataAndCutToLength(buf.str(), max_length);
}
bool IAST::childrenHaveSecretParts() const
{
for (const auto & child : children)
{
if (child->hasSecretParts())
return true;
}
return false;
2020-05-20 18:57:20 +00:00
}
void IAST::cloneChildren()
{
for (auto & child : children)
child = child->clone();
}
String IAST::getColumnName() const
{
2018-06-28 17:22:14 +00:00
WriteBufferFromOwnString write_buffer;
appendColumnName(write_buffer);
return write_buffer.str();
}
2021-01-10 09:40:47 +00:00
String IAST::getColumnNameWithoutAlias() const
{
WriteBufferFromOwnString write_buffer;
appendColumnNameWithoutAlias(write_buffer);
return write_buffer.str();
}
void IAST::FormatSettings::writeIdentifier(const String & name) const
{
switch (identifier_quoting_style)
{
case IdentifierQuotingStyle::None:
{
if (always_quote_identifiers)
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Incompatible arguments: always_quote_identifiers = true && "
"identifier_quoting_style == IdentifierQuotingStyle::None");
2020-11-09 16:05:40 +00:00
writeString(name, ostr);
break;
}
case IdentifierQuotingStyle::Backticks:
{
if (always_quote_identifiers)
2020-11-09 16:05:40 +00:00
writeBackQuotedString(name, ostr);
else
2020-11-09 16:05:40 +00:00
writeProbablyBackQuotedString(name, ostr);
break;
}
case IdentifierQuotingStyle::DoubleQuotes:
{
if (always_quote_identifiers)
2020-11-09 16:05:40 +00:00
writeDoubleQuotedString(name, ostr);
else
2020-11-09 16:05:40 +00:00
writeProbablyDoubleQuotedString(name, ostr);
break;
}
case IdentifierQuotingStyle::BackticksMySQL:
{
if (always_quote_identifiers)
2020-11-09 16:05:40 +00:00
writeBackQuotedStringMySQL(name, ostr);
else
2020-11-09 16:05:40 +00:00
writeProbablyBackQuotedStringMySQL(name, ostr);
break;
}
}
}
2020-11-09 19:07:38 +00:00
void IAST::dumpTree(WriteBuffer & ostr, size_t indent) const
{
String indent_str(indent, '-');
ostr << indent_str << getID() << ", ";
writePointerHex(this, ostr);
writeChar('\n', ostr);
for (const auto & child : children)
{
if (!child) throw Exception(ErrorCodes::UNKNOWN_ELEMENT_IN_AST, "Can't dump nullptr child");
2020-11-09 19:07:38 +00:00
child->dumpTree(ostr, indent + 1);
}
2020-11-09 19:07:38 +00:00
}
2020-12-09 11:14:40 +00:00
std::string IAST::dumpTree(size_t indent) const
{
WriteBufferFromOwnString wb;
dumpTree(wb, indent);
return wb.str();
}
}