2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/ExpressionActions.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.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-02-16 22:05:48 +00:00
|
|
|
std::pair<Field, std::shared_ptr<IDataType>> evaluateConstantExpression(std::shared_ptr<IAST> & node, const Context & context)
|
2016-02-13 06:37:19 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ExpressionActionsPtr expr_for_constant_folding = ExpressionAnalyzer(
|
|
|
|
node, context, nullptr, NamesAndTypesList{{ "_dummy", std::make_shared<DataTypeUInt8>() }}).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-04-01 07:20:54 +00:00
|
|
|
Block block_with_constants{{ std::make_shared<ColumnConstUInt8>(1, 0), 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)
|
|
|
|
throw Exception("Logical error: empty block after evaluation constant expression for IN or VALUES", 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))
|
|
|
|
throw Exception("Element of set in IN or VALUES 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-04-01 07:20:54 +00:00
|
|
|
if (!result_column.isConst())
|
|
|
|
throw Exception("Element of set in IN or VALUES 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
|
|
|
|
|
|
|
ASTPtr evaluateConstantExpressionAsLiteral(ASTPtr & node, const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (typeid_cast<const ASTLiteral *>(node.get()))
|
|
|
|
return node;
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<ASTLiteral>(node->range,
|
|
|
|
evaluateConstantExpression(node, context).first);
|
2016-08-25 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr evaluateConstantExpressionOrIdentidierAsLiteral(ASTPtr & node, const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (const ASTIdentifier * id = typeid_cast<const ASTIdentifier *>(node.get()))
|
|
|
|
return std::make_shared<ASTLiteral>(node->range, Field(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
|
|
|
}
|
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|