@@ -6,13 +6,36 @@ use super::value::sql_value_to_nodedb_value;
66
77/// Convert a `nodedb_sql::types::SqlExpr` (parser AST) to a
88/// `nodedb_query::expr::SqlExpr` (bridge evaluation type).
9+ ///
10+ /// Column references use the **bare** name (no table qualifier) for
11+ /// single-collection evaluation contexts (WHERE, CHECK, GENERATED).
12+ /// For join contexts where the merged document uses qualified keys
13+ /// (`"t1.col"`), use [`sql_expr_to_bridge_expr_qualified`] instead.
914pub ( super ) fn sql_expr_to_bridge_expr ( expr : & SqlExpr ) -> crate :: bridge:: expr_eval:: SqlExpr {
15+ convert_expr_inner ( expr, false )
16+ }
17+
18+ /// Like [`sql_expr_to_bridge_expr`] but qualifies column references
19+ /// with their table name (`t.col` → `"t.col"`) for join merged docs.
20+ pub ( super ) fn sql_expr_to_bridge_expr_qualified (
21+ expr : & SqlExpr ,
22+ ) -> crate :: bridge:: expr_eval:: SqlExpr {
23+ convert_expr_inner ( expr, true )
24+ }
25+
26+ fn convert_expr_inner ( expr : & SqlExpr , qualify : bool ) -> crate :: bridge:: expr_eval:: SqlExpr {
1027 use crate :: bridge:: expr_eval:: SqlExpr as BExpr ;
1128 match expr {
12- SqlExpr :: Column { name, .. } => BExpr :: Column ( name. clone ( ) ) ,
29+ SqlExpr :: Column { table, name } => {
30+ if qualify {
31+ BExpr :: Column ( nodedb_sql:: planner:: qualified_name ( table. as_deref ( ) , name) )
32+ } else {
33+ BExpr :: Column ( name. clone ( ) )
34+ }
35+ }
1336 SqlExpr :: Literal ( v) => BExpr :: Literal ( sql_value_to_nodedb_value ( v) ) ,
1437 SqlExpr :: BinaryOp { left, op, right } => BExpr :: BinaryOp {
15- left : Box :: new ( sql_expr_to_bridge_expr ( left) ) ,
38+ left : Box :: new ( convert_expr_inner ( left, qualify ) ) ,
1639 op : match op {
1740 nodedb_sql:: types:: BinaryOp :: Add => crate :: bridge:: expr_eval:: BinaryOp :: Add ,
1841 nodedb_sql:: types:: BinaryOp :: Sub => crate :: bridge:: expr_eval:: BinaryOp :: Sub ,
@@ -29,11 +52,14 @@ pub(super) fn sql_expr_to_bridge_expr(expr: &SqlExpr) -> crate::bridge::expr_eva
2952 nodedb_sql:: types:: BinaryOp :: Or => crate :: bridge:: expr_eval:: BinaryOp :: Or ,
3053 nodedb_sql:: types:: BinaryOp :: Concat => crate :: bridge:: expr_eval:: BinaryOp :: Concat ,
3154 } ,
32- right : Box :: new ( sql_expr_to_bridge_expr ( right) ) ,
55+ right : Box :: new ( convert_expr_inner ( right, qualify ) ) ,
3356 } ,
3457 SqlExpr :: Function { name, args, .. } => BExpr :: Function {
3558 name : name. clone ( ) ,
36- args : args. iter ( ) . map ( sql_expr_to_bridge_expr) . collect ( ) ,
59+ args : args
60+ . iter ( )
61+ . map ( |a| convert_expr_inner ( a, qualify) )
62+ . collect ( ) ,
3763 } ,
3864 SqlExpr :: Case {
3965 operand,
@@ -42,14 +68,19 @@ pub(super) fn sql_expr_to_bridge_expr(expr: &SqlExpr) -> crate::bridge::expr_eva
4268 } => BExpr :: Case {
4369 operand : operand
4470 . as_ref ( )
45- . map ( |e| Box :: new ( sql_expr_to_bridge_expr ( e ) ) ) ,
71+ . map ( |e| Box :: new ( convert_expr_inner ( e , qualify ) ) ) ,
4672 when_thens : when_then
4773 . iter ( )
48- . map ( |( w, t) | ( sql_expr_to_bridge_expr ( w) , sql_expr_to_bridge_expr ( t) ) )
74+ . map ( |( w, t) | {
75+ (
76+ convert_expr_inner ( w, qualify) ,
77+ convert_expr_inner ( t, qualify) ,
78+ )
79+ } )
4980 . collect ( ) ,
5081 else_expr : else_expr
5182 . as_ref ( )
52- . map ( |e| Box :: new ( sql_expr_to_bridge_expr ( e ) ) ) ,
83+ . map ( |e| Box :: new ( convert_expr_inner ( e , qualify ) ) ) ,
5384 } ,
5485 SqlExpr :: Cast { expr, to_type } => {
5586 let cast_type = match to_type. to_uppercase ( ) . as_str ( ) {
@@ -63,18 +94,18 @@ pub(super) fn sql_expr_to_bridge_expr(expr: &SqlExpr) -> crate::bridge::expr_eva
6394 _ => crate :: bridge:: expr_eval:: CastType :: String ,
6495 } ;
6596 BExpr :: Cast {
66- expr : Box :: new ( sql_expr_to_bridge_expr ( expr) ) ,
97+ expr : Box :: new ( convert_expr_inner ( expr, qualify ) ) ,
6798 to_type : cast_type,
6899 }
69100 }
70101 SqlExpr :: Wildcard => BExpr :: Column ( "*" . into ( ) ) ,
71102
72103 // NOT e / -e → evaluator's Negate (handles both bool and numeric).
73- SqlExpr :: UnaryOp { expr, .. } => BExpr :: Negate ( Box :: new ( sql_expr_to_bridge_expr ( expr) ) ) ,
104+ SqlExpr :: UnaryOp { expr, .. } => BExpr :: Negate ( Box :: new ( convert_expr_inner ( expr, qualify ) ) ) ,
74105
75106 // `e IS NULL` / `e IS NOT NULL` — direct passthrough.
76107 SqlExpr :: IsNull { expr, negated } => BExpr :: IsNull {
77- expr : Box :: new ( sql_expr_to_bridge_expr ( expr) ) ,
108+ expr : Box :: new ( convert_expr_inner ( expr, qualify ) ) ,
78109 negated : * negated,
79110 } ,
80111
@@ -87,9 +118,9 @@ pub(super) fn sql_expr_to_bridge_expr(expr: &SqlExpr) -> crate::bridge::expr_eva
87118 high,
88119 negated,
89120 } => {
90- let e = sql_expr_to_bridge_expr ( expr) ;
91- let l = sql_expr_to_bridge_expr ( low) ;
92- let h = sql_expr_to_bridge_expr ( high) ;
121+ let e = convert_expr_inner ( expr, qualify ) ;
122+ let l = convert_expr_inner ( low, qualify ) ;
123+ let h = convert_expr_inner ( high, qualify ) ;
93124 if * negated {
94125 let lt = BExpr :: BinaryOp {
95126 left : Box :: new ( e. clone ( ) ) ,
@@ -134,7 +165,7 @@ pub(super) fn sql_expr_to_bridge_expr(expr: &SqlExpr) -> crate::bridge::expr_eva
134165 list,
135166 negated,
136167 } => {
137- let target = sql_expr_to_bridge_expr ( expr) ;
168+ let target = convert_expr_inner ( expr, qualify ) ;
138169 if list. is_empty ( ) {
139170 // Empty list: `e IN ()` = false, `e NOT IN ()` = true.
140171 return BExpr :: Literal ( nodedb_types:: Value :: Bool ( * negated) ) ;
@@ -157,7 +188,7 @@ pub(super) fn sql_expr_to_bridge_expr(expr: &SqlExpr) -> crate::bridge::expr_eva
157188 . map ( |item| BExpr :: BinaryOp {
158189 left : Box :: new ( target. clone ( ) ) ,
159190 op : eq_op,
160- right : Box :: new ( sql_expr_to_bridge_expr ( item) ) ,
191+ right : Box :: new ( convert_expr_inner ( item, qualify ) ) ,
161192 } )
162193 . reduce ( |acc, next| BExpr :: BinaryOp {
163194 left : Box :: new ( acc) ,
@@ -178,8 +209,8 @@ pub(super) fn sql_expr_to_bridge_expr(expr: &SqlExpr) -> crate::bridge::expr_eva
178209 let call = BExpr :: Function {
179210 name : "like" . into ( ) ,
180211 args : vec ! [
181- sql_expr_to_bridge_expr ( expr) ,
182- sql_expr_to_bridge_expr ( pattern) ,
212+ convert_expr_inner ( expr, qualify ) ,
213+ convert_expr_inner ( pattern, qualify ) ,
183214 ] ,
184215 } ;
185216 if * negated {
0 commit comments