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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//
//! Physical assembly for lateral, hash, and nested-loop joins.
/// Build the physical operator for a join source.
use super::{
alias_join_operator, bind_join_on_schema, build_join_operator_with_ctes_at_path,
join_using_predicate, null_row_for_schema, physical_exec_error, physical_work_mem_bytes,
resolve_join_using, shape_join_using_output, ColumnPrune, CteScope, JoinExecutionStrategy,
PhysicalOperator, QualifierFilters, SQLError, SQLParam, SourceContext, SourcePlan,
};
use crate::physical::construction::OperatorBuild;
use crate::query::binding::sources::SourceBindings;
use crate::{HashJoin, NestedLoopJoin};
#[expect(
clippy::too_many_lines,
reason = "preserves source schema and row identity"
)]
#[expect(
clippy::too_many_arguments,
reason = "forwards source binding metadata and tuple recheck paths"
)]
pub(super) fn build_join_source_operator<'a, S: Clone + Send + Sync + 'static>(
context: &SourceContext<'a, S>,
from: &SourcePlan,
params: &'a [SQLParam],
ctes: &mut CteScope<S>,
prune: Option<&ColumnPrune>,
filters: Option<&QualifierFilters>,
recheck_path: Option<Vec<u8>>,
bindings: Option<&SourceBindings<'_>>,
) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
match from {
SourcePlan::Join {
left,
right,
kind,
on,
using,
natural,
alias,
column_aliases,
lateral,
strategy,
..
} => {
// A join alias hides the joined relations from the enclosing query block. The projections and predicates that block placed by relation name therefore cannot address any relation inside the alias, including one that reuses an outer name; conjuncts on the alias itself stay in the block's residual filter.
let (prune, filters) = if alias.is_some() {
(None, None)
} else {
(prune, filters)
};
let left_recheck_path = recheck_path.as_ref().map(|path| {
let mut path = path.clone();
path.push(0);
path
});
let right_recheck_path = recheck_path.as_ref().map(|path| {
let mut path = path.clone();
path.push(1);
path
});
let left_filters = filters.and_then(|filters| {
context
.planning
.propagated_join_filters(filters, right, left, on.as_ref())
});
let left_filter_ref = left_filters.as_ref().or(filters);
let left_operator = if context.planning.join_input_is_empty(from, false) {
super::empty_source_operator(context, left, params, ctes, None)?
} else {
build_join_operator_with_ctes_at_path(
context,
left,
params,
ctes,
prune,
left_filter_ref,
left_recheck_path,
bindings
.and_then(|bindings| bindings.join_child(false))
.as_ref(),
)?
};
OperatorBuild::build(left_operator, |left_operator| {
let source_binding = if ctes.source_outer_rewrites.is_some() {
let outer_schema = ctes.row_lock_outer_row().map(|row| row.schema.clone());
let preceding = uqa_sql::binding::SourceScope::new(outer_schema.as_ref())
.right_namespace(left, left_operator.row_schema(), *kind);
crate::query::binding::bind_outer_source_rewrites(
context.ctes.routines,
right,
params,
ctes,
uqa_sql::binding::SourceScope {
outer: outer_schema.as_ref(),
preceding: Some(&preceding),
inherited_lateral: *lateral,
retain_names: true,
},
)?
} else {
None
};
let mut source_scope;
let ctes = if let Some(binding) = &source_binding {
source_scope = ctes.enter_scalar_subqueries(&binding.subqueries);
&mut *source_scope
} else {
ctes
};
let bindings = bindings.filter(|_| source_binding.is_none());
let right = source_binding
.as_ref()
.map_or(right, |binding| &binding.source);
let implicit_lateral_function = match right.as_ref() {
SourcePlan::Function { name, .. } => {
let identity = name.to_ascii_lowercase();
let lower = uqa_sql::semantics::builtin_function_dispatch_name(&identity);
!uqa_sql::registry::is_operator_join_table_function(&lower)
}
SourcePlan::FunctionGroup { .. } => true,
_ => false,
};
let right_filters = filters.and_then(|filters| {
context
.planning
.propagated_join_filters(filters, left, right, on.as_ref())
});
let right_filter_ref = right_filters.as_ref().or(filters);
let lateral_input = (*lateral
|| implicit_lateral_function
|| matches!(
right.as_ref(),
SourcePlan::Join { .. }
| SourcePlan::Values { lateral: true, .. }
| SourcePlan::Subquery { lateral: true, .. }
))
.then(|| {
ctes.row_lock_outer_row().map_or_else(
|| left_operator.row_schema().clone(),
|outer| {
crate::RowSchema::with_outer_schema(
left_operator.row_schema(),
&outer.schema,
)
},
)
});
let right_empty = context.planning.join_input_is_empty(from, true);
let dependent = match lateral_input.as_ref().filter(|_| !right_empty) {
Some(input) => crate::query::binding::source_depends_on_current_row(
context.ctes.routines,
right,
input,
ctes,
)?,
None => false,
};
if dependent {
if !matches!(strategy, JoinExecutionStrategy::Auto) {
return Err(SQLError::Internal(
"optimizer selected a hash strategy for a lateral join".into(),
));
}
let input_schema = lateral_input.expect("dependent source has a calling scope");
return left_operator.attach(|left| {
super::lateral_binding::build_lateral_join(
context,
left,
params,
ctes,
super::lateral_binding::LateralBinding {
join: from,
right,
input_schema,
filters: right_filter_ref,
recheck_path: right_recheck_path.as_deref(),
},
)
});
}
let right_operator = if right_empty {
super::empty_source_operator(
context,
right,
params,
ctes,
lateral_input.as_ref(),
)?
} else {
build_join_operator_with_ctes_at_path(
context,
right,
params,
ctes,
prune,
right_filter_ref,
right_recheck_path,
bindings
.and_then(|bindings| bindings.join_child(true))
.as_ref(),
)?
};
let left_schema = left_operator.row_schema().clone();
let right_schema = right_operator.row_schema().clone();
let (
right_operator,
(
resolved_using,
effective_on,
evaluator,
hash_plan,
left_nulls,
right_nulls,
work_mem,
),
) = OperatorBuild::run(right_operator, |right_operator| {
let (on, evaluator) = bind_join_on_schema(
context,
on.as_ref(),
&left_schema,
&right_schema,
params,
ctes,
)?;
let resolved_using =
resolve_join_using(using.as_ref(), *natural, &left_schema, &right_schema)?;
let effective_on = resolved_using
.as_ref()
.and_then(|using| join_using_predicate(using, &left_schema, &right_schema))
.or(on);
let effective_on = effective_on.map(|predicate| {
let schema = crate::RowSchema::join(&left_schema, &right_schema, []);
evaluator.bind_type_introspection(predicate, &schema)
});
let hash_plan = super::join_keys::join_keys(
*kind,
effective_on.as_ref(),
&left_schema,
&right_schema,
params,
context.types,
context.volatility,
)?;
let left_nulls = null_row_for_schema(left_operator.schema());
let right_nulls = null_row_for_schema(right_operator.schema());
let work_mem = physical_work_mem_bytes(context.relational.runtime)?;
Ok((
resolved_using,
effective_on,
evaluator,
hash_plan,
left_nulls,
right_nulls,
work_mem,
))
})?;
left_operator.attach(|left_operator| {
let joined: Box<dyn PhysicalOperator + 'a> = match (strategy, hash_plan) {
(JoinExecutionStrategy::Auto | JoinExecutionStrategy::Hash, Some(keys)) => {
Box::new(
HashJoin::try_new_with_work_mem_and_predicate(
left_operator,
right_operator,
*kind,
keys.left,
keys.right,
keys.residual,
evaluator,
left_nulls,
right_nulls,
work_mem,
params,
)
.map_err(physical_exec_error)?,
)
}
(JoinExecutionStrategy::Auto | JoinExecutionStrategy::Hash, None) => {
Box::new(NestedLoopJoin::new_with_work_mem(
left_operator,
right_operator,
*kind,
effective_on,
evaluator,
left_nulls,
right_nulls,
work_mem,
))
}
};
Ok(joined)
})?;
if let Some(using) = resolved_using.as_ref() {
left_operator.attach(|input| {
shape_join_using_output(input, *kind, &left_schema, &right_schema, using)
})?;
}
left_operator
.attach(|input| alias_join_operator(input, alias.as_deref(), column_aliases))
})
}
_ => unreachable!("join source builder called for a different source kind"),
}
}