Coverage Report

Created: 2026-10-14 18:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_sequence_match.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
// This file is copied from
19
// https://github.com/ClickHouse/ClickHouse/blob/master/AggregateFunctionSequenceMatch.h
20
// and modified by Doris
21
22
#pragma once
23
24
#include <string.h>
25
26
#include <algorithm>
27
#include <bitset>
28
#include <boost/iterator/iterator_facade.hpp>
29
#include <cctype>
30
#include <cstdint>
31
#include <functional>
32
#include <iterator>
33
#include <memory>
34
#include <stack>
35
#include <string>
36
#include <tuple>
37
#include <utility>
38
#include <vector>
39
40
#include "common/exception.h"
41
#include "common/logging.h"
42
#include "core/assert_cast.h"
43
#include "core/column/column_string.h"
44
#include "core/column/column_vector.h"
45
#include "core/data_type/data_type_number.h"
46
#include "core/pod_array_fwd.h"
47
#include "core/string_ref.h"
48
#include "core/types.h"
49
#include "exprs/aggregate/aggregate_function.h"
50
#include "util/string_parser.hpp"
51
52
namespace doris {
53
class Arena;
54
class BufferReadable;
55
class BufferWritable;
56
class IColumn;
57
} // namespace doris
58
59
namespace doris {
60
61
template <template <typename> class Comparator>
62
struct ComparePairFirst final {
63
    template <typename T1, typename T2>
64
62
    bool operator()(const std::pair<T1, T2>& lhs, const std::pair<T1, T2>& rhs) const {
65
62
        return Comparator<T1> {}(lhs.first, rhs.first);
66
62
    }
_ZNK5doris16ComparePairFirstISt4lessEclINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEEEbRKSt4pairIT_T0_ESE_
Line
Count
Source
64
62
    bool operator()(const std::pair<T1, T2>& lhs, const std::pair<T1, T2>& rhs) const {
65
62
        return Comparator<T1> {}(lhs.first, rhs.first);
66
62
    }
Unexecuted instantiation: _ZNK5doris16ComparePairFirstISt4lessEclINS_16TimeStampNsValueESt6bitsetILm32EEEEbRKSt4pairIT_T0_ESC_
Unexecuted instantiation: _ZNK5doris16ComparePairFirstISt4lessEclINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEEEbRKSt4pairIT_T0_ESE_
Unexecuted instantiation: _ZNK5doris16ComparePairFirstISt4lessEclINS_16TimestampTzValueESt6bitsetILm32EEEEbRKSt4pairIT_T0_ESC_
67
};
68
69
static constexpr size_t MAX_EVENTS = 32;
70
71
/// Max number of iterations to match the pattern against a sequence, exception thrown when exceeded
72
constexpr auto sequence_match_max_iterations = 1000000l;
73
74
template <PrimitiveType T, typename Derived>
75
struct AggregateFunctionSequenceMatchData final {
76
    using Timestamp = typename PrimitiveTypeTraits<T>::CppType;
77
    using Events = std::bitset<MAX_EVENTS>;
78
    using TimestampEvents = std::pair<Timestamp, Events>;
79
    using Comparator = ComparePairFirst<std::less>;
80
81
453
    AggregateFunctionSequenceMatchData() { reset(); }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEEC2Ev
Line
Count
Source
81
228
    AggregateFunctionSequenceMatchData() { reset(); }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEEC2Ev
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEEC2Ev
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEEC2Ev
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEEC2Ev
Line
Count
Source
81
225
    AggregateFunctionSequenceMatchData() { reset(); }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEEC2Ev
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEEC2Ev
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEEC2Ev
82
83
public:
84
61
    const std::string get_pattern() const { return pattern; }
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE11get_patternB5cxx11Ev
Line
Count
Source
84
31
    const std::string get_pattern() const { return pattern; }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE11get_patternB5cxx11Ev
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE11get_patternB5cxx11Ev
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE11get_patternB5cxx11Ev
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE11get_patternB5cxx11Ev
Line
Count
Source
84
30
    const std::string get_pattern() const { return pattern; }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE11get_patternB5cxx11Ev
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE11get_patternB5cxx11Ev
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE11get_patternB5cxx11Ev
85
86
146
    size_t get_arg_count() const { return arg_count; }
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE13get_arg_countEv
Line
Count
Source
86
74
    size_t get_arg_count() const { return arg_count; }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE13get_arg_countEv
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE13get_arg_countEv
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE13get_arg_countEv
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE13get_arg_countEv
Line
Count
Source
86
72
    size_t get_arg_count() const { return arg_count; }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE13get_arg_countEv
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE13get_arg_countEv
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE13get_arg_countEv
87
88
433
    void init(const std::string pattern_, size_t arg_count_) {
89
433
        if (!init_flag) {
90
385
            this->pattern = pattern_;
91
385
            this->arg_count = arg_count_;
92
385
            parse_pattern();
93
385
            init_flag = true;
94
385
        }
95
433
    }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Line
Count
Source
88
219
    void init(const std::string pattern_, size_t arg_count_) {
89
219
        if (!init_flag) {
90
194
            this->pattern = pattern_;
91
194
            this->arg_count = arg_count_;
92
194
            parse_pattern();
93
194
            init_flag = true;
94
194
        }
95
219
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Line
Count
Source
88
214
    void init(const std::string pattern_, size_t arg_count_) {
89
214
        if (!init_flag) {
90
191
            this->pattern = pattern_;
91
191
            this->arg_count = arg_count_;
92
191
            parse_pattern();
93
191
            init_flag = true;
94
191
        }
95
214
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE4initENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
96
97
499
    void reset() {
98
499
        sorted = true;
99
499
        init_flag = false;
100
499
        pattern_has_time = false;
101
499
        pattern = "";
102
499
        arg_count = 0;
103
499
        conditions_met.reset();
104
499
        conditions_in_pattern.reset();
105
106
499
        events_list.clear();
107
499
        actions.clear();
108
499
        dfa_states.clear();
109
499
    }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE5resetEv
Line
Count
Source
97
251
    void reset() {
98
251
        sorted = true;
99
251
        init_flag = false;
100
251
        pattern_has_time = false;
101
251
        pattern = "";
102
251
        arg_count = 0;
103
251
        conditions_met.reset();
104
251
        conditions_in_pattern.reset();
105
106
251
        events_list.clear();
107
251
        actions.clear();
108
251
        dfa_states.clear();
109
251
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE5resetEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE5resetEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE5resetEv
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE5resetEv
Line
Count
Source
97
248
    void reset() {
98
248
        sorted = true;
99
248
        init_flag = false;
100
248
        pattern_has_time = false;
101
248
        pattern = "";
102
248
        arg_count = 0;
103
248
        conditions_met.reset();
104
248
        conditions_in_pattern.reset();
105
106
248
        events_list.clear();
107
248
        actions.clear();
108
248
        dfa_states.clear();
109
248
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE5resetEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE5resetEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE5resetEv
110
111
327
    void add(const Timestamp& timestamp, const Events& events) {
112
        /// store information exclusively for rows with at least one event
113
327
        if (events.any()) {
114
184
            events_list.emplace_back(timestamp, events);
115
184
            sorted = false;
116
184
            conditions_met |= events;
117
184
        }
118
327
    }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE3addERKNS_11DateV2ValueINS_19DateTimeV2ValueTypeEEERKSt6bitsetILm32EE
Line
Count
Source
111
165
    void add(const Timestamp& timestamp, const Events& events) {
112
        /// store information exclusively for rows with at least one event
113
165
        if (events.any()) {
114
92
            events_list.emplace_back(timestamp, events);
115
92
            sorted = false;
116
92
            conditions_met |= events;
117
92
        }
118
165
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE3addERKNS_16TimeStampNsValueERKSt6bitsetILm32EE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE3addERKNS_11DateV2ValueINS_15DateV2ValueTypeEEERKSt6bitsetILm32EE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE3addERKNS_16TimestampTzValueERKSt6bitsetILm32EE
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE3addERKNS_11DateV2ValueINS_19DateTimeV2ValueTypeEEERKSt6bitsetILm32EE
Line
Count
Source
111
162
    void add(const Timestamp& timestamp, const Events& events) {
112
        /// store information exclusively for rows with at least one event
113
162
        if (events.any()) {
114
92
            events_list.emplace_back(timestamp, events);
115
92
            sorted = false;
116
92
            conditions_met |= events;
117
92
        }
118
162
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE3addERKNS_16TimeStampNsValueERKSt6bitsetILm32EE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE3addERKNS_11DateV2ValueINS_15DateV2ValueTypeEEERKSt6bitsetILm32EE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE3addERKNS_16TimestampTzValueERKSt6bitsetILm32EE
119
120
160
    void merge(const AggregateFunctionSequenceMatchData& other) {
121
160
        if (other.events_list.empty()) {
122
88
            return;
123
88
        }
124
125
72
        if (events_list.empty()) {
126
38
            reset();
127
38
            init(other.pattern, other.arg_count);
128
38
        } else if (UNLIKELY(pattern != other.pattern || arg_count != other.arg_count)) {
129
16
            throw Exception(ErrorCode::INVALID_ARGUMENT,
130
16
                            "sequence aggregate states have incompatible patterns or event counts");
131
16
        }
132
133
56
        events_list.insert(std::end(events_list), std::begin(other.events_list),
134
56
                           std::end(other.events_list));
135
56
        sorted = false;
136
56
        conditions_met |= other.conditions_met;
137
56
    }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE5mergeERKS4_
Line
Count
Source
120
80
    void merge(const AggregateFunctionSequenceMatchData& other) {
121
80
        if (other.events_list.empty()) {
122
44
            return;
123
44
        }
124
125
36
        if (events_list.empty()) {
126
19
            reset();
127
19
            init(other.pattern, other.arg_count);
128
19
        } else if (UNLIKELY(pattern != other.pattern || arg_count != other.arg_count)) {
129
8
            throw Exception(ErrorCode::INVALID_ARGUMENT,
130
8
                            "sequence aggregate states have incompatible patterns or event counts");
131
8
        }
132
133
28
        events_list.insert(std::end(events_list), std::begin(other.events_list),
134
28
                           std::end(other.events_list));
135
28
        sorted = false;
136
28
        conditions_met |= other.conditions_met;
137
28
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE5mergeERKS4_
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE5mergeERKS4_
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE5mergeERKS4_
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE5mergeERKS4_
Line
Count
Source
120
80
    void merge(const AggregateFunctionSequenceMatchData& other) {
121
80
        if (other.events_list.empty()) {
122
44
            return;
123
44
        }
124
125
36
        if (events_list.empty()) {
126
19
            reset();
127
19
            init(other.pattern, other.arg_count);
128
19
        } else if (UNLIKELY(pattern != other.pattern || arg_count != other.arg_count)) {
129
8
            throw Exception(ErrorCode::INVALID_ARGUMENT,
130
8
                            "sequence aggregate states have incompatible patterns or event counts");
131
8
        }
132
133
28
        events_list.insert(std::end(events_list), std::begin(other.events_list),
134
28
                           std::end(other.events_list));
135
28
        sorted = false;
136
28
        conditions_met |= other.conditions_met;
137
28
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE5mergeERKS4_
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE5mergeERKS4_
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE5mergeERKS4_
138
139
    // todo: rethink the sort method.
140
77
    void sort() {
141
77
        if (sorted) return;
142
143
77
        std::sort(std::begin(events_list), std::end(events_list), Comparator {});
144
77
        sorted = true;
145
77
    }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE4sortEv
Line
Count
Source
140
39
    void sort() {
141
39
        if (sorted) return;
142
143
39
        std::sort(std::begin(events_list), std::end(events_list), Comparator {});
144
39
        sorted = true;
145
39
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE4sortEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE4sortEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE4sortEv
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE4sortEv
Line
Count
Source
140
38
    void sort() {
141
38
        if (sorted) return;
142
143
38
        std::sort(std::begin(events_list), std::end(events_list), Comparator {});
144
38
        sorted = true;
145
38
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE4sortEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE4sortEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE4sortEv
146
147
77
    void write(BufferWritable& buf) const {
148
77
        buf.write_binary(sorted);
149
77
        buf.write_binary(events_list.size());
150
151
77
        for (const auto& events : events_list) {
152
43
            buf.write_binary(events.first);
153
43
            buf.write_binary(events.second.to_ulong());
154
43
        }
155
156
        // This is std::bitset<32>, which will not exceed 32 bits.
157
77
        UInt32 conditions_met_value = (UInt32)conditions_met.to_ulong();
158
77
        buf.write_binary(conditions_met_value);
159
160
77
        buf.write_binary(pattern);
161
77
        buf.write_binary(arg_count);
162
77
    }
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE5writeERNS_14BufferWritableE
Line
Count
Source
147
39
    void write(BufferWritable& buf) const {
148
39
        buf.write_binary(sorted);
149
39
        buf.write_binary(events_list.size());
150
151
39
        for (const auto& events : events_list) {
152
22
            buf.write_binary(events.first);
153
22
            buf.write_binary(events.second.to_ulong());
154
22
        }
155
156
        // This is std::bitset<32>, which will not exceed 32 bits.
157
39
        UInt32 conditions_met_value = (UInt32)conditions_met.to_ulong();
158
39
        buf.write_binary(conditions_met_value);
159
160
39
        buf.write_binary(pattern);
161
39
        buf.write_binary(arg_count);
162
39
    }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE5writeERNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE5writeERNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE5writeERNS_14BufferWritableE
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE5writeERNS_14BufferWritableE
Line
Count
Source
147
38
    void write(BufferWritable& buf) const {
148
38
        buf.write_binary(sorted);
149
38
        buf.write_binary(events_list.size());
150
151
38
        for (const auto& events : events_list) {
152
21
            buf.write_binary(events.first);
153
21
            buf.write_binary(events.second.to_ulong());
154
21
        }
155
156
        // This is std::bitset<32>, which will not exceed 32 bits.
157
38
        UInt32 conditions_met_value = (UInt32)conditions_met.to_ulong();
158
38
        buf.write_binary(conditions_met_value);
159
160
38
        buf.write_binary(pattern);
161
38
        buf.write_binary(arg_count);
162
38
    }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE5writeERNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE5writeERNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE5writeERNS_14BufferWritableE
163
164
85
    void read(BufferReadable& buf) {
165
85
        buf.read_binary(sorted);
166
167
85
        size_t events_list_size;
168
85
        buf.read_binary(events_list_size);
169
170
85
        events_list.clear();
171
85
        events_list.reserve(events_list_size);
172
173
136
        for (size_t i = 0; i < events_list_size; ++i) {
174
51
            Timestamp timestamp;
175
51
            buf.read_binary(timestamp);
176
177
51
            UInt64 events;
178
51
            buf.read_binary(events);
179
180
51
            events_list.emplace_back(timestamp, Events {events});
181
51
        }
182
183
85
        UInt32 conditions_met_value;
184
85
        buf.read_binary(conditions_met_value);
185
85
        conditions_met = conditions_met_value;
186
187
85
        buf.read_binary(pattern);
188
85
        buf.read_binary(arg_count);
189
85
    }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE4readERNS_14BufferReadableE
Line
Count
Source
164
43
    void read(BufferReadable& buf) {
165
43
        buf.read_binary(sorted);
166
167
43
        size_t events_list_size;
168
43
        buf.read_binary(events_list_size);
169
170
43
        events_list.clear();
171
43
        events_list.reserve(events_list_size);
172
173
69
        for (size_t i = 0; i < events_list_size; ++i) {
174
26
            Timestamp timestamp;
175
26
            buf.read_binary(timestamp);
176
177
26
            UInt64 events;
178
26
            buf.read_binary(events);
179
180
26
            events_list.emplace_back(timestamp, Events {events});
181
26
        }
182
183
43
        UInt32 conditions_met_value;
184
43
        buf.read_binary(conditions_met_value);
185
43
        conditions_met = conditions_met_value;
186
187
43
        buf.read_binary(pattern);
188
43
        buf.read_binary(arg_count);
189
43
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE4readERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE4readERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE4readERNS_14BufferReadableE
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE4readERNS_14BufferReadableE
Line
Count
Source
164
42
    void read(BufferReadable& buf) {
165
42
        buf.read_binary(sorted);
166
167
42
        size_t events_list_size;
168
42
        buf.read_binary(events_list_size);
169
170
42
        events_list.clear();
171
42
        events_list.reserve(events_list_size);
172
173
67
        for (size_t i = 0; i < events_list_size; ++i) {
174
25
            Timestamp timestamp;
175
25
            buf.read_binary(timestamp);
176
177
25
            UInt64 events;
178
25
            buf.read_binary(events);
179
180
25
            events_list.emplace_back(timestamp, Events {events});
181
25
        }
182
183
42
        UInt32 conditions_met_value;
184
42
        buf.read_binary(conditions_met_value);
185
42
        conditions_met = conditions_met_value;
186
187
42
        buf.read_binary(pattern);
188
42
        buf.read_binary(arg_count);
189
42
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE4readERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE4readERNS_14BufferReadableE
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE4readERNS_14BufferReadableE
190
191
private:
192
    enum class PatternActionType {
193
        SpecificEvent,
194
        AnyEvent,
195
        KleeneStar,
196
        TimeLessOrEqual,
197
        TimeLess,
198
        TimeGreaterOrEqual,
199
        TimeGreater,
200
        TimeEqual
201
    };
202
203
    struct PatternAction final {
204
        PatternActionType type;
205
        std::uint64_t extra;
206
207
        PatternAction() = default;
208
        explicit PatternAction(const PatternActionType type_, const std::uint64_t extra_ = 0)
209
778
                : type {type_}, extra {extra_} {}
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE13PatternActionC2ENS4_17PatternActionTypeEm
Line
Count
Source
209
394
                : type {type_}, extra {extra_} {}
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE13PatternActionC2ENS4_17PatternActionTypeEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE13PatternActionC2ENS4_17PatternActionTypeEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE13PatternActionC2ENS4_17PatternActionTypeEm
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE13PatternActionC2ENS4_17PatternActionTypeEm
Line
Count
Source
209
384
                : type {type_}, extra {extra_} {}
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE13PatternActionC2ENS4_17PatternActionTypeEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE13PatternActionC2ENS4_17PatternActionTypeEm
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE13PatternActionC2ENS4_17PatternActionTypeEm
210
    };
211
212
    using PatternActions = PODArrayWithStackMemory<PatternAction, 64>;
213
214
    Derived& derived() { return assert_cast<Derived&, TypeCheckOnRelease::DISABLE>(*this); }
215
216
385
    void parse_pattern() {
217
385
        actions.clear();
218
385
        actions.emplace_back(PatternActionType::KleeneStar);
219
220
385
        dfa_states.clear();
221
385
        dfa_states.emplace_back(true);
222
223
385
        pattern_has_time = false;
224
385
        conditions_in_pattern.reset();
225
226
385
        const char* pos = pattern.data();
227
385
        const char* begin = pos;
228
385
        const char* end = pos + pattern.size();
229
385
        const size_t event_count = arg_count - 2;
230
231
        // FE checks a literal pattern, but a constant pattern only BE can evaluate is checked here.
232
385
        auto throw_exception = [&](const std::string& msg) {
233
7
            throw Exception(ErrorCode::INVALID_ARGUMENT, "{} '{}' at position {} of pattern '{}'",
234
7
                            msg, std::string(pos, end), pos - begin, pattern);
235
7
        };
_ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
Line
Count
Source
232
4
        auto throw_exception = [&](const std::string& msg) {
233
4
            throw Exception(ErrorCode::INVALID_ARGUMENT, "{} '{}' at position {} of pattern '{}'",
234
4
                            msg, std::string(pos, end), pos - begin, pattern);
235
4
        };
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
_ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
Line
Count
Source
232
3
        auto throw_exception = [&](const std::string& msg) {
233
3
            throw Exception(ErrorCode::INVALID_ARGUMENT, "{} '{}' at position {} of pattern '{}'",
234
3
                            msg, std::string(pos, end), pos - begin, pattern);
235
3
        };
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE13parse_patternEvENKUlRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE_clESC_
236
237
1.19k
        auto match = [&pos, end](const char* str) mutable {
238
1.19k
            size_t length = strlen(str);
239
1.19k
            if (pos + length <= end && 0 == memcmp(pos, str, length)) {
240
799
                pos += length;
241
799
                return true;
242
799
            }
243
397
            return false;
244
1.19k
        };
_ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE13parse_patternEvENUlPKcE_clES6_
Line
Count
Source
237
611
        auto match = [&pos, end](const char* str) mutable {
238
611
            size_t length = strlen(str);
239
611
            if (pos + length <= end && 0 == memcmp(pos, str, length)) {
240
410
                pos += length;
241
410
                return true;
242
410
            }
243
201
            return false;
244
611
        };
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE13parse_patternEvENUlPKcE_clES6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE13parse_patternEvENUlPKcE_clES6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE13parse_patternEvENUlPKcE_clES6_
_ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE13parse_patternEvENUlPKcE_clES6_
Line
Count
Source
237
585
        auto match = [&pos, end](const char* str) mutable {
238
585
            size_t length = strlen(str);
239
585
            if (pos + length <= end && 0 == memcmp(pos, str, length)) {
240
389
                pos += length;
241
389
                return true;
242
389
            }
243
196
            return false;
244
585
        };
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE13parse_patternEvENUlPKcE_clES6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE13parse_patternEvENUlPKcE_clES6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE13parse_patternEvENUlPKcE_clES6_
245
246
400
        auto parse_uint = [&pos, end](auto& value) {
247
400
            const auto* start = pos;
248
801
            while (pos < end && std::isdigit(static_cast<unsigned char>(*pos))) {
249
401
                ++pos;
250
401
            }
251
252
400
            if (pos == start) {
253
1
                return false;
254
1
            }
255
256
399
            StringParser::ParseResult result;
257
399
            value = StringParser::string_to_int<std::decay_t<decltype(value)>, false>(
258
399
                    start, pos - start, &result);
259
399
            return result == StringParser::PARSE_SUCCESS;
260
400
        };
_ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
Line
Count
Source
246
204
        auto parse_uint = [&pos, end](auto& value) {
247
204
            const auto* start = pos;
248
408
            while (pos < end && std::isdigit(static_cast<unsigned char>(*pos))) {
249
204
                ++pos;
250
204
            }
251
252
204
            if (pos == start) {
253
1
                return false;
254
1
            }
255
256
203
            StringParser::ParseResult result;
257
203
            value = StringParser::string_to_int<std::decay_t<decltype(value)>, false>(
258
203
                    start, pos - start, &result);
259
203
            return result == StringParser::PARSE_SUCCESS;
260
204
        };
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
_ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
Line
Count
Source
246
196
        auto parse_uint = [&pos, end](auto& value) {
247
196
            const auto* start = pos;
248
393
            while (pos < end && std::isdigit(static_cast<unsigned char>(*pos))) {
249
197
                ++pos;
250
197
            }
251
252
196
            if (pos == start) {
253
0
                return false;
254
0
            }
255
256
196
            StringParser::ParseResult result;
257
196
            value = StringParser::string_to_int<std::decay_t<decltype(value)>, false>(
258
196
                    start, pos - start, &result);
259
196
            return result == StringParser::PARSE_SUCCESS;
260
196
        };
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
Unexecuted instantiation: _ZZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE13parse_patternEvENKUlRT_E_clImEEDaS6_
261
262
778
        while (pos < end) {
263
400
            if (match("(?")) {
264
400
                if (match("t")) {
265
3
                    PatternActionType type;
266
267
3
                    if (match("<="))
268
3
                        type = PatternActionType::TimeLessOrEqual;
269
0
                    else if (match("<"))
270
0
                        type = PatternActionType::TimeLess;
271
0
                    else if (match(">="))
272
0
                        type = PatternActionType::TimeGreaterOrEqual;
273
0
                    else if (match(">"))
274
0
                        type = PatternActionType::TimeGreater;
275
0
                    else if (match("=="))
276
0
                        type = PatternActionType::TimeEqual;
277
0
                    else {
278
0
                        throw_exception("Unknown time condition");
279
0
                        return;
280
0
                    }
281
282
3
                    uint64_t duration = 0;
283
3
                    if (!parse_uint(duration)) {
284
1
                        throw_exception("Could not parse number");
285
1
                        return;
286
1
                    }
287
288
2
                    if (actions.back().type != PatternActionType::SpecificEvent &&
289
2
                        actions.back().type != PatternActionType::AnyEvent &&
290
2
                        actions.back().type != PatternActionType::KleeneStar) {
291
0
                        throw_exception(
292
0
                                "Temporal condition should be preceded by an event condition");
293
0
                        return;
294
0
                    }
295
296
2
                    pattern_has_time = true;
297
2
                    actions.emplace_back(type, duration);
298
397
                } else {
299
397
                    UInt64 event_number = 0;
300
397
                    if (!parse_uint(event_number)) {
301
0
                        throw_exception("Could not parse number");
302
0
                        return;
303
0
                    }
304
305
397
                    if (event_number == 0 || event_number > event_count) {
306
6
                        throw_exception("Event number " + std::to_string(event_number) +
307
6
                                        " is out of range");
308
6
                        return;
309
6
                    }
310
311
391
                    const auto event_index = event_number - 1;
312
391
                    actions.emplace_back(PatternActionType::SpecificEvent, event_index);
313
391
                    dfa_states.back().transition = DFATransition::SpecificEvent;
314
391
                    dfa_states.back().event = static_cast<uint32_t>(event_index);
315
391
                    dfa_states.emplace_back();
316
391
                    conditions_in_pattern.set(event_index);
317
391
                }
318
319
393
                if (!match(")")) {
320
0
                    throw_exception("Expected closing parenthesis, found");
321
0
                    return;
322
0
                }
323
324
393
            } else if (match(".*")) {
325
0
                actions.emplace_back(PatternActionType::KleeneStar);
326
0
                dfa_states.back().has_kleene = true;
327
0
            } else if (match(".")) {
328
0
                actions.emplace_back(PatternActionType::AnyEvent);
329
0
                dfa_states.back().transition = DFATransition::AnyEvent;
330
0
                dfa_states.emplace_back();
331
0
            } else {
332
0
                throw_exception("Could not parse pattern, unexpected starting symbol");
333
0
                return;
334
0
            }
335
400
        }
336
385
    }
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE13parse_patternEv
Line
Count
Source
216
194
    void parse_pattern() {
217
194
        actions.clear();
218
194
        actions.emplace_back(PatternActionType::KleeneStar);
219
220
194
        dfa_states.clear();
221
194
        dfa_states.emplace_back(true);
222
223
194
        pattern_has_time = false;
224
194
        conditions_in_pattern.reset();
225
226
194
        const char* pos = pattern.data();
227
194
        const char* begin = pos;
228
194
        const char* end = pos + pattern.size();
229
194
        const size_t event_count = arg_count - 2;
230
231
        // FE checks a literal pattern, but a constant pattern only BE can evaluate is checked here.
232
194
        auto throw_exception = [&](const std::string& msg) {
233
194
            throw Exception(ErrorCode::INVALID_ARGUMENT, "{} '{}' at position {} of pattern '{}'",
234
194
                            msg, std::string(pos, end), pos - begin, pattern);
235
194
        };
236
237
194
        auto match = [&pos, end](const char* str) mutable {
238
194
            size_t length = strlen(str);
239
194
            if (pos + length <= end && 0 == memcmp(pos, str, length)) {
240
194
                pos += length;
241
194
                return true;
242
194
            }
243
194
            return false;
244
194
        };
245
246
194
        auto parse_uint = [&pos, end](auto& value) {
247
194
            const auto* start = pos;
248
194
            while (pos < end && std::isdigit(static_cast<unsigned char>(*pos))) {
249
194
                ++pos;
250
194
            }
251
252
194
            if (pos == start) {
253
194
                return false;
254
194
            }
255
256
194
            StringParser::ParseResult result;
257
194
            value = StringParser::string_to_int<std::decay_t<decltype(value)>, false>(
258
194
                    start, pos - start, &result);
259
194
            return result == StringParser::PARSE_SUCCESS;
260
194
        };
261
262
394
        while (pos < end) {
263
204
            if (match("(?")) {
264
204
                if (match("t")) {
265
3
                    PatternActionType type;
266
267
3
                    if (match("<="))
268
3
                        type = PatternActionType::TimeLessOrEqual;
269
0
                    else if (match("<"))
270
0
                        type = PatternActionType::TimeLess;
271
0
                    else if (match(">="))
272
0
                        type = PatternActionType::TimeGreaterOrEqual;
273
0
                    else if (match(">"))
274
0
                        type = PatternActionType::TimeGreater;
275
0
                    else if (match("=="))
276
0
                        type = PatternActionType::TimeEqual;
277
0
                    else {
278
0
                        throw_exception("Unknown time condition");
279
0
                        return;
280
0
                    }
281
282
3
                    uint64_t duration = 0;
283
3
                    if (!parse_uint(duration)) {
284
1
                        throw_exception("Could not parse number");
285
1
                        return;
286
1
                    }
287
288
2
                    if (actions.back().type != PatternActionType::SpecificEvent &&
289
2
                        actions.back().type != PatternActionType::AnyEvent &&
290
2
                        actions.back().type != PatternActionType::KleeneStar) {
291
0
                        throw_exception(
292
0
                                "Temporal condition should be preceded by an event condition");
293
0
                        return;
294
0
                    }
295
296
2
                    pattern_has_time = true;
297
2
                    actions.emplace_back(type, duration);
298
201
                } else {
299
201
                    UInt64 event_number = 0;
300
201
                    if (!parse_uint(event_number)) {
301
0
                        throw_exception("Could not parse number");
302
0
                        return;
303
0
                    }
304
305
201
                    if (event_number == 0 || event_number > event_count) {
306
3
                        throw_exception("Event number " + std::to_string(event_number) +
307
3
                                        " is out of range");
308
3
                        return;
309
3
                    }
310
311
198
                    const auto event_index = event_number - 1;
312
198
                    actions.emplace_back(PatternActionType::SpecificEvent, event_index);
313
198
                    dfa_states.back().transition = DFATransition::SpecificEvent;
314
198
                    dfa_states.back().event = static_cast<uint32_t>(event_index);
315
198
                    dfa_states.emplace_back();
316
198
                    conditions_in_pattern.set(event_index);
317
198
                }
318
319
200
                if (!match(")")) {
320
0
                    throw_exception("Expected closing parenthesis, found");
321
0
                    return;
322
0
                }
323
324
200
            } else if (match(".*")) {
325
0
                actions.emplace_back(PatternActionType::KleeneStar);
326
0
                dfa_states.back().has_kleene = true;
327
0
            } else if (match(".")) {
328
0
                actions.emplace_back(PatternActionType::AnyEvent);
329
0
                dfa_states.back().transition = DFATransition::AnyEvent;
330
0
                dfa_states.emplace_back();
331
0
            } else {
332
0
                throw_exception("Could not parse pattern, unexpected starting symbol");
333
0
                return;
334
0
            }
335
204
        }
336
194
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE13parse_patternEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE13parse_patternEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE13parse_patternEv
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE13parse_patternEv
Line
Count
Source
216
191
    void parse_pattern() {
217
191
        actions.clear();
218
191
        actions.emplace_back(PatternActionType::KleeneStar);
219
220
191
        dfa_states.clear();
221
191
        dfa_states.emplace_back(true);
222
223
191
        pattern_has_time = false;
224
191
        conditions_in_pattern.reset();
225
226
191
        const char* pos = pattern.data();
227
191
        const char* begin = pos;
228
191
        const char* end = pos + pattern.size();
229
191
        const size_t event_count = arg_count - 2;
230
231
        // FE checks a literal pattern, but a constant pattern only BE can evaluate is checked here.
232
191
        auto throw_exception = [&](const std::string& msg) {
233
191
            throw Exception(ErrorCode::INVALID_ARGUMENT, "{} '{}' at position {} of pattern '{}'",
234
191
                            msg, std::string(pos, end), pos - begin, pattern);
235
191
        };
236
237
191
        auto match = [&pos, end](const char* str) mutable {
238
191
            size_t length = strlen(str);
239
191
            if (pos + length <= end && 0 == memcmp(pos, str, length)) {
240
191
                pos += length;
241
191
                return true;
242
191
            }
243
191
            return false;
244
191
        };
245
246
191
        auto parse_uint = [&pos, end](auto& value) {
247
191
            const auto* start = pos;
248
191
            while (pos < end && std::isdigit(static_cast<unsigned char>(*pos))) {
249
191
                ++pos;
250
191
            }
251
252
191
            if (pos == start) {
253
191
                return false;
254
191
            }
255
256
191
            StringParser::ParseResult result;
257
191
            value = StringParser::string_to_int<std::decay_t<decltype(value)>, false>(
258
191
                    start, pos - start, &result);
259
191
            return result == StringParser::PARSE_SUCCESS;
260
191
        };
261
262
384
        while (pos < end) {
263
196
            if (match("(?")) {
264
196
                if (match("t")) {
265
0
                    PatternActionType type;
266
267
0
                    if (match("<="))
268
0
                        type = PatternActionType::TimeLessOrEqual;
269
0
                    else if (match("<"))
270
0
                        type = PatternActionType::TimeLess;
271
0
                    else if (match(">="))
272
0
                        type = PatternActionType::TimeGreaterOrEqual;
273
0
                    else if (match(">"))
274
0
                        type = PatternActionType::TimeGreater;
275
0
                    else if (match("=="))
276
0
                        type = PatternActionType::TimeEqual;
277
0
                    else {
278
0
                        throw_exception("Unknown time condition");
279
0
                        return;
280
0
                    }
281
282
0
                    uint64_t duration = 0;
283
0
                    if (!parse_uint(duration)) {
284
0
                        throw_exception("Could not parse number");
285
0
                        return;
286
0
                    }
287
288
0
                    if (actions.back().type != PatternActionType::SpecificEvent &&
289
0
                        actions.back().type != PatternActionType::AnyEvent &&
290
0
                        actions.back().type != PatternActionType::KleeneStar) {
291
0
                        throw_exception(
292
0
                                "Temporal condition should be preceded by an event condition");
293
0
                        return;
294
0
                    }
295
296
0
                    pattern_has_time = true;
297
0
                    actions.emplace_back(type, duration);
298
196
                } else {
299
196
                    UInt64 event_number = 0;
300
196
                    if (!parse_uint(event_number)) {
301
0
                        throw_exception("Could not parse number");
302
0
                        return;
303
0
                    }
304
305
196
                    if (event_number == 0 || event_number > event_count) {
306
3
                        throw_exception("Event number " + std::to_string(event_number) +
307
3
                                        " is out of range");
308
3
                        return;
309
3
                    }
310
311
193
                    const auto event_index = event_number - 1;
312
193
                    actions.emplace_back(PatternActionType::SpecificEvent, event_index);
313
193
                    dfa_states.back().transition = DFATransition::SpecificEvent;
314
193
                    dfa_states.back().event = static_cast<uint32_t>(event_index);
315
193
                    dfa_states.emplace_back();
316
193
                    conditions_in_pattern.set(event_index);
317
193
                }
318
319
193
                if (!match(")")) {
320
0
                    throw_exception("Expected closing parenthesis, found");
321
0
                    return;
322
0
                }
323
324
193
            } else if (match(".*")) {
325
0
                actions.emplace_back(PatternActionType::KleeneStar);
326
0
                dfa_states.back().has_kleene = true;
327
0
            } else if (match(".")) {
328
0
                actions.emplace_back(PatternActionType::AnyEvent);
329
0
                dfa_states.back().transition = DFATransition::AnyEvent;
330
0
                dfa_states.emplace_back();
331
0
            } else {
332
0
                throw_exception("Could not parse pattern, unexpected starting symbol");
333
0
                return;
334
0
            }
335
196
        }
336
191
    }
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE13parse_patternEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE13parse_patternEv
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE13parse_patternEv
337
338
public:
339
    /// Uses a DFA based approach in order to better handle patterns without
340
    /// time assertions.
341
    ///
342
    /// NOTE: This implementation relies on the assumption that the pattern is *small*.
343
    ///
344
    /// This algorithm performs in O(mn) (with m the number of DFA states and N the number
345
    /// of events) with a memory consumption and memory allocations in O(m). It means that
346
    /// if n >>> m (which is expected to be the case), this algorithm can be considered linear.
347
    template <typename EventEntry>
348
37
    bool dfa_match(EventEntry& events_it, const EventEntry events_end) const {
349
37
        using ActiveStates = std::vector<bool>;
350
        /// Those two vectors keep track of which states should be considered for the current
351
        /// event as well as the states which should be considered for the next event.
352
37
        ActiveStates active_states(dfa_states.size(), false);
353
37
        ActiveStates next_active_states(dfa_states.size(), false);
354
37
        active_states[0] = true;
355
356
        /// Keeps track of dead-ends in order not to iterate over all the events to realize that
357
        /// the match failed.
358
37
        size_t n_active = 1;
359
360
77
        for (/* empty */; events_it != events_end && n_active > 0 && !active_states.back();
361
40
             ++events_it) {
362
40
            n_active = 0;
363
40
            next_active_states.assign(dfa_states.size(), false);
364
365
126
            for (size_t state = 0; state < dfa_states.size(); ++state) {
366
86
                if (!active_states[state]) {
367
43
                    continue;
368
43
                }
369
370
43
                switch (dfa_states[state].transition) {
371
0
                case DFATransition::None:
372
0
                    break;
373
0
                case DFATransition::AnyEvent:
374
0
                    next_active_states[state + 1] = true;
375
0
                    ++n_active;
376
0
                    break;
377
43
                case DFATransition::SpecificEvent:
378
43
                    if (events_it->second.test(dfa_states[state].event)) {
379
40
                        next_active_states[state + 1] = true;
380
40
                        ++n_active;
381
40
                    }
382
43
                    break;
383
43
                }
384
385
43
                if (dfa_states[state].has_kleene) {
386
40
                    next_active_states[state] = true;
387
40
                    ++n_active;
388
40
                }
389
43
            }
390
40
            swap(active_states, next_active_states);
391
40
        }
392
393
37
        return active_states.back();
394
37
    }
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE9dfa_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_
Line
Count
Source
348
37
    bool dfa_match(EventEntry& events_it, const EventEntry events_end) const {
349
37
        using ActiveStates = std::vector<bool>;
350
        /// Those two vectors keep track of which states should be considered for the current
351
        /// event as well as the states which should be considered for the next event.
352
37
        ActiveStates active_states(dfa_states.size(), false);
353
37
        ActiveStates next_active_states(dfa_states.size(), false);
354
37
        active_states[0] = true;
355
356
        /// Keeps track of dead-ends in order not to iterate over all the events to realize that
357
        /// the match failed.
358
37
        size_t n_active = 1;
359
360
77
        for (/* empty */; events_it != events_end && n_active > 0 && !active_states.back();
361
40
             ++events_it) {
362
40
            n_active = 0;
363
40
            next_active_states.assign(dfa_states.size(), false);
364
365
126
            for (size_t state = 0; state < dfa_states.size(); ++state) {
366
86
                if (!active_states[state]) {
367
43
                    continue;
368
43
                }
369
370
43
                switch (dfa_states[state].transition) {
371
0
                case DFATransition::None:
372
0
                    break;
373
0
                case DFATransition::AnyEvent:
374
0
                    next_active_states[state + 1] = true;
375
0
                    ++n_active;
376
0
                    break;
377
43
                case DFATransition::SpecificEvent:
378
43
                    if (events_it->second.test(dfa_states[state].event)) {
379
40
                        next_active_states[state + 1] = true;
380
40
                        ++n_active;
381
40
                    }
382
43
                    break;
383
43
                }
384
385
43
                if (dfa_states[state].has_kleene) {
386
40
                    next_active_states[state] = true;
387
40
                    ++n_active;
388
40
                }
389
43
            }
390
40
            swap(active_states, next_active_states);
391
40
        }
392
393
37
        return active_states.back();
394
37
    }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE9dfa_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE9dfa_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE9dfa_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_
395
396
    template <typename EventEntry>
397
49
    bool backtracking_match(EventEntry& events_it, const EventEntry events_end) const {
398
49
        const auto action_begin = std::begin(actions);
399
49
        const auto action_end = std::end(actions);
400
49
        auto action_it = action_begin;
401
402
49
        const auto events_begin = events_it;
403
49
        auto base_it = events_it;
404
405
        /// an iterator to action plus an iterator to row in events list plus timestamp at the start of sequence
406
49
        using backtrack_info = std::tuple<decltype(action_it), EventEntry, EventEntry>;
407
49
        std::stack<backtrack_info> back_stack;
408
409
        /// backtrack if possible
410
49
        const auto do_backtrack = [&] {
411
2
            while (!back_stack.empty()) {
412
2
                auto& top = back_stack.top();
413
414
2
                action_it = std::get<0>(top);
415
2
                events_it = std::next(std::get<1>(top));
416
2
                base_it = std::get<2>(top);
417
418
2
                back_stack.pop();
419
420
2
                if (events_it != events_end) return true;
421
2
            }
422
423
0
            return false;
424
2
        };
_ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_ENKUlvE_clEv
Line
Count
Source
410
2
        const auto do_backtrack = [&] {
411
2
            while (!back_stack.empty()) {
412
2
                auto& top = back_stack.top();
413
414
2
                action_it = std::get<0>(top);
415
2
                events_it = std::next(std::get<1>(top));
416
2
                base_it = std::get<2>(top);
417
418
2
                back_stack.pop();
419
420
2
                if (events_it != events_end) return true;
421
2
            }
422
423
0
            return false;
424
2
        };
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_ENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_ENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_ENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_ENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_ENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_ENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_ENKUlvE_clEv
425
426
49
        size_t i = 0;
427
162
        while (action_it != action_end && events_it != events_end) {
428
113
            if (action_it->type == PatternActionType::SpecificEvent) {
429
60
                if (events_it->second.test(action_it->extra)) {
430
                    /// move to the next action and events
431
58
                    base_it = events_it;
432
58
                    ++action_it, ++events_it;
433
58
                } else if (!do_backtrack())
434
                    /// backtracking failed, bail out
435
0
                    break;
436
60
            } else if (action_it->type == PatternActionType::AnyEvent) {
437
0
                base_it = events_it;
438
0
                ++action_it, ++events_it;
439
53
            } else if (action_it->type == PatternActionType::KleeneStar) {
440
49
                back_stack.emplace(action_it, events_it, base_it);
441
49
                base_it = events_it;
442
49
                ++action_it;
443
49
            } else if (action_it->type == PatternActionType::TimeLessOrEqual) {
444
4
                if (events_it->first.datetime_diff_in_seconds(base_it->first) <= action_it->extra) {
445
                    /// condition satisfied, move onto next action
446
4
                    back_stack.emplace(action_it, events_it, base_it);
447
4
                    base_it = events_it;
448
4
                    ++action_it;
449
4
                } else if (!do_backtrack())
450
0
                    break;
451
4
            } else if (action_it->type == PatternActionType::TimeLess) {
452
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) < action_it->extra) {
453
0
                    back_stack.emplace(action_it, events_it, base_it);
454
0
                    base_it = events_it;
455
0
                    ++action_it;
456
0
                } else if (!do_backtrack())
457
0
                    break;
458
0
            } else if (action_it->type == PatternActionType::TimeGreaterOrEqual) {
459
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) >= action_it->extra) {
460
0
                    back_stack.emplace(action_it, events_it, base_it);
461
0
                    base_it = events_it;
462
0
                    ++action_it;
463
0
                } else if (++events_it == events_end && !do_backtrack())
464
0
                    break;
465
0
            } else if (action_it->type == PatternActionType::TimeGreater) {
466
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) > action_it->extra) {
467
0
                    back_stack.emplace(action_it, events_it, base_it);
468
0
                    base_it = events_it;
469
0
                    ++action_it;
470
0
                } else if (++events_it == events_end && !do_backtrack())
471
0
                    break;
472
0
            } else if (action_it->type == PatternActionType::TimeEqual) {
473
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) == action_it->extra) {
474
0
                    back_stack.emplace(action_it, events_it, base_it);
475
0
                    base_it = events_it;
476
0
                    ++action_it;
477
0
                } else if (++events_it == events_end && !do_backtrack())
478
0
                    break;
479
0
            } else {
480
0
                LOG(WARNING) << "Unknown PatternActionType";
481
0
                return false;
482
0
            }
483
484
113
            if (++i > sequence_match_max_iterations) {
485
0
                LOG(WARNING)
486
0
                        << "Pattern application proves too difficult, exceeding max iterations (" +
487
0
                                   std::to_string(sequence_match_max_iterations) + ")";
488
0
                return false;
489
0
            }
490
113
        }
491
492
        /// if there are some actions remaining
493
49
        if (action_it != action_end) {
494
            /// match multiple empty strings at end
495
0
            while (action_it->type == PatternActionType::KleeneStar ||
496
0
                   action_it->type == PatternActionType::TimeLessOrEqual ||
497
0
                   action_it->type == PatternActionType::TimeLess ||
498
0
                   (action_it->type == PatternActionType::TimeGreaterOrEqual &&
499
0
                    action_it->extra == 0))
500
0
                ++action_it;
501
0
        }
502
503
49
        if (events_it == events_begin) ++events_it;
504
505
49
        return action_it == action_end;
506
49
    }
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_
Line
Count
Source
397
2
    bool backtracking_match(EventEntry& events_it, const EventEntry events_end) const {
398
2
        const auto action_begin = std::begin(actions);
399
2
        const auto action_end = std::end(actions);
400
2
        auto action_it = action_begin;
401
402
2
        const auto events_begin = events_it;
403
2
        auto base_it = events_it;
404
405
        /// an iterator to action plus an iterator to row in events list plus timestamp at the start of sequence
406
2
        using backtrack_info = std::tuple<decltype(action_it), EventEntry, EventEntry>;
407
2
        std::stack<backtrack_info> back_stack;
408
409
        /// backtrack if possible
410
2
        const auto do_backtrack = [&] {
411
2
            while (!back_stack.empty()) {
412
2
                auto& top = back_stack.top();
413
414
2
                action_it = std::get<0>(top);
415
2
                events_it = std::next(std::get<1>(top));
416
2
                base_it = std::get<2>(top);
417
418
2
                back_stack.pop();
419
420
2
                if (events_it != events_end) return true;
421
2
            }
422
423
2
            return false;
424
2
        };
425
426
2
        size_t i = 0;
427
14
        while (action_it != action_end && events_it != events_end) {
428
12
            if (action_it->type == PatternActionType::SpecificEvent) {
429
6
                if (events_it->second.test(action_it->extra)) {
430
                    /// move to the next action and events
431
4
                    base_it = events_it;
432
4
                    ++action_it, ++events_it;
433
4
                } else if (!do_backtrack())
434
                    /// backtracking failed, bail out
435
0
                    break;
436
6
            } else if (action_it->type == PatternActionType::AnyEvent) {
437
0
                base_it = events_it;
438
0
                ++action_it, ++events_it;
439
6
            } else if (action_it->type == PatternActionType::KleeneStar) {
440
2
                back_stack.emplace(action_it, events_it, base_it);
441
2
                base_it = events_it;
442
2
                ++action_it;
443
4
            } else if (action_it->type == PatternActionType::TimeLessOrEqual) {
444
4
                if (events_it->first.datetime_diff_in_seconds(base_it->first) <= action_it->extra) {
445
                    /// condition satisfied, move onto next action
446
4
                    back_stack.emplace(action_it, events_it, base_it);
447
4
                    base_it = events_it;
448
4
                    ++action_it;
449
4
                } else if (!do_backtrack())
450
0
                    break;
451
4
            } else if (action_it->type == PatternActionType::TimeLess) {
452
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) < action_it->extra) {
453
0
                    back_stack.emplace(action_it, events_it, base_it);
454
0
                    base_it = events_it;
455
0
                    ++action_it;
456
0
                } else if (!do_backtrack())
457
0
                    break;
458
0
            } else if (action_it->type == PatternActionType::TimeGreaterOrEqual) {
459
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) >= action_it->extra) {
460
0
                    back_stack.emplace(action_it, events_it, base_it);
461
0
                    base_it = events_it;
462
0
                    ++action_it;
463
0
                } else if (++events_it == events_end && !do_backtrack())
464
0
                    break;
465
0
            } else if (action_it->type == PatternActionType::TimeGreater) {
466
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) > action_it->extra) {
467
0
                    back_stack.emplace(action_it, events_it, base_it);
468
0
                    base_it = events_it;
469
0
                    ++action_it;
470
0
                } else if (++events_it == events_end && !do_backtrack())
471
0
                    break;
472
0
            } else if (action_it->type == PatternActionType::TimeEqual) {
473
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) == action_it->extra) {
474
0
                    back_stack.emplace(action_it, events_it, base_it);
475
0
                    base_it = events_it;
476
0
                    ++action_it;
477
0
                } else if (++events_it == events_end && !do_backtrack())
478
0
                    break;
479
0
            } else {
480
0
                LOG(WARNING) << "Unknown PatternActionType";
481
0
                return false;
482
0
            }
483
484
12
            if (++i > sequence_match_max_iterations) {
485
0
                LOG(WARNING)
486
0
                        << "Pattern application proves too difficult, exceeding max iterations (" +
487
0
                                   std::to_string(sequence_match_max_iterations) + ")";
488
0
                return false;
489
0
            }
490
12
        }
