1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Filtering types to select records from the database
//!
//! The creation of the types should be done with
//! [ScalarCompare](/query-connector/trait.ScalarCompare.html) and
//! [RelationCompare](/query-connector/trait.RelationCompare.html).
//! [CompositeCompare](/query-connector/trait.RelationCompare.html).

mod compare;
mod composite;
mod into_filter;
mod json;
mod list;
mod relation;
mod scalar;

pub use compare::*;
pub use composite::*;
pub use into_filter::*;
pub use json::*;
pub use list::*;
pub use relation::*;
pub use scalar::*;

use crate::ScalarFieldRef;

#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum Filter {
    And(Vec<Filter>),
    Or(Vec<Filter>),
    Not(Vec<Filter>),
    Scalar(ScalarFilter),
    ScalarList(ScalarListFilter),
    OneRelationIsNull(OneRelationIsNullFilter),
    Relation(RelationFilter),
    Composite(CompositeFilter),
    BoolFilter(bool),
    Aggregation(AggregationFilter),
    Empty,
}

#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum AggregationFilter {
    Count(Box<Filter>),
    Average(Box<Filter>),
    Sum(Box<Filter>),
    Min(Box<Filter>),
    Max(Box<Filter>),
}

impl AggregationFilter {
    pub fn filter(&self) -> &Filter {
        match self {
            AggregationFilter::Count(f) => f,
            AggregationFilter::Average(f) => f,
            AggregationFilter::Sum(f) => f,
            AggregationFilter::Min(f) => f,
            AggregationFilter::Max(f) => f,
        }
    }
}

impl Filter {
    pub fn and(filters: Vec<Filter>) -> Self {
        Filter::And(filters)
    }

    pub fn or(filters: Vec<Filter>) -> Self {
        Filter::Or(filters)
    }

    pub fn not(filters: Vec<Filter>) -> Self {
        Filter::Not(filters)
    }

    pub fn empty() -> Self {
        Filter::Empty
    }

    /// Returns the size of the topmost filter elements (does not recursively compute the size).
    pub fn size(&self) -> usize {
        match self {
            Self::And(v) => v.len(),
            Self::Or(v) => v.len(),
            Self::Not(v) => v.len(),
            Self::Empty => 0,
            _ => 1,
        }
    }

    pub fn should_batch(&self, chunk_size: usize) -> bool {
        match self {
            Self::Scalar(sf) => sf.should_batch(chunk_size),
            Self::And(filters) => filters.iter().any(|f| f.should_batch(chunk_size)),
            Self::Or(filters) => filters.iter().any(|f| f.should_batch(chunk_size)),
            _ => false,
        }
    }

    pub fn can_batch(&self) -> bool {
        match self {
            Self::Scalar(sf) => sf.can_batch(),
            Self::And(filters) => filters.iter().all(|f| f.can_batch()),
            Self::Or(filters) => filters.iter().all(|f| f.can_batch()),
            _ => true,
        }
    }

    pub fn batched(self, chunk_size: usize) -> Vec<Filter> {
        fn split_longest(mut filters: Vec<Filter>, chunk_size: usize) -> (Option<ScalarFilter>, Vec<Filter>) {
            let mut longest: Option<ScalarFilter> = None;
            let mut other = Vec::with_capacity(filters.len());

            while let Some(filter) = filters.pop() {
                match (filter, longest.as_mut()) {
                    (Filter::Scalar(sf), Some(ref mut prev)) if sf.len() > prev.len() => {
                        let previous = longest.replace(sf);
                        other.push(Filter::Scalar(previous.unwrap()));
                    }
                    (Filter::Scalar(sf), None) if sf.should_batch(chunk_size) => {
                        longest = Some(sf);
                    }
                    (filter, _) => other.push(filter),
                }
            }

            (longest, other)
        }

        fn batch<F>(filters: Vec<Filter>, chunk_size: usize, f: F) -> Vec<Filter>
        where
            F: Fn(Vec<Filter>) -> Filter,
        {
            let (longest, other) = split_longest(filters, chunk_size);
            let mut batched = Vec::new();

            if let Some(filter) = longest {
                for filter in filter.batched(chunk_size) {
                    batched.push(Filter::Scalar(filter))
                }

                batched
                    .into_iter()
                    .map(|batch| {
                        let mut filters = other.clone();
                        filters.push(batch);

                        f(filters)
                    })
                    .collect()
            } else {
                vec![f(other)]
            }
        }

        match self {
            Self::Scalar(sf) => sf.batched(chunk_size).into_iter().map(Self::Scalar).collect(),
            Self::And(filters) => batch(filters, chunk_size, Filter::And),
            Self::Or(filters) => batch(filters, chunk_size, Filter::Or),
            _ => vec![self],
        }
    }

