2011-08-28 05:13:24 +00:00
|
|
|
|
#include <DB/DataStreams/ExpressionBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/ProjectionBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/FilterBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/LimitBlockInputStream.h>
|
2011-09-04 05:14:52 +00:00
|
|
|
|
#include <DB/DataStreams/PartialSortingBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/MergeSortingBlockInputStream.h>
|
2011-09-25 03:37:09 +00:00
|
|
|
|
#include <DB/DataStreams/AggregatingBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/FinalizingAggregatedBlockInputStream.h>
|
2012-05-30 01:38:02 +00:00
|
|
|
|
#include <DB/DataStreams/MergingAggregatedBlockInputStream.h>
|
2011-11-28 05:51:33 +00:00
|
|
|
|
#include <DB/DataStreams/AsynchronousBlockInputStream.h>
|
2012-01-10 22:11:51 +00:00
|
|
|
|
#include <DB/DataStreams/UnionBlockInputStream.h>
|
2012-02-27 06:28:20 +00:00
|
|
|
|
#include <DB/DataStreams/ParallelAggregatingBlockInputStream.h>
|
2012-05-30 04:45:49 +00:00
|
|
|
|
#include <DB/DataStreams/NullBlockInputStream.h>
|
2012-06-25 02:52:51 +00:00
|
|
|
|
#include <DB/DataStreams/narrowBlockInputStreams.h>
|
2011-10-30 05:19:41 +00:00
|
|
|
|
#include <DB/DataStreams/copyData.h>
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
2011-09-04 05:14:52 +00:00
|
|
|
|
#include <DB/Parsers/ASTFunction.h>
|
2011-08-28 08:02:11 +00:00
|
|
|
|
#include <DB/Parsers/ASTLiteral.h>
|
2011-09-04 05:14:52 +00:00
|
|
|
|
#include <DB/Parsers/ASTOrderByElement.h>
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Interpreters/Expression.h>
|
|
|
|
|
#include <DB/Interpreters/InterpreterSelectQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2012-08-22 20:29:01 +00:00
|
|
|
|
InterpreterSelectQuery::InterpreterSelectQuery(ASTPtr query_ptr_, const Context & context_, QueryProcessingStage::Enum to_stage_)
|
2012-08-02 17:33:31 +00:00
|
|
|
|
: query_ptr(query_ptr_), query(dynamic_cast<ASTSelectQuery &>(*query_ptr)),
|
|
|
|
|
context(context_), settings(context.getSettings()), to_stage(to_stage_),
|
2012-06-25 03:56:45 +00:00
|
|
|
|
log(&Logger::get("InterpreterSelectQuery"))
|
2011-08-28 05:13:24 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 19:21:04 +00:00
|
|
|
|
void InterpreterSelectQuery::getDatabaseAndTableNames(String & database_name, String & table_name)
|
2011-08-28 05:13:24 +00:00
|
|
|
|
{
|
|
|
|
|
/** Если таблица не указана - используем таблицу system.one.
|
|
|
|
|
* Если база данных не указана - используем текущую базу данных.
|
|
|
|
|
*/
|
|
|
|
|
if (!query.table)
|
|
|
|
|
{
|
|
|
|
|
database_name = "system";
|
|
|
|
|
table_name = "one";
|
|
|
|
|
}
|
|
|
|
|
else if (!query.database)
|
2012-08-02 17:33:31 +00:00
|
|
|
|
database_name = context.getCurrentDatabase();
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
|
|
|
|
if (query.database)
|
|
|
|
|
database_name = dynamic_cast<ASTIdentifier &>(*query.database).name;
|
|
|
|
|
if (query.table)
|
|
|
|
|
table_name = dynamic_cast<ASTIdentifier &>(*query.table).name;
|
2012-08-20 19:21:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
2012-08-20 19:21:04 +00:00
|
|
|
|
StoragePtr InterpreterSelectQuery::getTable()
|
|
|
|
|
{
|
|
|
|
|
String database_name;
|
|
|
|
|
String table_name;
|
|
|
|
|
|
|
|
|
|
getDatabaseAndTableNames(database_name, table_name);
|
2012-08-02 17:33:31 +00:00
|
|
|
|
return context.getTable(database_name, table_name);
|
2011-08-28 05:13:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-20 19:21:04 +00:00
|
|
|
|
ASTPtr InterpreterSelectQuery::getCreateQuery()
|
|
|
|
|
{
|
|
|
|
|
String database_name;
|
|
|
|
|
String table_name;
|
|
|
|
|
|
|
|
|
|
getDatabaseAndTableNames(database_name, table_name);
|
|
|
|
|
return context.getCreateQuery(database_name, table_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-11-06 02:29:13 +00:00
|
|
|
|
void InterpreterSelectQuery::setColumns()
|
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
context.setColumns(!query.table || !dynamic_cast<ASTSelectQuery *>(&*query.table)
|
2011-11-06 02:29:13 +00:00
|
|
|
|
? getTable()->getColumnsList()
|
2012-08-02 17:33:31 +00:00
|
|
|
|
: InterpreterSelectQuery(query.table, context).getSampleBlock().getColumnsList());
|
2011-11-06 02:29:13 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
if (context.getColumns().empty())
|
2011-11-06 02:29:13 +00:00
|
|
|
|
throw Exception("There is no available columns", ErrorCodes::THERE_IS_NO_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-28 05:13:24 +00:00
|
|
|
|
DataTypes InterpreterSelectQuery::getReturnTypes()
|
|
|
|
|
{
|
2011-11-06 02:29:13 +00:00
|
|
|
|
setColumns();
|
2011-08-28 05:13:24 +00:00
|
|
|
|
Expression expression(dynamic_cast<ASTSelectQuery &>(*query_ptr).select_expression_list, context);
|
|
|
|
|
return expression.getReturnTypes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
|
Block InterpreterSelectQuery::getSampleBlock()
|
|
|
|
|
{
|
2011-11-06 02:29:13 +00:00
|
|
|
|
setColumns();
|
2011-10-30 05:19:41 +00:00
|
|
|
|
Expression expression(dynamic_cast<ASTSelectQuery &>(*query_ptr).select_expression_list, context);
|
|
|
|
|
return expression.getSampleBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-03-05 00:09:41 +00:00
|
|
|
|
/// Превращает источник в асинхронный, если это указано.
|
|
|
|
|
static inline BlockInputStreamPtr maybeAsynchronous(BlockInputStreamPtr in, bool is_async)
|
|
|
|
|
{
|
|
|
|
|
return is_async
|
|
|
|
|
? new AsynchronousBlockInputStream(in)
|
|
|
|
|
: in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-28 05:13:24 +00:00
|
|
|
|
BlockInputStreamPtr InterpreterSelectQuery::execute()
|
|
|
|
|
{
|
2012-05-09 13:12:38 +00:00
|
|
|
|
/// Добавляем в контекст список доступных столбцов.
|
|
|
|
|
setColumns();
|
|
|
|
|
|
|
|
|
|
/// Объект, с помощью которого анализируется запрос.
|
|
|
|
|
ExpressionPtr expression = new Expression(query_ptr, context);
|
|
|
|
|
|
|
|
|
|
/** Потоки данных. При параллельном выполнении запроса, имеем несколько потоков данных.
|
|
|
|
|
* Если нет GROUP BY, то выполним все операции до ORDER BY и LIMIT параллельно, затем
|
|
|
|
|
* если есть ORDER BY, то склеим потоки с помощью UnionBlockInputStream, а затем MergеSortingBlockInputStream,
|
|
|
|
|
* если нет, то склеим с помощью UnionBlockInputStream,
|
|
|
|
|
* затем применим LIMIT.
|
|
|
|
|
* Если есть GROUP BY, то выполним все операции до GROUP BY, включительно, параллельно;
|
|
|
|
|
* параллельный GROUP BY склеит потоки в один,
|
|
|
|
|
* затем выполним остальные операции с одним получившимся потоком.
|
|
|
|
|
*/
|
|
|
|
|
BlockInputStreams streams;
|
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
/** Вынем данные из Storage. from_stage - до какой стадии запрос был выполнен в Storage. */
|
|
|
|
|
QueryProcessingStage::Enum from_stage = executeFetchColumns(streams, expression);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
2012-05-30 04:45:49 +00:00
|
|
|
|
/** Если данных нет. */
|
|
|
|
|
if (streams.empty())
|
|
|
|
|
return new NullBlockInputStream;
|
|
|
|
|
|
2012-06-25 03:56:45 +00:00
|
|
|
|
LOG_TRACE(log, QueryProcessingStage::toString(from_stage) << " -> " << QueryProcessingStage::toString(to_stage));
|
2012-05-30 01:38:02 +00:00
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
if (to_stage > QueryProcessingStage::FetchColumns)
|
2012-05-09 13:12:38 +00:00
|
|
|
|
{
|
2012-08-22 23:09:26 +00:00
|
|
|
|
/// Вычислим подзапросы в секции IN.
|
|
|
|
|
expression->makeSets();
|
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
/// Нужно ли агрегировать.
|
|
|
|
|
bool need_aggregate = expression->hasAggregates() || query.group_expression_list;
|
2012-05-22 18:32:45 +00:00
|
|
|
|
|
|
|
|
|
if (from_stage < QueryProcessingStage::WithMergeableState)
|
|
|
|
|
{
|
|
|
|
|
executeWhere(streams, expression);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
if (need_aggregate)
|
|
|
|
|
executeAggregation(streams, expression);
|
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
if (from_stage <= QueryProcessingStage::WithMergeableState
|
|
|
|
|
&& to_stage > QueryProcessingStage::WithMergeableState)
|
2012-05-09 13:12:38 +00:00
|
|
|
|
{
|
|
|
|
|
if (need_aggregate)
|
2012-05-30 01:38:02 +00:00
|
|
|
|
{
|
|
|
|
|
/// Если нужно объединить агрегированные результаты с нескольких серверов
|
|
|
|
|
if (from_stage == QueryProcessingStage::WithMergeableState)
|
|
|
|
|
executeMergeAggregated(streams, expression);
|
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
executeFinalizeAggregates(streams, expression);
|
2012-05-30 01:38:02 +00:00
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
|
|
|
|
executeHaving(streams, expression);
|
|
|
|
|
executeOuterExpression(streams, expression);
|
|
|
|
|
executeOrder(streams, expression);
|
2012-06-24 23:17:06 +00:00
|
|
|
|
|
|
|
|
|
/** Оптимизация - если источников несколько и есть LIMIT, то сначала применим предварительный LIMIT,
|
|
|
|
|
* ограничивающий число записей в каждом до offset + limit.
|
|
|
|
|
*/
|
|
|
|
|
if (query.limit_length && streams.size() > 1)
|
|
|
|
|
executePreLimit(streams, expression);
|
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
executeUnion(streams, expression);
|
|
|
|
|
executeLimit(streams, expression);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
executeUnion(streams, expression);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
return streams[0];
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
static void getLimitLengthAndOffset(ASTSelectQuery & query, size_t & length, size_t & offset)
|
|
|
|
|
{
|
|
|
|
|
length = 0;
|
|
|
|
|
offset = 0;
|
|
|
|
|
if (query.limit_length)
|
|
|
|
|
{
|
|
|
|
|
length = boost::get<UInt64>(dynamic_cast<ASTLiteral &>(*query.limit_length).value);
|
|
|
|
|
if (query.limit_offset)
|
|
|
|
|
offset = boost::get<UInt64>(dynamic_cast<ASTLiteral &>(*query.limit_offset).value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
QueryProcessingStage::Enum InterpreterSelectQuery::executeFetchColumns(BlockInputStreams & streams, ExpressionPtr & expression)
|
2012-05-09 13:12:38 +00:00
|
|
|
|
{
|
2011-11-06 02:29:13 +00:00
|
|
|
|
/// Таблица, откуда читать данные, если не подзапрос.
|
|
|
|
|
StoragePtr table;
|
|
|
|
|
/// Интерпретатор подзапроса, если подзапрос
|
|
|
|
|
SharedPtr<InterpreterSelectQuery> interpreter_subquery;
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
2011-11-06 02:29:13 +00:00
|
|
|
|
/// Добавляем в контекст список доступных столбцов.
|
|
|
|
|
setColumns();
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
2011-11-06 22:00:39 +00:00
|
|
|
|
if (!query.table || !dynamic_cast<ASTSelectQuery *>(&*query.table))
|
2011-11-06 02:29:13 +00:00
|
|
|
|
table = getTable();
|
|
|
|
|
else
|
2012-03-05 00:09:41 +00:00
|
|
|
|
interpreter_subquery = new InterpreterSelectQuery(query.table, context);
|
2011-11-06 02:29:13 +00:00
|
|
|
|
|
|
|
|
|
/// Список столбцов, которых нужно прочитать, чтобы выполнить запрос.
|
2011-08-28 08:02:11 +00:00
|
|
|
|
Names required_columns = expression->getRequiredColumns();
|
|
|
|
|
|
|
|
|
|
/// Если не указан ни один столбец из таблицы, то будем читать первый попавшийся (чтобы хотя бы знать число строк).
|
|
|
|
|
if (required_columns.empty())
|
2012-05-09 13:12:38 +00:00
|
|
|
|
required_columns.push_back(getAnyColumn());
|
2011-08-28 08:02:11 +00:00
|
|
|
|
|
2011-09-04 05:14:52 +00:00
|
|
|
|
size_t limit_length = 0;
|
|
|
|
|
size_t limit_offset = 0;
|
2012-05-09 13:12:38 +00:00
|
|
|
|
getLimitLengthAndOffset(query, limit_length, limit_offset);
|
2011-09-04 05:14:52 +00:00
|
|
|
|
|
|
|
|
|
/** Оптимизация - если не указаны WHERE, GROUP, HAVING, ORDER, но указан LIMIT, и limit + offset < max_block_size,
|
2012-06-25 07:26:58 +00:00
|
|
|
|
* то в качестве размера блока будем использовать limit + offset (чтобы не читать из таблицы больше, чем запрошено),
|
|
|
|
|
* а также установим количество потоков в 1 и отменим асинхронное выполнение конвейера запроса.
|
2011-09-04 05:14:52 +00:00
|
|
|
|
*/
|
|
|
|
|
if (!query.where_expression && !query.group_expression_list && !query.having_expression && !query.order_expression_list
|
2012-08-02 17:33:31 +00:00
|
|
|
|
&& query.limit_length && !expression->hasAggregates() && limit_length + limit_offset < settings.max_block_size)
|
2011-09-04 05:14:52 +00:00
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
settings.max_block_size = limit_length + limit_offset;
|
|
|
|
|
settings.max_threads = 1;
|
|
|
|
|
settings.asynchronous = false;
|
2011-09-04 05:14:52 +00:00
|
|
|
|
}
|
2012-05-22 18:32:45 +00:00
|
|
|
|
|
|
|
|
|
QueryProcessingStage::Enum from_stage = QueryProcessingStage::FetchColumns;
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
2012-02-27 06:28:20 +00:00
|
|
|
|
/// Инициализируем изначальные потоки данных, на которые накладываются преобразования запроса. Таблица или подзапрос?
|
2011-11-06 22:00:39 +00:00
|
|
|
|
if (!query.table || !dynamic_cast<ASTSelectQuery *>(&*query.table))
|
2012-08-02 17:33:31 +00:00
|
|
|
|
streams = table->read(required_columns, query_ptr, from_stage, settings.max_block_size, settings.max_threads);
|
2011-11-06 02:29:13 +00:00
|
|
|
|
else
|
2012-08-02 17:33:31 +00:00
|
|
|
|
streams.push_back(maybeAsynchronous(interpreter_subquery->execute(), settings.asynchronous));
|
2012-02-27 07:54:16 +00:00
|
|
|
|
|
2012-06-25 03:01:37 +00:00
|
|
|
|
/** Если истчоников слишком много, то склеим их в max_threads источников.
|
|
|
|
|
* (Иначе действия в каждом маленьком источнике, а затем объединение состояний, слишком неэффективно.)
|
|
|
|
|
*/
|
2012-08-02 17:33:31 +00:00
|
|
|
|
if (streams.size() > settings.max_threads)
|
|
|
|
|
streams = narrowBlockInputStreams(streams, settings.max_threads);
|
2012-06-25 03:01:37 +00:00
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
return from_stage;
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
|
|
|
|
void InterpreterSelectQuery::executeWhere(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
2011-08-28 05:13:24 +00:00
|
|
|
|
/// Если есть условие WHERE - сначала выполним часть выражения, необходимую для его вычисления
|
|
|
|
|
if (query.where_expression)
|
|
|
|
|
{
|
|
|
|
|
setPartID(query.where_expression, PART_WHERE);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
bool is_async = settings.asynchronous && streams.size() <= settings.max_threads;
|
2012-02-27 06:28:20 +00:00
|
|
|
|
for (BlockInputStreams::iterator it = streams.begin(); it != streams.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr & stream = *it;
|
2012-06-21 19:51:20 +00:00
|
|
|
|
stream = maybeAsynchronous(new ExpressionBlockInputStream(stream, expression, PART_WHERE), is_async);
|
|
|
|
|
stream = maybeAsynchronous(new FilterBlockInputStream(stream), is_async);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
}
|
2011-08-28 05:13:24 +00:00
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
2011-08-28 05:13:24 +00:00
|
|
|
|
|
2011-09-25 03:37:09 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
void InterpreterSelectQuery::executeAggregation(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
|
|
|
|
expression->markBeforeAndAfterAggregation(PART_BEFORE_AGGREGATING, PART_AFTER_AGGREGATING);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
if (query.group_expression_list)
|
|
|
|
|
setPartID(query.group_expression_list, PART_GROUP);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
bool is_async = settings.asynchronous && streams.size() <= settings.max_threads;
|
2012-05-09 13:12:38 +00:00
|
|
|
|
for (BlockInputStreams::iterator it = streams.begin(); it != streams.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr & stream = *it;
|
2012-06-21 19:51:20 +00:00
|
|
|
|
stream = maybeAsynchronous(new ExpressionBlockInputStream(stream, expression, PART_GROUP | PART_BEFORE_AGGREGATING), is_async);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
BlockInputStreamPtr & stream = streams[0];
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-06-22 15:39:13 +00:00
|
|
|
|
/// Если источников несколько, то выполняем параллельную агрегацию
|
2012-05-09 13:12:38 +00:00
|
|
|
|
if (streams.size() > 1)
|
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
stream = maybeAsynchronous(new ParallelAggregatingBlockInputStream(streams, expression, settings.max_threads), settings.asynchronous);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
streams.resize(1);
|
2011-11-06 22:00:39 +00:00
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
else
|
2012-08-02 17:33:31 +00:00
|
|
|
|
stream = maybeAsynchronous(new AggregatingBlockInputStream(stream, expression), settings.asynchronous);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
2011-11-06 22:00:39 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
|
|
|
|
void InterpreterSelectQuery::executeFinalizeAggregates(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
|
|
|
|
/// Финализируем агрегатные функции - заменяем их состояния вычислений на готовые значения
|
|
|
|
|
BlockInputStreamPtr & stream = streams[0];
|
2012-08-02 17:33:31 +00:00
|
|
|
|
stream = maybeAsynchronous(new FinalizingAggregatedBlockInputStream(stream), settings.asynchronous);
|
2012-05-30 01:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSelectQuery::executeMergeAggregated(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
|
|
|
|
/// Если объединять нечего
|
|
|
|
|
if (streams.size() == 1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/// Склеим несколько источников в один
|
2012-08-02 17:33:31 +00:00
|
|
|
|
streams[0] = new UnionBlockInputStream(streams, settings.max_threads);
|
2012-05-30 01:38:02 +00:00
|
|
|
|
streams.resize(1);
|
|
|
|
|
|
|
|
|
|
/// Теперь объединим агрегированные блоки
|
2012-08-02 17:33:31 +00:00
|
|
|
|
streams[0] = maybeAsynchronous(new MergingAggregatedBlockInputStream(streams[0], expression), settings.asynchronous);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSelectQuery::executeHaving(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
2011-11-06 22:00:39 +00:00
|
|
|
|
/// Если есть условие HAVING - сначала выполним часть выражения, необходимую для его вычисления
|
|
|
|
|
if (query.having_expression)
|
|
|
|
|
{
|
|
|
|
|
setPartID(query.having_expression, PART_HAVING);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
bool is_async = settings.asynchronous && streams.size() <= settings.max_threads;
|
2012-02-27 06:28:20 +00:00
|
|
|
|
for (BlockInputStreams::iterator it = streams.begin(); it != streams.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr & stream = *it;
|
2012-06-21 19:51:20 +00:00
|
|
|
|
stream = maybeAsynchronous(new ExpressionBlockInputStream(stream, expression, PART_HAVING), is_async);
|
|
|
|
|
stream = maybeAsynchronous(new FilterBlockInputStream(stream), is_async);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
}
|
2011-09-25 03:37:09 +00:00
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-25 03:37:09 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
void InterpreterSelectQuery::executeOuterExpression(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
2011-08-28 05:13:24 +00:00
|
|
|
|
/// Выполним оставшуюся часть выражения
|
|
|
|
|
setPartID(query.select_expression_list, PART_SELECT);
|
2011-09-04 05:14:52 +00:00
|
|
|
|
if (query.order_expression_list)
|
|
|
|
|
setPartID(query.order_expression_list, PART_ORDER);
|
2011-09-05 00:32:22 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
bool is_async = settings.asynchronous && streams.size() <= settings.max_threads;
|
2012-02-27 06:28:20 +00:00
|
|
|
|
for (BlockInputStreams::iterator it = streams.begin(); it != streams.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr & stream = *it;
|
2012-06-21 19:51:20 +00:00
|
|
|
|
stream = maybeAsynchronous(new ExpressionBlockInputStream(stream, expression, PART_SELECT | PART_ORDER), is_async);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
|
|
|
|
/** Оставим только столбцы, нужные для SELECT и ORDER BY части.
|
|
|
|
|
* Если нет ORDER BY - то это последняя проекция, и нужно брать только столбцы из SELECT части.
|
|
|
|
|
*/
|
|
|
|
|
stream = new ProjectionBlockInputStream(stream, expression,
|
|
|
|
|
query.order_expression_list ? true : false,
|
|
|
|
|
PART_SELECT | PART_ORDER,
|
|
|
|
|
query.order_expression_list ? NULL : query.select_expression_list);
|
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSelectQuery::executeOrder(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
2011-09-04 05:14:52 +00:00
|
|
|
|
/// Если есть ORDER BY
|
|
|
|
|
if (query.order_expression_list)
|
|
|
|
|
{
|
|
|
|
|
SortDescription order_descr;
|
|
|
|
|
order_descr.reserve(query.order_expression_list->children.size());
|
|
|
|
|
for (ASTs::iterator it = query.order_expression_list->children.begin();
|
|
|
|
|
it != query.order_expression_list->children.end();
|
|
|
|
|
++it)
|
|
|
|
|
{
|
2011-09-26 01:50:32 +00:00
|
|
|
|
String name = (*it)->children.front()->getColumnName();
|
2011-09-04 05:14:52 +00:00
|
|
|
|
order_descr.push_back(SortColumnDescription(name, dynamic_cast<ASTOrderByElement &>(**it).direction));
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
bool is_async = settings.asynchronous && streams.size() <= settings.max_threads;
|
2012-02-27 06:28:20 +00:00
|
|
|
|
for (BlockInputStreams::iterator it = streams.begin(); it != streams.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr & stream = *it;
|
2012-06-21 19:51:20 +00:00
|
|
|
|
stream = maybeAsynchronous(new PartialSortingBlockInputStream(stream, order_descr), is_async);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr & stream = streams[0];
|
|
|
|
|
|
|
|
|
|
/// Если потоков несколько, то объединяем их в один
|
|
|
|
|
if (streams.size() > 1)
|
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
stream = new UnionBlockInputStream(streams, settings.max_threads);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
streams.resize(1);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-25 20:29:22 +00:00
|
|
|
|
/// Сливаем сортированные блоки
|
|
|
|
|
stream = maybeAsynchronous(new MergeSortingBlockInputStream(stream, order_descr), is_async);
|
|
|
|
|
|
2011-09-05 00:32:22 +00:00
|
|
|
|
/// Оставим только столбцы, нужные для SELECT части
|
2011-09-25 03:37:09 +00:00
|
|
|
|
stream = new ProjectionBlockInputStream(stream, expression, false, PART_SELECT, query.select_expression_list);
|
2011-09-04 05:14:52 +00:00
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
void InterpreterSelectQuery::executeUnion(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
2012-02-27 06:28:20 +00:00
|
|
|
|
/// Если до сих пор есть несколько потоков, то объединяем их в один
|
|
|
|
|
if (streams.size() > 1)
|
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
streams[0] = new UnionBlockInputStream(streams, settings.max_threads);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
streams.resize(1);
|
|
|
|
|
}
|
2012-05-09 13:12:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-24 23:17:06 +00:00
|
|
|
|
/// Предварительный LIMIT - применяется в каждом источнике, если источников несколько, до их объединения.
|
|
|
|
|
void InterpreterSelectQuery::executePreLimit(BlockInputStreams & streams, ExpressionPtr & expression)
|
2012-05-09 13:12:38 +00:00
|
|
|
|
{
|
|
|
|
|
size_t limit_length = 0;
|
|
|
|
|
size_t limit_offset = 0;
|
|
|
|
|
getLimitLengthAndOffset(query, limit_length, limit_offset);
|
2012-02-27 06:28:20 +00:00
|
|
|
|
|
2012-06-24 23:17:06 +00:00
|
|
|
|
/// Если есть LIMIT
|
|
|
|
|
if (query.limit_length)
|
|
|
|
|
{
|
|
|
|
|
for (BlockInputStreams::iterator it = streams.begin(); it != streams.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr & stream = *it;
|
|
|
|
|
stream = new LimitBlockInputStream(stream, limit_length + limit_offset, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSelectQuery::executeLimit(BlockInputStreams & streams, ExpressionPtr & expression)
|
|
|
|
|
{
|
|
|
|
|
size_t limit_length = 0;
|
|
|
|
|
size_t limit_offset = 0;
|
|
|
|
|
getLimitLengthAndOffset(query, limit_length, limit_offset);
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
2011-08-28 08:02:11 +00:00
|
|
|
|
/// Если есть LIMIT
|
|
|
|
|
if (query.limit_length)
|
|
|
|
|
{
|
2012-06-24 23:17:06 +00:00
|
|
|
|
BlockInputStreamPtr & stream = streams[0];
|
2011-08-28 08:02:11 +00:00
|
|
|
|
stream = new LimitBlockInputStream(stream, limit_length, limit_offset);
|
|
|
|
|
}
|
2011-08-28 05:13:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
|
BlockInputStreamPtr InterpreterSelectQuery::executeAndFormat(WriteBuffer & buf)
|
|
|
|
|
{
|
|
|
|
|
Block sample = getSampleBlock();
|
|
|
|
|
String format_name = query.format ? dynamic_cast<ASTIdentifier &>(*query.format).name : "TabSeparated";
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr in = execute();
|
2012-08-02 17:33:31 +00:00
|
|
|
|
BlockOutputStreamPtr out = context.getFormatFactory().getOutput(format_name, buf, sample);
|
2011-10-30 05:19:41 +00:00
|
|
|
|
|
|
|
|
|
copyData(*in, *out);
|
|
|
|
|
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-28 05:13:24 +00:00
|
|
|
|
void InterpreterSelectQuery::setPartID(ASTPtr ast, unsigned part_id)
|
|
|
|
|
{
|
2011-09-04 05:14:52 +00:00
|
|
|
|
ast->part_id |= part_id;
|
2011-10-30 05:19:41 +00:00
|
|
|
|
|
2011-08-28 05:13:24 +00:00
|
|
|
|
for (ASTs::iterator it = ast->children.begin(); it != ast->children.end(); ++it)
|
|
|
|
|
setPartID(*it, part_id);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
|
|
|
|
String InterpreterSelectQuery::getAnyColumn()
|
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
NamesAndTypesList::const_iterator it = context.getColumns().begin();
|
2012-05-09 13:12:38 +00:00
|
|
|
|
|
|
|
|
|
size_t min_size = it->second->isNumeric() ? it->second->getSizeOfField() : 100;
|
|
|
|
|
String res = it->first;
|
2012-08-02 17:33:31 +00:00
|
|
|
|
for (; it != context.getColumns().end(); ++it)
|
2012-05-09 13:12:38 +00:00
|
|
|
|
{
|
|
|
|
|
size_t current_size = it->second->isNumeric() ? it->second->getSizeOfField() : 100;
|
|
|
|
|
if (current_size < min_size)
|
|
|
|
|
{
|
|
|
|
|
min_size = current_size;
|
|
|
|
|
res = it->first;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-28 05:13:24 +00:00
|
|
|
|
}
|