Skip to content

Commit 27194ff

Browse files
committed
fix: fixed variant failed to successfully apply virtual column in bind join
Signed-off-by: Kould <[email protected]>
1 parent 59a3e4b commit 27194ff

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

โ€Žsrc/query/sql/src/planner/binder/bind_context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ impl VirtualColumnContext {
139139
virtual_columns: Vec::new(),
140140
}
141141
}
142+
143+
pub(crate) fn merge(&mut self, other: &VirtualColumnContext) {
144+
self.allow_pushdown = self.allow_pushdown || other.allow_pushdown;
145+
self.table_indices.extend(other.table_indices.clone());
146+
self.virtual_column_indices
147+
.extend(other.virtual_column_indices.clone());
148+
self.virtual_column_names
149+
.extend(other.virtual_column_names.clone());
150+
self.next_column_ids.extend(other.next_column_ids.clone());
151+
self.virtual_columns.extend(other.virtual_columns.clone());
152+
}
142153
}
143154

144155
/// `BindContext` stores all the free variables in a query and tracks the context of binding procedure.

โ€Žsrc/query/sql/src/planner/binder/bind_table_reference/bind_join.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ impl Binder {
9898
let mut right_column_bindings = right_context.columns.clone();
9999

100100
let mut bind_context = bind_context.replace();
101+
bind_context
102+
.virtual_column_context
103+
.merge(&left_context.virtual_column_context);
104+
bind_context
105+
.virtual_column_context
106+
.merge(&right_context.virtual_column_context);
101107

102108
self.check_table_name_and_condition(
103109
&left_column_bindings,
@@ -762,7 +768,7 @@ impl<'a> JoinConditionResolver<'a> {
762768
}
763769

764770
fn resolve_predicate(
765-
&self,
771+
&mut self,
766772
predicate: &Expr,
767773
left_join_conditions: &mut Vec<ScalarExpr>,
768774
right_join_conditions: &mut Vec<ScalarExpr>,
@@ -810,6 +816,7 @@ impl<'a> JoinConditionResolver<'a> {
810816
non_equi_conditions.push(predicate);
811817
}
812818
}
819+
*self.join_context = join_context;
813820
Ok(())
814821
}
815822

โ€Žtests/sqllogictests/suites/mode/standalone/ee/explain_virtual_column.test

+103-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ TableScan
133133
โ”œโ”€โ”€ virtual columns: [v['a'][0], v['b']]
134134
โ””โ”€โ”€ estimated rows: 0.00
135135

136-
statement ok
137-
drop table t1
138-
139136
statement ok
140137
drop table if exists t2
141138

@@ -256,6 +253,109 @@ Filter
256253
โ”œโ”€โ”€ virtual columns: [v['a'][0], v['b']]
257254
โ””โ”€โ”€ estimated rows: 1.00
258255

256+
query T
257+
explain select t1.a, t1.v['b'] from t1 left outer join t2 on t1.v['b'] = t2.a where t1.v['a'][0] = 1;
258+
----
259+
HashJoin
260+
โ”œโ”€โ”€ output columns: [t1.a (#0), t1.v['b'] (#4)]
261+
โ”œโ”€โ”€ join type: RIGHT OUTER
262+
โ”œโ”€โ”€ build keys: []
263+
โ”œโ”€โ”€ probe keys: []
264+
โ”œโ”€โ”€ keys is null equal: []
265+
โ”œโ”€โ”€ filters: [TRY_CAST(v['b'] (#4) AS Int32 NULL) = t2.a (#2)]
266+
โ”œโ”€โ”€ estimated rows: 0.00
267+
โ”œโ”€โ”€ TableScan(Build)
268+
โ”‚ โ”œโ”€โ”€ table: default.test_virtual_db.t1
269+
โ”‚ โ”œโ”€โ”€ output columns: [a (#0), v['b'] (#4)]
270+
โ”‚ โ”œโ”€โ”€ read rows: 1
271+
โ”‚ โ”œโ”€โ”€ read size: < 1 KiB
272+
โ”‚ โ”œโ”€โ”€ partitions total: 1
273+
โ”‚ โ”œโ”€โ”€ partitions scanned: 1
274+
โ”‚ โ”œโ”€โ”€ pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
275+
โ”‚ โ”œโ”€โ”€ push downs: [filters: [is_true(TRY_CAST(v['a'][0] (#5) AS UInt8 NULL) = 1)], limit: NONE]
276+
โ”‚ โ”œโ”€โ”€ virtual columns: [v['a'][0], v['b']]
277+
โ”‚ โ””โ”€โ”€ estimated rows: 0.00
278+
โ””โ”€โ”€ TableScan(Probe)
279+
โ”œโ”€โ”€ table: default.test_virtual_db.t2
280+
โ”œโ”€โ”€ output columns: [a (#2)]
281+
โ”œโ”€โ”€ read rows: 1
282+
โ”œโ”€โ”€ read size: < 1 KiB
283+
โ”œโ”€โ”€ partitions total: 1
284+
โ”œโ”€โ”€ partitions scanned: 1
285+
โ”œโ”€โ”€ pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
286+
โ”œโ”€โ”€ push downs: [filters: [], limit: NONE]
287+
โ””โ”€โ”€ estimated rows: 1.00
288+
289+
query T
290+
explain select t1.a, t1.v['b'] from t1 left outer join t2 on t1.v['b'] = t2.a where t1.v['a'][0] = 1;
291+
----
292+
HashJoin
293+
โ”œโ”€โ”€ output columns: [t1.a (#0), t1.v['b'] (#4)]
294+
โ”œโ”€โ”€ join type: RIGHT OUTER
295+
โ”œโ”€โ”€ build keys: []
296+
โ”œโ”€โ”€ probe keys: []
297+
โ”œโ”€โ”€ keys is null equal: []
298+
โ”œโ”€โ”€ filters: [TRY_CAST(v['b'] (#4) AS Int32 NULL) = t2.a (#2)]
299+
โ”œโ”€โ”€ estimated rows: 0.00
300+
โ”œโ”€โ”€ TableScan(Build)
301+
โ”‚ โ”œโ”€โ”€ table: default.test_virtual_db.t1
302+
โ”‚ โ”œโ”€โ”€ output columns: [a (#0), v['b'] (#4)]
303+
โ”‚ โ”œโ”€โ”€ read rows: 1
304+
โ”‚ โ”œโ”€โ”€ read size: < 1 KiB
305+
โ”‚ โ”œโ”€โ”€ partitions total: 1
306+
โ”‚ โ”œโ”€โ”€ partitions scanned: 1
307+
โ”‚ โ”œโ”€โ”€ pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
308+
โ”‚ โ”œโ”€โ”€ push downs: [filters: [is_true(TRY_CAST(v['a'][0] (#5) AS UInt8 NULL) = 1)], limit: NONE]
309+
โ”‚ โ”œโ”€โ”€ virtual columns: [v['a'][0], v['b']]
310+
โ”‚ โ””โ”€โ”€ estimated rows: 0.00
311+
โ””โ”€โ”€ TableScan(Probe)
312+
โ”œโ”€โ”€ table: default.test_virtual_db.t2
313+
โ”œโ”€โ”€ output columns: [a (#2)]
314+
โ”œโ”€โ”€ read rows: 1
315+
โ”œโ”€โ”€ read size: < 1 KiB
316+
โ”œโ”€โ”€ partitions total: 1
317+
โ”œโ”€โ”€ partitions scanned: 1
318+
โ”œโ”€โ”€ pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
319+
โ”œโ”€โ”€ push downs: [filters: [], limit: NONE]
320+
โ””โ”€โ”€ estimated rows: 1.00
321+
322+
323+
query T
324+
explain select * from t1 join t2 on t1.v['b'] = t2.a;
325+
----
326+
HashJoin
327+
โ”œโ”€โ”€ output columns: [t1.a (#0), t1.v (#1), t2.a (#2), t2.v (#3)]
328+
โ”œโ”€โ”€ join type: INNER
329+
โ”œโ”€โ”€ build keys: [t2.a (#2)]
330+
โ”œโ”€โ”€ probe keys: [CAST(v['b'] (#4) AS Int32 NULL)]
331+
โ”œโ”€โ”€ keys is null equal: [false]
332+
โ”œโ”€โ”€ filters: []
333+
โ”œโ”€โ”€ estimated rows: 1.00
334+
โ”œโ”€โ”€ TableScan(Build)
335+
โ”‚ โ”œโ”€โ”€ table: default.test_virtual_db.t2
336+
โ”‚ โ”œโ”€โ”€ output columns: [a (#2), v (#3)]
337+
โ”‚ โ”œโ”€โ”€ read rows: 1
338+
โ”‚ โ”œโ”€โ”€ read size: < 1 KiB
339+
โ”‚ โ”œโ”€โ”€ partitions total: 1
340+
โ”‚ โ”œโ”€โ”€ partitions scanned: 1
341+
โ”‚ โ”œโ”€โ”€ pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
342+
โ”‚ โ”œโ”€โ”€ push downs: [filters: [], limit: NONE]
343+
โ”‚ โ””โ”€โ”€ estimated rows: 1.00
344+
โ””โ”€โ”€ TableScan(Probe)
345+
โ”œโ”€โ”€ table: default.test_virtual_db.t1
346+
โ”œโ”€โ”€ output columns: [a (#0), v (#1), v['b'] (#4)]
347+
โ”œโ”€โ”€ read rows: 1
348+
โ”œโ”€โ”€ read size: < 1 KiB
349+
โ”œโ”€โ”€ partitions total: 1
350+
โ”œโ”€โ”€ partitions scanned: 1
351+
โ”œโ”€โ”€ pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
352+
โ”œโ”€โ”€ push downs: [filters: [], limit: NONE]
353+
โ”œโ”€โ”€ virtual columns: [v['b']]
354+
โ””โ”€โ”€ estimated rows: 1.00
355+
356+
statement ok
357+
drop table t1
358+
259359
statement ok
260360
drop table t2
261361

0 commit comments

Comments
ย (0)