    pub fn set_mode(&mut self, mode: QueryMode) {
        match self {
            Filter::And(inner) => inner.iter_mut().for_each(|f| f.set_mode(mode.clone())),
            Filter::Or(inner) => inner.iter_mut().for_each(|f| f.set_mode(mode.clone())),
            Filter::Not(inner) => inner.iter_mut().for_each(|f| f.set_mode(mode.clone())),
            Filter::Scalar(sf) => sf.mode = mode,
            _ => {}
        }
    }

    pub fn count(condition: Filter) -> Self {
        Self::Aggregation(AggregationFilter::Count(Box::new(condition)))
    }

    pub fn average(condition: Filter) -> Self {
        Self::Aggregation(AggregationFilter::Average(Box::new(condition)))
    }

    pub fn sum(condition: Filter) -> Self {
        Self::Aggregation(AggregationFilter::Sum(Box::new(condition)))
    }

    pub fn min(condition: Filter) -> Self {
        Self::Aggregation(AggregationFilter::Min(Box::new(condition)))
    }

    pub fn max(condition: Filter) -> Self {
        Self::Aggregation(AggregationFilter::Max(Box::new(condition)))
    }

    pub fn as_scalar(&self) -> Option<&ScalarFilter> {
        if let Self::Scalar(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn into_scalar(self) -> Option<ScalarFilter> {
        if let Self::Scalar(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn is_empty(&self) -> bool {
        self == &Filter::Empty
    }

    pub fn scalars(&self) -> Vec<ScalarFieldRef> {
        let mut scalars: Vec<ScalarFieldRef> = Vec::new();

        let filter_check = |_sf: &ScalarFilter| true;
        Self::filter_and_collect_scalars(self, filter_check, &mut scalars);
        scalars
    }

    pub fn unique_scalars(&self) -> Vec<ScalarFieldRef> {
        let mut uniques: Vec<ScalarFieldRef> = Vec::new();

        let filter_check = |sf: &ScalarFilter| sf.is_unique();
        Self::filter_and_collect_scalars(self, filter_check, &mut uniques);
        uniques
    }

    /// Returns true if filter contains conditions on relation fields.
    pub fn has_relations(&self) -> bool {
        use AggregationFilter::*;
        use Filter::*;
        match self {
            Not(branches) | Or(branches) | And(branches) => branches.iter().any(|filter| filter.has_relations()),
            Scalar(..) | ScalarList(..) | Composite(..) | BoolFilter(..) | Empty => false,
            Aggregation(filter) => match filter {
                Average(filter) | Count(filter) | Sum(filter) | Min(filter) | Max(filter) => filter.has_relations(),
            },
            OneRelationIsNull(..) | Relation(..) => true,
        }
    }

    fn filter_and_collect_scalars(
        filter: &Filter,
        filter_check: fn(&ScalarFilter) -> bool,
        scalars: &mut Vec<ScalarFieldRef>,
    ) {
        match filter {
            Filter::And(inner) => inner
                .iter()
                .for_each(|f| Self::filter_and_collect_scalars(f, filter_check, scalars)),
            Filter::Scalar(sf) => {
                if filter_check(sf) {
                    if let Some(field) = sf.scalar_ref() {
                        scalars.push(field.to_owned())
                    }
                }
            }
            _ => (),
        }
    }
}

impl From<ScalarFilter> for Filter {
    fn from(sf: ScalarFilter) -> Self {
        Filter::Scalar(sf)
    }
}

impl From<ScalarListFilter> for Filter {
    fn from(sf: ScalarListFilter) -> Self {
        Filter::ScalarList(sf)
    }
}

impl From<OneRelationIsNullFilter> for Filter {
    fn from(sf: OneRelationIsNullFilter) -> Self {
        Filter::OneRelationIsNull(sf)
    }
}

impl From<RelationFilter> for Filter {
    fn from(sf: RelationFilter) -> Self {
        Filter::Relation(sf)
    }
}

impl From<bool> for Filter {
    fn from(b: bool) -> Self {
        Filter::BoolFilter(b)
    }
}

impl From<CompositeFilter> for Filter {
    fn from(cf: CompositeFilter) -> Self {
        Filter::Composite(cf)
    }
}