491
492
        /// if there are some actions remaining
493
2
        if (action_it != action_end) {
494
            /// match multiple empty strings at end
495
0
            while (action_it->type == PatternActionType::KleeneStar ||
496
0
                   action_it->type == PatternActionType::TimeLessOrEqual ||
497
0
                   action_it->type == PatternActionType::TimeLess ||
498
0
                   (action_it->type == PatternActionType::TimeGreaterOrEqual &&
499
0
                    action_it->extra == 0))
500
0
                ++action_it;
501
0
        }
502
503
2
        if (events_it == events_begin) ++events_it;
504
505
2
        return action_it == action_end;
506
2
    }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_
Line
Count
Source
397
47
    bool backtracking_match(EventEntry& events_it, const EventEntry events_end) const {
398
47
        const auto action_begin = std::begin(actions);
399
47
        const auto action_end = std::end(actions);
400
47
        auto action_it = action_begin;
401
402
47
        const auto events_begin = events_it;
403
47
        auto base_it = events_it;
404
405
        /// an iterator to action plus an iterator to row in events list plus timestamp at the start of sequence
406
47
        using backtrack_info = std::tuple<decltype(action_it), EventEntry, EventEntry>;
407
47
        std::stack<backtrack_info> back_stack;
408
409
        /// backtrack if possible
410
47
        const auto do_backtrack = [&] {
411
47
            while (!back_stack.empty()) {
412
47
                auto& top = back_stack.top();
413
414
47
                action_it = std::get<0>(top);
415
47
                events_it = std::next(std::get<1>(top));
416
47
                base_it = std::get<2>(top);
417
418
47
                back_stack.pop();
419
420
47
                if (events_it != events_end) return true;
421
47
            }
422
423
47
            return false;
424
47
        };
425
426
47
        size_t i = 0;
427
148
        while (action_it != action_end && events_it != events_end) {
428
101
            if (action_it->type == PatternActionType::SpecificEvent) {
429
54
                if (events_it->second.test(action_it->extra)) {
430
                    /// move to the next action and events
431
54
                    base_it = events_it;
432
54
                    ++action_it, ++events_it;
433
54
                } else if (!do_backtrack())
434
                    /// backtracking failed, bail out
435
0
                    break;
436
54
            } else if (action_it->type == PatternActionType::AnyEvent) {
437
0
                base_it = events_it;
438
0
                ++action_it, ++events_it;
439
47
            } else if (action_it->type == PatternActionType::KleeneStar) {
440
47
                back_stack.emplace(action_it, events_it, base_it);
441
47
                base_it = events_it;
442
47
                ++action_it;
443
47
            } else if (action_it->type == PatternActionType::TimeLessOrEqual) {
444
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) <= action_it->extra) {
445
                    /// condition satisfied, move onto next action
446
0
                    back_stack.emplace(action_it, events_it, base_it);
447
0
                    base_it = events_it;
448
0
                    ++action_it;
449
0
                } else if (!do_backtrack())
450
0
                    break;
451
0
            } else if (action_it->type == PatternActionType::TimeLess) {
452
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) < action_it->extra) {
453
0
                    back_stack.emplace(action_it, events_it, base_it);
454
0
                    base_it = events_it;
455
0
                    ++action_it;
456
0
                } else if (!do_backtrack())
