Skip to main content

reddb_rql/
sql_lowering.rs

1use crate::ast::{
2    BinOp, CompareOp, DeleteQuery, Expr, FieldRef, Filter, GraphQuery, InsertQuery, JoinQuery,
3    PathQuery, Projection, SelectItem, Span, TableQuery, UnaryOp, UpdateQuery, VectorQuery,
4};
5use reddb_types::types::Value;
6use reddb_types::vector_metadata::MetadataFilter;
7
8pub const PARAMETER_PROJECTION_PREFIX: &str = "__user_param_projection__:";
9
10/// Recursively check whether `expr` contains any `Expr::Parameter` node.
11/// Used by the INSERT parser to know when to defer literal folding to
12/// the user_params binder.
13pub fn expr_contains_parameter(expr: &Expr) -> bool {
14    match expr {
15        Expr::Parameter { .. } => true,
16        Expr::Literal { .. } | Expr::Column { .. } => false,
17        Expr::BinaryOp { lhs, rhs, .. } => {
18            expr_contains_parameter(lhs) || expr_contains_parameter(rhs)
19        }
20        Expr::UnaryOp { operand, .. } => expr_contains_parameter(operand),
21        Expr::Cast { inner, .. } => expr_contains_parameter(inner),
22        Expr::FunctionCall { args, .. } => args.iter().any(expr_contains_parameter),
23        Expr::Case {
24            branches, else_, ..
25        } => {
26            branches
27                .iter()
28                .any(|(c, v)| expr_contains_parameter(c) || expr_contains_parameter(v))
29                || else_.as_deref().is_some_and(expr_contains_parameter)
30        }
31        Expr::IsNull { operand, .. } => expr_contains_parameter(operand),
32        Expr::InList { target, values, .. } => {
33            expr_contains_parameter(target) || values.iter().any(expr_contains_parameter)
34        }
35        Expr::Between {
36            target, low, high, ..
37        } => {
38            expr_contains_parameter(target)
39                || expr_contains_parameter(low)
40                || expr_contains_parameter(high)
41        }
42        Expr::Subquery { .. } => false,
43        Expr::WindowFunctionCall { args, window, .. } => {
44            args.iter().any(expr_contains_parameter)
45                || window.partition_by.iter().any(expr_contains_parameter)
46                || window
47                    .order_by
48                    .iter()
49                    .any(|o| expr_contains_parameter(&o.expr))
50        }
51    }
52}
53
54pub fn expr_to_projection(expr: &Expr) -> Option<Projection> {
55    match expr {
56        Expr::Literal { value, .. } => projection_from_literal(value),
57        Expr::Column { field, .. } => {
58            if matches!(
59                field,
60                FieldRef::TableColumn { table, column } if table.is_empty() && column == "*"
61            ) {
62                Some(Projection::All)
63            } else {
64                Some(Projection::Field(field.clone(), None))
65            }
66        }
67        Expr::Parameter { index, .. } => Some(Projection::Column(format!(
68            "{PARAMETER_PROJECTION_PREFIX}{index}"
69        ))),
70        Expr::BinaryOp { op, lhs, rhs, .. } => match op {
71            BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod | BinOp::Concat => {
72                Some(Projection::Function(
73                    projection_binop_name(*op).to_string(),
74                    vec![expr_to_projection(lhs)?, expr_to_projection(rhs)?],
75                ))
76            }
77            _ => Some(boolean_expr_projection(expr.clone())),
78        },
79        Expr::UnaryOp { op, operand, .. } => match op {
80            UnaryOp::Neg => Some(Projection::Function(
81                "SUB".to_string(),
82                vec![
83                    Projection::Column("LIT:0".to_string()),
84                    expr_to_projection(operand)?,
85                ],
86            )),
87            UnaryOp::Not => Some(boolean_expr_projection(expr.clone())),
88        },
89        Expr::Cast { inner, target, .. } => Some(Projection::Function(
90            "CAST".to_string(),
91            vec![
92                expr_to_projection(inner)?,
93                Projection::Column(format!("TYPE:{target}")),
94            ],
95        )),
96        Expr::FunctionCall { name, args, .. } => Some(Projection::Function(
97            name.to_uppercase(),
98            args.iter()
99                .map(expr_to_projection)
100                .collect::<Option<Vec<_>>>()?,
101        )),
102        Expr::Case {
103            branches, else_, ..
104        } => {
105            let mut args = Vec::with_capacity(branches.len() * 2 + usize::from(else_.is_some()));
106            for (cond, value) in branches {
107                args.push(case_condition_projection(cond.clone()));
108                args.push(expr_to_projection(value)?);
109            }
110            if let Some(else_expr) = else_ {
111                args.push(expr_to_projection(else_expr)?);
112            }
113            Some(Projection::Function("CASE".to_string(), args))
114        }
115        Expr::IsNull { .. }
116        | Expr::InList { .. }
117        | Expr::Between { .. }
118        | Expr::Subquery { .. } => Some(boolean_expr_projection(expr.clone())),
119        Expr::WindowFunctionCall {
120            name, args, window, ..
121        } => {
122            let lowered_args = args
123                .iter()
124                .map(expr_to_projection)
125                .collect::<Option<Vec<_>>>()?;
126            Some(crate::ast::Projection::Window {
127                name: name.to_uppercase(),
128                args: lowered_args,
129                window: Box::new(window.clone()),
130                alias: None,
131            })
132        }
133    }
134}
135
136pub fn select_item_to_projection(item: &SelectItem) -> Option<Projection> {
137    match item {
138        SelectItem::Wildcard => Some(Projection::All),
139        SelectItem::Expr { expr, alias } => {
140            let projection = expr_to_projection(expr)?;
141            // Attach ONLY an explicit alias here. The previous
142            // `.or_else(|| Some(render_expr_label(expr)))` synthesized an implicit
143            // output label from the expression text and baked it into the legacy
144            // Projection — mangling function names (`CAST` → `CAST:CAST(.. AS ..)`),
145            // wrapping bare columns in a redundant `Alias(name, name)`, and thereby
146            // breaking render→parse→render idempotency. The default output-column
147            // label for an un-aliased projection is derived at render time from the
148            // SelectItem (which keeps `alias: None`), not from this lowering.
149            Some(attach_projection_alias(projection, alias.clone()))
150        }
151    }
152}
153
154pub fn effective_table_projections(query: &TableQuery) -> Vec<Projection> {
155    if !query.select_items.is_empty() {
156        return query
157            .select_items
158            .iter()
159            .filter_map(select_item_to_projection)
160            .collect();
161    }
162    if query.columns.is_empty() {
163        vec![Projection::All]
164    } else {
165        query.columns.clone()
166    }
167}
168
169pub fn effective_table_filter(query: &TableQuery) -> Option<Filter> {
170    query
171        .filter
172        .clone()
173        .or_else(|| query.where_expr.as_ref().map(expr_to_filter))
174        .map(|f| f.optimize()) // OR-of-Eq → In; AND/OR flatten; constant fold
175}
176
177pub fn effective_table_group_by_exprs(query: &TableQuery) -> Vec<Expr> {
178    if !query.group_by_exprs.is_empty() {
179        query.group_by_exprs.clone()
180    } else {
181        query
182            .group_by
183            .iter()
184            .map(|column| Expr::Column {
185                field: FieldRef::TableColumn {
186                    table: String::new(),
187                    column: column.clone(),
188                },
189                span: Span::synthetic(),
190            })
191            .collect()
192    }
193}
194
195pub fn effective_table_having_filter(query: &TableQuery) -> Option<Filter> {
196    query
197        .having
198        .clone()
199        .or_else(|| query.having_expr.as_ref().map(expr_to_filter))
200}
201
202pub fn effective_update_filter(query: &UpdateQuery) -> Option<Filter> {
203    query
204        .filter
205        .clone()
206        .or_else(|| query.where_expr.as_ref().map(expr_to_filter))
207}
208
209pub fn effective_insert_rows(query: &InsertQuery) -> Result<Vec<Vec<Value>>, String> {
210    if !query.value_exprs.is_empty() {
211        return query
212            .value_exprs
213            .iter()
214            .cloned()
215            .map(|row| row.into_iter().map(fold_expr_to_value).collect())
216            .collect();
217    }
218    Ok(query.values.clone())
219}
220
221pub fn effective_delete_filter(query: &DeleteQuery) -> Option<Filter> {
222    query
223        .filter
224        .clone()
225        .or_else(|| query.where_expr.as_ref().map(expr_to_filter))
226}
227
228pub fn effective_join_filter(query: &JoinQuery) -> Option<Filter> {
229    query.filter.clone()
230}
231
232pub fn effective_graph_filter(query: &GraphQuery) -> Option<Filter> {
233    query.filter.clone()
234}
235
236pub fn effective_graph_projections(query: &GraphQuery) -> Vec<Projection> {
237    query.return_.clone()
238}
239
240pub fn effective_path_filter(query: &PathQuery) -> Option<Filter> {
241    query.filter.clone()
242}
243
244pub fn effective_path_projections(query: &PathQuery) -> Vec<Projection> {
245    query.return_.clone()
246}
247
248pub fn effective_vector_filter(query: &VectorQuery) -> Option<MetadataFilter> {
249    query.filter.clone()
250}
251
252pub fn projection_to_expr(projection: &Projection) -> Option<(Expr, Option<String>)> {
253    match projection {
254        Projection::All => Some((
255            Expr::Column {
256                field: FieldRef::TableColumn {
257                    table: String::new(),
258                    column: "*".to_string(),
259                },
260                span: Span::synthetic(),
261            },
262            None,
263        )),
264        Projection::Column(column) => Some((projection_column_to_expr(column), None)),
265        Projection::Alias(column, alias) => {
266            Some((projection_column_to_expr(column), Some(alias.clone())))
267        }
268        Projection::Function(name, args) => {
269            let (name, alias) = split_projection_function_alias(name);
270            let args = args
271                .iter()
272                .map(projection_to_expr)
273                .collect::<Option<Vec<_>>>()?
274                .into_iter()
275                .map(|(expr, _)| expr)
276                .collect();
277            Some((
278                Expr::FunctionCall {
279                    name,
280                    args,
281                    span: Span::synthetic(),
282                },
283                alias,
284            ))
285        }
286        Projection::Expression(filter, alias) => Some((filter_to_expr(filter), alias.clone())),
287        Projection::Field(field, alias) => Some((
288            Expr::Column {
289                field: field.clone(),
290                span: Span::synthetic(),
291            },
292            alias.clone(),
293        )),
294        Projection::Window {
295            name,
296            args,
297            window,
298            alias,
299        } => {
300            let args = args
301                .iter()
302                .map(projection_to_expr)
303                .collect::<Option<Vec<_>>>()?
304                .into_iter()
305                .map(|(expr, _)| expr)
306                .collect();
307            Some((
308                Expr::WindowFunctionCall {
309                    name: name.clone(),
310                    args,
311                    window: (**window).clone(),
312                    span: Span::synthetic(),
313                },
314                alias.clone(),
315            ))
316        }
317    }
318}
319
320fn projection_column_to_expr(column: &str) -> Expr {
321    if let Some(value) = projection_literal_value(column) {
322        return Expr::Literal {
323            value,
324            span: Span::synthetic(),
325        };
326    }
327
328    Expr::Column {
329        field: FieldRef::TableColumn {
330            table: String::new(),
331            column: column.to_string(),
332        },
333        span: Span::synthetic(),
334    }
335}
336
337fn projection_literal_value(column: &str) -> Option<Value> {
338    let literal = column.strip_prefix("LIT:")?;
339    if literal.is_empty() {
340        return Some(Value::Null);
341    }
342    if let Ok(value) = literal.parse::<i64>() {
343        return Some(Value::Integer(value));
344    }
345    if let Ok(value) = literal.parse::<f64>() {
346        return Some(Value::Float(value));
347    }
348    Some(Value::text(literal.to_string()))
349}
350
351pub fn projection_to_select_item(projection: &Projection) -> Option<SelectItem> {
352    match projection {
353        Projection::All => Some(SelectItem::Wildcard),
354        other => {
355            let (expr, alias) = projection_to_expr(other)?;
356            Some(SelectItem::Expr { expr, alias })
357        }
358    }
359}
360
361pub fn effective_join_projections(query: &JoinQuery) -> Vec<Projection> {
362    if !query.return_items.is_empty() {
363        return query
364            .return_items
365            .iter()
366            .filter_map(select_item_to_projection)
367            .collect();
368    }
369    query.return_.clone()
370}
371
372pub fn expr_to_filter(expr: &Expr) -> Filter {
373    match expr {
374        Expr::BinaryOp { op, lhs, rhs, .. } => match op {
375            BinOp::And => Filter::And(Box::new(expr_to_filter(lhs)), Box::new(expr_to_filter(rhs))),
376            BinOp::Or => Filter::Or(Box::new(expr_to_filter(lhs)), Box::new(expr_to_filter(rhs))),
377            BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge => {
378                try_specialized_compare_filter(lhs, *op, rhs).unwrap_or_else(|| {
379                    Filter::CompareExpr {
380                        lhs: lhs.as_ref().clone(),
381                        op: binop_to_compare_op(*op),
382                        rhs: rhs.as_ref().clone(),
383                    }
384                })
385            }
386            BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod | BinOp::Concat => {
387                Filter::CompareExpr {
388                    lhs: expr.clone(),
389                    op: CompareOp::Eq,
390                    rhs: Expr::lit(Value::Boolean(true)),
391                }
392            }
393        },
394        Expr::UnaryOp {
395            op: UnaryOp::Not,
396            operand,
397            ..
398        } => Filter::Not(Box::new(expr_to_filter(operand))),
399        Expr::IsNull {
400            operand, negated, ..
401        } => match operand.as_ref() {
402            Expr::Column { field, .. } => {
403                if *negated {
404                    Filter::IsNotNull(field.clone())
405                } else {
406                    Filter::IsNull(field.clone())
407                }
408            }
409            _ => Filter::CompareExpr {
410                lhs: expr.clone(),
411                op: CompareOp::Eq,
412                rhs: Expr::lit(Value::Boolean(true)),
413            },
414        },
415        Expr::InList {
416            target,
417            values,
418            negated,
419            ..
420        } => match (target.as_ref(), all_literal_values(values)) {
421            (Expr::Column { field, .. }, Some(values)) if !negated => Filter::In {
422                field: field.clone(),
423                values,
424            },
425            _ => Filter::CompareExpr {
426                lhs: expr.clone(),
427                op: CompareOp::Eq,
428                rhs: Expr::lit(Value::Boolean(true)),
429            },
430        },
431        Expr::Between {
432            target,
433            low,
434            high,
435            negated,
436            ..
437        } => match (
438            target.as_ref(),
439            literal_expr_value(low),
440            literal_expr_value(high),
441        ) {
442            (Expr::Column { field, .. }, Some(low), Some(high)) if !negated => Filter::Between {
443                field: field.clone(),
444                low,
445                high,
446            },
447            _ => Filter::CompareExpr {
448                lhs: expr.clone(),
449                op: CompareOp::Eq,
450                rhs: Expr::lit(Value::Boolean(true)),
451            },
452        },
453        Expr::Subquery { .. } => Filter::CompareExpr {
454            lhs: expr.clone(),
455            op: CompareOp::Eq,
456            rhs: Expr::lit(Value::Boolean(true)),
457        },
458        // Reverse-lower the string-predicate FunctionCall forms emitted by
459        // `filter_to_expr` (`LIKE`, `STARTS_WITH`, `ENDS_WITH`, `CONTAINS`)
460        // back to the typed `Filter` variants. The runtime filter
461        // evaluators (`runtime::join_filter`, virtual `red.*` reads) only
462        // understand the typed variants; without this round-trip step a
463        // `WHERE path STARTS WITH 'infra'` clause survives the parser as
464        // `Filter::StartsWith` but is reduced to a `where_expr`-only
465        // `FunctionCall` after subquery resolution clears `table.filter`,
466        // and `effective_table_filter` would then fall through to a
467        // generic `CompareExpr(FunctionCall, =, true)` that no virtual
468        // table can evaluate. Refs #785.
469        Expr::FunctionCall { name, args, .. } => string_predicate_from_function_call(name, args)
470            .unwrap_or_else(|| Filter::CompareExpr {
471                lhs: expr.clone(),
472                op: CompareOp::Eq,
473                rhs: Expr::lit(Value::Boolean(true)),
474            }),
475        _ => Filter::CompareExpr {
476            lhs: expr.clone(),
477            op: CompareOp::Eq,
478            rhs: Expr::lit(Value::Boolean(true)),
479        },
480    }
481}
482
483fn string_predicate_from_function_call(name: &str, args: &[Expr]) -> Option<Filter> {
484    if args.len() != 2 {
485        return None;
486    }
487    let field = match &args[0] {
488        Expr::Column { field, .. } => field.clone(),
489        _ => return None,
490    };
491    let text = match &args[1] {
492        Expr::Literal {
493            value: Value::Text(value),
494            ..
495        } => value.as_ref().to_string(),
496        _ => return None,
497    };
498    if name.eq_ignore_ascii_case("LIKE") {
499        Some(Filter::Like {
500            field,
501            pattern: text,
502        })
503    } else if name.eq_ignore_ascii_case("STARTS_WITH") {
504        Some(Filter::StartsWith {
505            field,
506            prefix: text,
507        })
508    } else if name.eq_ignore_ascii_case("ENDS_WITH") {
509        Some(Filter::EndsWith {
510            field,
511            suffix: text,
512        })
513    } else if name.eq_ignore_ascii_case("CONTAINS") {
514        Some(Filter::Contains {
515            field,
516            substring: text,
517        })
518    } else {
519        None
520    }
521}
522
523pub fn boolean_expr_projection(expr: Expr) -> Projection {
524    Projection::Expression(
525        Box::new(Filter::CompareExpr {
526            lhs: expr,
527            op: CompareOp::Eq,
528            rhs: Expr::Literal {
529                value: Value::Boolean(true),
530                span: Span::synthetic(),
531            },
532        }),
533        None,
534    )
535}
536
537pub fn filter_to_expr(filter: &Filter) -> Expr {
538    match filter {
539        Filter::Compare { field, op, value } => Expr::BinaryOp {
540            op: compare_op_to_binop(*op),
541            lhs: Box::new(Expr::Column {
542                field: field.clone(),
543                span: Span::synthetic(),
544            }),
545            rhs: Box::new(Expr::Literal {
546                value: value.clone(),
547                span: Span::synthetic(),
548            }),
549            span: Span::synthetic(),
550        },
551        Filter::CompareFields { left, op, right } => Expr::BinaryOp {
552            op: compare_op_to_binop(*op),
553            lhs: Box::new(Expr::Column {
554                field: left.clone(),
555                span: Span::synthetic(),
556            }),
557            rhs: Box::new(Expr::Column {
558                field: right.clone(),
559                span: Span::synthetic(),
560            }),
561            span: Span::synthetic(),
562        },
563        Filter::CompareExpr { lhs, op, rhs } => Expr::BinaryOp {
564            op: compare_op_to_binop(*op),
565            lhs: Box::new(lhs.clone()),
566            rhs: Box::new(rhs.clone()),
567            span: Span::synthetic(),
568        },
569        Filter::And(left, right) => Expr::BinaryOp {
570            op: BinOp::And,
571            lhs: Box::new(filter_to_expr(left)),
572            rhs: Box::new(filter_to_expr(right)),
573            span: Span::synthetic(),
574        },
575        Filter::Or(left, right) => Expr::BinaryOp {
576            op: BinOp::Or,
577            lhs: Box::new(filter_to_expr(left)),
578            rhs: Box::new(filter_to_expr(right)),
579            span: Span::synthetic(),
580        },
581        Filter::Not(inner) => Expr::UnaryOp {
582            op: UnaryOp::Not,
583            operand: Box::new(filter_to_expr(inner)),
584            span: Span::synthetic(),
585        },
586        Filter::IsNull(field) => Expr::IsNull {
587            operand: Box::new(Expr::Column {
588                field: field.clone(),
589                span: Span::synthetic(),
590            }),
591            negated: false,
592            span: Span::synthetic(),
593        },
594        Filter::IsNotNull(field) => Expr::IsNull {
595            operand: Box::new(Expr::Column {
596                field: field.clone(),
597                span: Span::synthetic(),
598            }),
599            negated: true,
600            span: Span::synthetic(),
601        },
602        Filter::In { field, values } => Expr::InList {
603            target: Box::new(Expr::Column {
604                field: field.clone(),
605                span: Span::synthetic(),
606            }),
607            values: values
608                .iter()
609                .cloned()
610                .map(|value| Expr::Literal {
611                    value,
612                    span: Span::synthetic(),
613                })
614                .collect(),
615            negated: false,
616            span: Span::synthetic(),
617        },
618        Filter::Between { field, low, high } => Expr::Between {
619            target: Box::new(Expr::Column {
620                field: field.clone(),
621                span: Span::synthetic(),
622            }),
623            low: Box::new(Expr::Literal {
624                value: low.clone(),
625                span: Span::synthetic(),
626            }),
627            high: Box::new(Expr::Literal {
628                value: high.clone(),
629                span: Span::synthetic(),
630            }),
631            negated: false,
632            span: Span::synthetic(),
633        },
634        Filter::Like { field, pattern } => Expr::FunctionCall {
635            name: "LIKE".to_string(),
636            args: vec![
637                Expr::Column {
638                    field: field.clone(),
639                    span: Span::synthetic(),
640                },
641                Expr::Literal {
642                    value: Value::text(pattern.clone()),
643                    span: Span::synthetic(),
644                },
645            ],
646            span: Span::synthetic(),
647        },
648        Filter::StartsWith { field, prefix } => Expr::FunctionCall {
649            name: "STARTS_WITH".to_string(),
650            args: vec![
651                Expr::Column {
652                    field: field.clone(),
653                    span: Span::synthetic(),
654                },
655                Expr::Literal {
656                    value: Value::text(prefix.clone()),
657                    span: Span::synthetic(),
658                },
659            ],
660            span: Span::synthetic(),
661        },
662        Filter::EndsWith { field, suffix } => Expr::FunctionCall {
663            name: "ENDS_WITH".to_string(),
664            args: vec![
665                Expr::Column {
666                    field: field.clone(),
667                    span: Span::synthetic(),
668                },
669                Expr::Literal {
670                    value: Value::text(suffix.clone()),
671                    span: Span::synthetic(),
672                },
673            ],
674            span: Span::synthetic(),
675        },
676        Filter::Contains { field, substring } => Expr::FunctionCall {
677            name: "CONTAINS".to_string(),
678            args: vec![
679                Expr::Column {
680                    field: field.clone(),
681                    span: Span::synthetic(),
682                },
683                Expr::Literal {
684                    value: Value::text(substring.clone()),
685                    span: Span::synthetic(),
686                },
687            ],
688            span: Span::synthetic(),
689        },
690    }
691}
692
693pub fn projection_from_literal(value: &Value) -> Option<Projection> {
694    match value {
695        Value::Boolean(_) => Some(boolean_expr_projection(Expr::Literal {
696            value: value.clone(),
697            span: Span::synthetic(),
698        })),
699        _ => Some(Projection::Column(format!(
700            "LIT:{}",
701            render_projection_literal(value)
702        ))),
703    }
704}
705
706pub fn case_condition_projection(condition: Expr) -> Projection {
707    Projection::Expression(
708        Box::new(Filter::CompareExpr {
709            lhs: condition,
710            op: CompareOp::Eq,
711            rhs: Expr::Literal {
712                value: Value::Boolean(true),
713                span: Span::synthetic(),
714            },
715        }),
716        None,
717    )
718}
719
720pub fn fold_expr_to_value(expr: Expr) -> Result<Value, String> {
721    match expr {
722        Expr::Literal { value, .. } => Ok(value),
723        Expr::FunctionCall { name, args, .. } => {
724            if (name.eq_ignore_ascii_case("PASSWORD") || name.eq_ignore_ascii_case("SECRET"))
725                && args.len() == 1
726            {
727                let plaintext = match fold_expr_to_value(args.into_iter().next().unwrap())? {
728                    Value::Text(text) => text,
729                    other => {
730                        return Err(format!(
731                            "{name}() expects a string literal argument, got {other:?}"
732                        ))
733                    }
734                };
735                return Ok(if name.eq_ignore_ascii_case("PASSWORD") {
736                    Value::Password(format!("@@plain@@{plaintext}"))
737                } else {
738                    Value::Secret(format!("@@plain@@{plaintext}").into_bytes())
739                });
740            }
741            Err(format!(
742                "expression is not a foldable literal: FunctionCall({name})"
743            ))
744        }
745        Expr::UnaryOp { op, operand, .. } => {
746            let inner = fold_expr_to_value(*operand)?;
747            match (op, inner) {
748                (UnaryOp::Neg, Value::Integer(n)) => Ok(Value::Integer(-n)),
749                (UnaryOp::Neg, Value::UnsignedInteger(n)) => Ok(Value::Integer(-(n as i64))),
750                (UnaryOp::Neg, Value::Float(f)) => Ok(Value::Float(-f)),
751                (UnaryOp::Not, Value::Boolean(b)) => Ok(Value::Boolean(!b)),
752                (other_op, other) => Err(format!(
753                    "unary `{other_op:?}` cannot fold to literal Value (operand: {other:?})"
754                )),
755            }
756        }
757        Expr::Cast { inner, .. } => fold_expr_to_value(*inner),
758        other => Err(format!("expression is not a foldable literal: {other:?}")),
759    }
760}
761
762fn projection_binop_name(op: BinOp) -> &'static str {
763    match op {
764        BinOp::Add => "ADD",
765        BinOp::Sub => "SUB",
766        BinOp::Mul => "MUL",
767        BinOp::Div => "DIV",
768        BinOp::Mod => "MOD",
769        BinOp::Concat => "CONCAT",
770        BinOp::Eq
771        | BinOp::Ne
772        | BinOp::Lt
773        | BinOp::Le
774        | BinOp::Gt
775        | BinOp::Ge
776        | BinOp::And
777        | BinOp::Or => {
778            unreachable!("boolean operators are lowered through Projection::Expression")
779        }
780    }
781}
782
783// SQL-text label rendering for projection expressions. Retained verbatim from
784// the pre-move module (no live caller today); kept for the upcoming Fase 2
785// projection-aliasing work rather than dropped during the byte-faithful move.
786#[allow(dead_code)]
787fn render_expr_label(expr: &Expr) -> String {
788    render_expr_label_prec(expr, 0)
789}
790
791#[allow(dead_code)]
792fn render_expr_label_prec(expr: &Expr, parent_prec: u8) -> String {
793    match expr {
794        Expr::Literal { value, .. } => render_sql_literal_label(value),
795        Expr::Column { field, .. } => render_field_label(field),
796        Expr::Parameter { index, .. } => format!("${index}"),
797        Expr::BinaryOp { op, lhs, rhs, .. } => {
798            let prec = op.precedence();
799            let rendered = format!(
800                "{} {} {}",
801                render_expr_label_prec(lhs, prec),
802                render_binop_label(*op),
803                render_expr_label_prec(rhs, prec + 1)
804            );
805            if prec < parent_prec {
806                format!("({rendered})")
807            } else {
808                rendered
809            }
810        }
811        Expr::UnaryOp { op, operand, .. } => match op {
812            UnaryOp::Neg => format!("-{}", render_expr_label_prec(operand, u8::MAX)),
813            UnaryOp::Not => format!("NOT {}", render_expr_label_prec(operand, u8::MAX)),
814        },
815        Expr::Cast { inner, target, .. } => {
816            format!("CAST({} AS {target})", render_expr_label(inner))
817        }
818        Expr::FunctionCall { name, args, .. } => {
819            let args = args
820                .iter()
821                .map(render_expr_label)
822                .collect::<Vec<_>>()
823                .join(", ");
824            format!("{name}({args})")
825        }
826        Expr::Case {
827            branches, else_, ..
828        } => {
829            let mut out = String::from("CASE");
830            for (condition, value) in branches {
831                out.push_str(" WHEN ");
832                out.push_str(&render_expr_label(condition));
833                out.push_str(" THEN ");
834                out.push_str(&render_expr_label(value));
835            }
836            if let Some(else_expr) = else_ {
837                out.push_str(" ELSE ");
838                out.push_str(&render_expr_label(else_expr));
839            }
840            out.push_str(" END");
841            out
842        }
843        Expr::IsNull {
844            operand, negated, ..
845        } => {
846            let op = if *negated { "IS NOT NULL" } else { "IS NULL" };
847            format!("{} {op}", render_expr_label_prec(operand, u8::MAX))
848        }
849        Expr::InList {
850            target,
851            values,
852            negated,
853            ..
854        } => {
855            let op = if *negated { "NOT IN" } else { "IN" };
856            let values = values
857                .iter()
858                .map(render_expr_label)
859                .collect::<Vec<_>>()
860                .join(", ");
861            format!("{} {op} ({values})", render_expr_label(target))
862        }
863        Expr::Between {
864            target,
865            low,
866            high,
867            negated,
868            ..
869        } => {
870            let op = if *negated { "NOT BETWEEN" } else { "BETWEEN" };
871            format!(
872                "{} {op} {} AND {}",
873                render_expr_label(target),
874                render_expr_label(low),
875                render_expr_label(high)
876            )
877        }
878        Expr::Subquery { .. } => "subquery".to_string(),
879        Expr::WindowFunctionCall { name, args, .. } => {
880            let args = args
881                .iter()
882                .map(render_expr_label)
883                .collect::<Vec<_>>()
884                .join(", ");
885            format!("{name}({args}) OVER (...)")
886        }
887    }
888}
889
890#[allow(dead_code)]
891fn render_binop_label(op: BinOp) -> &'static str {
892    match op {
893        BinOp::Add => "+",
894        BinOp::Sub => "-",
895        BinOp::Mul => "*",
896        BinOp::Div => "/",
897        BinOp::Mod => "%",
898        BinOp::Concat => "||",
899        BinOp::Eq => "=",
900        BinOp::Ne => "!=",
901        BinOp::Lt => "<",
902        BinOp::Le => "<=",
903        BinOp::Gt => ">",
904        BinOp::Ge => ">=",
905        BinOp::And => "AND",
906        BinOp::Or => "OR",
907    }
908}
909
910#[allow(dead_code)]
911fn render_field_label(field: &FieldRef) -> String {
912    match field {
913        FieldRef::TableColumn { table, column } => {
914            if table.is_empty() {
915                column.clone()
916            } else {
917                format!("{table}.{column}")
918            }
919        }
920        FieldRef::NodeProperty { alias, property } => format!("{alias}.{property}"),
921        FieldRef::EdgeProperty { alias, property } => format!("{alias}.{property}"),
922        FieldRef::NodeId { alias } => format!("{alias}.id"),
923    }
924}
925
926#[allow(dead_code)]
927fn render_sql_literal_label(value: &Value) -> String {
928    match value {
929        Value::Null => "NULL".to_string(),
930        Value::Text(value) => format!("'{}'", value.replace('\'', "''")),
931        Value::Boolean(value) => value.to_string(),
932        Value::Integer(value) => value.to_string(),
933        Value::UnsignedInteger(value) => value.to_string(),
934        Value::Float(value) => {
935            if value.fract().abs() < f64::EPSILON {
936                (*value as i64).to_string()
937            } else {
938                value.to_string()
939            }
940        }
941        other => other.to_string(),
942    }
943}
944
945fn binop_to_compare_op(op: BinOp) -> CompareOp {
946    match op {
947        BinOp::Eq => CompareOp::Eq,
948        BinOp::Ne => CompareOp::Ne,
949        BinOp::Lt => CompareOp::Lt,
950        BinOp::Le => CompareOp::Le,
951        BinOp::Gt => CompareOp::Gt,
952        BinOp::Ge => CompareOp::Ge,
953        other => unreachable!("non-compare binop cannot lower to CompareOp: {other:?}"),
954    }
955}
956
957fn compare_op_to_binop(op: CompareOp) -> BinOp {
958    match op {
959        CompareOp::Eq => BinOp::Eq,
960        CompareOp::Ne => BinOp::Ne,
961        CompareOp::Lt => BinOp::Lt,
962        CompareOp::Le => BinOp::Le,
963        CompareOp::Gt => BinOp::Gt,
964        CompareOp::Ge => BinOp::Ge,
965    }
966}
967
968fn attach_projection_alias(proj: Projection, alias: Option<String>) -> Projection {
969    let Some(alias) = alias else { return proj };
970    match proj {
971        Projection::Field(f, _) => Projection::Field(f, Some(alias)),
972        Projection::Expression(filter, _) => Projection::Expression(filter, Some(alias)),
973        Projection::Function(name, args) => {
974            if name.contains(':') {
975                Projection::Function(name, args)
976            } else {
977                Projection::Function(format!("{name}:{alias}"), args)
978            }
979        }
980        Projection::Column(c) => Projection::Alias(c, alias),
981        Projection::Window {
982            name, args, window, ..
983        } => Projection::Window {
984            name,
985            args,
986            window,
987            alias: Some(alias),
988        },
989        other => other,
990    }
991}
992
993fn split_projection_function_alias(name: &str) -> (String, Option<String>) {
994    match name.split_once(':') {
995        Some((function, alias)) if !function.is_empty() && !alias.is_empty() => {
996            (function.to_string(), Some(alias.to_string()))
997        }
998        _ => (name.to_string(), None),
999    }
1000}
1001
1002fn render_projection_literal(value: &Value) -> String {
1003    match value {
1004        Value::Null => String::new(),
1005        Value::Integer(v) => v.to_string(),
1006        Value::UnsignedInteger(v) => v.to_string(),
1007        Value::Float(v) => {
1008            if v.fract().abs() < f64::EPSILON {
1009                (*v as i64).to_string()
1010            } else {
1011                v.to_string()
1012            }
1013        }
1014        Value::Text(v) => v.to_string(),
1015        Value::Boolean(true) => "true".to_string(),
1016        Value::Boolean(false) => "false".to_string(),
1017        // Composite values (arrays, vectors, blobs) would lose fidelity
1018        // going through `Display` — `Vec<Value>` turns into
1019        // "<vector dim=N>". Use a JSON sentinel so the reader in
1020        // `eval_projection_value` can round-trip the exact Value.
1021        Value::Array(_) | Value::Vector(_) | Value::Json(_) | Value::Blob(_) => {
1022            format!("@RL:{}", serialize_value_json(value))
1023        }
1024        other => other.to_string(),
1025    }
1026}
1027
1028fn serialize_value_json(value: &Value) -> String {
1029    // Uses `crate::serde_json` which is already a workspace dep.
1030    match value {
1031        Value::Array(items) => {
1032            let mut out = String::from("[");
1033            for (i, item) in items.iter().enumerate() {
1034                if i > 0 {
1035                    out.push(',');
1036                }
1037                out.push_str(&serialize_value_json(item));
1038            }
1039            out.push(']');
1040            out
1041        }
1042        Value::Vector(items) => {
1043            let mut out = String::from("V[");
1044            for (i, f) in items.iter().enumerate() {
1045                if i > 0 {
1046                    out.push(',');
1047                }
1048                out.push_str(&f.to_string());
1049            }
1050            out.push(']');
1051            out
1052        }
1053        Value::Integer(n) | Value::BigInt(n) => n.to_string(),
1054        Value::UnsignedInteger(n) => n.to_string(),
1055        Value::Float(f) => f.to_string(),
1056        Value::Text(s) => format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")),
1057        Value::Boolean(b) => b.to_string(),
1058        Value::Null => "null".to_string(),
1059        other => format!("\"{}\"", other.to_string().replace('"', "\\\"")),
1060    }
1061}
1062
1063fn try_specialized_compare_filter(lhs: &Expr, op: BinOp, rhs: &Expr) -> Option<Filter> {
1064    let op = binop_to_compare_op(op);
1065    match (lhs, rhs) {
1066        (Expr::Column { field, .. }, Expr::Literal { value, .. }) => Some(Filter::Compare {
1067            field: field.clone(),
1068            op,
1069            value: value.clone(),
1070        }),
1071        (Expr::Literal { value, .. }, Expr::Column { field, .. }) => Some(Filter::Compare {
1072            field: field.clone(),
1073            op: flipped_compare_op(op),
1074            value: value.clone(),
1075        }),
1076        (Expr::Column { field: left, .. }, Expr::Column { field: right, .. }) => {
1077            Some(Filter::CompareFields {
1078                left: left.clone(),
1079                op,
1080                right: right.clone(),
1081            })
1082        }
1083        _ => None,
1084    }
1085}
1086
1087fn flipped_compare_op(op: CompareOp) -> CompareOp {
1088    match op {
1089        CompareOp::Eq => CompareOp::Eq,
1090        CompareOp::Ne => CompareOp::Ne,
1091        CompareOp::Lt => CompareOp::Gt,
1092        CompareOp::Le => CompareOp::Ge,
1093        CompareOp::Gt => CompareOp::Lt,
1094        CompareOp::Ge => CompareOp::Le,
1095    }
1096}
1097
1098fn literal_expr_value(expr: &Expr) -> Option<Value> {
1099    match expr {
1100        Expr::Literal { value, .. } => Some(value.clone()),
1101        _ => None,
1102    }
1103}
1104
1105fn all_literal_values(values: &[Expr]) -> Option<Vec<Value>> {
1106    values.iter().map(literal_expr_value).collect()
1107}
1108
1109#[cfg(test)]
1110mod tests {
1111    use super::*;
1112    use crate::ast::{
1113        GraphPattern, GraphQuery, JoinCondition, JoinQuery, NodeSelector, OrderByClause, PathQuery,
1114        QueryExpr, VectorQuery, VectorSource, WindowOrderItem, WindowSpec,
1115    };
1116
1117    fn field(name: &str) -> FieldRef {
1118        FieldRef::column("", name)
1119    }
1120
1121    fn col(name: &str) -> Expr {
1122        Expr::Column {
1123            field: field(name),
1124            span: Span::synthetic(),
1125        }
1126    }
1127
1128    fn lit(value: Value) -> Expr {
1129        Expr::Literal {
1130            value,
1131            span: Span::synthetic(),
1132        }
1133    }
1134
1135    fn parameter(index: usize) -> Expr {
1136        Expr::Parameter {
1137            index,
1138            span: Span::synthetic(),
1139        }
1140    }
1141
1142    fn bin(op: BinOp, lhs: Expr, rhs: Expr) -> Expr {
1143        Expr::BinaryOp {
1144            op,
1145            lhs: Box::new(lhs),
1146            rhs: Box::new(rhs),
1147            span: Span::synthetic(),
1148        }
1149    }
1150
1151    #[test]
1152    fn expr_contains_parameter_walks_nested_expression_shapes() {
1153        assert!(expr_contains_parameter(&bin(
1154            BinOp::Add,
1155            col("age"),
1156            parameter(1)
1157        )));
1158
1159        let case = Expr::Case {
1160            branches: vec![(col("active"), lit(Value::Integer(1)))],
1161            else_: Some(Box::new(parameter(2))),
1162            span: Span::synthetic(),
1163        };
1164        assert!(expr_contains_parameter(&case));
1165
1166        let window = Expr::WindowFunctionCall {
1167            name: "row_number".to_string(),
1168            args: Vec::new(),
1169            window: WindowSpec {
1170                partition_by: vec![col("tenant_id")],
1171                order_by: vec![WindowOrderItem {
1172                    expr: parameter(3),
1173                    ascending: true,
1174                    nulls_first: false,
1175                }],
1176                frame: None,
1177            },
1178            span: Span::synthetic(),
1179        };
1180        assert!(expr_contains_parameter(&window));
1181
1182        let no_parameter = Expr::FunctionCall {
1183            name: "lower".to_string(),
1184            args: vec![col("name")],
1185            span: Span::synthetic(),
1186        };
1187        assert!(!expr_contains_parameter(&no_parameter));
1188    }
1189
1190    #[test]
1191    fn expressions_lower_to_legacy_projections_with_aliases_preserved() {
1192        assert!(matches!(
1193            expr_to_projection(&col("*")),
1194            Some(Projection::All)
1195        ));
1196
1197        let param_projection = expr_to_projection(&parameter(7)).unwrap();
1198        assert!(matches!(
1199            param_projection,
1200            Projection::Column(value) if value == format!("{PARAMETER_PROJECTION_PREFIX}7")
1201        ));
1202
1203        let arithmetic = bin(BinOp::Add, col("age"), lit(Value::Integer(1)));
1204        let projection = select_item_to_projection(&SelectItem::Expr {
1205            expr: arithmetic,
1206            alias: Some("age_plus_one".to_string()),
1207        })
1208        .unwrap();
1209        assert!(matches!(
1210            projection,
1211            Projection::Function(ref name, ref args)
1212                if name == "ADD:age_plus_one" && args.len() == 2
1213        ));
1214
1215        let negated = Expr::UnaryOp {
1216            op: UnaryOp::Neg,
1217            operand: Box::new(col("age")),
1218            span: Span::synthetic(),
1219        };
1220        assert!(matches!(
1221            expr_to_projection(&negated),
1222            Some(Projection::Function(name, args)) if name == "SUB" && args.len() == 2
1223        ));
1224
1225        let cast = Expr::Cast {
1226            inner: Box::new(col("age")),
1227            target: reddb_types::types::DataType::Text,
1228            span: Span::synthetic(),
1229        };
1230        assert!(matches!(
1231            expr_to_projection(&cast),
1232            Some(Projection::Function(name, args)) if name == "CAST" && args.len() == 2
1233        ));
1234
1235        let window = Expr::WindowFunctionCall {
1236            name: "sum".to_string(),
1237            args: vec![col("amount")],
1238            window: WindowSpec::default(),
1239            span: Span::synthetic(),
1240        };
1241        assert!(matches!(
1242            select_item_to_projection(&SelectItem::Expr {
1243                expr: window,
1244                alias: Some("running_sum".to_string()),
1245            }),
1246            Some(Projection::Window { name, alias, .. })
1247                if name == "SUM" && alias.as_deref() == Some("running_sum")
1248        ));
1249    }
1250
1251    #[test]
1252    fn projections_raise_back_to_select_items_and_expression_nodes() {
1253        assert!(matches!(
1254            projection_to_select_item(&Projection::All),
1255            Some(SelectItem::Wildcard)
1256        ));
1257
1258        let literal = projection_to_expr(&Projection::Column("LIT:42".to_string())).unwrap();
1259        assert!(matches!(
1260            literal,
1261            (
1262                Expr::Literal {
1263                    value: Value::Integer(42),
1264                    ..
1265                },
1266                None
1267            )
1268        ));
1269
1270        let float_literal = projection_to_expr(&Projection::Column("LIT:3.5".to_string())).unwrap();
1271        assert!(matches!(
1272            float_literal,
1273            (Expr::Literal { value: Value::Float(v), .. }, None) if (v - 3.5).abs() < f64::EPSILON
1274        ));
1275
1276        let null_literal = projection_to_expr(&Projection::Column("LIT:".to_string())).unwrap();
1277        assert!(matches!(
1278            null_literal,
1279            (
1280                Expr::Literal {
1281                    value: Value::Null,
1282                    ..
1283                },
1284                None
1285            )
1286        ));
1287
1288        let function = Projection::Function(
1289            "LOWER:lower_name".to_string(),
1290            vec![Projection::Field(field("name"), None)],
1291        );
1292        let (expr, alias) = projection_to_expr(&function).unwrap();
1293        assert_eq!(alias.as_deref(), Some("lower_name"));
1294        assert!(
1295            matches!(expr, Expr::FunctionCall { name, args, .. } if name == "LOWER" && args.len() == 1)
1296        );
1297
1298        let window = Projection::Window {
1299            name: "ROW_NUMBER".to_string(),
1300            args: Vec::new(),
1301            window: Box::new(WindowSpec::default()),
1302            alias: Some("rn".to_string()),
1303        };
1304        let (expr, alias) = projection_to_expr(&window).unwrap();
1305        assert_eq!(alias.as_deref(), Some("rn"));
1306        assert!(matches!(expr, Expr::WindowFunctionCall { name, .. } if name == "ROW_NUMBER"));
1307    }
1308
1309    #[test]
1310    fn filters_round_trip_through_expression_forms() {
1311        let filters = vec![
1312            Filter::Compare {
1313                field: field("age"),
1314                op: CompareOp::Ge,
1315                value: Value::Integer(18),
1316            },
1317            Filter::CompareFields {
1318                left: field("updated_at"),
1319                op: CompareOp::Gt,
1320                right: field("created_at"),
1321            },
1322            Filter::And(
1323                Box::new(Filter::IsNotNull(field("email"))),
1324                Box::new(Filter::Like {
1325                    field: field("email"),
1326                    pattern: "%@example.com".to_string(),
1327                }),
1328            ),
1329            Filter::Or(
1330                Box::new(Filter::StartsWith {
1331                    field: field("path"),
1332                    prefix: "infra/".to_string(),
1333                }),
1334                Box::new(Filter::EndsWith {
1335                    field: field("path"),
1336                    suffix: ".log".to_string(),
1337                }),
1338            ),
1339            Filter::Not(Box::new(Filter::Contains {
1340                field: field("body"),
1341                substring: "secret".to_string(),
1342            })),
1343            Filter::IsNull(field("deleted_at")),
1344            Filter::In {
1345                field: field("status"),
1346                values: vec![Value::text("open"), Value::text("pending")],
1347            },
1348            Filter::Between {
1349                field: field("score"),
1350                low: Value::Integer(10),
1351                high: Value::Integer(20),
1352            },
1353        ];
1354
1355        for filter in filters {
1356            let expr = filter_to_expr(&filter);
1357            assert_eq!(expr_to_filter(&expr), filter);
1358        }
1359    }
1360
1361    #[test]
1362    fn expression_filters_specialize_common_predicates_and_fallbacks() {
1363        let flipped = expr_to_filter(&bin(BinOp::Lt, lit(Value::Integer(10)), col("age")));
1364        assert_eq!(
1365            flipped,
1366            Filter::Compare {
1367                field: field("age"),
1368                op: CompareOp::Gt,
1369                value: Value::Integer(10),
1370            }
1371        );
1372
1373        let field_to_field = expr_to_filter(&bin(BinOp::Eq, col("lhs"), col("rhs")));
1374        assert_eq!(
1375            field_to_field,
1376            Filter::CompareFields {
1377                left: field("lhs"),
1378                op: CompareOp::Eq,
1379                right: field("rhs"),
1380            }
1381        );
1382
1383        let arithmetic = expr_to_filter(&bin(BinOp::Add, col("age"), lit(Value::Integer(1))));
1384        assert!(matches!(
1385            arithmetic,
1386            Filter::CompareExpr {
1387                op: CompareOp::Eq,
1388                rhs: Expr::Literal {
1389                    value: Value::Boolean(true),
1390                    ..
1391                },
1392                ..
1393            }
1394        ));
1395
1396        let negated_in = Expr::InList {
1397            target: Box::new(col("status")),
1398            values: vec![lit(Value::text("closed"))],
1399            negated: true,
1400            span: Span::synthetic(),
1401        };
1402        assert!(matches!(
1403            expr_to_filter(&negated_in),
1404            Filter::CompareExpr {
1405                op: CompareOp::Eq,
1406                ..
1407            }
1408        ));
1409    }
1410
1411    #[test]
1412    fn table_effective_helpers_prefer_canonical_expr_fields() {
1413        let mut query = TableQuery::new("users");
1414        query.select_items = vec![
1415            SelectItem::Expr {
1416                expr: col("name"),
1417                alias: Some("display_name".to_string()),
1418            },
1419            SelectItem::Expr {
1420                expr: bin(BinOp::Add, col("age"), lit(Value::Integer(1))),
1421                alias: Some("next_age".to_string()),
1422            },
1423        ];
1424        query.where_expr = Some(bin(BinOp::Eq, col("active"), lit(Value::Boolean(true))));
1425        query.group_by_exprs = vec![col("name")];
1426        query.group_by = vec!["legacy_group".to_string()];
1427        query.having_expr = Some(bin(BinOp::Gt, col("age"), lit(Value::Integer(18))));
1428
1429        let projections = effective_table_projections(&query);
1430        assert_eq!(projections.len(), 2);
1431        assert!(matches!(
1432            &projections[0],
1433            Projection::Field(FieldRef::TableColumn { column, .. }, Some(alias))
1434                if column == "name" && alias == "display_name"
1435        ));
1436
1437        assert!(matches!(
1438            effective_table_filter(&query),
1439            Some(Filter::Compare {
1440                field: FieldRef::TableColumn { column, .. },
1441                op: CompareOp::Eq,
1442                value: Value::Boolean(true)
1443            }) if column == "active"
1444        ));
1445        assert_eq!(effective_table_group_by_exprs(&query), vec![col("name")]);
1446        assert!(matches!(
1447            effective_table_having_filter(&query),
1448            Some(Filter::Compare {
1449                field: FieldRef::TableColumn { column, .. },
1450                op: CompareOp::Gt,
1451                value: Value::Integer(18)
1452            }) if column == "age"
1453        ));
1454
1455        let mut legacy = TableQuery::new("users");
1456        legacy.columns = vec![Projection::column("id")];
1457        legacy.group_by = vec!["tenant_id".to_string()];
1458        assert!(matches!(
1459            effective_table_projections(&legacy).as_slice(),
1460            [Projection::Column(column)] if column == "id"
1461        ));
1462        assert_eq!(
1463            effective_table_group_by_exprs(&legacy),
1464            vec![Expr::Column {
1465                field: field("tenant_id"),
1466                span: Span::synthetic(),
1467            }]
1468        );
1469
1470        let default_projection = TableQuery::new("users");
1471        assert!(matches!(
1472            effective_table_projections(&default_projection).as_slice(),
1473            [Projection::All]
1474        ));
1475    }
1476
1477    #[test]
1478    fn non_table_effective_helpers_preserve_existing_query_fields() {
1479        let mut join = JoinQuery::new(
1480            QueryExpr::Table(TableQuery::new("users")),
1481            QueryExpr::Graph(GraphQuery::new(GraphPattern::new())),
1482            JoinCondition::new(field("id"), FieldRef::node_id("n")),
1483        );
1484        join.filter = Some(Filter::IsNotNull(field("id")));
1485        join.return_items = vec![SelectItem::Expr {
1486            expr: col("name"),
1487            alias: Some("display_name".to_string()),
1488        }];
1489        join.return_ = vec![Projection::Column("legacy_name".to_string())];
1490
1491        assert_eq!(
1492            effective_join_filter(&join),
1493            Some(Filter::IsNotNull(field("id")))
1494        );
1495        assert!(matches!(
1496            effective_join_projections(&join).as_slice(),
1497            [Projection::Field(FieldRef::TableColumn { column, .. }, Some(alias))]
1498                if column == "name" && alias == "display_name"
1499        ));
1500
1501        join.return_items.clear();
1502        assert_eq!(
1503            effective_join_projections(&join),
1504            vec![Projection::Column("legacy_name".to_string())]
1505        );
1506
1507        let graph_filter = Filter::StartsWith {
1508            field: FieldRef::node_prop("n", "path"),
1509            prefix: "infra/".to_string(),
1510        };
1511        let graph_return = vec![Projection::Field(FieldRef::node_prop("n", "name"), None)];
1512        let mut graph = GraphQuery::new(GraphPattern::new());
1513        graph.filter = Some(graph_filter.clone());
1514        graph.return_ = graph_return.clone();
1515        assert_eq!(effective_graph_filter(&graph), Some(graph_filter));
1516        assert_eq!(effective_graph_projections(&graph), graph_return);
1517
1518        let path_filter = Filter::Contains {
1519            field: FieldRef::edge_prop("e", "label"),
1520            substring: "depends".to_string(),
1521        };
1522        let path_return = vec![Projection::Column("path".to_string())];
1523        let mut path = PathQuery::new(NodeSelector::by_id("start"), NodeSelector::by_id("end"));
1524        path.filter = Some(path_filter.clone());
1525        path.return_ = path_return.clone();
1526        assert_eq!(effective_path_filter(&path), Some(path_filter));
1527        assert_eq!(effective_path_projections(&path), path_return);
1528
1529        let mut vector = VectorQuery::new("embeddings", VectorSource::literal(vec![0.1, 0.2]));
1530        assert!(effective_vector_filter(&vector).is_none());
1531        vector.filter = Some(MetadataFilter::eq("source", "nmap"));
1532        assert!(matches!(
1533            effective_vector_filter(&vector),
1534            Some(MetadataFilter::Eq(key, reddb_types::vector_metadata::MetadataValue::String(value)))
1535                if key == "source" && value == "nmap"
1536        ));
1537    }
1538
1539    #[test]
1540    fn insert_update_delete_helpers_fold_canonical_expressions() {
1541        let insert = InsertQuery {
1542            table: "users".to_string(),
1543            entity_type: crate::ast::InsertEntityType::Row,
1544            columns: vec!["name".to_string(), "password".to_string()],
1545            value_exprs: vec![vec![
1546                lit(Value::text("ada")),
1547                Expr::FunctionCall {
1548                    name: "PASSWORD".to_string(),
1549                    args: vec![lit(Value::text("pw"))],
1550                    span: Span::synthetic(),
1551                },
1552            ]],
1553            values: Vec::new(),
1554            returning: None,
1555            ttl_ms: None,
1556            expires_at_ms: None,
1557            with_metadata: Vec::new(),
1558            auto_embed: None,
1559            suppress_events: false,
1560        };
1561        let rows = effective_insert_rows(&insert).unwrap();
1562        assert!(matches!(
1563            rows.as_slice(),
1564            [row] if row[0] == Value::text("ada")
1565                && matches!(&row[1], Value::Password(value) if value == "@@plain@@pw")
1566        ));
1567
1568        let update = UpdateQuery {
1569            table: "users".to_string(),
1570            target: crate::ast::UpdateTarget::Rows,
1571            assignment_exprs: Vec::new(),
1572            compound_assignment_ops: Vec::new(),
1573            assignments: Vec::new(),
1574            where_expr: Some(bin(BinOp::Eq, col("id"), lit(Value::Integer(1)))),
1575            filter: None,
1576            ttl_ms: None,
1577            expires_at_ms: None,
1578            with_metadata: Vec::new(),
1579            returning: None,
1580            order_by: vec![OrderByClause::asc(field("id"))],
1581            limit: Some(1),
1582            suppress_events: false,
1583        };
1584        assert!(matches!(
1585            effective_update_filter(&update),
1586            Some(Filter::Compare {
1587                field: FieldRef::TableColumn { column, .. },
1588                value: Value::Integer(1),
1589                ..
1590            }) if column == "id"
1591        ));
1592
1593        let delete = DeleteQuery {
1594            table: "users".to_string(),
1595            where_expr: Some(Expr::IsNull {
1596                operand: Box::new(col("deleted_at")),
1597                negated: false,
1598                span: Span::synthetic(),
1599            }),
1600            filter: None,
1601            returning: None,
1602            suppress_events: false,
1603        };
1604        assert!(matches!(
1605            effective_delete_filter(&delete),
1606            Some(Filter::IsNull(FieldRef::TableColumn { column, .. })) if column == "deleted_at"
1607        ));
1608    }
1609
1610    #[test]
1611    fn fold_expr_to_value_handles_secret_constructors_unary_and_errors() {
1612        assert_eq!(
1613            fold_expr_to_value(Expr::UnaryOp {
1614                op: UnaryOp::Neg,
1615                operand: Box::new(lit(Value::UnsignedInteger(7))),
1616                span: Span::synthetic(),
1617            })
1618            .unwrap(),
1619            Value::Integer(-7)
1620        );
1621        assert_eq!(
1622            fold_expr_to_value(Expr::UnaryOp {
1623                op: UnaryOp::Not,
1624                operand: Box::new(lit(Value::Boolean(false))),
1625                span: Span::synthetic(),
1626            })
1627            .unwrap(),
1628            Value::Boolean(true)
1629        );
1630
1631        let secret = fold_expr_to_value(Expr::FunctionCall {
1632            name: "SECRET".to_string(),
1633            args: vec![lit(Value::text("token"))],
1634            span: Span::synthetic(),
1635        })
1636        .unwrap();
1637        assert!(matches!(secret, Value::Secret(bytes) if bytes == b"@@plain@@token"));
1638
1639        let casted = fold_expr_to_value(Expr::Cast {
1640            inner: Box::new(lit(Value::Integer(5))),
1641            target: reddb_types::types::DataType::Text,
1642            span: Span::synthetic(),
1643        })
1644        .unwrap();
1645        assert_eq!(casted, Value::Integer(5));
1646
1647        assert!(fold_expr_to_value(bin(BinOp::Add, col("age"), lit(Value::Integer(1)))).is_err());
1648        assert!(fold_expr_to_value(Expr::FunctionCall {
1649            name: "PASSWORD".to_string(),
1650            args: vec![lit(Value::Integer(1))],
1651            span: Span::synthetic(),
1652        })
1653        .is_err());
1654    }
1655
1656    #[test]
1657    fn render_label_and_literal_helpers_cover_private_round_trip_paths() {
1658        assert_eq!(
1659            render_expr_label(&lit(Value::text("O'Reilly"))),
1660            "'O''Reilly'"
1661        );
1662        assert_eq!(render_expr_label(&parameter(4)), "$4");
1663        assert_eq!(
1664            render_expr_label(&bin(
1665                BinOp::Mul,
1666                bin(BinOp::Add, col("a"), col("b")),
1667                col("c")
1668            )),
1669            "(a + b) * c"
1670        );
1671        assert_eq!(
1672            render_expr_label(&Expr::UnaryOp {
1673                op: UnaryOp::Not,
1674                operand: Box::new(col("active")),
1675                span: Span::synthetic(),
1676            }),
1677            "NOT active"
1678        );
1679        assert_eq!(
1680            render_expr_label(&Expr::Cast {
1681                inner: Box::new(col("age")),
1682                target: reddb_types::types::DataType::Text,
1683                span: Span::synthetic(),
1684            }),
1685            "CAST(age AS TEXT)"
1686        );
1687        assert_eq!(
1688            render_expr_label(&Expr::FunctionCall {
1689                name: "lower".to_string(),
1690                args: vec![col("name")],
1691                span: Span::synthetic(),
1692            }),
1693            "lower(name)"
1694        );
1695        assert_eq!(
1696            render_expr_label(&Expr::Case {
1697                branches: vec![(col("active"), lit(Value::text("yes")))],
1698                else_: Some(Box::new(lit(Value::text("no")))),
1699                span: Span::synthetic(),
1700            }),
1701            "CASE WHEN active THEN 'yes' ELSE 'no' END"
1702        );
1703        assert_eq!(
1704            render_expr_label(&Expr::IsNull {
1705                operand: Box::new(col("deleted_at")),
1706                negated: true,
1707                span: Span::synthetic(),
1708            }),
1709            "deleted_at IS NOT NULL"
1710        );
1711        assert_eq!(
1712            render_expr_label(&Expr::InList {
1713                target: Box::new(col("status")),
1714                values: vec![lit(Value::text("closed"))],
1715                negated: true,
1716                span: Span::synthetic(),
1717            }),
1718            "status NOT IN ('closed')"
1719        );
1720        assert_eq!(
1721            render_expr_label(&Expr::Between {
1722                target: Box::new(col("age")),
1723                low: Box::new(lit(Value::Integer(18))),
1724                high: Box::new(lit(Value::Integer(65))),
1725                negated: false,
1726                span: Span::synthetic(),
1727            }),
1728            "age BETWEEN 18 AND 65"
1729        );
1730        assert_eq!(
1731            render_expr_label(&Expr::Subquery {
1732                query: crate::ast::ExprSubquery {
1733                    query: Box::new(QueryExpr::Table(TableQuery::new("users"))),
1734                },
1735                span: Span::synthetic(),
1736            }),
1737            "subquery"
1738        );
1739        assert_eq!(
1740            render_expr_label(&Expr::WindowFunctionCall {
1741                name: "sum".to_string(),
1742                args: vec![col("amount")],
1743                window: WindowSpec::default(),
1744                span: Span::synthetic(),
1745            }),
1746            "sum(amount) OVER (...)"
1747        );
1748
1749        assert_eq!(render_field_label(&FieldRef::node_id("n")), "n.id");
1750        assert_eq!(
1751            render_field_label(&FieldRef::edge_prop("e", "weight")),
1752            "e.weight"
1753        );
1754        assert_eq!(render_binop_label(BinOp::Concat), "||");
1755        assert_eq!(render_sql_literal_label(&Value::Float(3.0)), "3");
1756        assert_eq!(
1757            render_projection_literal(&Value::Array(vec![Value::Integer(1), Value::text("x"),])),
1758            "@RL:[1,\"x\"]"
1759        );
1760        assert_eq!(
1761            render_projection_literal(&Value::Vector(vec![1.0, 2.5])),
1762            "@RL:V[1,2.5]"
1763        );
1764        assert_eq!(
1765            render_projection_literal(&Value::Json(br#"{"a":1}"#.to_vec())),
1766            "@RL:\"<json 7 bytes>\""
1767        );
1768        assert!(matches!(
1769            projection_from_literal(&Value::Boolean(true)),
1770            Some(Projection::Expression(_, None))
1771        ));
1772        assert_eq!(
1773            split_projection_function_alias("LOWER:name").1.as_deref(),
1774            Some("name")
1775        );
1776        assert_eq!(split_projection_function_alias(":bad").1, None);
1777    }
1778
1779    #[test]
1780    fn lowering_fallbacks_cover_alias_legacy_and_non_specialized_paths() {
1781        for (op, name) in [
1782            (BinOp::Sub, "SUB"),
1783            (BinOp::Div, "DIV"),
1784            (BinOp::Mod, "MOD"),
1785            (BinOp::Concat, "CONCAT"),
1786        ] {
1787            assert!(matches!(
1788                expr_to_projection(&bin(op, col("lhs"), col("rhs"))),
1789                Some(Projection::Function(function, args))
1790                    if function == name && args.len() == 2
1791            ));
1792        }
1793
1794        assert!(matches!(
1795            select_item_to_projection(&SelectItem::Expr {
1796                expr: lit(Value::Integer(1)),
1797                alias: Some("one".to_string()),
1798            }),
1799            Some(Projection::Alias(column, alias)) if column == "LIT:1" && alias == "one"
1800        ));
1801        assert!(matches!(
1802            select_item_to_projection(&SelectItem::Expr {
1803                expr: Expr::UnaryOp {
1804                    op: UnaryOp::Not,
1805                    operand: Box::new(col("active")),
1806                    span: Span::synthetic(),
1807                },
1808                alias: Some("inactive".to_string()),
1809            }),
1810            Some(Projection::Expression(_, Some(alias))) if alias == "inactive"
1811        ));
1812
1813        let legacy_insert = InsertQuery {
1814            table: "users".to_string(),
1815            entity_type: crate::ast::InsertEntityType::Row,
1816            columns: vec!["id".to_string()],
1817            value_exprs: Vec::new(),
1818            values: vec![vec![Value::Integer(1)]],
1819            returning: None,
1820            ttl_ms: None,
1821            expires_at_ms: None,
1822            with_metadata: Vec::new(),
1823            auto_embed: None,
1824            suppress_events: false,
1825        };
1826        assert_eq!(
1827            effective_insert_rows(&legacy_insert).unwrap(),
1828            vec![vec![Value::Integer(1)]]
1829        );
1830
1831        assert!(matches!(
1832            expr_to_filter(&Expr::IsNull {
1833                operand: Box::new(lit(Value::Null)),
1834                negated: false,
1835                span: Span::synthetic(),
1836            }),
1837            Filter::CompareExpr { .. }
1838        ));
1839        assert!(matches!(
1840            expr_to_filter(&Expr::InList {
1841                target: Box::new(col("status")),
1842                values: vec![col("other_status")],
1843                negated: false,
1844                span: Span::synthetic(),
1845            }),
1846            Filter::CompareExpr { .. }
1847        ));
1848        assert!(matches!(
1849            expr_to_filter(&Expr::Between {
1850                target: Box::new(col("age")),
1851                low: Box::new(lit(Value::Integer(18))),
1852                high: Box::new(lit(Value::Integer(65))),
1853                negated: true,
1854                span: Span::synthetic(),
1855            }),
1856            Filter::CompareExpr { .. }
1857        ));
1858        assert!(matches!(
1859            expr_to_filter(&Expr::FunctionCall {
1860                name: "UNKNOWN".to_string(),
1861                args: vec![col("name"), lit(Value::text("a"))],
1862                span: Span::synthetic(),
1863            }),
1864            Filter::CompareExpr { .. }
1865        ));
1866        assert!(matches!(
1867            expr_to_filter(&Expr::FunctionCall {
1868                name: "LIKE".to_string(),
1869                args: vec![lit(Value::text("not_field")), lit(Value::text("a"))],
1870                span: Span::synthetic(),
1871            }),
1872            Filter::CompareExpr { .. }
1873        ));
1874
1875        assert_eq!(
1876            fold_expr_to_value(Expr::UnaryOp {
1877                op: UnaryOp::Neg,
1878                operand: Box::new(lit(Value::Float(1.5))),
1879                span: Span::synthetic(),
1880            })
1881            .unwrap(),
1882            Value::Float(-1.5)
1883        );
1884        assert!(fold_expr_to_value(Expr::UnaryOp {
1885            op: UnaryOp::Not,
1886            operand: Box::new(lit(Value::Integer(1))),
1887            span: Span::synthetic(),
1888        })
1889        .is_err());
1890        assert!(fold_expr_to_value(Expr::FunctionCall {
1891            name: "LOWER".to_string(),
1892            args: vec![lit(Value::text("Ada"))],
1893            span: Span::synthetic(),
1894        })
1895        .is_err());
1896
1897        assert_eq!(render_projection_literal(&Value::Null), "");
1898        assert_eq!(render_projection_literal(&Value::UnsignedInteger(7)), "7");
1899        assert_eq!(render_projection_literal(&Value::Boolean(false)), "false");
1900        assert_eq!(
1901            render_projection_literal(&Value::Blob(vec![1, 2, 3])),
1902            "@RL:\"<blob 3 bytes>\""
1903        );
1904        assert_eq!(serialize_value_json(&Value::Null), "null");
1905    }
1906}