Skip to main content

nodedb_sql/planner/
ast_helpers.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Shared AST manipulation helpers for DML planners.
4
5use sqlparser::ast;
6
7use crate::error::Result;
8use crate::parser::normalize::normalize_ident;
9use crate::planner::select::convert_where_to_filters;
10use crate::types::Filter;
11
12/// Return `(table, column)` for a `table.col` compound identifier, or `None`.
13pub fn qualified_ident_pair(expr: &ast::Expr) -> Option<(String, String)> {
14    match expr {
15        ast::Expr::CompoundIdentifier(parts) if parts.len() == 2 => {
16            Some((normalize_ident(&parts[0]), normalize_ident(&parts[1])))
17        }
18        _ => None,
19    }
20}
21
22/// Flatten a right-leaning AND expression tree into a list of conjuncts.
23pub fn flatten_and_expr(expr: &ast::Expr, out: &mut Vec<ast::Expr>) {
24    match expr {
25        ast::Expr::BinaryOp {
26            left,
27            op: ast::BinaryOperator::And,
28            right,
29        } => {
30            flatten_and_expr(left, out);
31            flatten_and_expr(right, out);
32        }
33        other => out.push(other.clone()),
34    }
35}
36
37/// Reassemble conjuncts into a right-leaning AND tree. Panics if empty.
38pub fn rebuild_and_expr(mut conjuncts: Vec<ast::Expr>) -> ast::Expr {
39    let last = conjuncts.pop().expect("non-empty conjuncts");
40    conjuncts
41        .into_iter()
42        .rfold(last, |acc, next| ast::Expr::BinaryOp {
43            left: Box::new(next),
44            op: ast::BinaryOperator::And,
45            right: Box::new(acc),
46        })
47}
48
49/// Walk an expression and replace every `table.col` compound identifier where
50/// `table == qualifier` with a bare `col` identifier. Lets target-side
51/// predicates like `t.score > 15` be evaluated against documents that store
52/// fields without a table qualifier.
53pub fn strip_table_qualifier(expr: &ast::Expr, qualifier: &str) -> ast::Expr {
54    match expr {
55        ast::Expr::CompoundIdentifier(parts) if parts.len() == 2 => {
56            if normalize_ident(&parts[0]) == qualifier {
57                ast::Expr::Identifier(parts[1].clone())
58            } else {
59                expr.clone()
60            }
61        }
62        ast::Expr::BinaryOp { left, op, right } => ast::Expr::BinaryOp {
63            left: Box::new(strip_table_qualifier(left, qualifier)),
64            op: op.clone(),
65            right: Box::new(strip_table_qualifier(right, qualifier)),
66        },
67        ast::Expr::UnaryOp { op, expr: inner } => ast::Expr::UnaryOp {
68            op: *op,
69            expr: Box::new(strip_table_qualifier(inner, qualifier)),
70        },
71        ast::Expr::Nested(inner) => {
72            ast::Expr::Nested(Box::new(strip_table_qualifier(inner, qualifier)))
73        }
74        ast::Expr::IsNull(inner) => {
75            ast::Expr::IsNull(Box::new(strip_table_qualifier(inner, qualifier)))
76        }
77        ast::Expr::IsNotNull(inner) => {
78            ast::Expr::IsNotNull(Box::new(strip_table_qualifier(inner, qualifier)))
79        }
80        other => other.clone(),
81    }
82}
83
84/// Strip `qualifier.` from all compound identifiers in `expr`, then convert
85/// the result to `Vec<Filter>` via `convert_where_to_filters`.
86pub fn strip_and_convert_filters(
87    conjuncts: Vec<ast::Expr>,
88    qualifier: &str,
89) -> Result<Vec<Filter>> {
90    if conjuncts.is_empty() {
91        return Ok(Vec::new());
92    }
93    let stripped: Vec<ast::Expr> = conjuncts
94        .into_iter()
95        .map(|c| strip_table_qualifier(&c, qualifier))
96        .collect();
97    let rebuilt = rebuild_and_expr(stripped);
98    convert_where_to_filters(&rebuilt)
99}