457
0
                    break;
458
0
            } else if (action_it->type == PatternActionType::TimeGreaterOrEqual) {
459
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) >= action_it->extra) {
460
0
                    back_stack.emplace(action_it, events_it, base_it);
461
0
                    base_it = events_it;
462
0
                    ++action_it;
463
0
                } else if (++events_it == events_end && !do_backtrack())
464
0
                    break;
465
0
            } else if (action_it->type == PatternActionType::TimeGreater) {
466
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) > action_it->extra) {
467
0
                    back_stack.emplace(action_it, events_it, base_it);
468
0
                    base_it = events_it;
469
0
                    ++action_it;
470
0
                } else if (++events_it == events_end && !do_backtrack())
471
0
                    break;
472
0
            } else if (action_it->type == PatternActionType::TimeEqual) {
473
0
                if (events_it->first.datetime_diff_in_seconds(base_it->first) == action_it->extra) {
474
0
                    back_stack.emplace(action_it, events_it, base_it);
475
0
                    base_it = events_it;
476
0
                    ++action_it;
477
0
                } else if (++events_it == events_end && !do_backtrack())
478
0
                    break;
479
0
            } else {
480
0
                LOG(WARNING) << "Unknown PatternActionType";
481
0
                return false;
482
0
            }
483
484
101
            if (++i > sequence_match_max_iterations) {
485
0
                LOG(WARNING)
486
0
                        << "Pattern application proves too difficult, exceeding max iterations (" +
487
0
                                   std::to_string(sequence_match_max_iterations) + ")";
488
0
                return false;
489
0
            }
