linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Expression tool functions
//!
//! Provide a specialized tool function for rewriting expressions
//!
//! # Design Notes
//!
//! The responsibility of the Rewrite layer is to rewrite expressions, which requires:
//! 1. Analyze the structure of the existing expression.
//! 2. Create a new expression.
//! 3. Register the new expression in the ExpressionContext.
//!
//! Therefore, the Rewrite layer needs to have access to the internal structure of the Expression.
//! This is a necessary trade-off in terms of design, because:
//! “ContextualExpression” is a lightweight reference that does not contain the structure of the expression itself.
//! The rewrite operation requires the creation of a new Expression.
//! The new Expression must be registered with the ExpressionContext in order to be used.
//!
//! # Note
//!
//! The general expression tools and functions (such as extract_property_refs, is_constant) have been moved to another location.
//! `core::types::expression::common_utils`; this module keeps only the rewrite-specific functions.

use linkrs_core::types::expr::contextual::ContextualExpression;
use linkrs_core::types::expr::expression_context::ExpressionAnalysisContext;
use linkrs_core::types::expr::ExpressionMeta;
use linkrs_core::types::expr::FunctionArg;
use linkrs_core::types::expr::PropertyContainsChecker;
use linkrs_core::types::operators::BinaryOperator;
use linkrs_core::Expression;
use std::sync::Arc;

/// Check whether the expression contains the specified attribute name.
///
/// # Parameters
/// `property_names`: List of property names
/// `expr`: The expression to be checked.
///
/// # Return
/// If the expression contains any of the attributes from the list of attribute names, return true.
pub fn check_col_name(property_names: &[String], expr: &Expression) -> bool {
    PropertyContainsChecker::check(expr, property_names)
}

/// Rewrite the contextual expression
///
/// Rewrite the `ContextualExpression` according to the `rewrite_map`, and register the result in the `ExpressionContext`.
///
/// # Parameters
/// `expr`: The `ContextualExpression` that needs to be rewritten.
/// `rewrite_map`: A map for rewriting expressions; the keys represent variable names, and the values represent the `ContextualExpression` objects that need to be replaced.
/// `expr_context`: The context in which the expression is used, used for registering new expressions.
///
/// # Back
/// Revised ContextualExpression
pub fn rewrite_contextual_expression(
    expr: &ContextualExpression,
    rewrite_map: &std::collections::HashMap<String, ContextualExpression>,
    expr_context: Arc<ExpressionAnalysisContext>,
) -> ContextualExpression {
    let expr_meta = match expr.expression() {
        Some(e) => e,
        None => return expr.clone(),
    };
    let inner_expr = expr_meta.inner();

    let rewritten_expr = rewrite_expression_with_map(inner_expr, rewrite_map, expr_context.clone());
    let meta = ExpressionMeta::new(rewritten_expr);
    let id = expr_context.register_expression(meta);
    ContextualExpression::new(id, expr_context)
}

