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
//! User-facing query representation.

use crate::document::Document;
use crate::index::IndexSchema;
use crate::value::Value;

use std::collections::HashMap;
use std::ops::Bound;

pub type FieldPath = Vec<String>;

/// The order that the results should be sorted in.
pub enum ResultOrder {
    /// Sorted ascending according to the given field.
    Asc(FieldPath),
    /// Sorted descending according to the given field.
    Desc(FieldPath),
}

/// Document type modeling query constraints.
pub type ConstraintDocument = HashMap<FieldPath, Constraint>;

/// Exists so that we can implement functions on ConstraintDocument.
pub trait ConstraintDocumentTrait {
    /// Constructs a reduced ConstraintDocument by removing the fields
    /// that are part of the IndexSchema.
    fn remove_index_fields(&self, index_schema: &IndexSchema) -> Self;

    /// Returns whether the current constraint document matches a document.
    fn matches_document(&self, doc: &Document) -> bool;
}

impl ConstraintDocumentTrait for ConstraintDocument {
    fn remove_index_fields(&self, _index_schema: &IndexSchema) -> ConstraintDocument {
        // For the time being, don't remove index fields, due to the issue with
        // inclusive/exclusive bounds
        self.clone()

        // let mut reduced_constraints = HashMap::new();

        // let constraints_set: HashSet<&FieldPath> = self.keys().collect();
        // let index_constraints_set: HashSet<&FieldPath> =
        //     HashSet::from_iter(index_schema.get_fields().iter().clone());
        // constraints_set
        //     .difference(&index_constraints_set)
        //     .for_each(|field_path| {
        //         match self.get(field_path.clone()) {
        //             None => panic!("missing field path"),
        //             Some(constraint) => {
        //                 reduced_constraints.insert(field_path.clone().clone(), constraint.clone())
        //             }
        //         };
        //     });
        // reduced_constraints
    }

    /// Check if a Document matches the constraints. If a Document is missing a Value,
    /// it automatically doesn't match the Constraint.
    fn matches_document(&self, doc: &Document) -> bool {
        for (path, constraint) in self.iter() {
            match doc.get(path) {
                Some(value) => {
                    if !constraint.matches(&value) {
                        return false;
                    }
                }
                None => return false,
            }
        }

        true
    }
}

/// A single query constraint on a field.
///
/// Note that Constraints applied to an array
/// value will map the constraint over the array.
#[derive(Clone, Debug)]
pub enum Constraint {
    /// Constraints on subdocuments (hashtables).
    MatchesDocument(ConstraintDocument),

    /// Equality constraint on a value.
    Equals(Value),

    /// Less-than constraint on a value.
    LessThan(Value),

    /// Greater-than constraint on a value.
    GreaterThan(Value),

    /// Constraint if value is in specified list of values.
    In(Vec<Value>),

    /// Disjunction of constraints on a single field.
    Or(Box<Constraint>, Box<Constraint>),

    /// Conjunction of constraints on a single field.
    And(Box<Constraint>, Box<Constraint>),
}

impl Constraint {
    /// Determine the type of a constraint.
    /// Return None if the Constraint is invalid due to mismatched subconstraints.
    pub fn get_value_type(&self) -> Option<Value> {
        match self {
            Self::Equals(value) | Self::LessThan(value) | Self::GreaterThan(value) => {
                Some(value.clone())
            }
            Self::Or(constraint1, constraint2) | Self::And(constraint1, constraint2) => {
                match (constraint1.get_value_type(), constraint2.get_value_type()) {
                    (None, _) | (_, None) => None,
                    (Some(value1), Some(value2)) => {
                        if value1.is_variant_equal(&value2) {
                            Some(value1)
                        } else {
                            None
                        }
                    }
                }
            }
            Self::In(values) => {
                for i in 1..values.len() {
                    if !values[i - 1].is_variant_equal(&values[i]) {
                        return None;
                    }
                }
                return Some(values[0].clone());
            }
            _ => panic!("currently unsupported constraint type"),
        }
    }

    /// Whether a Value matches a constraint.
    pub fn matches(&self, value: &Value) -> bool {
        match self {
            Constraint::MatchesDocument(constraint_doc) => match value {
                Value::Dict(doc) => constraint_doc.matches_document(doc),
                _ => false,
            },
            Constraint::Equals(value2) => value == value2,
            Constraint::LessThan(value2) => value < value2,
            Constraint::GreaterThan(value2) => value > value2,
            Constraint::And(constraint1, constraint2) => {
                constraint1.matches(value) && constraint2.matches(value)
            }
            Constraint::Or(constraint1, constraint2) => {
                constraint1.matches(value) || constraint2.matches(value)
            }
            Constraint::In(values) => values.iter().any(|value2| value == value2),
        }
    }