490
101
        }
491
492
        /// if there are some actions remaining
493
47
        if (action_it != action_end) {
494
            /// match multiple empty strings at end
495
0
            while (action_it->type == PatternActionType::KleeneStar ||
496
0
                   action_it->type == PatternActionType::TimeLessOrEqual ||
497
0
                   action_it->type == PatternActionType::TimeLess ||
498
0
                   (action_it->type == PatternActionType::TimeGreaterOrEqual &&
499
0
                    action_it->extra == 0))
500
0
                ++action_it;
501
0
        }
502
503
47
        if (events_it == events_begin) ++events_it;
504
505
47
        return action_it == action_end;
506
47
    }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbRT_SL_
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE18backtracking_matchIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbRT_SJ_
507
508
    /// Splits the pattern into deterministic parts separated by non-deterministic fragments
509
    /// (time constraints and Kleene stars), and tries to match the deterministic parts in their specified order,
510
    /// ignoring the non-deterministic fragments.
511
    /// This function can quickly check that a full match is not possible if some deterministic fragment is missing.
512
    template <typename EventEntry>
513
    bool could_match_deterministic_parts(const EventEntry events_begin, const EventEntry events_end,
514
40
                                         bool limit_iterations = true) const {
515
40
        size_t events_processed = 0;
516
40
        auto events_it = events_begin;
517
518
40
        const auto actions_end = std::end(actions);
519
40
        auto actions_it = std::begin(actions);
520
40
        auto det_part_begin = actions_it;
521
522
40
        auto match_deterministic_part = [&events_it, events_end, &events_processed, det_part_begin,
523
82
                                         actions_it, limit_iterations]() {
524
82
            auto events_it_init = events_it;
525
82
            auto det_part_it = det_part_begin;
526
527
82
            while (det_part_it != actions_it && events_it != events_end) {
528
                /// matching any event
529
0
                if (det_part_it->type == PatternActionType::AnyEvent) ++events_it, ++det_part_it;
530
531
                /// matching specific event
532
0
                else {
533
0
                    if (events_it->second.test(det_part_it->extra)) ++events_it, ++det_part_it;
534
535
                    /// abandon current matching, try to match the deterministic fragment further in the list
536
0
                    else {
537
0
                        events_it = ++events_it_init;
538
0
                        det_part_it = det_part_begin;
539
0
                    }
540
0
                }
541
542
0
                if (limit_iterations && ++events_processed > sequence_match_max_iterations) {
543
0
                    LOG(WARNING) << "Pattern application proves too difficult, exceeding max "
544
0
                                    "iterations are " +
545
0
                                            std::to_string(sequence_match_max_iterations);
546
0
                    return false;
547
0
                }
548
0
            }
549
550
82
            return det_part_it == actions_it;
551
82
        };
_ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_bENKUlvE_clEv
Line
Count
Source
523
6
                                         actions_it, limit_iterations]() {
524
6
            auto events_it_init = events_it;
525
6
            auto det_part_it = det_part_begin;
526
527
6
            while (det_part_it != actions_it && events_it != events_end) {
528
                /// matching any event
529
0
                if (det_part_it->type == PatternActionType::AnyEvent) ++events_it, ++det_part_it;
530
531
                /// matching specific event
532
0
                else {
533
0
                    if (events_it->second.test(det_part_it->extra)) ++events_it, ++det_part_it;
534
535
                    /// abandon current matching, try to match the deterministic fragment further in the list
536
0
                    else {
537
0
                        events_it = ++events_it_init;
538
0
                        det_part_it = det_part_begin;
539
0
                    }
540
0
                }
541
542
0
                if (limit_iterations && ++events_processed > sequence_match_max_iterations) {
543
0
                    LOG(WARNING) << "Pattern application proves too difficult, exceeding max "
544
0
                                    "iterations are " +
545
0
                                            std::to_string(sequence_match_max_iterations);
546
0
                    return false;
547
0
                }
548
0
            }
549
550
6
            return det_part_it == actions_it;
551
6
        };
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_bENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_bENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_bENKUlvE_clEv
_ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_bENKUlvE_clEv
Line
Count
Source
523
76
                                         actions_it, limit_iterations]() {
524
76
            auto events_it_init = events_it;
525
76
            auto det_part_it = det_part_begin;
526
527
76
            while (det_part_it != actions_it && events_it != events_end) {
528
                /// matching any event
529
0
                if (det_part_it->type == PatternActionType::AnyEvent) ++events_it, ++det_part_it;
530
531
                /// matching specific event
532
0
                else {
533
0
                    if (events_it->second.test(det_part_it->extra)) ++events_it, ++det_part_it;
534
535
                    /// abandon current matching, try to match the deterministic fragment further in the list
536
0
                    else {
537
0
                        events_it = ++events_it_init;
538
0
                        det_part_it = det_part_begin;
539
0
                    }
540
0
                }
541
542
0
                if (limit_iterations && ++events_processed > sequence_match_max_iterations) {
543
0
                    LOG(WARNING) << "Pattern application proves too difficult, exceeding max "
544
0
                                    "iterations are " +
545
0
                                            std::to_string(sequence_match_max_iterations);
546
0
                    return false;
547
0
                }
548
0
            }
549
550
76
            return det_part_it == actions_it;
551
76
        };
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_bENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_bENKUlvE_clEv
Unexecuted instantiation: _ZZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_bENKUlvE_clEv
552
553
128
        for (; actions_it != actions_end; ++actions_it)
