2016-05-23 00:40:28 +00:00
|
|
|
#include <numeric>
|
|
|
|
|
2014-02-26 10:59:56 +00:00
|
|
|
#include <DB/Interpreters/Context.h>
|
2014-12-30 18:04:53 +00:00
|
|
|
#include <DB/Interpreters/ExpressionAnalyzer.h>
|
2016-01-13 00:32:59 +00:00
|
|
|
#include <DB/Interpreters/ExpressionActions.h>
|
2017-01-14 09:00:19 +00:00
|
|
|
|
2014-02-26 10:59:56 +00:00
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
|
|
|
#include <DB/Parsers/ASTExpressionList.h>
|
|
|
|
#include <DB/Parsers/ASTLiteral.h>
|
2017-01-14 09:00:19 +00:00
|
|
|
#include <DB/Parsers/ASTFunction.h>
|
2014-02-26 10:59:56 +00:00
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
2014-12-30 18:04:53 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
#include <DB/Columns/ColumnsNumber.h>
|
|
|
|
|
2014-12-30 18:04:53 +00:00
|
|
|
#include <DB/Common/VirtualColumnUtils.h>
|
|
|
|
|
2014-02-26 10:59:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace VirtualColumnUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
String chooseSuffix(const NamesAndTypesList & columns, const String & name)
|
|
|
|
{
|
|
|
|
int id = 0;
|
|
|
|
String current_suffix;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
bool done = true;
|
|
|
|
for (const auto & it : columns)
|
2014-07-09 11:45:51 +00:00
|
|
|
if (it.name == name + current_suffix)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
|
|
|
done = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (done) break;
|
|
|
|
++id;
|
|
|
|
current_suffix = toString<Int32>(id);
|
|
|
|
}
|
|
|
|
return current_suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
String chooseSuffixForSet(const NamesAndTypesList & columns, const std::vector<String> & names)
|
|
|
|
{
|
|
|
|
int id = 0;
|
|
|
|
String current_suffix;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
bool done = true;
|
|
|
|
for (const auto & it : columns)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < names.size(); ++i)
|
|
|
|
{
|
2014-07-09 11:45:51 +00:00
|
|
|
if (it.name == names[i] + current_suffix)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
|
|
|
done = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (done)
|
|
|
|
break;
|
2014-03-27 19:09:23 +00:00
|
|
|
++id;
|
2014-02-26 10:59:56 +00:00
|
|
|
current_suffix = toString<Int32>(id);
|
|
|
|
}
|
|
|
|
return current_suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewriteEntityInAst(ASTPtr ast, const String & column_name, const Field & value)
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
ASTSelectQuery & select = typeid_cast<ASTSelectQuery &>(*ast);
|
|
|
|
ASTExpressionList & node = typeid_cast<ASTExpressionList &>(*select.select_expression_list);
|
2014-02-26 10:59:56 +00:00
|
|
|
ASTs & asts = node.children;
|
2016-05-28 15:42:22 +00:00
|
|
|
auto cur = std::make_shared<ASTLiteral>(StringRange(), value);
|
2014-02-26 10:59:56 +00:00
|
|
|
cur->alias = column_name;
|
|
|
|
ASTPtr column_value = cur;
|
|
|
|
bool is_replaced = false;
|
|
|
|
for (size_t i = 0; i < asts.size(); ++i)
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
if (const ASTIdentifier * identifier = typeid_cast<const ASTIdentifier *>(&* asts[i]))
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
|
|
|
if (identifier->kind == ASTIdentifier::Kind::Column && identifier->name == column_name)
|
|
|
|
{
|
|
|
|
asts[i] = column_value;
|
|
|
|
is_replaced = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_replaced)
|
|
|
|
asts.insert(asts.begin(), column_value);
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Verifying that the function depends only on the specified columns
|
2014-07-29 14:05:15 +00:00
|
|
|
static bool isValidFunction(ASTPtr expression, const NameSet & columns)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < expression->children.size(); ++i)
|
|
|
|
if (!isValidFunction(expression->children[i], columns))
|
|
|
|
return false;
|
|
|
|
|
2014-10-06 03:49:56 +00:00
|
|
|
if (const ASTIdentifier * identifier = typeid_cast<const ASTIdentifier *>(&*expression))
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
|
|
|
if (identifier->kind == ASTIdentifier::Kind::Column)
|
2014-07-29 14:05:15 +00:00
|
|
|
return columns.count(identifier->name);
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Extract all subfunctions of the main conjunction, but depending only on the specified columns
|
2014-07-29 14:05:15 +00:00
|
|
|
static void extractFunctions(ASTPtr expression, const NameSet & columns, std::vector<ASTPtr> & result)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
2014-10-06 03:49:56 +00:00
|
|
|
const ASTFunction * function = typeid_cast<const ASTFunction *>(&*expression);
|
2014-08-08 09:04:06 +00:00
|
|
|
if (function && function->name == "and")
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
2014-08-08 09:04:06 +00:00
|
|
|
for (size_t i = 0; i < function->arguments->children.size(); ++i)
|
|
|
|
extractFunctions(function->arguments->children[i], columns, result);
|
|
|
|
}
|
|
|
|
else if (isValidFunction(expression, columns))
|
|
|
|
{
|
|
|
|
result.push_back(expression->clone());
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Construct a conjunction from given functions
|
2014-02-26 10:59:56 +00:00
|
|
|
static ASTPtr buildWhereExpression(const ASTs & functions)
|
|
|
|
{
|
2014-04-08 07:58:53 +00:00
|
|
|
if (functions.size() == 0) return nullptr;
|
2014-02-26 10:59:56 +00:00
|
|
|
if (functions.size() == 1) return functions[0];
|
2016-05-28 15:42:22 +00:00
|
|
|
ASTPtr new_query = std::make_shared<ASTFunction>();
|
2014-06-26 00:58:14 +00:00
|
|
|
ASTFunction & new_function = typeid_cast<ASTFunction & >(*new_query);
|
2014-02-26 10:59:56 +00:00
|
|
|
new_function.name = "and";
|
2016-05-28 15:42:22 +00:00
|
|
|
new_function.arguments = std::make_shared<ASTExpressionList>();
|
2014-02-26 10:59:56 +00:00
|
|
|
new_function.arguments->children = functions;
|
|
|
|
new_function.children.push_back(new_function.arguments);
|
|
|
|
return new_query;
|
|
|
|
}
|
|
|
|
|
2014-07-29 14:05:15 +00:00
|
|
|
bool filterBlockWithQuery(ASTPtr query, Block & block, const Context & context)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
2014-10-07 18:42:35 +00:00
|
|
|
query = query->clone();
|
2014-06-26 00:58:14 +00:00
|
|
|
const ASTSelectQuery & select = typeid_cast<ASTSelectQuery & >(*query);
|
2014-07-28 07:53:55 +00:00
|
|
|
if (!select.where_expression && !select.prewhere_expression)
|
2014-07-29 14:05:15 +00:00
|
|
|
return false;
|
2014-02-26 10:59:56 +00:00
|
|
|
|
2014-07-29 14:05:15 +00:00
|
|
|
NameSet columns;
|
|
|
|
for (const auto & it : block.getColumnsList())
|
|
|
|
columns.insert(it.name);
|
2014-02-26 10:59:56 +00:00
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// We will create an expression that evaluates the expressions in WHERE and PREWHERE, depending only on the existing columns.
|
2014-02-26 10:59:56 +00:00
|
|
|
std::vector<ASTPtr> functions;
|
2014-07-28 07:53:55 +00:00
|
|
|
if (select.where_expression)
|
|
|
|
extractFunctions(select.where_expression, columns, functions);
|
|
|
|
if (select.prewhere_expression)
|
|
|
|
extractFunctions(select.prewhere_expression, columns, functions);
|
2014-07-29 14:05:15 +00:00
|
|
|
ASTPtr expression_ast = buildWhereExpression(functions);
|
|
|
|
if (!expression_ast)
|
|
|
|
return false;
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Let's parse and calculate the expression.
|
2015-07-15 01:26:35 +00:00
|
|
|
ExpressionAnalyzer analyzer(expression_ast, context, {}, block.getColumnsList());
|
2014-07-29 14:05:15 +00:00
|
|
|
ExpressionActionsPtr actions = analyzer.getActions(false);
|
|
|
|
actions->execute(block);
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Filter the block.
|
2014-07-29 14:05:15 +00:00
|
|
|
String filter_column_name = expression_ast->getColumnName();
|
|
|
|
ColumnPtr filter_column = block.getByName(filter_column_name).column;
|
2015-10-28 21:21:18 +00:00
|
|
|
if (auto converted = filter_column->convertToFullColumnIfConst())
|
|
|
|
filter_column = converted;
|
2014-07-29 14:05:15 +00:00
|
|
|
const IColumn::Filter & filter = dynamic_cast<ColumnUInt8 &>(*filter_column).getData();
|
|
|
|
|
|
|
|
if (std::accumulate(filter.begin(), filter.end(), 0ul) == filter.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < block.columns(); ++i)
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
ColumnPtr & column = block.safeGetByPosition(i).column;
|
2015-12-05 07:01:18 +00:00
|
|
|
column = column->filter(filter, -1);
|
2014-07-29 14:05:15 +00:00
|
|
|
}
|
2014-02-26 10:59:56 +00:00
|
|
|
|
2014-07-29 14:05:15 +00:00
|
|
|
return true;
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-07-06 00:19:49 +00:00
|
|
|
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|