ClickHouse/dbms/src/Interpreters/Expression.cpp

247 lines
7.2 KiB
C++
Raw Normal View History

2011-08-12 18:27:39 +00:00
#include <DB/DataTypes/FieldToDataType.h>
#include <DB/Parsers/ASTFunction.h>
#include <DB/Parsers/ASTIdentifier.h>
#include <DB/Parsers/ASTLiteral.h>
#include <DB/Parsers/ASTExpressionList.h>
#include <DB/Interpreters/Expression.h>
namespace DB
{
void Expression::addSemantic(ASTPtr ast)
{
if (ASTFunction * node = dynamic_cast<ASTFunction *>(&*ast))
{
2011-08-19 18:31:14 +00:00
Functions::const_iterator it = context.functions->find(node->name);
if (it == context.functions->end())
2011-08-12 18:27:39 +00:00
throw Exception("Unknown function " + node->name, ErrorCodes::UNKNOWN_FUNCTION);
node->function = it->second;
}
else if (ASTIdentifier * node = dynamic_cast<ASTIdentifier *>(&*ast))
{
2011-08-28 02:22:23 +00:00
if (node->kind == ASTIdentifier::Column)
{
NamesAndTypes::const_iterator it = context.columns.find(node->name);
if (it == context.columns.end())
throw Exception("Unknown identifier " + node->name, ErrorCodes::UNKNOWN_IDENTIFIER);
2011-08-12 18:27:39 +00:00
2011-08-28 02:22:23 +00:00
node->type = it->second;
}
2011-08-12 18:27:39 +00:00
}
else if (ASTLiteral * node = dynamic_cast<ASTLiteral *>(&*ast))
{
node->type = boost::apply_visitor(FieldToDataType(), node->value);
}
2011-08-13 23:03:07 +00:00
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
2011-08-12 18:27:39 +00:00
addSemantic(*it);
}
void Expression::checkTypes(ASTPtr ast)
{
/// Обход в глубину
2011-08-13 23:03:07 +00:00
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
2011-08-12 18:27:39 +00:00
checkTypes(*it);
if (ASTFunction * node = dynamic_cast<ASTFunction *>(&*ast))
{
/// Типы аргументов
DataTypes argument_types;
ASTs & arguments = dynamic_cast<ASTExpressionList &>(*node->arguments).children;
for (ASTs::iterator it = arguments.begin(); it != arguments.end(); ++it)
{
if (ASTFunction * arg = dynamic_cast<ASTFunction *>(&**it))
argument_types.insert(argument_types.end(), arg->return_types.begin(), arg->return_types.end());
else if (ASTIdentifier * arg = dynamic_cast<ASTIdentifier *>(&**it))
argument_types.push_back(arg->type);
else if (ASTLiteral * arg = dynamic_cast<ASTLiteral *>(&**it))
argument_types.push_back(arg->type);
}
/// Получаем типы результата
node->return_types = node->function->getReturnTypes(argument_types);
}
}
void Expression::glueTree(ASTPtr ast)
{
Subtrees subtrees;
glueTreeImpl(ast, subtrees);
}
void Expression::glueTreeImpl(ASTPtr ast, Subtrees & subtrees)
{
/// Обход в глубину
2011-08-13 23:03:07 +00:00
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
2011-08-12 18:27:39 +00:00
glueTreeImpl(*it, subtrees);
if (ASTFunction * node = dynamic_cast<ASTFunction *>(&*ast))
{
String tree_id = node->arguments->getTreeID();
if (subtrees.end() == subtrees.find(tree_id))
subtrees[tree_id] = node->arguments;
else
node->arguments = subtrees[tree_id];
}
else if (ASTExpressionList * node = dynamic_cast<ASTExpressionList *>(&*ast))
{
for (ASTs::iterator it = node->children.begin(); it != node->children.end(); ++it)
{
String tree_id = (*it)->getTreeID();
if (subtrees.end() == subtrees.find(tree_id))
subtrees[tree_id] = *it;
else
*it = subtrees[tree_id];
}
}
}
2011-08-28 02:22:23 +00:00
void Expression::setNotCalculated(ASTPtr ast, unsigned part_id)
2011-08-12 18:27:39 +00:00
{
2011-08-28 02:22:23 +00:00
if (ast->part_id == part_id)
ast->calculated = false;
2011-08-13 23:03:07 +00:00
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
2011-08-28 02:22:23 +00:00
setNotCalculated(*it, part_id);
2011-08-12 18:27:39 +00:00
}
2011-08-28 02:22:23 +00:00
void Expression::execute(Block & block, unsigned part_id)
2011-08-12 18:27:39 +00:00
{
2011-08-28 02:22:23 +00:00
setNotCalculated(ast, part_id);
executeImpl(ast, block, part_id);
2011-08-12 18:27:39 +00:00
}
2011-08-28 02:22:23 +00:00
void Expression::executeImpl(ASTPtr ast, Block & block, unsigned part_id)
2011-08-12 18:27:39 +00:00
{
/// Обход в глубину
2011-08-13 23:03:07 +00:00
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
2011-08-28 02:22:23 +00:00
executeImpl(*it, block, part_id);
2011-08-12 18:27:39 +00:00
2011-08-28 02:22:23 +00:00
if (ast->calculated || ast->part_id != part_id)
2011-08-12 18:27:39 +00:00
return;
/** Столбцы из таблицы уже загружены в блок.
* Вычисление состоит в добавлении в блок новых столбцов - констант и результатов вычислений функций.
*/
if (ASTFunction * node = dynamic_cast<ASTFunction *>(&*ast))
{
2011-08-14 00:49:30 +00:00
//std::cerr << node->getTreeID() << std::endl;
2011-08-12 18:27:39 +00:00
2011-08-12 20:39:42 +00:00
/// Вставляем в блок столбцы - результаты вычисления функции
ColumnNumbers argument_numbers;
2011-08-13 21:14:02 +00:00
ColumnNumbers & result_numbers = node->return_column_numbers;
result_numbers.clear();
2011-08-12 20:39:42 +00:00
size_t res_num = 0;
for (DataTypes::const_iterator it = node->return_types.begin(); it != node->return_types.end(); ++it)
{
ColumnWithNameAndType column;
column.type = *it;
2011-08-13 21:14:02 +00:00
column.name = node->getTreeID() + "_" + Poco::NumberFormatter::format(res_num);
2011-08-12 20:39:42 +00:00
result_numbers.push_back(block.columns());
block.insert(column);
++res_num;
}
2011-08-13 21:05:18 +00:00
ASTs arguments = node->arguments->children;
2011-08-12 20:39:42 +00:00
for (ASTs::iterator it = arguments.begin(); it != arguments.end(); ++it)
{
if (ASTIdentifier * ident = dynamic_cast<ASTIdentifier *>(&**it))
argument_numbers.push_back(block.getPositionByName(ident->name));
2011-08-13 21:14:02 +00:00
else if (ASTFunction * func = dynamic_cast<ASTFunction *>(&**it))
argument_numbers.insert(argument_numbers.end(), func->return_column_numbers.begin(), func->return_column_numbers.end());
2011-08-12 20:39:42 +00:00
else
argument_numbers.push_back(block.getPositionByName((*it)->getTreeID()));
}
node->function->execute(block, argument_numbers, result_numbers);
2011-08-12 18:27:39 +00:00
}
else if (ASTLiteral * node = dynamic_cast<ASTLiteral *>(&*ast))
{
2011-08-14 00:49:30 +00:00
//std::cerr << node->getTreeID() << std::endl;
2011-08-12 20:39:42 +00:00
/// Вставляем в блок столбец - константу
2011-08-12 18:27:39 +00:00
2011-08-12 20:39:42 +00:00
ColumnWithNameAndType column;
column.column = node->type->createConstColumn(block.rows(), node->value);
column.type = node->type;
column.name = node->getTreeID();
2011-08-21 03:41:37 +00:00
2011-08-12 20:39:42 +00:00
block.insert(column);
2011-08-12 18:27:39 +00:00
}
2011-08-12 20:39:42 +00:00
ast->calculated = true;
2011-08-12 18:27:39 +00:00
}
2011-08-13 23:03:07 +00:00
2011-08-28 02:22:23 +00:00
Block Expression::projectResult(Block & block)
2011-08-13 23:03:07 +00:00
{
Block res;
collectFinalColumns(ast, block, res);
return res;
}
void Expression::collectFinalColumns(ASTPtr ast, Block & src, Block & dst)
{
2011-08-28 00:48:51 +00:00
if (ASTIdentifier * ident = dynamic_cast<ASTIdentifier *>(&*ast))
2011-08-28 02:22:23 +00:00
{
if (ident->kind == ASTIdentifier::Column)
dst.insert(src.getByName(ident->name));
}
2011-08-28 00:48:51 +00:00
else if (dynamic_cast<ASTLiteral *>(&*ast))
dst.insert(src.getByName(ast->getTreeID()));
else if (ASTFunction * func = dynamic_cast<ASTFunction *>(&*ast))
for (ColumnNumbers::const_iterator jt = func->return_column_numbers.begin(); jt != func->return_column_numbers.end(); ++jt)
dst.insert(src.getByPosition(*jt));
2011-08-13 23:03:07 +00:00
else
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
2011-08-28 00:48:51 +00:00
collectFinalColumns(*it, src, dst);
2011-08-13 23:03:07 +00:00
}
2011-08-14 00:49:30 +00:00
DataTypes Expression::getReturnTypes()
{
DataTypes res;
getReturnTypesImpl(ast, res);
return res;
}
void Expression::getReturnTypesImpl(ASTPtr ast, DataTypes & res)
{
if (ASTExpressionList * node = dynamic_cast<ASTExpressionList *>(&*ast))
{
for (ASTs::iterator it = node->children.begin(); it != node->children.end(); ++it)
{
if (ASTIdentifier * ident = dynamic_cast<ASTIdentifier *>(&**it))
res.push_back(ident->type);
else if (ASTFunction * func = dynamic_cast<ASTFunction *>(&**it))
res.insert(res.end(), func->return_types.begin(), func->return_types.end());
else if (ASTLiteral * lit = dynamic_cast<ASTLiteral *>(&**it))
res.push_back(lit->type);
}
}
else
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
getReturnTypesImpl(*it, res);
}
2011-08-12 18:27:39 +00:00
}