554
88
            if (actions_it->type != PatternActionType::SpecificEvent &&
555
88
                actions_it->type != PatternActionType::AnyEvent) {
556
42
                if (!match_deterministic_part()) return false;
557
42
                det_part_begin = std::next(actions_it);
558
42
            }
559
560
40
        return match_deterministic_part();
561
40
    }
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_b
Line
Count
Source
514
2
                                         bool limit_iterations = true) const {
515
2
        size_t events_processed = 0;
516
2
        auto events_it = events_begin;
517
518
2
        const auto actions_end = std::end(actions);
519
2
        auto actions_it = std::begin(actions);
520
2
        auto det_part_begin = actions_it;
521
522
2
        auto match_deterministic_part = [&events_it, events_end, &events_processed, det_part_begin,
523
2
                                         actions_it, limit_iterations]() {
524
2
            auto events_it_init = events_it;
525
2
            auto det_part_it = det_part_begin;
526
527
2
            while (det_part_it != actions_it && events_it != events_end) {
528
                /// matching any event
529
2
                if (det_part_it->type == PatternActionType::AnyEvent) ++events_it, ++det_part_it;
530
531
                /// matching specific event
532
2
                else {
533
2
                    if (events_it->second.test(det_part_it->extra)) ++events_it, ++det_part_it;
534
535
                    /// abandon current matching, try to match the deterministic fragment further in the list
536
2
                    else {
537
2
                        events_it = ++events_it_init;
538
2
                        det_part_it = det_part_begin;
539
2
                    }
540
2
                }
541
542
2
                if (limit_iterations && ++events_processed > sequence_match_max_iterations) {
543
2
                    LOG(WARNING) << "Pattern application proves too difficult, exceeding max "
544
2
                                    "iterations are " +
545
2
                                            std::to_string(sequence_match_max_iterations);
546
2
                    return false;
547
2
                }
548
2
            }
549
550
2
            return det_part_it == actions_it;
551
2
        };
552
553
10
        for (; actions_it != actions_end; ++actions_it)
554
8
            if (actions_it->type != PatternActionType::SpecificEvent &&
555
8
                actions_it->type != PatternActionType::AnyEvent) {
556
4
                if (!match_deterministic_part()) return false;
557
4
                det_part_begin = std::next(actions_it);
558
4
            }
559
560
2
        return match_deterministic_part();
561
2
    }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_b
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_b
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_b
_ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_b
Line
Count
Source
514
38
                                         bool limit_iterations = true) const {
515
38
        size_t events_processed = 0;
516
38
        auto events_it = events_begin;
517
518
38
        const auto actions_end = std::end(actions);
519
38
        auto actions_it = std::begin(actions);
520
38
        auto det_part_begin = actions_it;
521
522
38
        auto match_deterministic_part = [&events_it, events_end, &events_processed, det_part_begin,
523
38
                                         actions_it, limit_iterations]() {
524
38
            auto events_it_init = events_it;
525
38
            auto det_part_it = det_part_begin;
526
527
38
            while (det_part_it != actions_it && events_it != events_end) {
528
                /// matching any event
529
38
                if (det_part_it->type == PatternActionType::AnyEvent) ++events_it, ++det_part_it;
530
531
                /// matching specific event
532
38
                else {
533
38
                    if (events_it->second.test(det_part_it->extra)) ++events_it, ++det_part_it;
534
535
                    /// abandon current matching, try to match the deterministic fragment further in the list
536
38
                    else {
537
38
                        events_it = ++events_it_init;
538
38
                        det_part_it = det_part_begin;
539
38
                    }
540
38
                }
541
542
38
                if (limit_iterations && ++events_processed > sequence_match_max_iterations) {
543
38
                    LOG(WARNING) << "Pattern application proves too difficult, exceeding max "
544
38
                                    "iterations are " +
545
38
                                            std::to_string(sequence_match_max_iterations);
546
38
                    return false;
547
38
                }
548
38
            }
549
550
38
            return det_part_it == actions_it;
551
38
        };
552
553
118
        for (; actions_it != actions_end; ++actions_it)
554
80
            if (actions_it->type != PatternActionType::SpecificEvent &&
555
80
                actions_it->type != PatternActionType::AnyEvent) {
556
38
                if (!match_deterministic_part()) return false;
557
38
                det_part_begin = std::next(actions_it);
558
38
            }
559
560
38
        return match_deterministic_part();
561
38
    }
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimeStampNsValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_b
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_11DateV2ValueINS_15DateV2ValueTypeEEESt6bitsetILm32EEESt6vectorISE_SaISE_EEEEEEbT_SL_b
Unexecuted instantiation: _ZNK5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE31could_match_deterministic_partsIN9__gnu_cxx17__normal_iteratorIPKSt4pairINS_16TimestampTzValueESt6bitsetILm32EEESt6vectorISC_SaISC_EEEEEEbT_SJ_b
562
563
private:
564
    enum class DFATransition : char {
565
        ///   .-------.
566
        ///   |       |
567
        ///   `-------'
568
        None,
569
        ///   .-------.  (?[0-9])
570
        ///   |       | ----------
571
        ///   `-------'
572
        SpecificEvent,
573
        ///   .-------.      .
574
        ///   |       | ----------
575
        ///   `-------'
576
        AnyEvent,
577
    };
