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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
|
|
|
|
|
|
/** Более точное сравнение.
|
|
|
|
|
* Отличается от Field::operator< и Field::operator== тем, что сравнивает значения разных числовых типов между собой.
|
|
|
|
|
* Правила сравнения - такие же, что и в FunctionsComparison.
|
|
|
|
|
* В том числе, сравнение знаковых и беззнаковых оставляем UB.
|
|
|
|
|
*/
|
|
|
|
|
class FieldVisitorAccurateEquals : public StaticVisitor<bool>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
bool operator() (const Null & l, const Null & r) const { return true; }
|
|
|
|
|
bool operator() (const Null & l, const UInt64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Null & l, const Int64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Null & l, const Float64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Null & l, const String & r) const { return false; }
|
|
|
|
|
bool operator() (const Null & l, const Array & r) const { return false; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const UInt64 & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const UInt64 & l, const UInt64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const UInt64 & l, const Int64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const UInt64 & l, const Float64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const UInt64 & l, const String & r) const { return false; }
|
|
|
|
|
bool operator() (const UInt64 & l, const Array & r) const { return false; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const Int64 & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const Int64 & l, const UInt64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const Int64 & l, const Int64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const Int64 & l, const Float64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const Int64 & l, const String & r) const { return false; }
|
|
|
|
|
bool operator() (const Int64 & l, const Array & r) const { return false; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const Float64 & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const Float64 & l, const UInt64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const Float64 & l, const Int64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const Float64 & l, const Float64 & r) const { return l == r; }
|
|
|
|
|
bool operator() (const Float64 & l, const String & r) const { return false; }
|
|
|
|
|
bool operator() (const Float64 & l, const Array & r) const { return false; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const String & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const UInt64 & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const Int64 & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const Float64 & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const String & r) const { return l == r; }
|
|
|
|
|
bool operator() (const String & l, const Array & r) const { return false; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const Array & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const UInt64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const Int64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const Float64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const String & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const Array & r) const { return l == r; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FieldVisitorAccurateLess : public StaticVisitor<bool>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
bool operator() (const Null & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const Null & l, const UInt64 & r) const { return true; }
|
|
|
|
|
bool operator() (const Null & l, const Int64 & r) const { return true; }
|
|
|
|
|
bool operator() (const Null & l, const Float64 & r) const { return true; }
|
|
|
|
|
bool operator() (const Null & l, const String & r) const { return true; }
|
|
|
|
|
bool operator() (const Null & l, const Array & r) const { return true; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const UInt64 & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const UInt64 & l, const UInt64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const UInt64 & l, const Int64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const UInt64 & l, const Float64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const UInt64 & l, const String & r) const { return true; }
|
|
|
|
|
bool operator() (const UInt64 & l, const Array & r) const { return true; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const Int64 & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const Int64 & l, const UInt64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const Int64 & l, const Int64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const Int64 & l, const Float64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const Int64 & l, const String & r) const { return true; }
|
|
|
|
|
bool operator() (const Int64 & l, const Array & r) const { return true; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const Float64 & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const Float64 & l, const UInt64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const Float64 & l, const Int64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const Float64 & l, const Float64 & r) const { return l < r; }
|
|
|
|
|
bool operator() (const Float64 & l, const String & r) const { return true; }
|
|
|
|
|
bool operator() (const Float64 & l, const Array & r) const { return true; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const String & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const UInt64 & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const Int64 & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const Float64 & r) const { return false; }
|
|
|
|
|
bool operator() (const String & l, const String & r) const { return l < r; }
|
|
|
|
|
bool operator() (const String & l, const Array & r) const { return true; }
|
|
|
|
|
|
|
|
|
|
bool operator() (const Array & l, const Null & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const UInt64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const Int64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const Float64 & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const String & r) const { return false; }
|
|
|
|
|
bool operator() (const Array & l, const Array & r) const { return l < r; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
static bool equals(const Field & lhs, const Field & rhs) { return apply_visitor(FieldVisitorAccurateEquals(), lhs, rhs); }
|
|
|
|
|
static bool less(const Field & lhs, const Field & rhs) { return apply_visitor(FieldVisitorAccurateLess(), lhs, rhs); }
|
|
|
|
|
|
|
|
|
|
public:
|
2012-12-05 12:44:55 +00:00
|
|
|
|
Field left; /// левая граница, если есть
|
|
|
|
|
Field right; /// правая граница, если есть
|
|
|
|
|
bool left_bounded; /// ограничен ли слева
|
|
|
|
|
bool right_bounded; /// ограничен ли справа
|
|
|
|
|
bool left_included; /// включает левую границу, если есть
|
|
|
|
|
bool right_included; /// включает правую границу, если есть
|
|
|
|
|
|
|
|
|
|
/// Всё множество.
|
|
|
|
|
Range() : left(), right(), left_bounded(false), right_bounded(false), left_included(false), right_included(false) {}
|
|
|
|
|
|
|
|
|
|
/// Одна точка.
|
|
|
|
|
Range(const Field & point) : left(point), right(point), left_bounded(true), right_bounded(true), left_included(true), right_included(true) {}
|
|
|
|
|
|
|
|
|
|
/// Ограниченный с двух сторон диапазон.
|
|
|
|
|
Range(const Field & left_, bool left_included_, const Field & right_, bool right_included_)
|
|
|
|
|
: left(left_), right(right_), left_bounded(true), right_bounded(true), left_included(left_included_), right_included(right_included_) {}
|
|
|
|
|
|
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;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Установить левую границу.
|
|
|
|
|
void setLeft(const Field & point, bool included)
|
|
|
|
|
{
|
|
|
|
|
left = point;
|
|
|
|
|
left_bounded = true;
|
|
|
|
|
left_included = included;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Установить правую границу.
|
|
|
|
|
void setRight(const Field & point, bool included)
|
|
|
|
|
{
|
|
|
|
|
right = point;
|
|
|
|
|
right_bounded = true;
|
|
|
|
|
right_included = included;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 20:44:06 +00:00
|
|
|
|
String toString() const
|
2012-12-05 12:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
std::stringstream str;
|
|
|
|
|
|
|
|
|
|
if (!left_bounded)
|
|
|
|
|
str << "(-inf, ";
|
|
|
|
|
else
|
2013-01-05 20:03:19 +00:00
|
|
|
|
str << (left_included ? '[' : '(') << apply_visitor(FieldVisitorToString(), left) << ", ";
|
2012-12-05 12:44:55 +00:00
|
|
|
|
|
|
|
|
|
if (!right_bounded)
|
|
|
|
|
str << "+inf)";
|
|
|
|
|
else
|
2013-01-05 20:03:19 +00:00
|
|
|
|
str << apply_visitor(FieldVisitorToString(), right) << (right_included ? ']' : ')');
|
2012-12-05 12:44:55 +00:00
|
|
|
|
|
|
|
|
|
return str.str();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
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 - набор всех столбцов таблицы.
|
|
|
|
|
PKCondition(ASTPtr query, const Context & context, const NamesAndTypesList & all_columns, const SortDescription & sort_descr);
|
2012-12-05 12:44:55 +00:00
|
|
|
|
|
2012-12-06 09:45:09 +00:00
|
|
|
|
/// Выполнимо ли условие в диапазоне ключей.
|
2012-12-05 12:44:55 +00:00
|
|
|
|
/// left_pk и right_pk должны содержать все поля из sort_descr в соответствующем порядке.
|
2013-12-09 00:29:24 +00:00
|
|
|
|
bool mayBeTrueInRange(const Field * left_pk, const Field * right_pk);
|
2012-12-05 12:44:55 +00:00
|
|
|
|
|
2012-12-06 09:45:09 +00:00
|
|
|
|
/// Выполнимо ли условие в полубесконечном (не ограниченном справа) диапазоне ключей.
|
|
|
|
|
/// left_pk должен содержать все поля из sort_descr в соответствующем порядке.
|
2013-12-09 00:29:24 +00:00
|
|
|
|
bool mayBeTrueAfter(const Field * left_pk);
|
2012-12-06 09:45:09 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
bool alwaysTrue()
|
|
|
|
|
{
|
|
|
|
|
return rpn.size() == 1 && rpn[0].function == RPNElement::FUNCTION_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-12 14:25:55 +00:00
|
|
|
|
/// Наложить дополнительное условие: значение в столбце column должно быть в диапазоне range.
|
|
|
|
|
/// Возвращает, есть ли такой столбец в первичном ключе.
|
|
|
|
|
bool addCondition(const String & column, const Range & range);
|
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
String toString();
|
|
|
|
|
private:
|
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,
|
|
|
|
|
FUNCTION_UNKNOWN, /// Может принимать любое значение.
|
|
|
|
|
/// Операторы логического выражения.
|
|
|
|
|
FUNCTION_NOT,
|
|
|
|
|
FUNCTION_AND,
|
|
|
|
|
FUNCTION_OR,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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_)
|
2012-12-12 14:38:39 +00:00
|
|
|
|
: function(function_), range(range_), key_column(key_column_){}
|
2012-12-05 12:44:55 +00:00
|
|
|
|
|
|
|
|
|
String toString()
|
|
|
|
|
{
|
|
|
|
|
switch (function)
|
|
|
|
|
{
|
|
|
|
|
case FUNCTION_AND:
|
|
|
|
|
return "and";
|
|
|
|
|
case FUNCTION_OR:
|
|
|
|
|
return "or";
|
|
|
|
|
case FUNCTION_NOT:
|
|
|
|
|
return "not";
|
|
|
|
|
case FUNCTION_UNKNOWN:
|
|
|
|
|
return "unknown";
|
|
|
|
|
case FUNCTION_IN_RANGE:
|
|
|
|
|
case FUNCTION_NOT_IN_RANGE:
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
ss << "(column " << key_column << (function == FUNCTION_NOT_IN_RANGE ? " not" : "") << " in " << range.toString() << ")";
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return "ERROR";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function function;
|
|
|
|
|
|
|
|
|
|
/// Для FUNCTION_IN_RANGE и FUNCTION_NOT_IN_RANGE.
|
|
|
|
|
Range range;
|
|
|
|
|
size_t key_column;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef std::vector<RPNElement> RPN;
|
|
|
|
|
typedef std::map<String, size_t> ColumnIndices;
|
|
|
|
|
|
2013-12-09 00:29:24 +00:00
|
|
|
|
bool mayBeTrueInRange(const Field * left_pk, const Field * right_pk, bool right_bounded);
|
2012-12-06 09:45:09 +00:00
|
|
|
|
|
2012-12-05 12:44:55 +00:00
|
|
|
|
void traverseAST(ASTPtr & node, Block & block_with_constants);
|
|
|
|
|
bool atomFromAST(ASTPtr & node, Block & block_with_constants, RPNElement & out);
|
|
|
|
|
bool operatorFromAST(ASTFunction * func, RPNElement & out);
|
|
|
|
|
|
|
|
|
|
RPN rpn;
|
|
|
|
|
|
|
|
|
|
SortDescription sort_descr;
|
|
|
|
|
ColumnIndices pk_columns;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|