iridium-db 0.4.0

A high-performance vector-graph hybrid storage and indexing engine
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use super::{
    Ast, BitmapPredicate, ProjectionItem, QueryError, Result, VectorPredicate, WhereClause,
};

pub(super) fn parse(query: &str) -> Result<Ast> {
    let trimmed = query.trim();
    if trimmed.is_empty() {
        return Err(QueryError::InvalidSyntax(
            "query must not be empty".to_string(),
        ));
    }

    let match_pos = require_keyword(trimmed, "MATCH", 0)?;
    if match_pos != 0 {
        return Err(QueryError::InvalidSyntax(
            "query must start with MATCH".to_string(),
        ));
    }

    let return_pos = find_keyword(trimmed, "RETURN", 0)
        .ok_or_else(|| QueryError::InvalidSyntax("missing RETURN clause".to_string()))?;
    let where_pos = find_keyword(trimmed, "WHERE", "MATCH".len());
    let with_pos = find_keyword(trimmed, "WITH", "MATCH".len());
    let limit_pos = find_keyword(trimmed, "LIMIT", return_pos + "RETURN".len());

    if let Some(where_idx) = where_pos {
        if where_idx > return_pos {
            return Err(QueryError::InvalidSyntax(
                "WHERE must appear before RETURN".to_string(),
            ));
        }
    }
    if let Some(with_idx) = with_pos {
        if with_idx > return_pos {
            return Err(QueryError::InvalidSyntax(
                "WITH must appear before RETURN".to_string(),
            ));
        }
    }

    let mut clause_end_candidates = vec![return_pos];
    if let Some(where_idx) = where_pos {
        clause_end_candidates.push(where_idx);
    }
    if let Some(with_idx) = with_pos {
        clause_end_candidates.push(with_idx);
    }
    let match_slice_end = *clause_end_candidates.iter().min().unwrap_or(&return_pos);
    let match_aliases = parse_match_aliases(&trimmed["MATCH".len()..match_slice_end])?;
    let match_alias = match_aliases.first().cloned().ok_or_else(|| {
        QueryError::InvalidSyntax("MATCH must contain at least one alias".to_string())
    })?;

    let where_clause = if let Some(where_idx) = where_pos {
        let where_end = with_pos.unwrap_or(return_pos);
        let where_raw = trimmed[where_idx + "WHERE".len()..where_end].trim();
        Some(parse_where_clause(where_raw)?)
    } else {
        None
    };
    let where_predicate = where_clause_to_vector(where_clause.as_ref());
    let where_bitmap_predicate = where_clause_to_bitmap(where_clause.as_ref());

    let with_items = if let Some(with_idx) = with_pos {
        let with_end = return_pos;
        let with_raw = trimmed[with_idx + "WITH".len()..with_end].trim();
        parse_projection_list(with_raw)?
    } else {
        Vec::new()
    };

    let return_clause = if let Some(limit_idx) = limit_pos {
        &trimmed[return_pos + "RETURN".len()..limit_idx]
    } else {
        &trimmed[return_pos + "RETURN".len()..]
    };
    let return_items = parse_projection_list(return_clause)?;
    let return_alias = primary_return_alias(&return_items)?;

    let limit = if let Some(limit_idx) = limit_pos {
        let raw = trimmed[limit_idx + "LIMIT".len()..].trim();
        if raw.is_empty() {
            return Err(QueryError::InvalidSyntax("LIMIT missing value".to_string()));
        }
        Some(
            raw.parse::<u64>()
                .map_err(|_| QueryError::InvalidSyntax("invalid LIMIT value".to_string()))?,
        )
    } else {
        None
    };

    Ok(Ast {
        match_aliases,
        match_alias,
        where_clause,
        where_predicate,
        where_bitmap_predicate,
        with_items,
        return_items,
        return_alias,
        limit,
    })
}

fn parse_match_aliases(input: &str) -> Result<Vec<String>> {
    let patterns = split_top_level(input, ',');
    if patterns.is_empty() {
        return Err(QueryError::InvalidSyntax(
            "MATCH must contain at least one pattern".to_string(),
        ));
    }

    let mut aliases = Vec::with_capacity(patterns.len());
    for pattern in patterns {
        let open = pattern
            .find('(')
            .ok_or_else(|| QueryError::InvalidSyntax("MATCH missing '('".to_string()))?;
        let close = pattern[open + 1..]
            .find(')')
            .ok_or_else(|| QueryError::InvalidSyntax("MATCH missing ')'".to_string()))?
            + open
            + 1;
        let inside = pattern[open + 1..close].trim();
        let alias = inside
            .split([':', ' ', '\t'])
            .find(|value| !value.is_empty())
            .ok_or_else(|| {
                QueryError::InvalidSyntax("MATCH alias must not be empty".to_string())
            })?;
        aliases.push(alias.to_string());
    }
    Ok(aliases)
}

