2015-04-29 20:31:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/PODArray.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2015-04-29 20:31:28 +00:00
|
|
|
#include <bitset>
|
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int TOO_SLOW;
|
|
|
|
extern const int SYNTAX_ERROR;
|
2017-04-08 01:32:05 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2017-07-27 23:23:13 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-12 02:21:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 20:31:28 +00:00
|
|
|
/// helper type for comparing `std::pair`s using solely the .first member
|
|
|
|
template <template <typename> class Comparator>
|
|
|
|
struct ComparePairFirst final
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool operator()(const std::pair<T1, T2> & lhs, const std::pair<T1, T2> & rhs) const
|
|
|
|
{
|
|
|
|
return Comparator<T1>{}(lhs.first, rhs.first);
|
|
|
|
}
|
2015-04-29 20:31:28 +00:00
|
|
|
};
|
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
static constexpr auto max_events = 32;
|
|
|
|
|
|
|
|
template <typename T>
|
2015-04-29 20:31:28 +00:00
|
|
|
struct AggregateFunctionSequenceMatchData final
|
|
|
|
{
|
2019-05-20 04:02:54 +00:00
|
|
|
using Timestamp = T;
|
2017-04-01 07:20:54 +00:00
|
|
|
using Events = std::bitset<max_events>;
|
|
|
|
using TimestampEvents = std::pair<Timestamp, Events>;
|
|
|
|
using Comparator = ComparePairFirst<std::less>;
|
|
|
|
|
|
|
|
bool sorted = true;
|
2019-06-28 12:51:01 +00:00
|
|
|
PODArrayWithStackMemory<TimestampEvents, 64> events_list;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void add(const Timestamp timestamp, const Events & events)
|
|
|
|
{
|
|
|
|
/// store information exclusively for rows with at least one event
|
|
|
|
if (events.any())
|
|
|
|
{
|
2017-04-20 16:41:03 +00:00
|
|
|
events_list.emplace_back(timestamp, events);
|
2017-04-01 07:20:54 +00:00
|
|
|
sorted = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 03:58:10 +00:00
|
|
|
void merge(const AggregateFunctionSequenceMatchData & other)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-05-22 03:58:10 +00:00
|
|
|
if (other.events_list.empty())
|
|
|
|
return;
|
|
|
|
|
2017-04-20 16:41:03 +00:00
|
|
|
const auto size = events_list.size();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-20 16:41:03 +00:00
|
|
|
events_list.insert(std::begin(other.events_list), std::end(other.events_list));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// either sort whole container or do so partially merging ranges afterwards
|
|
|
|
if (!sorted && !other.sorted)
|
2017-04-20 16:41:03 +00:00
|
|
|
std::sort(std::begin(events_list), std::end(events_list), Comparator{});
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
{
|
2017-04-20 16:41:03 +00:00
|
|
|
const auto begin = std::begin(events_list);
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto middle = std::next(begin, size);
|
2017-04-20 16:41:03 +00:00
|
|
|
const auto end = std::end(events_list);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!sorted)
|
|
|
|
std::sort(begin, middle, Comparator{});
|
|
|
|
|
|
|
|
if (!other.sorted)
|
|
|
|
std::sort(middle, end, Comparator{});
|
|
|
|
|
|
|
|
std::inplace_merge(begin, middle, end, Comparator{});
|
|
|
|
}
|
|
|
|
|
|
|
|
sorted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sort()
|
|
|
|
{
|
|
|
|
if (!sorted)
|
|
|
|
{
|
2017-04-20 16:41:03 +00:00
|
|
|
std::sort(std::begin(events_list), std::end(events_list), Comparator{});
|
2017-04-01 07:20:54 +00:00
|
|
|
sorted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(WriteBuffer & buf) const
|
|
|
|
{
|
|
|
|
writeBinary(sorted, buf);
|
2017-04-20 16:41:03 +00:00
|
|
|
writeBinary(events_list.size(), buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-20 16:41:03 +00:00
|
|
|
for (const auto & events : events_list)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
writeBinary(events.first, buf);
|
|
|
|
writeBinary(events.second.to_ulong(), buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
readBinary(sorted, buf);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t size;
|
2017-04-01 07:20:54 +00:00
|
|
|
readBinary(size, buf);
|
|
|
|
|
2017-04-20 16:41:03 +00:00
|
|
|
events_list.clear();
|
|
|
|
events_list.reserve(size);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-05-20 04:02:54 +00:00
|
|
|
Timestamp timestamp;
|
2017-04-01 07:20:54 +00:00
|
|
|
readBinary(timestamp, buf);
|
|
|
|
|
|
|
|
UInt64 events;
|
|
|
|
readBinary(events, buf);
|
|
|
|
|
2017-04-20 16:41:03 +00:00
|
|
|
events_list.emplace_back(timestamp, Events{events});
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-29 20:31:28 +00:00
|
|
|
};
|
|
|
|
|
2015-08-18 16:11:21 +00:00
|
|
|
|
|
|
|
/// Max number of iterations to match the pattern against a sequence, exception thrown when exceeded
|
|
|
|
constexpr auto sequence_match_max_iterations = 1000000;
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
template <typename T, typename Data, typename Derived>
|
|
|
|
class AggregateFunctionSequenceBase : public IAggregateFunctionDataHelper<Data, Derived>
|
2015-04-29 20:31:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
AggregateFunctionSequenceBase(const DataTypes & arguments, const Array & params, const String & pattern_)
|
2019-05-20 04:02:54 +00:00
|
|
|
: IAggregateFunctionDataHelper<Data, Derived>(arguments, params)
|
2019-08-03 11:02:40 +00:00
|
|
|
, pattern(pattern_)
|
2017-12-20 20:25:22 +00:00
|
|
|
{
|
|
|
|
arg_count = arguments.size();
|
|
|
|
parsePattern();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, const size_t row_num, Arena *) const override
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const auto timestamp = assert_cast<const ColumnVector<T> *>(columns[0])->getData()[row_num];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
typename Data::Events events;
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto i : ext::range(1, arg_count))
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const auto event = assert_cast<const ColumnUInt8 *>(columns[i])->getData()[row_num];
|
2017-04-01 07:20:54 +00:00
|
|
|
events.set(i - 1, event);
|
|
|
|
}
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).add(timestamp, events);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:51:50 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
this->data(place).merge(this->data(rhs));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).serialize(buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).deserialize(buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 20:31:28 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
enum class PatternActionType
|
|
|
|
{
|
|
|
|
SpecificEvent,
|
|
|
|
AnyEvent,
|
|
|
|
KleeneStar,
|
|
|
|
TimeLessOrEqual,
|
|
|
|
TimeLess,
|
|
|
|
TimeGreaterOrEqual,
|
|
|
|
TimeGreater
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PatternAction final
|
|
|
|
{
|
|
|
|
PatternActionType type;
|
2019-05-20 04:31:52 +00:00
|
|
|
std::uint64_t extra;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
PatternAction() = default;
|
2019-08-03 11:02:40 +00:00
|
|
|
PatternAction(const PatternActionType type_, const std::uint64_t extra_ = 0) : type{type_}, extra{extra_} {}
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2019-06-28 12:51:01 +00:00
|
|
|
using PatternActions = PODArrayWithStackMemory<PatternAction, 64>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
Derived & derived() { return static_cast<Derived &>(*this); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void parsePattern()
|
|
|
|
{
|
|
|
|
actions.clear();
|
|
|
|
actions.emplace_back(PatternActionType::KleeneStar);
|
|
|
|
|
2019-01-15 16:39:09 +00:00
|
|
|
dfa_states.clear();
|
|
|
|
dfa_states.emplace_back(true);
|
|
|
|
|
|
|
|
pattern_has_time = false;
|
|
|
|
|
2017-05-04 11:56:13 +00:00
|
|
|
const char * pos = pattern.data();
|
2017-07-12 02:40:28 +00:00
|
|
|
const char * begin = pos;
|
|
|
|
const char * end = pos + pattern.size();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-12 02:40:28 +00:00
|
|
|
auto throw_exception = [&](const std::string & msg)
|
2017-04-20 18:35:39 +00:00
|
|
|
{
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{msg + " '" + std::string(pos, end) + "' at position " + toString(pos - begin), ErrorCodes::SYNTAX_ERROR};
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2017-07-13 02:48:34 +00:00
|
|
|
auto match = [&pos, end](const char * str) mutable
|
2017-07-12 02:40:28 +00:00
|
|
|
{
|
|
|
|
size_t length = strlen(str);
|
2017-07-13 01:54:51 +00:00
|
|
|
if (pos + length <= end && 0 == memcmp(pos, str, length))
|
2017-07-12 02:40:28 +00:00
|
|
|
{
|
|
|
|
pos += length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos < end)
|
|
|
|
{
|
2017-07-12 02:40:28 +00:00
|
|
|
if (match("(?"))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-12 02:40:28 +00:00
|
|
|
if (match("t"))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
PatternActionType type;
|
|
|
|
|
2017-07-12 02:40:28 +00:00
|
|
|
if (match("<="))
|
2017-04-01 07:20:54 +00:00
|
|
|
type = PatternActionType::TimeLessOrEqual;
|
2017-07-12 02:40:28 +00:00
|
|
|
else if (match("<"))
|
2017-04-01 07:20:54 +00:00
|
|
|
type = PatternActionType::TimeLess;
|
2017-07-12 02:40:28 +00:00
|
|
|
else if (match(">="))
|
2017-04-01 07:20:54 +00:00
|
|
|
type = PatternActionType::TimeGreaterOrEqual;
|
2017-07-12 02:40:28 +00:00
|
|
|
else if (match(">"))
|
2017-04-01 07:20:54 +00:00
|
|
|
type = PatternActionType::TimeGreater;
|
|
|
|
else
|
|
|
|
throw_exception("Unknown time condition");
|
|
|
|
|
2017-07-12 02:40:28 +00:00
|
|
|
UInt64 duration = 0;
|
|
|
|
auto prev_pos = pos;
|
|
|
|
pos = tryReadIntText(duration, pos, end);
|
|
|
|
if (pos == prev_pos)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw_exception("Could not parse number");
|
|
|
|
|
|
|
|
if (actions.back().type != PatternActionType::SpecificEvent &&
|
|
|
|
actions.back().type != PatternActionType::AnyEvent &&
|
|
|
|
actions.back().type != PatternActionType::KleeneStar)
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Temporal condition should be preceeded by an event condition", ErrorCodes::BAD_ARGUMENTS};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-15 16:39:09 +00:00
|
|
|
pattern_has_time = true;
|
2017-07-12 02:40:28 +00:00
|
|
|
actions.emplace_back(type, duration);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-12 02:40:28 +00:00
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-12 02:40:28 +00:00
|
|
|
UInt64 event_number = 0;
|
|
|
|
auto prev_pos = pos;
|
|
|
|
pos = tryReadIntText(event_number, pos, end);
|
|
|
|
if (pos == prev_pos)
|
|
|
|
throw_exception("Could not parse number");
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (event_number > arg_count - 1)
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Event number " + toString(event_number) + " is out of range", ErrorCodes::BAD_ARGUMENTS};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
actions.emplace_back(PatternActionType::SpecificEvent, event_number - 1);
|
2019-01-15 16:39:09 +00:00
|
|
|
dfa_states.back().transition = DFATransition::SpecificEvent;
|
|
|
|
dfa_states.back().event = event_number - 1;
|
|
|
|
dfa_states.emplace_back();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 02:40:28 +00:00
|
|
|
if (!match(")"))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw_exception("Expected closing parenthesis, found");
|
|
|
|
|
|
|
|
}
|
2019-01-16 16:20:29 +00:00
|
|
|
else if (match(".*"))
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
actions.emplace_back(PatternActionType::KleeneStar);
|
2019-01-15 16:39:09 +00:00
|
|
|
dfa_states.back().has_kleene = true;
|
|
|
|
}
|
2019-01-16 16:20:29 +00:00
|
|
|
else if (match("."))
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
actions.emplace_back(PatternActionType::AnyEvent);
|
2019-01-15 16:39:09 +00:00
|
|
|
dfa_states.back().transition = DFATransition::AnyEvent;
|
|
|
|
dfa_states.emplace_back();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
throw_exception("Could not parse pattern, unexpected starting symbol");
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 20:31:28 +00:00
|
|
|
|
2015-08-21 15:57:26 +00:00
|
|
|
protected:
|
2019-01-16 09:11:48 +00:00
|
|
|
/// Uses a DFA based approach in order to better handle patterns without
|
|
|
|
/// time assertions.
|
|
|
|
///
|
2020-01-18 19:29:53 +00:00
|
|
|
/// NOTE: This implementation relies on the assumption that the pattern is *small*.
|
2019-01-16 09:11:48 +00:00
|
|
|
///
|
|
|
|
/// This algorithm performs in O(mn) (with m the number of DFA states and N the number
|
|
|
|
/// of events) with a memory consumption and memory allocations in O(m). It means that
|
|
|
|
/// if n >>> m (which is expected to be the case), this algorithm can be considered linear.
|
2019-05-20 04:02:54 +00:00
|
|
|
template <typename EventEntry>
|
|
|
|
bool dfaMatch(EventEntry & events_it, const EventEntry events_end) const
|
2019-01-15 16:39:09 +00:00
|
|
|
{
|
|
|
|
using ActiveStates = std::vector<bool>;
|
|
|
|
|
2019-01-16 09:11:48 +00:00
|
|
|
/// Those two vectors keep track of which states should be considered for the current
|
|
|
|
/// event as well as the states which should be considered for the next event.
|
2019-01-15 16:39:09 +00:00
|
|
|
ActiveStates active_states(dfa_states.size(), false);
|
|
|
|
ActiveStates next_active_states(dfa_states.size(), false);
|
|
|
|
active_states[0] = true;
|
|
|
|
|
2019-01-16 09:11:48 +00:00
|
|
|
/// Keeps track of dead-ends in order not to iterate over all the events to realize that
|
|
|
|
/// the match failed.
|
2019-01-15 16:39:09 +00:00
|
|
|
size_t n_active = 1;
|
|
|
|
|
2019-01-16 16:20:29 +00:00
|
|
|
for (/* empty */; events_it != events_end && n_active > 0 && !active_states.back(); ++events_it)
|
|
|
|
{
|
2019-01-15 16:39:09 +00:00
|
|
|
n_active = 0;
|
|
|
|
next_active_states.assign(dfa_states.size(), false);
|
|
|
|
|
2019-01-16 16:20:29 +00:00
|
|
|
for (size_t state = 0; state < dfa_states.size(); ++state)
|
|
|
|
{
|
|
|
|
if (!active_states[state])
|
|
|
|
{
|
2019-01-15 16:39:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-16 16:20:29 +00:00
|
|
|
switch (dfa_states[state].transition)
|
|
|
|
{
|
2019-01-16 17:25:06 +00:00
|
|
|
case DFATransition::None:
|
|
|
|
break;
|
|
|
|
case DFATransition::AnyEvent:
|
2019-01-15 16:39:09 +00:00
|
|
|
next_active_states[state + 1] = true;
|
|
|
|
++n_active;
|
2019-01-16 17:25:06 +00:00
|
|
|
break;
|
|
|
|
case DFATransition::SpecificEvent:
|
|
|
|
if (events_it->second.test(dfa_states[state].event))
|
|
|
|
{
|
|
|
|
next_active_states[state + 1] = true;
|
|
|
|
++n_active;
|
|
|
|
}
|
|
|
|
break;
|
2019-01-15 16:39:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 16:20:29 +00:00
|
|
|
if (dfa_states[state].has_kleene)
|
|
|
|
{
|
2019-01-15 16:39:09 +00:00
|
|
|
next_active_states[state] = true;
|
|
|
|
++n_active;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
swap(active_states, next_active_states);
|
|
|
|
}
|
|
|
|
|
|
|
|
return active_states.back();
|
|
|
|
}
|
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
template <typename EventEntry>
|
|
|
|
bool backtrackingMatch(EventEntry & events_it, const EventEntry events_end) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const auto action_begin = std::begin(actions);
|
|
|
|
const auto action_end = std::end(actions);
|
|
|
|
auto action_it = action_begin;
|
|
|
|
|
|
|
|
const auto events_begin = events_it;
|
|
|
|
auto base_it = events_it;
|
|
|
|
|
|
|
|
/// an iterator to action plus an iterator to row in events list plus timestamp at the start of sequence
|
2019-05-20 04:02:54 +00:00
|
|
|
using backtrack_info = std::tuple<decltype(action_it), EventEntry, EventEntry>;
|
2017-04-01 07:20:54 +00:00
|
|
|
std::stack<backtrack_info> back_stack;
|
|
|
|
|
|
|
|
/// backtrack if possible
|
2018-01-06 18:10:44 +00:00
|
|
|
const auto do_backtrack = [&]
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
while (!back_stack.empty())
|
|
|
|
{
|
|
|
|
auto & top = back_stack.top();
|
|
|
|
|
|
|
|
action_it = std::get<0>(top);
|
|
|
|
events_it = std::next(std::get<1>(top));
|
|
|
|
base_it = std::get<2>(top);
|
|
|
|
|
|
|
|
back_stack.pop();
|
|
|
|
|
|
|
|
if (events_it != events_end)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t i = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
while (action_it != action_end && events_it != events_end)
|
|
|
|
{
|
|
|
|
if (action_it->type == PatternActionType::SpecificEvent)
|
|
|
|
{
|
|
|
|
if (events_it->second.test(action_it->extra))
|
|
|
|
{
|
|
|
|
/// move to the next action and events
|
|
|
|
base_it = events_it;
|
|
|
|
++action_it, ++events_it;
|
|
|
|
}
|
|
|
|
else if (!do_backtrack())
|
|
|
|
/// backtracking failed, bail out
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (action_it->type == PatternActionType::AnyEvent)
|
|
|
|
{
|
|
|
|
base_it = events_it;
|
|
|
|
++action_it, ++events_it;
|
|
|
|
}
|
|
|
|
else if (action_it->type == PatternActionType::KleeneStar)
|
|
|
|
{
|
|
|
|
back_stack.emplace(action_it, events_it, base_it);
|
|
|
|
base_it = events_it;
|
|
|
|
++action_it;
|
|
|
|
}
|
|
|
|
else if (action_it->type == PatternActionType::TimeLessOrEqual)
|
|
|
|
{
|
2019-05-20 04:31:52 +00:00
|
|
|
if (events_it->first <= base_it->first + action_it->extra)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
/// condition satisfied, move onto next action
|
|
|
|
back_stack.emplace(action_it, events_it, base_it);
|
|
|
|
base_it = events_it;
|
|
|
|
++action_it;
|
|
|
|
}
|
|
|
|
else if (!do_backtrack())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (action_it->type == PatternActionType::TimeLess)
|
|
|
|
{
|
2019-05-20 04:31:52 +00:00
|
|
|
if (events_it->first < base_it->first + action_it->extra)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
back_stack.emplace(action_it, events_it, base_it);
|
|
|
|
base_it = events_it;
|
|
|
|
++action_it;
|
|
|
|
}
|
|
|
|
else if (!do_backtrack())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (action_it->type == PatternActionType::TimeGreaterOrEqual)
|
|
|
|
{
|
2019-05-20 04:31:52 +00:00
|
|
|
if (events_it->first >= base_it->first + action_it->extra)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
back_stack.emplace(action_it, events_it, base_it);
|
|
|
|
base_it = events_it;
|
|
|
|
++action_it;
|
|
|
|
}
|
|
|
|
else if (++events_it == events_end && !do_backtrack())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (action_it->type == PatternActionType::TimeGreater)
|
|
|
|
{
|
2019-05-20 04:31:52 +00:00
|
|
|
if (events_it->first > base_it->first + action_it->extra)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
back_stack.emplace(action_it, events_it, base_it);
|
|
|
|
base_it = events_it;
|
|
|
|
++action_it;
|
|
|
|
}
|
|
|
|
else if (++events_it == events_end && !do_backtrack())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Unknown PatternActionType", ErrorCodes::LOGICAL_ERROR};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (++i > sequence_match_max_iterations)
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Pattern application proves too difficult, exceeding max iterations (" + toString(sequence_match_max_iterations) + ")",
|
2017-07-27 23:23:13 +00:00
|
|
|
ErrorCodes::TOO_SLOW};
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// if there are some actions remaining
|
|
|
|
if (action_it != action_end)
|
|
|
|
{
|
|
|
|
/// match multiple empty strings at end
|
|
|
|
while (action_it->type == PatternActionType::KleeneStar ||
|
|
|
|
action_it->type == PatternActionType::TimeLessOrEqual ||
|
|
|
|
action_it->type == PatternActionType::TimeLess ||
|
|
|
|
(action_it->type == PatternActionType::TimeGreaterOrEqual && action_it->extra == 0))
|
|
|
|
++action_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events_it == events_begin)
|
|
|
|
++events_it;
|
|
|
|
|
|
|
|
return action_it == action_end;
|
|
|
|
}
|
2015-04-29 20:31:28 +00:00
|
|
|
|
2019-01-15 16:39:09 +00:00
|
|
|
private:
|
|
|
|
enum class DFATransition : char
|
|
|
|
{
|
2019-01-16 09:11:48 +00:00
|
|
|
/// .-------.
|
|
|
|
/// | |
|
|
|
|
/// `-------'
|
2019-01-15 16:39:09 +00:00
|
|
|
None,
|
2019-01-16 09:11:48 +00:00
|
|
|
/// .-------. (?[0-9])
|
|
|
|
/// | | ----------
|
|
|
|
/// `-------'
|
2019-01-15 16:39:09 +00:00
|
|
|
SpecificEvent,
|
2019-01-16 09:11:48 +00:00
|
|
|
/// .-------. .
|
|
|
|
/// | | ----------
|
|
|
|
/// `-------'
|
2019-01-15 16:39:09 +00:00
|
|
|
AnyEvent,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DFAState
|
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
DFAState(bool has_kleene_ = false)
|
|
|
|
: has_kleene{has_kleene_}, event{0}, transition{DFATransition::None}
|
2019-01-15 16:39:09 +00:00
|
|
|
{}
|
|
|
|
|
2019-01-16 09:11:48 +00:00
|
|
|
/// .-------.
|
|
|
|
/// | | - - -
|
|
|
|
/// `-------'
|
|
|
|
/// |_^
|
2019-01-15 16:39:09 +00:00
|
|
|
bool has_kleene;
|
2019-01-16 09:11:48 +00:00
|
|
|
/// In the case of a state transitions with a `SpecificEvent`,
|
|
|
|
/// `event` contains the value of the event.
|
2019-01-15 16:39:09 +00:00
|
|
|
uint32_t event;
|
2019-01-16 09:11:48 +00:00
|
|
|
/// The kind of transition out of this state.
|
2019-01-15 16:39:09 +00:00
|
|
|
DFATransition transition;
|
|
|
|
};
|
|
|
|
|
|
|
|
using DFAStates = std::vector<DFAState>;
|
|
|
|
|
|
|
|
protected:
|
2019-01-16 09:11:48 +00:00
|
|
|
/// `True` if the parsed pattern contains time assertions (?t...), `false` otherwise.
|
2019-01-15 16:39:09 +00:00
|
|
|
bool pattern_has_time;
|
|
|
|
|
2015-08-21 15:57:26 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string pattern;
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t arg_count;
|
2017-04-01 07:20:54 +00:00
|
|
|
PatternActions actions;
|
2019-01-15 16:39:09 +00:00
|
|
|
|
|
|
|
DFAStates dfa_states;
|
2015-04-29 20:31:28 +00:00
|
|
|
};
|
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
template <typename T, typename Data>
|
|
|
|
class AggregateFunctionSequenceMatch final : public AggregateFunctionSequenceBase<T, Data, AggregateFunctionSequenceMatch<T, Data>>
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
AggregateFunctionSequenceMatch(const DataTypes & arguments, const Array & params, const String & pattern_)
|
|
|
|
: AggregateFunctionSequenceBase<T, Data, AggregateFunctionSequenceMatch<T, Data>>(arguments, params, pattern_) {}
|
2019-02-11 19:26:32 +00:00
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
using AggregateFunctionSequenceBase<T, Data, AggregateFunctionSequenceMatch<T, Data>>::AggregateFunctionSequenceBase;
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
String getName() const override { return "sequenceMatch"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeUInt8>(); }
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
2019-05-20 04:02:54 +00:00
|
|
|
const_cast<Data &>(this->data(place)).sort();
|
2017-12-20 07:36:30 +00:00
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
const auto & data_ref = this->data(place);
|
2017-12-20 07:36:30 +00:00
|
|
|
|
|
|
|
const auto events_begin = std::begin(data_ref.events_list);
|
|
|
|
const auto events_end = std::end(data_ref.events_list);
|
|
|
|
auto events_it = events_begin;
|
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
bool match = this->pattern_has_time ? this->backtrackingMatch(events_it, events_end) : this->dfaMatch(events_it, events_end);
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt8 &>(to).getData().push_back(match);
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
template <typename T, typename Data>
|
|
|
|
class AggregateFunctionSequenceCount final : public AggregateFunctionSequenceBase<T, Data, AggregateFunctionSequenceCount<T, Data>>
|
2015-08-21 15:57:26 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
AggregateFunctionSequenceCount(const DataTypes & arguments, const Array & params, const String & pattern_)
|
|
|
|
: AggregateFunctionSequenceBase<T, Data, AggregateFunctionSequenceCount<T, Data>>(arguments, params, pattern_) {}
|
2019-02-11 19:26:32 +00:00
|
|
|
|
2019-05-20 04:02:54 +00:00
|
|
|
using AggregateFunctionSequenceBase<T, Data, AggregateFunctionSequenceCount<T, Data>>::AggregateFunctionSequenceBase;
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "sequenceCount"; }
|
2015-08-21 15:57:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeUInt64>(); }
|
2015-08-21 15:57:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
2019-05-20 04:02:54 +00:00
|
|
|
const_cast<Data &>(this->data(place)).sort();
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt64 &>(to).getData().push_back(count(place));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-08-21 15:57:26 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 count(const ConstAggregateDataPtr & place) const
|
|
|
|
{
|
2019-05-20 04:02:54 +00:00
|
|
|
const auto & data_ref = this->data(place);
|
2015-08-21 15:57:26 +00:00
|
|
|
|
2017-04-20 16:41:03 +00:00
|
|
|
const auto events_begin = std::begin(data_ref.events_list);
|
|
|
|
const auto events_end = std::end(data_ref.events_list);
|
2017-04-01 07:20:54 +00:00
|
|
|
auto events_it = events_begin;
|
2015-08-21 15:57:26 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t count = 0;
|
2019-05-20 04:02:54 +00:00
|
|
|
while (events_it != events_end && this->backtrackingMatch(events_it, events_end))
|
2017-04-01 07:20:54 +00:00
|
|
|
++count;
|
2015-08-21 15:57:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return count;
|
|
|
|
}
|
2015-08-21 15:57:26 +00:00
|
|
|
};
|
|
|
|
|
2015-04-29 20:31:28 +00:00
|
|
|
}
|