578
579
    struct DFAState {
580
        explicit DFAState(bool has_kleene_ = false)
581
776
                : has_kleene {has_kleene_}, event {0}, transition {DFATransition::None} {}
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE8DFAStateC2Eb
Line
Count
Source
581
392
                : has_kleene {has_kleene_}, event {0}, transition {DFATransition::None} {}
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE8DFAStateC2Eb
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE8DFAStateC2Eb
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE8DFAStateC2Eb
_ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE8DFAStateC2Eb
Line
Count
Source
581
384
                : has_kleene {has_kleene_}, event {0}, transition {DFATransition::None} {}
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE8DFAStateC2Eb
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE8DFAStateC2Eb
Unexecuted instantiation: _ZN5doris34AggregateFunctionSequenceMatchDataILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE8DFAStateC2Eb
582
583
        ///   .-------.
584
        ///   |       | - - -
585
        ///   `-------'
586
        ///     |_^
587
        bool has_kleene;
588
        /// In the case of a state transitions with a `SpecificEvent`,
589
        /// `event` contains the value of the event.
590
        uint32_t event;
591
        /// The kind of transition out of this state.
592
        DFATransition transition;
593
    };
594
595
    using DFAStates = std::vector<DFAState>;
596
597
public:
598
    bool sorted = true;