fn parse_projection_list(input: &str) -> Result<Vec<ProjectionItem>> {
    let parts = split_top_level(input, ',');
    if parts.is_empty() {
        return Err(QueryError::InvalidSyntax(
            "projection list must not be empty".to_string(),
        ));
    }

    let mut items = Vec::with_capacity(parts.len());
    for part in parts {
        items.push(parse_projection_item(&part)?);
    }
    Ok(items)
}

fn parse_projection_item(input: &str) -> Result<ProjectionItem> {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return Err(QueryError::InvalidSyntax(
            "projection item must not be empty".to_string(),
        ));
    }

    let (expr, alias) = split_optional_alias(trimmed);
    let open = expr.find('(');
    let close = expr.rfind(')');
    if let (Some(open), Some(close)) = (open, close) {
        if close <= open {
            return Err(QueryError::InvalidSyntax(
                "function projection has invalid parentheses".to_string(),
            ));
        }
        let name = expr[..open].trim();
        let argument = expr[open + 1..close].trim();
        if name.is_empty() || argument.is_empty() {
            return Err(QueryError::InvalidSyntax(
                "function projection missing name or argument".to_string(),
            ));
        }
        return Ok(ProjectionItem::Function {
            name: name.to_string(),
            argument: argument.to_string(),
            alias,
        });
    }

    if alias.is_some() {
        return Err(QueryError::InvalidSyntax(
            "AS alias is only valid for function projections".to_string(),
        ));
    }
    Ok(ProjectionItem::Identifier(expr.to_string()))
}

fn split_optional_alias(input: &str) -> (&str, Option<String>) {
    let uppercase = input.to_ascii_uppercase();
    if let Some(idx) = uppercase.rfind(" AS ") {
        let expr = input[..idx].trim();
        let alias = input[idx + 4..].trim();
        return (
            expr,
            if alias.is_empty() {
                None
            } else {
                Some(alias.to_string())
            },
        );
    }
    (input, None)
}

fn parse_where_clause(input: &str) -> Result<WhereClause> {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return Err(QueryError::InvalidSyntax(
            "WHERE predicate must not be empty".to_string(),
        ));
    }

    if let Some(open) = trimmed.find('(') {
        let close = trimmed
            .rfind(')')
            .ok_or_else(|| QueryError::InvalidSyntax("WHERE predicate missing ')'".to_string()))?;
        if close <= open {
            return Err(QueryError::InvalidSyntax(
                "WHERE predicate has invalid function arguments".to_string(),
            ));
        }
        let function = trimmed[..open].trim();
        if function.is_empty() {
            return Err(QueryError::InvalidSyntax(
                "WHERE predicate missing function".to_string(),
            ));
        }
        let args_str = trimmed[open + 1..close].trim();
        let args = split_top_level(args_str, ',');
        if args.is_empty() {
            return Err(QueryError::InvalidSyntax(
                "WHERE predicate requires function args".to_string(),
            ));
        }
        let tail = trimmed[close + 1..].trim();
        let (operator, threshold) = parse_operator_threshold(tail)?;
        return Ok(WhereClause::Function {
            function: function.to_string(),
            args,
            operator: operator.to_string(),
            threshold: threshold.to_string(),
        });
    }

    let (operator, right, operator_idx) = parse_operator_threshold_with_index(trimmed)?;
    let left = trimmed[..operator_idx].trim();
    if left.is_empty() {
        return Err(QueryError::InvalidSyntax(
            "WHERE predicate missing left operand".to_string(),
        ));
    }
    Ok(WhereClause::Comparison {
        left: left.to_string(),
        operator: operator.to_string(),
        right: right.to_string(),
    })
}

fn where_clause_to_vector(where_clause: Option<&WhereClause>) -> Option<VectorPredicate> {
    match where_clause {
        Some(WhereClause::Function {
            function,
            args,
            operator,
            threshold,
        }) if function.starts_with("vector.") && args.len() == 2 => Some(VectorPredicate {
            function: function.clone(),
            target: args[0].clone(),
            param: args[1].clone(),
            operator: operator.clone(),
            threshold: threshold.clone(),
        }),
        _ => None,
    }
}