/// Rewrite the expression using the ContextualExpression mapping table.
///
/// # Parameters
/// `expr`: The Expression that needs to be rewritten.
/// - `rewrite_map`: rewrite mapping table, key is variable name, value is ContextualExpression to be replaced
/// - `expr_context`: expression context, used to register new expressions
///
/// # Back
/// The rewritten expression
fn rewrite_expression_with_map(
    expr: &Expression,
    rewrite_map: &std::collections::HashMap<String, ContextualExpression>,
    expr_context: Arc<ExpressionAnalysisContext>,
) -> Expression {
    match expr {
        Expression::Variable(name) => {
            if let Some(new_ctx_expr) = rewrite_map.get(name) {
                let new_expr_meta = match new_ctx_expr.expression() {
                    Some(e) => e,
                    None => return expr.clone(),
                };
                new_expr_meta.inner().clone()
            } else {
                expr.clone()
            }
        }
        Expression::Property { object, property } => {
            if let Expression::Variable(obj_name) = object.as_ref() {
                let full_name = format!("{}.{}", obj_name, property);
                if let Some(new_ctx_expr) = rewrite_map.get(&full_name) {
                    let new_expr_meta = match new_ctx_expr.expression() {
                        Some(e) => e,
                        None => return expr.clone(),
                    };
                    return new_expr_meta.inner().clone();
                }
                if let Some(new_ctx_expr) = rewrite_map.get(property) {
                    let new_expr_meta = match new_ctx_expr.expression() {
                        Some(e) => e,
                        None => return expr.clone(),
                    };
                    return Expression::Property {
                        object: Box::new(new_expr_meta.inner().clone()),
                        property: property.clone(),
                    };
                }
            }
            Expression::Property {
                object: Box::new(rewrite_expression_with_map(
                    object,
                    rewrite_map,
                    expr_context,
                )),
                property: property.clone(),
            }
        }
        Expression::Binary { left, op, right } => Expression::Binary {
            left: Box::new(rewrite_expression_with_map(
                left,
                rewrite_map,
                expr_context.clone(),
            )),
            op: *op,
            right: Box::new(rewrite_expression_with_map(
                right,
                rewrite_map,
                expr_context,
            )),
        },
        Expression::Unary { op, operand } => Expression::Unary {
            op: *op,
            operand: Box::new(rewrite_expression_with_map(
                operand,
                rewrite_map,
                expr_context,
            )),
        },
        Expression::Function { name, args } => Expression::Function {
            name: name.clone(),
            args: args
                .iter()
                .map(|arg| {
                    FunctionArg::Positional(rewrite_expression_with_map(
                        arg.as_expr(),
                        rewrite_map,
                        expr_context.clone(),
                    ))
                })
                .collect(),
        },
        Expression::Aggregate {
            func,
            args,
            distinct,
            filter,
        } => Expression::Aggregate {
            func: *func,
            args: args
                .iter()
                .map(|a| rewrite_expression_with_map(a, rewrite_map, expr_context.clone()))
                .collect(),
            distinct: *distinct,
            filter: filter
                .as_ref()
                .map(|f| Box::new(rewrite_expression_with_map(f, rewrite_map, expr_context))),
        },
        Expression::List(list) => Expression::List(
            list.iter()
                .map(|item| rewrite_expression_with_map(item, rewrite_map, expr_context.clone()))
                .collect(),
        ),
        Expression::Map(map) => Expression::Map(
            map.iter()
                .map(|(k, v)| {
                    (
                        k.clone(),
                        rewrite_expression_with_map(v, rewrite_map, expr_context.clone()),
                    )
                })
                .collect(),
        ),
        Expression::Case {
            test_expr,
            conditions,
            default,
        } => Expression::Case {
            test_expr: test_expr.as_ref().map(|e| {
                Box::new(rewrite_expression_with_map(
                    e,
                    rewrite_map,
                    expr_context.clone(),
                ))
            }),
            conditions: conditions
                .iter()
                .map(|(w, t)| {
                    (
                        rewrite_expression_with_map(w, rewrite_map, expr_context.clone()),
                        rewrite_expression_with_map(t, rewrite_map, expr_context.clone()),
                    )
                })
                .collect(),
            default: default
                .as_ref()
                .map(|e| Box::new(rewrite_expression_with_map(e, rewrite_map, expr_context))),
        },
        Expression::TypeCast {
            expression,
            target_type,
        } => Expression::TypeCast {
            expression: Box::new(rewrite_expression_with_map(
                expression,
                rewrite_map,
                expr_context,
            )),
            target_type: target_type.clone(),
        },
        Expression::Subscript { collection, index } => Expression::Subscript {
            collection: Box::new(rewrite_expression_with_map(
                collection,
                rewrite_map,
                expr_context.clone(),
            )),
            index: Box::new(rewrite_expression_with_map(
                index,
                rewrite_map,
                expr_context,
            )),
        },
        _ => expr.clone(),
    }
}

/// Split filter criteria
///
/// Split complex filter conditions (such as those connected by the AND operator) into two parts:
/// The part that corresponds to the selector function
/// The remaining part
///
/// # Parameters
/// `ctx_expr`: The context expression for the filtering criteria
/// `picker`: A selector function that returns `true` if the corresponding element should be selected.
///
/// # Back
/// (The selected part; the remaining part)
pub fn split_filter<F>(
    ctx_expr: &ContextualExpression,
    picker: F,
) -> (Option<ContextualExpression>, Option<ContextualExpression>)
where
    F: Fn(&Expression) -> bool,
{
    let expr_meta = match ctx_expr.expression() {
        Some(e) => e,
        None => return (None, None),
    };
    let expr = expr_meta.inner();
    let (picked_expr, remained_expr) = split_filter_impl(expr, &picker);

    let expr_context = ctx_expr.context().clone();
    let picked = picked_expr.map(|e| {
        let meta = ExpressionMeta::new(e);
        let id = expr_context.register_expression(meta);
        ContextualExpression::new(id, expr_context.clone())
    });

    let remained = remained_expr.map(|e| {
        let meta = ExpressionMeta::new(e);
        let id = expr_context.register_expression(meta);
        ContextualExpression::new(id, expr_context.clone())
    });

    (picked, remained)
}

fn split_filter_impl<F>(
    condition: &Expression,
    picker: &F,
) -> (Option<Expression>, Option<Expression>)
where
    F: Fn(&Expression) -> bool,
{
    match condition {
        Expression::Binary {
            op: BinaryOperator::And,
            left,
            right,
        } => {
            // Handle the left and right sides recursively.
            let (left_picked, left_remained) = split_filter_impl(left, picker);
            let (right_picked, right_remained) = split_filter_impl(right, picker);

            // Merge the selected sections.
            let picked = match (left_picked, right_picked) {
                (Some(l), Some(r)) => Some(Expression::Binary {
                    op: BinaryOperator::And,
                    left: Box::new(l),
                    right: Box::new(r),
                }),
                (Some(l), None) => Some(l),
                (None, Some(r)) => Some(r),
                (None, None) => None,
            };

            // Merge the remaining parts.
            let remained = match (left_remained, right_remained) {
                (Some(l), Some(r)) => Some(Expression::Binary {
                    op: BinaryOperator::And,
                    left: Box::new(l),
                    right: Box::new(r),
                }),
                (Some(l), None) => Some(l),
                (None, Some(r)) => Some(r),
                (None, None) => None,
            };

            (picked, remained)
        }
        _ => {
            // Basic situation: Check whether the current expression meets the criteria of the selector.
            if picker(condition) {
                (Some(condition.clone()), None)
            } else {
                (None, Some(condition.clone()))
            }
        }
    }
}

