2012-12-05 12:44:55 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
#include <DB/Interpreters/Context.h>
|
|
|
|
|
#include <DB/Core/SortDescription.h>
|
|
|
|
|
#include <DB/Parsers/ASTExpressionList.h>
|
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
|
#include <DB/Parsers/ASTFunction.h>
|
|
|
|
|
#include <DB/Parsers/ASTLiteral.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
2013-12-12 00:50:54 +00:00
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
|
class IFunction;
|
|
|
|
|
using FunctionPtr = std::shared_ptr<IFunction>;
|
|
|
|
|
|
2013-12-12 00:50:54 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/** Диапазон с открытыми или закрытыми концами; возможно, неограниченный.
|
2013-12-12 00:50:54 +00:00
|
|
|
|
*/
|
2012-12-05 12:44:55 +00:00
|
|
|
|
struct Range
|
|
|
|
|
{
|
2013-12-12 00:50:54 +00:00
|
|
|
|
private:
|
2015-06-24 21:24:06 +00:00
|
|
|
|
static bool equals(const Field & lhs, const Field & rhs);
|
|
|
|
|
static bool less(const Field & lhs, const Field & rhs);
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2013-12-12 00:50:54 +00:00
|
|
|
|
public:
|
2012-12-05 12:44:55 +00:00
|
|
|
|
Field left; /// левая граница, если есть
|
|
|
|
|
Field right; /// правая граница, если есть
|
2015-05-26 00:35:13 +00:00
|
|
|
|
bool left_bounded = false; /// ограничен ли слева
|
|
|
|
|
bool right_bounded = false; /// ограничен ли справа
|
|
|
|
|
bool left_included = false; /// включает левую границу, если есть
|
|
|
|
|
bool right_included = false; /// включает правую границу, если есть
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// Всё множество.
|
2015-05-26 00:35:13 +00:00
|
|
|
|
Range() {}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// Одна точка.
|
2016-03-27 12:50:49 +00:00
|
|
|
|
Range(const Field & point)
|
|
|
|
|
: left(point), right(point), left_bounded(true), right_bounded(true), left_included(true), right_included(true) {}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// Ограниченный с двух сторон диапазон.
|
|
|
|
|
Range(const Field & left_, bool left_included_, const Field & right_, bool right_included_)
|
2016-03-27 12:50:49 +00:00
|
|
|
|
: left(left_), right(right_),
|
|
|
|
|
left_bounded(true), right_bounded(true),
|
|
|
|
|
left_included(left_included_), right_included(right_included_)
|
|
|
|
|
{
|
|
|
|
|
shrinkToIncludedIfPossible();
|
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2013-12-11 20:44:06 +00:00
|
|
|
|
static Range createRightBounded(const Field & right_point, bool right_included)
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
Range r;
|
|
|
|
|
r.right = right_point;
|
|
|
|
|
r.right_bounded = true;
|
|
|
|
|
r.right_included = right_included;
|
2016-03-27 12:50:49 +00:00
|
|
|
|
r.shrinkToIncludedIfPossible();
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2013-12-11 20:44:06 +00:00
|
|
|
|
static Range createLeftBounded(const Field & left_point, bool left_included)
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
Range r;
|
2013-12-11 20:44:06 +00:00
|
|
|
|
r.left = left_point;
|
2012-12-05 12:44:55 +00:00
|
|
|
|
r.left_bounded = true;
|
|
|
|
|
r.left_included = left_included;
|
2016-03-27 12:50:49 +00:00
|
|
|
|
r.shrinkToIncludedIfPossible();
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2016-03-27 12:50:49 +00:00
|
|
|
|
/** Оптимизировать диапазон. Если у него есть открытая граница и тип Field "неплотный"
|
|
|
|
|
* - то преобразовать её в закрытую, сузив на единицу.
|
|
|
|
|
* То есть, например, превратить (0,2) в [1].
|
|
|
|
|
*/
|
|
|
|
|
void shrinkToIncludedIfPossible()
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
2016-03-27 12:50:49 +00:00
|
|
|
|
if (left_bounded && !left_included)
|
|
|
|
|
{
|
|
|
|
|
if (left.getType() == Field::Types::UInt64 && left.get<UInt64>() != std::numeric_limits<UInt64>::max())
|
|
|
|
|
{
|
|
|
|
|
++left.get<UInt64 &>();
|
|
|
|
|
left_included = true;
|
|
|
|
|
}
|
|
|
|
|
if (left.getType() == Field::Types::Int64 && left.get<Int64>() != std::numeric_limits<Int64>::max())
|
|
|
|
|
{
|
|
|
|
|
++left.get<Int64 &>();
|
|
|
|
|
left_included = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (right_bounded && !right_included)
|
|
|
|
|
{
|
|
|
|
|
if (right.getType() == Field::Types::UInt64 && right.get<UInt64>() != std::numeric_limits<UInt64>::min())
|
|
|
|
|
{
|
|
|
|
|
--right.get<UInt64 &>();
|
|
|
|
|
right_included = true;
|
|
|
|
|
}
|
|
|
|
|
if (right.getType() == Field::Types::Int64 && right.get<Int64>() != std::numeric_limits<Int64>::min())
|
|
|
|
|
{
|
|
|
|
|
--right.get<Int64 &>();
|
|
|
|
|
right_included = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-05 12:44:55 +00:00
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2016-03-27 12:50:49 +00:00
|
|
|
|
bool empty() const
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
2016-03-27 12:50:49 +00:00
|
|
|
|
return left_bounded && right_bounded
|
|
|
|
|
&& (less(right, left)
|
|
|
|
|
|| ((!left_included || !right_included) && !less(left, right)));
|
2012-12-05 12:44:55 +00:00
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// x входит в range
|
2013-12-11 20:44:06 +00:00
|
|
|
|
bool contains(const Field & x) const
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
return !leftThan(x) && !rightThan(x);
|
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// x находится левее
|
2013-12-11 20:44:06 +00:00
|
|
|
|
bool rightThan(const Field & x) const
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
return (left_bounded
|
2013-12-12 00:50:54 +00:00
|
|
|
|
? !(less(left, x) || (left_included && equals(x, left)))
|
2013-12-11 20:44:06 +00:00
|
|
|
|
: false);
|
2012-12-05 12:44:55 +00:00
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// x находится правее
|
2013-12-11 20:44:06 +00:00
|
|
|
|
bool leftThan(const Field & x) const
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
return (right_bounded
|
2013-12-12 00:50:54 +00:00
|
|
|
|
? !(less(x, right) || (right_included && equals(x, right)))
|
2013-12-11 20:44:06 +00:00
|
|
|
|
: false);
|
2012-12-05 12:44:55 +00:00
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2013-12-11 20:44:06 +00:00
|
|
|
|
bool intersectsRange(const Range & r) const
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
/// r левее меня.
|
2013-12-11 20:44:06 +00:00
|
|
|
|
if (r.right_bounded
|
|
|
|
|
&& left_bounded
|
2013-12-12 00:50:54 +00:00
|
|
|
|
&& (less(r.right, left)
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|| ((!left_included || !r.right_included)
|
2013-12-12 00:50:54 +00:00
|
|
|
|
&& equals(r.right, left))))
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return false;
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// r правее меня.
|
2013-12-11 20:44:06 +00:00
|
|
|
|
if (r.left_bounded
|
|
|
|
|
&& right_bounded
|
2013-12-12 00:50:54 +00:00
|
|
|
|
&& (less(right, r.left) /// ...} {...
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|| ((!right_included || !r.left_included) /// ...)[... или ...](...
|
2013-12-12 00:50:54 +00:00
|
|
|
|
&& equals(r.left, right))))
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return false;
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2013-12-11 20:44:06 +00:00
|
|
|
|
bool containsRange(const Range & r) const
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
/// r начинается левее меня.
|
2013-12-11 20:44:06 +00:00
|
|
|
|
if (left_bounded
|
|
|
|
|
&& (!r.left_bounded
|
2013-12-12 00:50:54 +00:00
|
|
|
|
|| less(r.left, left)
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|| (r.left_included
|
|
|
|
|
&& !left_included
|
2013-12-12 00:50:54 +00:00
|
|
|
|
&& equals(r.left, left))))
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return false;
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// r заканчивается правее меня.
|
2013-12-11 20:44:06 +00:00
|
|
|
|
if (right_bounded
|
|
|
|
|
&& (!r.right_bounded
|
2013-12-12 00:50:54 +00:00
|
|
|
|
|| less(right, r.right)
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|| (r.right_included
|
|
|
|
|
&& !right_included
|
2013-12-12 00:50:54 +00:00
|
|
|
|
&& equals(r.right, right))))
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return false;
|
2013-12-11 20:44:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2015-11-29 08:06:29 +00:00
|
|
|
|
void swapLeftAndRight()
|
|
|
|
|
{
|
|
|
|
|
std::swap(left, right);
|
|
|
|
|
std::swap(left_bounded, right_bounded);
|
|
|
|
|
std::swap(left_included, right_included);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
|
String toString() const;
|
2012-12-05 12:44:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-03-27 11:37:25 +00:00
|
|
|
|
|
2014-05-12 00:45:50 +00:00
|
|
|
|
class ASTSet;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Условие на индекс.
|
|
|
|
|
*
|
|
|
|
|
* Состоит из условий на принадлежность ключа всевозможным диапазонам или множествам,
|
|
|
|
|
* а также логических связок AND/OR/NOT над этими условиями.
|
|
|
|
|
*
|
|
|
|
|
* Составляет reverse polish notation от этих условий
|
|
|
|
|
* и умеет вычислять (интерпретировать) её выполнимость над диапазонами ключа.
|
|
|
|
|
*/
|
2012-12-10 10:23:10 +00:00
|
|
|
|
class PKCondition
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2013-05-06 12:15:34 +00:00
|
|
|
|
/// Не учитывает секцию SAMPLE. all_columns - набор всех столбцов таблицы.
|
2016-11-21 12:58:42 +00:00
|
|
|
|
PKCondition(ASTPtr & query, const Context & context, const NamesAndTypesList & all_columns, const SortDescription & sort_descr,
|
2016-11-23 19:58:33 +00:00
|
|
|
|
const Block & pk_sample_block);
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-06 09:45:09 +00:00
|
|
|
|
/// Выполнимо ли условие в диапазоне ключей.
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// left_pk и right_pk должны содержать все поля из sort_descr в соответствующем порядке.
|
2015-11-29 08:06:29 +00:00
|
|
|
|
/// data_types - типы столбцов первичного ключа.
|
2016-03-27 11:37:25 +00:00
|
|
|
|
bool mayBeTrueInRange(size_t used_key_size, const Field * left_pk, const Field * right_pk, const DataTypes & data_types) const;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-06 09:45:09 +00:00
|
|
|
|
/// Выполнимо ли условие в полубесконечном (не ограниченном справа) диапазоне ключей.
|
|
|
|
|
/// left_pk должен содержать все поля из sort_descr в соответствующем порядке.
|
2016-03-27 11:37:25 +00:00
|
|
|
|
bool mayBeTrueAfter(size_t used_key_size, const Field * left_pk, const DataTypes & data_types) const;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
|
|
|
|
/// Проверяет, что индекс не может быть использован.
|
2016-01-22 21:28:58 +00:00
|
|
|
|
bool alwaysUnknownOrTrue() const;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2016-03-27 11:37:25 +00:00
|
|
|
|
/// Получить максимальный номер используемого в условии элемента первичного ключа.
|
|
|
|
|
size_t getMaxKeyColumn() const;
|
|
|
|
|
|
2012-12-12 14:25:55 +00:00
|
|
|
|
/// Наложить дополнительное условие: значение в столбце column должно быть в диапазоне range.
|
|
|
|
|
/// Возвращает, есть ли такой столбец в первичном ключе.
|
|
|
|
|
bool addCondition(const String & column, const Range & range);
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2015-03-27 03:37:46 +00:00
|
|
|
|
String toString() const;
|
2015-09-21 12:13:05 +00:00
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
|
|
2012-12-10 10:23:10 +00:00
|
|
|
|
/// Выражение хранится в виде обратной польской строки (Reverse Polish Notation).
|
2012-12-05 12:44:55 +00:00
|
|
|
|
struct RPNElement
|
|
|
|
|
{
|
|
|
|
|
enum Function
|
|
|
|
|
{
|
|
|
|
|
/// Атомы логического выражения.
|
|
|
|
|
FUNCTION_IN_RANGE,
|
|
|
|
|
FUNCTION_NOT_IN_RANGE,
|
2014-03-20 12:25:26 +00:00
|
|
|
|
FUNCTION_IN_SET,
|
2014-03-26 10:56:21 +00:00
|
|
|
|
FUNCTION_NOT_IN_SET,
|
2012-12-05 12:44:55 +00:00
|
|
|
|
FUNCTION_UNKNOWN, /// Может принимать любое значение.
|
|
|
|
|
/// Операторы логического выражения.
|
|
|
|
|
FUNCTION_NOT,
|
|
|
|
|
FUNCTION_AND,
|
|
|
|
|
FUNCTION_OR,
|
2016-01-22 21:28:58 +00:00
|
|
|
|
/// Константы
|
|
|
|
|
ALWAYS_FALSE,
|
|
|
|
|
ALWAYS_TRUE,
|
2012-12-05 12:44:55 +00:00
|
|
|
|
};
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
RPNElement() {}
|
|
|
|
|
RPNElement(Function function_) : function(function_) {}
|
|
|
|
|
RPNElement(Function function_, size_t key_column_) : function(function_), key_column(key_column_) {}
|
2012-12-12 14:25:55 +00:00
|
|
|
|
RPNElement(Function function_, size_t key_column_, const Range & range_)
|
2014-04-22 22:52:00 +00:00
|
|
|
|
: function(function_), range(range_), key_column(key_column_) {}
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2015-03-27 03:37:46 +00:00
|
|
|
|
String toString() const;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2015-10-28 21:00:44 +00:00
|
|
|
|
Function function = FUNCTION_UNKNOWN;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// Для FUNCTION_IN_RANGE и FUNCTION_NOT_IN_RANGE.
|
|
|
|
|
Range range;
|
|
|
|
|
size_t key_column;
|
2015-11-29 08:06:29 +00:00
|
|
|
|
/// Для FUNCTION_IN_SET, FUNCTION_NOT_IN_SET
|
2014-03-31 14:49:43 +00:00
|
|
|
|
ASTPtr in_function;
|
2015-11-29 08:06:29 +00:00
|
|
|
|
|
|
|
|
|
/** Цепочка возможно-монотонных функций.
|
|
|
|
|
* Если столбец первичного ключа завёрнут в функции, которые могут быть монотонными в некоторых диапазонах значений
|
|
|
|
|
* (например: -toFloat64(toDayOfWeek(date))), то здесь будут расположены функции: toDayOfWeek, toFloat64, negate.
|
|
|
|
|
*/
|
|
|
|
|
using MonotonicFunctionsChain = std::vector<FunctionPtr>;
|
|
|
|
|
mutable MonotonicFunctionsChain monotonic_functions_chain; /// Выполнение функции не нарушает константность.
|
2012-12-05 12:44:55 +00:00
|
|
|
|
};
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
|
static Block getBlockWithConstants(
|
|
|
|
|
const ASTPtr & query, const Context & context, const NamesAndTypesList & all_columns);
|
|
|
|
|
|
|
|
|
|
using AtomMap = std::unordered_map<std::string, bool(*)(RPNElement & out, const Field & value, ASTPtr & node)>;
|
|
|
|
|
static const AtomMap atom_map;
|
|
|
|
|
|
|
|
|
|
private:
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using RPN = std::vector<RPNElement>;
|
|
|
|
|
using ColumnIndices = std::map<String, size_t>;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2016-03-27 11:37:25 +00:00
|
|
|
|
bool mayBeTrueInRange(
|
|
|
|
|
size_t used_key_size,
|
|
|
|
|
const Field * left_pk,
|
|
|
|
|
const Field * right_pk,
|
|
|
|
|
const DataTypes & data_types,
|
|
|
|
|
bool right_bounded) const;
|
|
|
|
|
|
|
|
|
|
bool mayBeTrueInRangeImpl(const std::vector<Range> & key_ranges, const DataTypes & data_types) const;
|
2015-11-29 08:06:29 +00:00
|
|
|
|
|
|
|
|
|
void traverseAST(ASTPtr & node, const Context & context, Block & block_with_constants);
|
|
|
|
|
bool atomFromAST(ASTPtr & node, const Context & context, Block & block_with_constants, RPNElement & out);
|
|
|
|
|
bool operatorFromAST(const ASTFunction * func, RPNElement & out);
|
|
|
|
|
|
2016-11-23 19:58:33 +00:00
|
|
|
|
/** Is node the primary key column
|
|
|
|
|
* or expression in which column of primary key is wrapped by chain of functions,
|
|
|
|
|
* that can be monotomic on certain ranges?
|
|
|
|
|
* If these conditions are true, then returns number of column in primary key, type of resulting expression
|
|
|
|
|
* and fills chain of possibly-monotonic functions.
|
2015-11-29 08:06:29 +00:00
|
|
|
|
*/
|
|
|
|
|
bool isPrimaryKeyPossiblyWrappedByMonotonicFunctions(
|
|
|
|
|
const ASTPtr & node,
|
|
|
|
|
const Context & context,
|
|
|
|
|
size_t & out_primary_key_column_num,
|
2016-11-23 19:58:33 +00:00
|
|
|
|
DataTypePtr & out_primary_key_res_column_type,
|
2015-11-29 08:06:29 +00:00
|
|
|
|
RPNElement::MonotonicFunctionsChain & out_functions_chain);
|
|
|
|
|
|
|
|
|
|
bool isPrimaryKeyPossiblyWrappedByMonotonicFunctionsImpl(
|
|
|
|
|
const ASTPtr & node,
|
|
|
|
|
size_t & out_primary_key_column_num,
|
2016-11-23 19:58:33 +00:00
|
|
|
|
DataTypePtr & out_primary_key_column_type,
|
2015-11-29 08:06:29 +00:00
|
|
|
|
std::vector<const ASTFunction *> & out_functions_chain);
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
RPN rpn;
|
2015-03-27 03:06:06 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
SortDescription sort_descr;
|
|
|
|
|
ColumnIndices pk_columns;
|
2016-11-21 12:58:42 +00:00
|
|
|
|
const Block & pk_sample_block;
|
2012-12-05 12:44:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|