2017-05-23 18:01:50 +00:00
|
|
|
#include <Core/NamesAndTypes.h>
|
2016-05-23 00:40:28 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-07-22 17:13:05 +00:00
|
|
|
#include <Interpreters/TreeRewriter.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2017-01-14 09:00:19 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2014-12-30 18:04:53 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Columns/ColumnsCommon.h>
|
2020-07-08 23:52:57 +00:00
|
|
|
#include <Columns/FilterDescription.h>
|
2017-01-14 09:00:19 +00:00
|
|
|
|
2017-11-20 05:22:54 +00:00
|
|
|
#include <Storages/VirtualColumnUtils.h>
|
2018-06-05 19:46:49 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2014-12-30 18:04:53 +00:00
|
|
|
|
2014-02-26 10:59:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-05-20 12:16:51 +00:00
|
|
|
namespace
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Verifying that the function depends only on the specified columns
|
2019-05-20 12:16:51 +00:00
|
|
|
bool isValidFunction(const ASTPtr & expression, const NameSet & columns)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
2020-03-09 01:22:33 +00:00
|
|
|
for (const auto & child : expression->children)
|
|
|
|
if (!isValidFunction(child, columns))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2019-01-25 15:42:24 +00:00
|
|
|
if (auto opt_name = IdentifierSemantic::getColumnName(expression))
|
2019-01-14 18:15:04 +00:00
|
|
|
return columns.count(*opt_name);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Extract all subfunctions of the main conjunction, but depending only on the specified columns
|
2019-05-20 12:16:51 +00:00
|
|
|
void extractFunctions(const ASTPtr & expression, const NameSet & columns, std::vector<ASTPtr> & result)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * function = expression->as<ASTFunction>();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (function && function->name == "and")
|
|
|
|
{
|
2020-03-09 01:22:33 +00:00
|
|
|
for (const auto & child : function->arguments->children)
|
|
|
|
extractFunctions(child, columns, result);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
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
|
2019-05-20 12:16:51 +00:00
|
|
|
ASTPtr buildWhereExpression(const ASTs & functions)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
2020-03-08 21:40:00 +00:00
|
|
|
if (functions.empty())
|
2017-12-15 03:47:43 +00:00
|
|
|
return nullptr;
|
|
|
|
if (functions.size() == 1)
|
|
|
|
return functions[0];
|
2019-11-06 17:37:30 +00:00
|
|
|
return makeASTFunction("and", functions);
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 12:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace VirtualColumnUtils
|
|
|
|
{
|
|
|
|
|
2019-11-06 17:37:30 +00:00
|
|
|
void rewriteEntityInAst(ASTPtr ast, const String & column_name, const Field & value, const String & func)
|
|
|
|
{
|
|
|
|
auto & select = ast->as<ASTSelectQuery &>();
|
|
|
|
if (!select.with())
|
|
|
|
select.setExpression(ASTSelectQuery::Expression::WITH, std::make_shared<ASTExpressionList>());
|
|
|
|
|
|
|
|
|
|
|
|
if (func.empty())
|
|
|
|
{
|
|
|
|
auto literal = std::make_shared<ASTLiteral>(value);
|
|
|
|
literal->alias = column_name;
|
|
|
|
literal->prefer_alias_to_column_name = true;
|
|
|
|
select.with()->children.push_back(literal);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto literal = std::make_shared<ASTLiteral>(value);
|
|
|
|
literal->prefer_alias_to_column_name = true;
|
|
|
|
|
|
|
|
auto function = makeASTFunction(func, literal);
|
|
|
|
function->alias = column_name;
|
|
|
|
function->prefer_alias_to_column_name = true;
|
|
|
|
select.with()->children.push_back(function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 19:42:42 +00:00
|
|
|
void filterBlockWithQuery(const ASTPtr & query, Block & block, const Context & context)
|
2014-02-26 10:59:56 +00:00
|
|
|
{
|
2019-03-15 17:09:14 +00:00
|
|
|
const auto & select = query->as<ASTSelectQuery &>();
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!select.where() && !select.prewhere())
|
2018-02-21 19:42:42 +00:00
|
|
|
return;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
NameSet columns;
|
2017-12-25 21:57:29 +00:00
|
|
|
for (const auto & it : block.getNamesAndTypesList())
|
2017-04-01 07:20:54 +00:00
|
|
|
columns.insert(it.name);
|
|
|
|
|
|
|
|
/// We will create an expression that evaluates the expressions in WHERE and PREWHERE, depending only on the existing columns.
|
|
|
|
std::vector<ASTPtr> functions;
|
2019-04-09 14:22:35 +00:00
|
|
|
if (select.where())
|
|
|
|
extractFunctions(select.where(), columns, functions);
|
|
|
|
if (select.prewhere())
|
|
|
|
extractFunctions(select.prewhere(), columns, functions);
|
2018-02-23 08:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr expression_ast = buildWhereExpression(functions);
|
|
|
|
if (!expression_ast)
|
2018-02-21 19:42:42 +00:00
|
|
|
return;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-15 03:48:36 +00:00
|
|
|
/// Let's analyze and calculate the expression.
|
2020-07-22 17:13:05 +00:00
|
|
|
auto syntax_result = TreeRewriter(context).analyze(expression_ast, block.getNamesAndTypesList());
|
2018-11-08 17:28:52 +00:00
|
|
|
ExpressionAnalyzer analyzer(expression_ast, syntax_result, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
ExpressionActionsPtr actions = analyzer.getActions(false);
|
2018-02-23 08:42:10 +00:00
|
|
|
|
2018-03-06 16:11:29 +00:00
|
|
|
Block block_with_filter = block;
|
|
|
|
actions->execute(block_with_filter);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Filter the block.
|
|
|
|
String filter_column_name = expression_ast->getColumnName();
|
2018-12-21 16:00:07 +00:00
|
|
|
ColumnPtr filter_column = block_with_filter.getByName(filter_column_name).column->convertToFullColumnIfConst();
|
2020-07-08 23:52:57 +00:00
|
|
|
|
|
|
|
ConstantFilterDescription constant_filter(*filter_column);
|
|
|
|
|
|
|
|
if (constant_filter.always_true)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (constant_filter.always_false)
|
|
|
|
block = block.cloneEmpty();
|
|
|
|
|
|
|
|
FilterDescription filter(*filter_column);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < block.columns(); ++i)
|
|
|
|
{
|
|
|
|
ColumnPtr & column = block.safeGetByPosition(i).column;
|
2020-07-08 23:52:57 +00:00
|
|
|
column = column->filter(*filter.data, -1);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-07-06 00:19:49 +00:00
|
|
|
|
2014-02-26 10:59:56 +00:00
|
|
|
}
|