/// To combine two filter conditions, use the AND operator.
///
/// # Parameters
/// “left”: The left-side condition
/// “right”: The condition on the right side
///
/// # Back
/// The merged conditions
pub fn and_condition(
    left: Option<ContextualExpression>,
    right: Option<ContextualExpression>,
) -> Option<ContextualExpression> {
    match (left, right) {
        (Some(l), Some(r)) => {
            let expr_context = l.context().clone();
            let l_expr = match l.expression() {
                Some(e) => e,
                None => return Some(r),
            };
            let r_expr = match r.expression() {
                Some(e) => e,
                None => return Some(l),
            };
            let combined_expr = Expression::Binary {
                op: BinaryOperator::And,
                left: Box::new(l_expr.inner().clone()),
                right: Box::new(r_expr.inner().clone()),
            };
            let meta = ExpressionMeta::new(combined_expr);
            let id = expr_context.register_expression(meta);
            Some(ContextualExpression::new(id, expr_context))
        }
        (Some(l), None) => Some(l),
        (None, Some(r)) => Some(r),
        (None, None) => None,
    }
}

/// To combine multiple filter conditions, use the AND operator.
///
/// # Parameters
/// `conditions`: List of conditions
///
/// # Back
/// Post-merger conditions
pub fn and_conditions(
    conditions: Vec<Option<ContextualExpression>>,
) -> Option<ContextualExpression> {
    conditions.into_iter().fold(None, and_condition)
}

#[cfg(test)]
mod tests {
    use super::*;
    use linkrs_core::Value;

    #[test]
    fn test_check_col_name() {
        let property_names = vec!["a".to_string(), "b".to_string()];

        let expr = Expression::Property {
            object: Box::new(Expression::Variable("v".to_string())),
            property: "a".to_string(),
        };
        assert!(check_col_name(&property_names, &expr));

        let expr = Expression::Property {
            object: Box::new(Expression::Variable("v".to_string())),
            property: "c".to_string(),
        };
        assert!(!check_col_name(&property_names, &expr));

        let expr = Expression::Binary {
            op: BinaryOperator::Equal,
            left: Box::new(Expression::Property {
                object: Box::new(Expression::Variable("v".to_string())),
                property: "a".to_string(),
            }),
            right: Box::new(Expression::Literal(Value::Int(1))),
        };
        assert!(check_col_name(&property_names, &expr));
    }

    #[test]
    fn test_split_filter() {
        let expr_context = Arc::new(ExpressionAnalysisContext::new());

        let condition = Expression::Binary {
            op: BinaryOperator::And,
            left: Box::new(Expression::Binary {
                op: BinaryOperator::And,
                left: Box::new(Expression::Binary {
                    op: BinaryOperator::Equal,
                    left: Box::new(Expression::Property {
                        object: Box::new(Expression::Variable("v".to_string())),
                        property: "a".to_string(),
                    }),
                    right: Box::new(Expression::Literal(Value::Int(1))),
                }),
                right: Box::new(Expression::Binary {
                    op: BinaryOperator::Equal,
                    left: Box::new(Expression::Property {
                        object: Box::new(Expression::Variable("v".to_string())),
                        property: "b".to_string(),
                    }),
                    right: Box::new(Expression::Literal(Value::Int(2))),
                }),
            }),
            right: Box::new(Expression::Binary {
                op: BinaryOperator::Equal,
                left: Box::new(Expression::Property {
                    object: Box::new(Expression::Variable("v".to_string())),
                    property: "c".to_string(),
                }),
                right: Box::new(Expression::Literal(Value::Int(3))),
            }),
        };

        let meta = ExpressionMeta::new(condition);
        let id = expr_context.register_expression(meta);
        let ctx_condition = ContextualExpression::new(id, expr_context.clone());

        let picker = |expr: &Expression| -> bool {
            let mut collector =
                linkrs_core::types::expr::visitor_collectors::PropertyCollector::new();
            linkrs_core::types::expr::ExpressionVisitor::visit(&mut collector, expr);
            collector.properties.contains(&"a".to_string())
                || collector.properties.contains(&"b".to_string())
        };

        let (picked, remained) = split_filter(&ctx_condition, picker);

        assert!(picked.is_some());
        let picked_props = linkrs_core::types::expr::expression_utils::extract_property_refs(
            picked.as_ref().expect("Failed to get picked expression"),
        );
        assert!(picked_props.contains(&"a".to_string()));
        assert!(picked_props.contains(&"b".to_string()));

        assert!(remained.is_some());
        let remained_props = linkrs_core::types::expr::expression_utils::extract_property_refs(
            remained
                .as_ref()
                .expect("Failed to get remained expression"),
        );
        assert!(remained_props.contains(&"c".to_string()));
    }
}