2022-04-19 17:22:04 +00:00
|
|
|
#include <Parsers/ASTColumnsMatcher.h>
|
|
|
|
|
|
|
|
#include <IO/Operators.h>
|
2019-07-12 11:17:38 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2020-08-20 02:01:40 +00:00
|
|
|
#include <Common/SipHash.h>
|
2022-04-19 17:22:04 +00:00
|
|
|
#include <Common/quoteString.h>
|
2019-07-16 20:05:00 +00:00
|
|
|
|
|
|
|
|
2023-09-14 16:12:29 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
|
|
|
|
#endif
|
|
|
|
#include <re2/re2.h>
|
|
|
|
#ifdef __clang__
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2019-07-12 11:17:38 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2022-04-19 17:22:04 +00:00
|
|
|
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_COMPILE_REGEXP;
|
|
|
|
}
|
2019-07-12 11:17:38 +00:00
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
ASTPtr ASTColumnsRegexpMatcher::clone() const
|
2019-07-12 11:17:38 +00:00
|
|
|
{
|
2022-04-19 17:22:04 +00:00
|
|
|
auto clone = std::make_shared<ASTColumnsRegexpMatcher>(*this);
|
2023-02-27 11:26:36 +00:00
|
|
|
clone->children.clear();
|
2022-11-02 06:11:37 +00:00
|
|
|
|
|
|
|
if (expression) { clone->expression = expression->clone(); clone->children.push_back(clone->expression); }
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers) { clone->transformers = transformers->clone(); clone->children.push_back(clone->transformers); }
|
2022-11-02 06:11:37 +00:00
|
|
|
|
2019-07-12 11:17:38 +00:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
void ASTColumnsRegexpMatcher::appendColumnName(WriteBuffer & ostr) const
|
|
|
|
{
|
2022-11-02 06:11:37 +00:00
|
|
|
if (expression)
|
|
|
|
{
|
|
|
|
expression->appendColumnName(ostr);
|
|
|
|
writeCString(".", ostr);
|
|
|
|
}
|
2022-04-19 17:22:04 +00:00
|
|
|
writeCString("COLUMNS(", ostr);
|
|
|
|
writeQuotedString(original_pattern, ostr);
|
|
|
|
writeChar(')', ostr);
|
|
|
|
}
|
2019-07-12 11:17:38 +00:00
|
|
|
|
2023-11-10 12:15:23 +00:00
|
|
|
void ASTColumnsRegexpMatcher::updateTreeHashImpl(SipHash & hash_state, bool ignore_aliases) const
|
2020-08-20 02:01:40 +00:00
|
|
|
{
|
|
|
|
hash_state.update(original_pattern.size());
|
|
|
|
hash_state.update(original_pattern);
|
2023-11-10 12:15:23 +00:00
|
|
|
IAST::updateTreeHashImpl(hash_state, ignore_aliases);
|
2020-08-20 02:01:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
void ASTColumnsRegexpMatcher::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
2019-07-12 11:17:38 +00:00
|
|
|
{
|
2022-11-02 06:11:37 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "");
|
|
|
|
|
|
|
|
if (expression)
|
|
|
|
{
|
|
|
|
expression->formatImpl(settings, state, frame);
|
|
|
|
settings.ostr << ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.ostr << "COLUMNS" << (settings.hilite ? hilite_none : "") << "(";
|
2022-04-19 17:22:04 +00:00
|
|
|
settings.ostr << quoteString(original_pattern);
|
2020-09-16 12:44:05 +00:00
|
|
|
settings.ostr << ")";
|
2022-04-19 17:22:04 +00:00
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers)
|
2020-08-29 05:33:46 +00:00
|
|
|
{
|
2022-11-30 02:14:04 +00:00
|
|
|
transformers->formatImpl(settings, state, frame);
|
2020-08-29 05:33:46 +00:00
|
|
|
}
|
2019-07-16 20:05:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
void ASTColumnsRegexpMatcher::setPattern(String pattern)
|
2019-07-16 20:05:00 +00:00
|
|
|
{
|
|
|
|
original_pattern = std::move(pattern);
|
2019-07-21 02:13:42 +00:00
|
|
|
column_matcher = std::make_shared<RE2>(original_pattern, RE2::Quiet);
|
2019-07-16 20:05:00 +00:00
|
|
|
if (!column_matcher->ok())
|
2023-01-23 13:16:14 +00:00
|
|
|
throw DB::Exception(DB::ErrorCodes::CANNOT_COMPILE_REGEXP,
|
|
|
|
"COLUMNS pattern {} cannot be compiled: {}", original_pattern, column_matcher->error());
|
2019-07-12 11:17:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:11:37 +00:00
|
|
|
const String & ASTColumnsRegexpMatcher::getPattern() const
|
|
|
|
{
|
|
|
|
return original_pattern;
|
|
|
|
}
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
const std::shared_ptr<re2::RE2> & ASTColumnsRegexpMatcher::getMatcher() const
|
|
|
|
{
|
|
|
|
return column_matcher;
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
bool ASTColumnsRegexpMatcher::isColumnMatching(const String & column_name) const
|
2019-07-16 20:05:00 +00:00
|
|
|
{
|
2019-07-21 02:13:42 +00:00
|
|
|
return RE2::PartialMatch(column_name, *column_matcher);
|
2019-07-16 20:05:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
ASTPtr ASTColumnsListMatcher::clone() const
|
|
|
|
{
|
|
|
|
auto clone = std::make_shared<ASTColumnsListMatcher>(*this);
|
2023-02-27 11:26:36 +00:00
|
|
|
clone->children.clear();
|
2022-11-02 06:11:37 +00:00
|
|
|
|
|
|
|
if (expression) { clone->expression = expression->clone(); clone->children.push_back(clone->expression); }
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers) { clone->transformers = transformers->clone(); clone->children.push_back(clone->transformers); }
|
2022-11-02 06:11:37 +00:00
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
clone->column_list = column_list->clone();
|
2022-11-02 06:11:37 +00:00
|
|
|
clone->children.push_back(clone->column_list);
|
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTColumnsListMatcher::appendColumnName(WriteBuffer & ostr) const
|
|
|
|
{
|
2022-11-02 06:11:37 +00:00
|
|
|
if (expression)
|
|
|
|
{
|
|
|
|
expression->appendColumnName(ostr);
|
|
|
|
writeCString(".", ostr);
|
|
|
|
}
|
2022-04-19 17:22:04 +00:00
|
|
|
writeCString("COLUMNS(", ostr);
|
2022-10-18 09:40:12 +00:00
|
|
|
for (auto * it = column_list->children.begin(); it != column_list->children.end(); ++it)
|
2022-04-19 17:22:04 +00:00
|
|
|
{
|
|
|
|
if (it != column_list->children.begin())
|
|
|
|
writeCString(", ", ostr);
|
|
|
|
|
|
|
|
(*it)->appendColumnName(ostr);
|
|
|
|
}
|
|
|
|
writeChar(')', ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTColumnsListMatcher::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
|
|
|
{
|
2022-11-02 06:11:37 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "");
|
|
|
|
|
|
|
|
if (expression)
|
|
|
|
{
|
|
|
|
expression->formatImpl(settings, state, frame);
|
|
|
|
settings.ostr << ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.ostr << "COLUMNS" << (settings.hilite ? hilite_none : "") << "(";
|
2022-04-19 17:22:04 +00:00
|
|
|
|
|
|
|
for (ASTs::const_iterator it = column_list->children.begin(); it != column_list->children.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != column_list->children.begin())
|
|
|
|
{
|
|
|
|
settings.ostr << ", ";
|
|
|
|
}
|
|
|
|
(*it)->formatImpl(settings, state, frame);
|
|
|
|
}
|
|
|
|
settings.ostr << ")";
|
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers)
|
2022-04-19 17:22:04 +00:00
|
|
|
{
|
2022-11-30 02:14:04 +00:00
|
|
|
transformers->formatImpl(settings, state, frame);
|
2022-04-19 17:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-16 20:05:00 +00:00
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
ASTPtr ASTQualifiedColumnsRegexpMatcher::clone() const
|
|
|
|
{
|
|
|
|
auto clone = std::make_shared<ASTQualifiedColumnsRegexpMatcher>(*this);
|
2023-02-27 11:26:36 +00:00
|
|
|
clone->children.clear();
|
2022-11-02 06:11:37 +00:00
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers) { clone->transformers = transformers->clone(); clone->children.push_back(clone->transformers); }
|
2022-11-02 06:11:37 +00:00
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
clone->qualifier = qualifier->clone();
|
2022-11-02 06:11:37 +00:00
|
|
|
clone->children.push_back(clone->qualifier);
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTQualifiedColumnsRegexpMatcher::appendColumnName(WriteBuffer & ostr) const
|
|
|
|
{
|
|
|
|
qualifier->appendColumnName(ostr);
|
|
|
|
writeCString(".COLUMNS(", ostr);
|
|
|
|
writeQuotedString(original_pattern, ostr);
|
|
|
|
writeChar(')', ostr);
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:11:37 +00:00
|
|
|
void ASTQualifiedColumnsRegexpMatcher::setPattern(String pattern, bool set_matcher)
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
|
|
|
original_pattern = std::move(pattern);
|
2022-11-02 06:11:37 +00:00
|
|
|
|
|
|
|
if (!set_matcher)
|
|
|
|
return;
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
column_matcher = std::make_shared<RE2>(original_pattern, RE2::Quiet);
|
|
|
|
if (!column_matcher->ok())
|
2023-01-23 13:16:14 +00:00
|
|
|
throw DB::Exception(DB::ErrorCodes::CANNOT_COMPILE_REGEXP,
|
|
|
|
"COLUMNS pattern {} cannot be compiled: {}", original_pattern, column_matcher->error());
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTQualifiedColumnsRegexpMatcher::setMatcher(std::shared_ptr<re2::RE2> matcher)
|
|
|
|
{
|
|
|
|
column_matcher = std::move(matcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::shared_ptr<re2::RE2> & ASTQualifiedColumnsRegexpMatcher::getMatcher() const
|
|
|
|
{
|
|
|
|
return column_matcher;
|
|
|
|
}
|
|
|
|
|
2023-11-10 12:15:23 +00:00
|
|
|
void ASTQualifiedColumnsRegexpMatcher::updateTreeHashImpl(SipHash & hash_state, bool ignore_aliases) const
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
|
|
|
hash_state.update(original_pattern.size());
|
|
|
|
hash_state.update(original_pattern);
|
2023-11-10 12:15:23 +00:00
|
|
|
IAST::updateTreeHashImpl(hash_state, ignore_aliases);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTQualifiedColumnsRegexpMatcher::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
|
|
|
{
|
2022-10-19 10:25:27 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "");
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
qualifier->formatImpl(settings, state, frame);
|
|
|
|
|
|
|
|
settings.ostr << ".COLUMNS" << (settings.hilite ? hilite_none : "") << "(";
|
|
|
|
settings.ostr << quoteString(original_pattern);
|
|
|
|
settings.ostr << ")";
|
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers)
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-11-30 02:14:04 +00:00
|
|
|
transformers->formatImpl(settings, state, frame);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ASTQualifiedColumnsListMatcher::clone() const
|
|
|
|
{
|
|
|
|
auto clone = std::make_shared<ASTQualifiedColumnsListMatcher>(*this);
|
2023-02-27 11:26:36 +00:00
|
|
|
clone->children.clear();
|
2022-11-02 06:11:37 +00:00
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers) { clone->transformers = transformers->clone(); clone->children.push_back(clone->transformers); }
|
|
|
|
|
2022-11-02 06:11:37 +00:00
|
|
|
clone->qualifier = qualifier->clone();
|
2022-07-14 11:20:16 +00:00
|
|
|
clone->column_list = column_list->clone();
|
2022-11-02 06:11:37 +00:00
|
|
|
|
|
|
|
clone->children.push_back(clone->qualifier);
|
|
|
|
clone->children.push_back(clone->column_list);
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTQualifiedColumnsListMatcher::appendColumnName(WriteBuffer & ostr) const
|
|
|
|
{
|
|
|
|
qualifier->appendColumnName(ostr);
|
|
|
|
writeCString(".COLUMNS(", ostr);
|
|
|
|
|
2022-10-27 11:22:44 +00:00
|
|
|
for (auto * it = column_list->children.begin(); it != column_list->children.end(); ++it)
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
|
|
|
if (it != column_list->children.begin())
|
|
|
|
writeCString(", ", ostr);
|
|
|
|
|
|
|
|
(*it)->appendColumnName(ostr);
|
|
|
|
}
|
|
|
|
writeChar(')', ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTQualifiedColumnsListMatcher::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
|
|
|
{
|
2022-10-19 10:25:27 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "");
|
2022-07-14 11:20:16 +00:00
|
|
|
qualifier->formatImpl(settings, state, frame);
|
2022-10-19 10:25:27 +00:00
|
|
|
settings.ostr << ".COLUMNS" << (settings.hilite ? hilite_none : "") << "(";
|
2022-07-14 11:20:16 +00:00
|
|
|
|
|
|
|
for (ASTs::const_iterator it = column_list->children.begin(); it != column_list->children.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != column_list->children.begin())
|
|
|
|
settings.ostr << ", ";
|
2022-10-19 16:49:17 +00:00
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
(*it)->formatImpl(settings, state, frame);
|
|
|
|
}
|
|
|
|
settings.ostr << ")";
|
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (transformers)
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-11-30 02:14:04 +00:00
|
|
|
transformers->formatImpl(settings, state, frame);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:17:38 +00:00
|
|
|
}
|