Skip to content

Commit 715c54f

Browse files
authored
fix: cross join return wrong column (#18355)
* fix: cross join return wrong column * add test
1 parent 3f3a9f7 commit 715c54f

File tree

4 files changed

+174
-1
lines changed

4 files changed

+174
-1
lines changed

src/query/service/src/pipelines/builders/builder_join.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl PipelineBuilder {
139139
self.func_ctx.clone(),
140140
state.clone(),
141141
&join.probe_projections,
142+
&join.build_projections,
142143
&join.probe_keys,
143144
join.probe.output_schema()?,
144145
&join.join_type,

src/query/service/src/pipelines/processors/transforms/hash_join/hash_join_probe_state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub struct HashJoinProbeState {
8181
/// `probe_projections` only contains the columns from upstream required columns
8282
/// and columns from other_condition which are in probe schema.
8383
pub(crate) probe_projections: ColumnSet,
84+
// used in cross join
85+
pub(crate) build_projections: ColumnSet,
8486
/// Todo(xudong): add more detailed comments for the following fields.
8587
/// Final scan tasks
8688
pub(crate) final_scan_tasks: RwLock<VecDeque<usize>>,
@@ -99,6 +101,7 @@ impl HashJoinProbeState {
99101
func_ctx: FunctionContext,
100102
hash_join_state: Arc<HashJoinState>,
101103
probe_projections: &ColumnSet,
104+
build_projections: &ColumnSet,
102105
probe_keys: &[RemoteExpr],
103106
mut probe_schema: DataSchemaRef,
104107
join_type: &JoinType,
@@ -138,6 +141,7 @@ impl HashJoinProbeState {
138141
merge_into_final_partial_unmodified_scan_tasks: RwLock::new(VecDeque::new()),
139142
mark_scan_map_lock: Mutex::new(()),
140143
hash_method: method,
144+
build_projections: build_projections.clone(),
141145
})
142146
}
143147

src/query/service/src/pipelines/processors/transforms/hash_join/probe_join/cross_join.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl HashJoinProbeState {
3535
}
3636
let mut probe_block = input.project(&self.probe_projections);
3737
let build_block = DataBlock::concat(build_blocks)?;
38+
let build_block = build_block.project(&self.build_projections);
3839
if build_num_rows == 1 {
3940
for col in build_block.columns() {
4041
let scalar = unsafe { col.index_unchecked(0) };

tests/sqllogictests/suites/query/join/cross.test

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,171 @@ statement ok
2020
drop table onecolumn
2121

2222
statement ok
23-
drop table empty
23+
drop table empty
24+
25+
statement ok
26+
CREATE TABLE cte_test1 (
27+
"Price Branch Region" VARCHAR NULL,
28+
"PriceBranchID" VARCHAR NULL,
29+
"ProductType" VARCHAR NULL,
30+
"Product Type" VARCHAR NULL,
31+
"BILL TO_CustomerID" VARCHAR NULL,
32+
"BILL TO_CustomerName" VARCHAR NULL,
33+
"CustomerType" VARCHAR NULL,
34+
"MarketCategory" VARCHAR NULL,
35+
"OrderChannel" VARCHAR NULL,
36+
"Price Override" VARCHAR NULL,
37+
"Value" DECIMAL(38, 10) NULL
38+
);
39+
40+
statement ok
41+
INSERT INTO cte_test1 VALUES (
42+
'Price Branch Region'
43+
, 'PriceBranchID'
44+
, 'ProductType'
45+
, 'Product Type'
46+
, 'BILL TO_CustomerID'
47+
, 'BILL TO_CustomerName'
48+
, 'CustomerType'
49+
, 'MarketCategory'
50+
, 'OrderChannel'
51+
, 'Price Override'
52+
, 10
53+
);
54+
55+
statement ok
56+
CREATE TABLE cte_test2 (
57+
"Year" VARCHAR NULL,
58+
"PriceBranchGlRegionID" VARCHAR NULL,
59+
"Price Branch Region" VARCHAR NULL,
60+
"PriceBranchID" VARCHAR NULL,
61+
"Enterprise Profit" VARCHAR NULL,
62+
"Tier Group" VARCHAR NULL,
63+
"Tier Group 2" VARCHAR NULL,
64+
"Tier Group Sort" VARCHAR NULL,
65+
"Tier" VARCHAR NULL,
66+
"Tier Name" VARCHAR NULL,
67+
"Tier Label" VARCHAR NULL,
68+
"Short Name" VARCHAR NULL,
69+
"Economic Tree" VARCHAR NULL,
70+
"Tier Group v2" VARCHAR NULL,
71+
"Tier Name v2" VARCHAR NULL,
72+
"Tier Sort v2" VARCHAR NULL,
73+
"Tier Group 2 v2" VARCHAR NULL,
74+
"Tier Group v3" VARCHAR NULL,
75+
"Tier Name v3" VARCHAR NULL,
76+
"Short Name v3" VARCHAR NULL,
77+
"Tier Sort v3" VARCHAR NULL,
78+
"ProductType" VARCHAR NULL,
79+
"Product Type" VARCHAR NULL,
80+
"BILL TO_CustomerID" VARCHAR NULL,
81+
"BILL TO_CustomerName" VARCHAR NULL,
82+
"CustomerType" VARCHAR NULL,
83+
"MarketCategory" VARCHAR NULL,
84+
"OrderChannel" VARCHAR NULL,
85+
"Price Override" VARCHAR NULL,
86+
"Value" DECIMAL(38, 10) NULL,
87+
"BuyLineID" VARCHAR NULL
88+
);
89+
90+
statement ok
91+
INSERT INTO cte_test2 VALUES (
92+
'Year',
93+
'PriceBranchGlRegionID',
94+
'Price Branch Region',
95+
'PriceBranchID',
96+
'Enterprise Profit',
97+
'Tier Group',
98+
'Tier Group 2',
99+
'Tier Group Sort',
100+
'Tier',
101+
'Tier Name',
102+
'Tier Label',
103+
'Short Name',
104+
'Economic Tree',
105+
'Tier Group v2',
106+
'Tier Name v2',
107+
'Tier Sort v2',
108+
'Tier Group 2 v2',
109+
'Tier Group v3',
110+
'Tier Name v3',
111+
'Short Name v3',
112+
'Tier Sort v3',
113+
'ProductType',
114+
'Product Type',
115+
'BILL TO_CustomerID',
116+
'BILL TO_CustomerName',
117+
'CustomerType',
118+
'MarketCategory',
119+
'OrderChannel',
120+
'Price Override',
121+
20,
122+
'BuyLineID'
123+
);
124+
125+
query T
126+
WITH GrossRev AS
127+
(
128+
SELECT
129+
"ProductType"
130+
, "Product Type"
131+
, "BILL TO_CustomerID"
132+
, "BILL TO_CustomerName"
133+
, "CustomerType"
134+
, "OrderChannel"
135+
, "MarketCategory"
136+
, "Price Branch Region"
137+
, "PriceBranchID"
138+
, "Price Override"
139+
, SUM(0) "Value"
140+
, SUM("Value") "GrossRevenue"
141+
FROM cte_test1
142+
GROUP BY
143+
"ProductType"
144+
, "Product Type"
145+
, "BILL TO_CustomerID"
146+
, "BILL TO_CustomerName"
147+
, "CustomerType"
148+
, "OrderChannel"
149+
, "MarketCategory"
150+
, "Price Branch Region"
151+
, "PriceBranchID"
152+
, "Price Override"
153+
)
154+
, ShortName AS
155+
(
156+
SELECT
157+
"Tier Group v3"
158+
, "Short Name v3"
159+
, "Tier Sort v3"
160+
FROM cte_test2
161+
GROUP BY
162+
"Tier Group v3"
163+
, "Short Name v3"
164+
, "Tier Sort v3"
165+
)
166+
, res AS
167+
(
168+
SELECT
169+
'GrossRevData' "DataSet"
170+
, sn."Tier Group v3"
171+
, sn."Short Name v3"
172+
, sn."Tier Sort v3"
173+
, gr."ProductType"
174+
, gr."Product Type"
175+
, gr."BILL TO_CustomerID"
176+
, gr."BILL TO_CustomerName"
177+
, gr."CustomerType"
178+
, gr."OrderChannel"
179+
, gr."MarketCategory"
180+
, gr."Price Branch Region"
181+
, gr."PriceBranchID"
182+
, gr."Price Override"
183+
, gr."Value"
184+
, gr."GrossRevenue"
185+
FROM ShortName sn
186+
CROSS JOIN GrossRev gr
187+
)
188+
select "BILL TO_CustomerName" from res;
189+
----
190+
BILL TO_CustomerName

0 commit comments

Comments
 (0)