2018-12-11 17:43:12 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Core/Block.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Interpreters/convertFieldToType.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/SyntaxAnalyzer.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
2018-07-24 14:05:37 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2016-02-13 06:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2017-12-18 01:11:48 +00:00
|
|
|
std::pair<Field, std::shared_ptr<const IDataType>> evaluateConstantExpression(const ASTPtr & node, const Context & context)
|
2016-02-13 06:37:19 +00:00
|
|
|
{
|
2018-11-08 15:43:14 +00:00
|
|
|
NamesAndTypesList source_columns = {{ "_dummy", std::make_shared<DataTypeUInt8>() }};
|
2018-11-08 17:28:52 +00:00
|
|
|
auto ast = node->clone();
|
2019-01-09 16:16:59 +00:00
|
|
|
auto syntax_result = SyntaxAnalyzer(context).analyze(ast, source_columns);
|
2018-11-08 17:28:52 +00:00
|
|
|
ExpressionActionsPtr expr_for_constant_folding = ExpressionAnalyzer(ast, syntax_result, context).getConstActions();
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// There must be at least one column in the block so that it knows the number of rows.
|
2017-12-14 01:43:19 +00:00
|
|
|
Block block_with_constants{{ ColumnConst::create(ColumnUInt8::create(1, 0), 1), std::make_shared<DataTypeUInt8>(), "_dummy" }};
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
expr_for_constant_folding->execute(block_with_constants);
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block_with_constants || block_with_constants.rows() == 0)
|
2019-02-03 18:31:17 +00:00
|
|
|
throw Exception("Logical error: empty block after evaluation of constant expression for IN, VALUES or LIMIT", ErrorCodes::LOGICAL_ERROR);
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String name = node->getColumnName();
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block_with_constants.has(name))
|
2019-02-03 18:31:17 +00:00
|
|
|
throw Exception("Element of set in IN, VALUES or LIMIT is not a constant expression: " + name, ErrorCodes::BAD_ARGUMENTS);
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnWithTypeAndName & result = block_with_constants.getByName(name);
|
|
|
|
const IColumn & result_column = *result.column;
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-12-09 10:14:45 +00:00
|
|
|
if (!result_column.isColumnConst())
|
2019-02-03 18:31:17 +00:00
|
|
|
throw Exception("Element of set in IN, VALUES or LIMIT is not a constant expression: " + name, ErrorCodes::BAD_ARGUMENTS);
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_pair(result_column[0], result.type);
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
ASTPtr evaluateConstantExpressionAsLiteral(const ASTPtr & node, const Context & context)
|
2018-07-25 12:31:47 +00:00
|
|
|
{
|
2018-12-11 17:43:12 +00:00
|
|
|
/// Branch with string in query.
|
2019-03-08 09:45:34 +00:00
|
|
|
if (node->As<ASTLiteral>())
|
2017-04-01 07:20:54 +00:00
|
|
|
return node;
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2018-07-25 12:31:47 +00:00
|
|
|
/// Branch with TableFunction in query.
|
2019-03-11 12:49:39 +00:00
|
|
|
if (const auto * table_func_ptr = node->As<ASTFunction>())
|
2018-07-24 13:10:34 +00:00
|
|
|
if (TableFunctionFactory::instance().isTableFunctionName(table_func_ptr->name))
|
|
|
|
return node;
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
return std::make_shared<ASTLiteral>(evaluateConstantExpression(node, context).first);
|
2016-08-25 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
ASTPtr evaluateConstantExpressionOrIdentifierAsLiteral(const ASTPtr & node, const Context & context)
|
2016-08-25 17:23:29 +00:00
|
|
|
{
|
2019-03-08 09:45:34 +00:00
|
|
|
if (const auto * id = node->As<ASTIdentifier>())
|
2018-10-22 08:54:54 +00:00
|
|
|
return std::make_shared<ASTLiteral>(id->name);
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return evaluateConstantExpressionAsLiteral(node, context);
|
2016-08-25 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 17:43:12 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using Conjunction = ColumnsWithTypeAndName;
|
|
|
|
using Disjunction = std::vector<Conjunction>;
|
|
|
|
|
|
|
|
Disjunction analyzeEquals(const ASTIdentifier * identifier, const ASTLiteral * literal, const ExpressionActionsPtr & expr)
|
|
|
|
{
|
|
|
|
if (!identifier || !literal)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & name_and_type : expr->getRequiredColumnsWithTypes())
|
|
|
|
{
|
|
|
|
const auto & name = name_and_type.name;
|
|
|
|
const auto & type = name_and_type.type;
|
|
|
|
|
|
|
|
if (name == identifier->name)
|
|
|
|
{
|
|
|
|
ColumnWithTypeAndName column;
|
|
|
|
// FIXME: what to do if field is not convertable?
|
|
|
|
column.column = type->createColumnConst(1, convertFieldToType(literal->value, *type));
|
|
|
|
column.name = name;
|
|
|
|
column.type = type;
|
|
|
|
return {{std::move(column)}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction andDNF(const Disjunction & left, const Disjunction & right)
|
|
|
|
{
|
|
|
|
if (left.empty())
|
|
|
|
{
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction result;
|
|
|
|
|
|
|
|
for (const auto & conjunct1 : left)
|
|
|
|
{
|
|
|
|
for (const auto & conjunct2 : right)
|
|
|
|
{
|
|
|
|
Conjunction new_conjunct{conjunct1};
|
|
|
|
new_conjunct.insert(new_conjunct.end(), conjunct2.begin(), conjunct2.end());
|
|
|
|
result.emplace_back(new_conjunct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction analyzeFunction(const ASTFunction * fn, const ExpressionActionsPtr & expr)
|
|
|
|
{
|
|
|
|
if (!fn)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: enumerate all possible function names!
|
|
|
|
|
|
|
|
if (fn->name == "equals")
|
|
|
|
{
|
|
|
|
const auto * left = fn->arguments->children.front().get();
|
|
|
|
const auto * right = fn->arguments->children.back().get();
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto * identifier = left->As<ASTIdentifier>() ? left->As<ASTIdentifier>() : right->As<ASTIdentifier>();
|
|
|
|
const auto * literal = left->As<ASTLiteral>() ? left->As<ASTLiteral>() : right->As<ASTLiteral>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
return analyzeEquals(identifier, literal, expr);
|
|
|
|
}
|
|
|
|
else if (fn->name == "in")
|
|
|
|
{
|
|
|
|
const auto * left = fn->arguments->children.front().get();
|
|
|
|
const auto * right = fn->arguments->children.back().get();
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto * identifier = left->As<ASTIdentifier>();
|
|
|
|
const auto * inner_fn = right->As<ASTFunction>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (!inner_fn)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto * tuple = inner_fn->children.front()->As<ASTExpressionList>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (!tuple)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction result;
|
|
|
|
|
|
|
|
for (const auto & child : tuple->children)
|
|
|
|
{
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto * literal = child->As<ASTLiteral>();
|
2018-12-11 17:43:12 +00:00
|
|
|
const auto dnf = analyzeEquals(identifier, literal, expr);
|
|
|
|
|
|
|
|
if (dnf.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
result.insert(result.end(), dnf.begin(), dnf.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (fn->name == "or")
|
|
|
|
{
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto * args = fn->children.front()->As<ASTExpressionList>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction result;
|
|
|
|
|
|
|
|
for (const auto & arg : args->children)
|
|
|
|
{
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto dnf = analyzeFunction(arg->As<ASTFunction>(), expr);
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (dnf.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
result.insert(result.end(), dnf.begin(), dnf.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (fn->name == "and")
|
|
|
|
{
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto * args = fn->children.front()->As<ASTExpressionList>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction result;
|
|
|
|
|
|
|
|
for (const auto & arg : args->children)
|
|
|
|
{
|
2019-03-08 09:45:34 +00:00
|
|
|
const auto dnf = analyzeFunction(arg->As<ASTFunction>(), expr);
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (dnf.empty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = andDNF(result, dnf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 12:38:13 +00:00
|
|
|
std::optional<Blocks> evaluateExpressionOverConstantCondition(const ASTPtr & node, const ExpressionActionsPtr & target_expr)
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
|
|
|
Blocks result;
|
|
|
|
|
|
|
|
// TODO: `node` may be always-false literal.
|
|
|
|
|
2019-03-08 09:45:34 +00:00
|
|
|
if (const auto * fn = node->As<ASTFunction>())
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
|
|
|
const auto dnf = analyzeFunction(fn, target_expr);
|
|
|
|
|
|
|
|
if (dnf.empty())
|
|
|
|
{
|
2018-12-19 12:38:13 +00:00
|
|
|
return {};
|
2018-12-11 17:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto hasRequiredColumns = [&target_expr](const Block & block) -> bool
|
|
|
|
{
|
|
|
|
for (const auto & name : target_expr->getRequiredColumns())
|
|
|
|
{
|
|
|
|
bool hasColumn = false;
|
|
|
|
for (const auto & column_name : block.getNames())
|
|
|
|
{
|
|
|
|
if (column_name == name)
|
|
|
|
{
|
|
|
|
hasColumn = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasColumn)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto & conjunct : dnf)
|
|
|
|
{
|
|
|
|
Block block(conjunct);
|
|
|
|
|
|
|
|
// Block should contain all required columns from `target_expr`
|
|
|
|
if (!hasRequiredColumns(block))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
target_expr->execute(block);
|
|
|
|
|
|
|
|
if (block.rows() == 1)
|
|
|
|
{
|
|
|
|
result.push_back(block);
|
|
|
|
}
|
|
|
|
else if (block.rows() == 0)
|
|
|
|
{
|
|
|
|
// filter out cases like "WHERE a = 1 AND a = 2"
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// FIXME: shouldn't happen
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 12:38:13 +00:00
|
|
|
return {result};
|
2018-12-11 17:43:12 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|