599
    std::vector<TimestampEvents> events_list;
600
    // sequenceMatch conditions met at least once in events_list
601
    std::bitset<MAX_EVENTS> conditions_met;
602
    // sequenceMatch conditions met at least once in the pattern
603
    std::bitset<MAX_EVENTS> conditions_in_pattern;
604
    // `True` if the parsed pattern contains time assertions (?t...), `false` otherwise.
605
    bool pattern_has_time;
606
607
private:
608
    std::string pattern;
609
    size_t arg_count;
610
    bool init_flag = false;
611
612
    PatternActions actions;
613
    DFAStates dfa_states;
614
};
615
616
template <PrimitiveType T, typename Derived>
617
class AggregateFunctionSequenceBase
618
        : public IAggregateFunctionDataHelper<AggregateFunctionSequenceMatchData<T, Derived>,
619
                                              Derived> {
620
public:
621
    AggregateFunctionSequenceBase(const DataTypes& arguments)
622
43
            : IAggregateFunctionDataHelper<AggregateFunctionSequenceMatchData<T, Derived>, Derived>(
623
43
                      arguments) {
624
43
        arg_count = arguments.size();
625
43
    }
_ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Line
Count
Source
622
20
            : IAggregateFunctionDataHelper<AggregateFunctionSequenceMatchData<T, Derived>, Derived>(
623
20
                      arguments) {
624
20
        arg_count = arguments.size();
625
20
    }
_ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Line
Count
Source
622
1
            : IAggregateFunctionDataHelper<AggregateFunctionSequenceMatchData<T, Derived>, Derived>(
623
1
                      arguments) {
624
1
        arg_count = arguments.size();
625
1
    }
Unexecuted instantiation: _ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Unexecuted instantiation: _ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
_ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Line
Count
Source
622
21
            : IAggregateFunctionDataHelper<AggregateFunctionSequenceMatchData<T, Derived>, Derived>(
623
21
                      arguments) {
624
21
        arg_count = arguments.size();
625
21
    }
_ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Line
Count
Source
622
1
            : IAggregateFunctionDataHelper<AggregateFunctionSequenceMatchData<T, Derived>, Derived>(
623
1
                      arguments) {
624
1
        arg_count = arguments.size();
625
1
    }
Unexecuted instantiation: _ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
Unexecuted instantiation: _ZN5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS9_EE
626
627
8
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE5resetEPc
Line
Count
Source
627
4
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE5resetEPc
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE5resetEPc
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE5resetEPc
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE5resetEPc
Line
Count
Source
627
4
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE5resetEPc
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE5resetEPc
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE5resetEPc
628
629
    void add(AggregateDataPtr __restrict place, const IColumn** columns, const ssize_t row_num,
630
332
             Arena&) const override {
631
332
        std::string pattern =
632
332
                assert_cast<const ColumnString*, TypeCheckOnRelease::DISABLE>(columns[0])
633
332
                        ->get_data_at(0)
634
332
                        .to_string();
635
332
        this->data(place).init(pattern, arg_count);
636
637
332
        const auto& timestamp = assert_cast<const typename PrimitiveTypeTraits<T>::ColumnType&,
638
332
                                            TypeCheckOnRelease::DISABLE>(*columns[1])
639
332
                                        .get_data()[row_num];
640
332
        typename AggregateFunctionSequenceMatchData<T, Derived>::Events events;
641
642
993
        for (auto i = 2; i < arg_count; i++) {
643
661
            const auto event =
644
661
                    assert_cast<const ColumnUInt8*, TypeCheckOnRelease::DISABLE>(columns[i])
645
661
                            ->get_data()[row_num];
646
661
            events.set(i - 2, event);
647
661
        }
648
649
332
        this->data(place).add(timestamp, events);
650
332
    }
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Line
Count
Source
630
168
             Arena&) const override {
631
168
        std::string pattern =
632
168
                assert_cast<const ColumnString*, TypeCheckOnRelease::DISABLE>(columns[0])
633
168
                        ->get_data_at(0)
634
168
                        .to_string();
635
168
        this->data(place).init(pattern, arg_count);
636
637
168
        const auto& timestamp = assert_cast<const typename PrimitiveTypeTraits<T>::ColumnType&,
638
168
                                            TypeCheckOnRelease::DISABLE>(*columns[1])
639
168
                                        .get_data()[row_num];
640
168
        typename AggregateFunctionSequenceMatchData<T, Derived>::Events events;
641
642
505
        for (auto i = 2; i < arg_count; i++) {
643
337
            const auto event =
644
337
                    assert_cast<const ColumnUInt8*, TypeCheckOnRelease::DISABLE>(columns[i])
645
337
                            ->get_data()[row_num];
646
337
            events.set(i - 2, event);
647
337
        }
648
649
168
        this->data(place).add(timestamp, events);
650
168
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Line
Count
Source
630
164
             Arena&) const override {
631
164
        std::string pattern =
632
164
                assert_cast<const ColumnString*, TypeCheckOnRelease::DISABLE>(columns[0])
633
164
                        ->get_data_at(0)
634
164
                        .to_string();
635
164
        this->data(place).init(pattern, arg_count);
636
637
164
        const auto& timestamp = assert_cast<const typename PrimitiveTypeTraits<T>::ColumnType&,
638
164
                                            TypeCheckOnRelease::DISABLE>(*columns[1])
639
164
                                        .get_data()[row_num];
640
164
        typename AggregateFunctionSequenceMatchData<T, Derived>::Events events;
641
642
488
        for (auto i = 2; i < arg_count; i++) {
643
324
            const auto event =
644
324
                    assert_cast<const ColumnUInt8*, TypeCheckOnRelease::DISABLE>(columns[i])
645
324
                            ->get_data()[row_num];
646
324
            events.set(i - 2, event);
647
324
        }
648
649
164
        this->data(place).add(timestamp, events);
650
164
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
651
652
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
653
160
               Arena&) const override {
654
160
        this->data(place).merge(this->data(rhs));
655
160
    }
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE5mergeEPcPKcRNS_5ArenaE
Line
Count
Source
653
80
               Arena&) const override {
654
80
        this->data(place).merge(this->data(rhs));
655
80
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE5mergeEPcPKcRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE5mergeEPcPKcRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE5mergeEPcPKcRNS_5ArenaE
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE5mergeEPcPKcRNS_5ArenaE
Line
Count
Source
653
80
               Arena&) const override {
654
80
        this->data(place).merge(this->data(rhs));
655
80
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE5mergeEPcPKcRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE5mergeEPcPKcRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE5mergeEPcPKcRNS_5ArenaE
656
657
77
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
658
77
        this->data(place).write(buf);
659
77
    }
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE9serializeEPKcRNS_14BufferWritableE
Line
Count
Source
657
39
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
658
39
        this->data(place).write(buf);
659
39
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE9serializeEPKcRNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE9serializeEPKcRNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE9serializeEPKcRNS_14BufferWritableE
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE9serializeEPKcRNS_14BufferWritableE
Line
Count
Source
657
38
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
658
38
        this->data(place).write(buf);