    /// Generate the value range(s) for this constraint.
    ///
    /// Note: for the time being, this generates inclusive ranges only, because
    /// we cannot mix inclusive and exclusive ranges in a multi-field index.
    /// Note that since we also cannot mix unbounded ranges, one-sided ranges
    /// (such as inequalities) use a predefined min/max value. For unbounded numbers,
    /// this limits the range of valid numbers that can be returned with a range.
    /// This can be considered a "feature" of our implementation due to the limitations
    /// of using the builtin BTreeMap implementation. An alternative implementation
    /// is to create a special value type that represents the extrema of a type
    /// (which would probably be a cleaner and more robust solution if only I had
    /// thought of it earlier).
    ///
    /// Note: disjunction (OR) operator assumes ranges are non-overlapping.
    pub fn generate_value_ranges(&self) -> Vec<(Bound<Value>, Bound<Value>)> {
        match self {
            Constraint::Equals(value) => vec![(
                Bound::Included(value.clone()),
                Bound::Included(value.clone()),
            )],
            Constraint::LessThan(value) => vec![(
                Bound::Included(value.get_min_value()),
                Bound::Included(value.clone()),
            )],
            Constraint::GreaterThan(value) => vec![(
                Bound::Included(value.clone()),
                Bound::Included(value.get_max_value()),
            )],

            // Conjunction: Combines each pair of ranges
            Constraint::And(constraint1, constraint2) => {
                let value_ranges1 = constraint1.generate_value_ranges();
                let value_ranges2 = constraint2.generate_value_ranges();

                let mut value_ranges = Vec::new();
                for (value_range1_min, value_range1_max) in &value_ranges1 {
                    for (value_range2_min, value_range2_max) in &value_ranges2 {
                        // Assert that range bounds are inclusive.
                        let (min1, max1, min2, max2) = match (
                            value_range1_min,
                            value_range1_max,
                            value_range2_min,
                            value_range2_max,
                        ) {
                            (
                                Bound::Included(min1),
                                Bound::Included(max1),
                                Bound::Included(min2),
                                Bound::Included(max2),
                            ) => (min1, max1, min2, max2),
                            _ => panic!("non-inclusive bounds"),
                        };

                        // Combine range if overlapping
                        let (range_min, range_max) =
                            (std::cmp::max(min1, min2), std::cmp::min(max1, max2));
                        if range_max > range_min {
                            value_ranges.push((
                                Bound::Included(range_min.clone()),
                                Bound::Included(range_max.clone()),
                            ));
                        }
                    }
                }
                value_ranges
            }

            // Disjunction: Returns disjoint ranges
            Constraint::Or(constraint1, constraint2) => {
                let mut value_ranges = constraint1.generate_value_ranges();
                value_ranges.append(&mut constraint2.generate_value_ranges());

                // Remove overlapping ranges. Sort ranges by higher element,
                // iterate through this and determine overlapping ranges.
                value_ranges.sort_by(|vr1, vr2| match (vr1, vr2) {
                    ((_, Bound::Included(higher1)), (_, Bound::Included(higher2))) => {
                        higher1.cmp(higher2)
                    }
                    _ => panic!("non-inclusive bounds"),
                });

                // If any of the ranges from the left overlap any of the
                // ranges from the right, then join them into a single range.
                let mut value_ranges_nonoverlap = Vec::new();
                let mut current_interval = value_ranges[0].clone();
                for i in 1..value_ranges.len() {
                    match (current_interval.clone(), value_ranges[i].clone()) {
                        (
                            (_, Bound::Included(higher1)),
                            (Bound::Included(lower2), Bound::Included(higher2)),
                        ) => {
                            if lower2 <= higher1 {
                                // Join intervals
                                current_interval.1 = Bound::Included(higher2);
                            } else {
                                // Intervals split
                                value_ranges_nonoverlap.push(current_interval);
                                current_interval =
                                    (Bound::Included(lower2), Bound::Included(higher2));
                            }
                        }
                        _ => panic!("non-inclusive bounds"),
                    }
                }
                value_ranges_nonoverlap.push(current_interval);
                value_ranges_nonoverlap
            }

            _ => panic!("unsupported range type"),
        }
    }
}

/// Projection of fields during a query (analogous to SQL `SELECT`).
pub type ProjectionDocument = HashMap<FieldPath, Projection>;

/// Projection of a single field of the projection document.
pub enum Projection {
    // Recursive projections on subdocuments
    ProjectDocument(ProjectionDocument),

    // Exclude projecting a value
    Exclude,

    // Project a value
    Include,
}

/// Complete query operation.
pub struct Query {
    // Constraint document (`WHERE`)
    pub constraints: ConstraintDocument,

    // Projection document (`SELECT`)
    pub projection: ProjectionDocument,

    // Ordering document (`ORDER BY`)
    pub order: Option<Vec<ResultOrder>>,
}