fn where_clause_to_bitmap(where_clause: Option<&WhereClause>) -> Option<BitmapPredicate> {
    match where_clause {
        Some(WhereClause::Function {
            function,
            args,
            operator,
            threshold,
        }) if function.starts_with("bitmap.") && args.len() == 2 => {
            if operator != "=" {
                return None;
            }
            let threshold_lc = threshold.trim().to_ascii_lowercase();
            if threshold_lc != "1" && threshold_lc != "true" {
                return None;
            }
            Some(BitmapPredicate {
                function: function.clone(),
                index_name: strip_optional_quotes(&args[0]),
                value_key: strip_optional_quotes(&args[1]),
            })
        }
        _ => None,
    }
}

fn strip_optional_quotes(input: &str) -> String {
    let trimmed = input.trim();
    if trimmed.len() >= 2 && trimmed.starts_with('"') && trimmed.ends_with('"') {
        trimmed[1..trimmed.len() - 1].to_string()
    } else {
        trimmed.to_string()
    }
}

fn primary_return_alias(items: &[ProjectionItem]) -> Result<String> {
    let first = items.first().ok_or_else(|| {
        QueryError::InvalidSyntax("RETURN must contain at least one projection".to_string())
    })?;
    let alias = match first {
        ProjectionItem::Identifier(value) => value.clone(),
        ProjectionItem::Function { alias: Some(a), .. } => a.clone(),
        ProjectionItem::Function { name, .. } => format!("{}()", name),
    };
    if alias.trim().is_empty() {
        return Err(QueryError::InvalidSyntax(
            "RETURN alias must not be empty".to_string(),
        ));
    }
    Ok(alias)
}

fn parse_operator_threshold(input: &str) -> Result<(&str, &str)> {
    let (operator, right, _) = parse_operator_threshold_with_index(input)?;
    Ok((operator, right))
}

fn parse_operator_threshold_with_index(input: &str) -> Result<(&str, &str, usize)> {
    for op in [">=", "<=", "=", ">", "<"] {
        if let Some(idx) = input.find(op) {
            let threshold = input[idx + op.len()..].trim();
            if threshold.is_empty() {
                return Err(QueryError::InvalidSyntax(
                    "WHERE missing threshold".to_string(),
                ));
            }
            return Ok((op, threshold, idx));
        }
    }
    Err(QueryError::InvalidSyntax(
        "WHERE predicate missing operator".to_string(),
    ))
}

fn split_top_level(input: &str, delim: char) -> Vec<String> {
    let mut parts = Vec::new();
    let mut depth = 0_usize;
    let mut start = 0_usize;
    for (idx, ch) in input.char_indices() {
        match ch {
            '(' => depth = depth.saturating_add(1),
            ')' => depth = depth.saturating_sub(1),
            _ => {}
        }
        if ch == delim && depth == 0 {
            let part = input[start..idx].trim();
            if !part.is_empty() {
                parts.push(part.to_string());
            }
            start = idx + ch.len_utf8();
        }
    }
    let tail = input[start..].trim();
    if !tail.is_empty() {
        parts.push(tail.to_string());
    }
    parts
}

fn find_keyword(input: &str, keyword: &str, start: usize) -> Option<usize> {
    let upper_input = input.to_ascii_uppercase();
    let upper_keyword = keyword.to_ascii_uppercase();
    let mut offset = start;

    while offset <= upper_input.len() {
        let search = &upper_input[offset..];
        let found = search.find(&upper_keyword)?;
        let idx = offset + found;
        let before_ok = idx == 0
            || !upper_input[..idx]
                .chars()
                .next_back()
                .is_some_and(|c| c.is_ascii_alphanumeric() || c == '_');
        let after_idx = idx + upper_keyword.len();
        let after_ok = after_idx >= upper_input.len()
            || !upper_input[after_idx..]
                .chars()
                .next()
                .is_some_and(|c| c.is_ascii_alphanumeric() || c == '_');
        if before_ok && after_ok {
            return Some(idx);
        }
        offset = idx + 1;
    }
    None
}

fn require_keyword(input: &str, keyword: &str, start: usize) -> Result<usize> {
    find_keyword(input, keyword, start)
        .ok_or_else(|| QueryError::InvalidSyntax(format!("missing {} clause", keyword)))
}