659
38
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE9serializeEPKcRNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE9serializeEPKcRNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE9serializeEPKcRNS_14BufferWritableE
660
661
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
662
85
                     Arena&) const override {
663
85
        auto& state = AggregateFunctionSequenceBase::data(place);
664
85
        state.read(buf);
665
        // A serialized uninitialized state has no arguments and must stay uninitialized.
666
85
        if (state.get_arg_count() == 0) {
667
24
            return;
668
24
        }
669
61
        state.init(state.get_pattern(), state.get_arg_count());
670
61
    }
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Line
Count
Source
662
43
                     Arena&) const override {
663
43
        auto& state = AggregateFunctionSequenceBase::data(place);
664
43
        state.read(buf);
665
        // A serialized uninitialized state has no arguments and must stay uninitialized.
666
43
        if (state.get_arg_count() == 0) {
667
12
            return;
668
12
        }
669
31
        state.init(state.get_pattern(), state.get_arg_count());
670
31
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Line
Count
Source
662
42
                     Arena&) const override {
663
42
        auto& state = AggregateFunctionSequenceBase::data(place);
664
42
        state.read(buf);
665
        // A serialized uninitialized state has no arguments and must stay uninitialized.
666
42
        if (state.get_arg_count() == 0) {
667
12
            return;
668
12
        }
669
30
        state.init(state.get_pattern(), state.get_arg_count());
670
30
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
671
672
0
    void check_input_columns_type(const IColumn** columns) const override {
673
0
        this->template check_argument_column_type<ColumnString>(columns[0]);
674
0
        this->template check_argument_column_type<typename PrimitiveTypeTraits<T>::ColumnType>(
675
0
                columns[1]);
676
0
        for (size_t i = 2; i < arg_count; ++i) {
677
0
            this->template check_argument_column_type<ColumnUInt8>(columns[i]);
678
0
        }
679
0
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE24check_input_columns_typeEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE24check_input_columns_typeEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE24check_input_columns_typeEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE24check_input_columns_typeEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE24check_input_columns_typeEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE24check_input_columns_typeEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE24check_input_columns_typeEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE24check_input_columns_typeEPPKNS_7IColumnE
680
681
    // the pattern is a constant checked in FE: either a literal or a value only BE can evaluate.
682
    // A literal NULL pattern is rejected by FE; reject a BE-evaluated NULL here too, and validate
683
    // the pattern syntax eagerly. Both checks are normally done lazily in add(), but the nullable
684
    // wrapper above can skip every row (and so skip add() entirely) when any argument, including
685
    // a non-constant one such as the timestamp, is NULL for every row.
686
4
    void check_nullable_input_columns(const IColumn** columns) const {
687
4
        if (columns[0]->empty()) {
688
0
            return;
689
0
        }
690
4
        if (columns[0]->is_null_at(0)) {
691
2
            throw Exception(ErrorCode::INVALID_ARGUMENT,
692
2
                            "the pattern argument of {} must not be null", this->get_name());
693
2
        }
694
2
        std::string pattern = columns[0]->get_data_at(0).to_string();
695
2
        AggregateFunctionSequenceMatchData<T, Derived> validator;
696
2
        validator.init(pattern, arg_count);
697
2
    }
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceMatchILS1_26EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
Line
Count
Source
686
2
    void check_nullable_input_columns(const IColumn** columns) const {
687
2
        if (columns[0]->empty()) {
688
0
            return;
689
0
        }
690
2
        if (columns[0]->is_null_at(0)) {
691
1
            throw Exception(ErrorCode::INVALID_ARGUMENT,
692
1
                            "the pattern argument of {} must not be null", this->get_name());
693
1
        }
694
1
        std::string pattern = columns[0]->get_data_at(0).to_string();
695
1
        AggregateFunctionSequenceMatchData<T, Derived> validator;
696
1
        validator.init(pattern, arg_count);
697
1
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceMatchILS1_43EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceMatchILS1_25EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceMatchILS1_42EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
_ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE26ENS_30AggregateFunctionSequenceCountILS1_26EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
Line
Count
Source
686
2
    void check_nullable_input_columns(const IColumn** columns) const {
687
2
        if (columns[0]->empty()) {
688
0
            return;
689
0
        }
690
2
        if (columns[0]->is_null_at(0)) {
691
1
            throw Exception(ErrorCode::INVALID_ARGUMENT,
692
1
                            "the pattern argument of {} must not be null", this->get_name());
693
1
        }
694
1
        std::string pattern = columns[0]->get_data_at(0).to_string();
695
1
        AggregateFunctionSequenceMatchData<T, Derived> validator;
696
1
        validator.init(pattern, arg_count);
697
1
    }
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE43ENS_30AggregateFunctionSequenceCountILS1_43EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE25ENS_30AggregateFunctionSequenceCountILS1_25EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
Unexecuted instantiation: _ZNK5doris29AggregateFunctionSequenceBaseILNS_13PrimitiveTypeE42ENS_30AggregateFunctionSequenceCountILS1_42EEEE28check_nullable_input_columnsEPPKNS_7IColumnE
698
699
private:
700
    size_t arg_count;
701
};
702
703
template <PrimitiveType T>
704
class AggregateFunctionSequenceMatch final
705
        : public AggregateFunctionSequenceBase<T, AggregateFunctionSequenceMatch<T>>,
706
          VarargsExpression,
707
          NullableAggregateFunction {
708
public:
709
    AggregateFunctionSequenceMatch(const DataTypes& arguments, const String& pattern_)
710
            : AggregateFunctionSequenceBase<T, AggregateFunctionSequenceMatch<T>>(arguments,
711
                                                                                  pattern_) {}
712
713
    using AggregateFunctionSequenceBase<
714
            T, AggregateFunctionSequenceMatch<T>>::AggregateFunctionSequenceBase;
715
716
1
    String get_name() const override { return "sequence_match"; }
_ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE26EE8get_nameB5cxx11Ev
Line
Count
Source
716
1
    String get_name() const override { return "sequence_match"; }
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE43EE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE25EE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE42EE8get_nameB5cxx11Ev
717
718
104
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeUInt8>(); }
_ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE26EE15get_return_typeEv
Line
Count
Source
718
104
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeUInt8>(); }
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE43EE15get_return_typeEv
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE25EE15get_return_typeEv
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE42EE15get_return_typeEv
719
720
112
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
721
112
        auto& output = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(to).get_data();
722
112
        if (!this->data(place).conditions_in_pattern.any()) {
723
13
            output.push_back(false);
724
13
            return;
725
13
        }
726
727
99
        if ((this->data(place).conditions_in_pattern & this->data(place).conditions_met) !=
728
99
            this->data(place).conditions_in_pattern) {
729
60
            output.push_back(false);
730
60
            return;
731
60
        }
732
        // place is essentially an AggregateDataPtr, passed as a ConstAggregateDataPtr.
733
39
        this->data(const_cast<AggregateDataPtr>(place)).sort();
734
735
39
        const auto& data_ref = this->data(place);
736
737
39
        const auto events_begin = std::begin(data_ref.events_list);
738
39
        const auto events_end = std::end(data_ref.events_list);
739
39
        auto events_it = events_begin;
740
741
39
        bool match = (this->data(place).pattern_has_time
742
39
                              ? (this->data(place).could_match_deterministic_parts(events_begin,
743
2
                                                                                   events_end) &&
744
2
                                 this->data(place).backtracking_match(events_it, events_end))
745
39
                              : this->data(place).dfa_match(events_it, events_end));
746
39
        output.push_back(match);
747
39
    }
_ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE26EE18insert_result_intoEPKcRNS_7IColumnE
Line
Count
Source
720
112
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
721
112
        auto& output = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(to).get_data();
722
112
        if (!this->data(place).conditions_in_pattern.any()) {
723
13
            output.push_back(false);
724
13
            return;
725
13
        }
726
727
99
        if ((this->data(place).conditions_in_pattern & this->data(place).conditions_met) !=
728
99
            this->data(place).conditions_in_pattern) {
729
60
            output.push_back(false);
730
60
            return;
731
60
        }
732
        // place is essentially an AggregateDataPtr, passed as a ConstAggregateDataPtr.
733
39
        this->data(const_cast<AggregateDataPtr>(place)).sort();
734
735
39
        const auto& data_ref = this->data(place);
736
737
39
        const auto events_begin = std::begin(data_ref.events_list);
738
39
        const auto events_end = std::end(data_ref.events_list);
739
39
        auto events_it = events_begin;
740
741
39
        bool match = (this->data(place).pattern_has_time
742
39
                              ? (this->data(place).could_match_deterministic_parts(events_begin,
743
2
                                                                                   events_end) &&
744
2
                                 this->data(place).backtracking_match(events_it, events_end))
745
39
                              : this->data(place).dfa_match(events_it, events_end));
746
39
        output.push_back(match);
747
39
    }
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE43EE18insert_result_intoEPKcRNS_7IColumnE
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE25EE18insert_result_intoEPKcRNS_7IColumnE
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceMatchILNS_13PrimitiveTypeE42EE18insert_result_intoEPKcRNS_7IColumnE
748
};
749
750
template <PrimitiveType T>
751
class AggregateFunctionSequenceCount final
752
        : public AggregateFunctionSequenceBase<T, AggregateFunctionSequenceCount<T>>,
753
          VarargsExpression,
754
          NotNullableAggregateFunction {
755
public:
756
    AggregateFunctionSequenceCount(const DataTypes& arguments, const String& pattern_)
757
            : AggregateFunctionSequenceBase<T, AggregateFunctionSequenceCount<T>>(arguments,
758
                                                                                  pattern_) {}
759
760
    using AggregateFunctionSequenceBase<
761
            T, AggregateFunctionSequenceCount<T>>::AggregateFunctionSequenceBase;
762
763
1
    String get_name() const override { return "sequence_count"; }
_ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE26EE8get_nameB5cxx11Ev
Line
Count
Source
763
1
    String get_name() const override { return "sequence_count"; }
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE43EE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE25EE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE42EE8get_nameB5cxx11Ev
764
765
104
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); }
_ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE26EE15get_return_typeEv
Line
Count
Source
765
104
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeInt64>(); }
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE43EE15get_return_typeEv
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE25EE15get_return_typeEv
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE42EE15get_return_typeEv
766
767
110
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
768
110
        auto& output = assert_cast<ColumnInt64&, TypeCheckOnRelease::DISABLE>(to).get_data();
769
110
        if (!this->data(place).conditions_in_pattern.any()) {
770
13
            output.push_back(0);
771
13
            return;
772
13
        }
773
774
97
        if ((this->data(place).conditions_in_pattern & this->data(place).conditions_met) !=
775
97
            this->data(place).conditions_in_pattern) {
776
59
            output.push_back(0);
777
59
            return;
778
59
        }
779
        // place is essentially an AggregateDataPtr, passed as a ConstAggregateDataPtr.
780
38
        this->data(const_cast<AggregateDataPtr>(place)).sort();
781
38
        output.push_back(count(place));
782
38
    }
_ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE26EE18insert_result_intoEPKcRNS_7IColumnE
Line
Count
Source
767
110
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
768
110
        auto& output = assert_cast<ColumnInt64&, TypeCheckOnRelease::DISABLE>(to).get_data();
769
110
        if (!this->data(place).conditions_in_pattern.any()) {
770
13
            output.push_back(0);
771
13
            return;
772
13
        }
773
774
97
        if ((this->data(place).conditions_in_pattern & this->data(place).conditions_met) !=
775
97
            this->data(place).conditions_in_pattern) {
776
59
            output.push_back(0);
777
59
            return;
778
59
        }
779
        // place is essentially an AggregateDataPtr, passed as a ConstAggregateDataPtr.
780
38
        this->data(const_cast<AggregateDataPtr>(place)).sort();
781
38
        output.push_back(count(place));
782
38
    }
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE43EE18insert_result_intoEPKcRNS_7IColumnE
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE25EE18insert_result_intoEPKcRNS_7IColumnE
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE42EE18insert_result_intoEPKcRNS_7IColumnE
783
784
private:
785
38
    UInt64 count(ConstAggregateDataPtr __restrict place) const {
786
38
        const auto& data_ref = this->data(place);
787
788
38
        const auto events_begin = std::begin(data_ref.events_list);
789
38
        const auto events_end = std::end(data_ref.events_list);
790
38
        auto events_it = events_begin;
791
792
38
        size_t count = 0;
793
        // check if there is a chance of matching the sequence at least once
794
38
        if (this->data(place).could_match_deterministic_parts(events_begin, events_end)) {
795
85
            while (events_it != events_end &&
796
85
                   this->data(place).backtracking_match(events_it, events_end))
797
47
                ++count;
798
38
        }
799
800
38
        return count;
801
38
    }
_ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE26EE5countEPKc
Line
Count
Source
785
38
    UInt64 count(ConstAggregateDataPtr __restrict place) const {
786
38
        const auto& data_ref = this->data(place);
787
788
38
        const auto events_begin = std::begin(data_ref.events_list);
789
38
        const auto events_end = std::end(data_ref.events_list);
790
38
        auto events_it = events_begin;
791
792
38
        size_t count = 0;
793
        // check if there is a chance of matching the sequence at least once
794
38
        if (this->data(place).could_match_deterministic_parts(events_begin, events_end)) {
795
85
            while (events_it != events_end &&
796
85
                   this->data(place).backtracking_match(events_it, events_end))
797
47
                ++count;
798
38
        }
799
800
38
        return count;
801
38
    }
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE43EE5countEPKc
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE25EE5countEPKc
Unexecuted instantiation: _ZNK5doris30AggregateFunctionSequenceCountILNS_13PrimitiveTypeE42EE5countEPKc
802
};
803
804
} // namespace doris