Skip to content

Commit 0d1a3d8

Browse files
authored
fix(cubestore): Panic on combination of Union All and single select in root Union All (#6012)
1 parent 2d12eff commit 0d1a3d8

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

rust/cubestore/cubestore/src/queryplanner/flatten_union.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ fn try_remove_sub_union(
6363
parent_inputs: &Vec<LogicalPlan>,
6464
parent_schema: Arc<DFSchema>,
6565
) -> Vec<LogicalPlan> {
66-
let mut may_be_result = Vec::new();
66+
let mut result = Vec::new();
6767
for inp in parent_inputs.iter() {
6868
match inp {
6969
LogicalPlan::Union { inputs, schema, .. } => {
7070
if schema.to_schema_ref() == parent_schema.to_schema_ref() {
71-
may_be_result.extend(inputs.iter().cloned());
71+
result.extend(inputs.iter().cloned());
7272
} else {
7373
return parent_inputs.clone();
7474
}
7575
}
7676
_ => {
77-
return parent_inputs.clone();
77+
result.push(inp.clone());
7878
}
7979
}
8080
}
81-
return may_be_result;
81+
return result;
8282
}

rust/cubestore/cubestore/src/sql/mod.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,6 +2358,80 @@ mod tests {
23582358
}
23592359
_ => assert!(false),
23602360
};
2361+
2362+
let result = service.exec_query("EXPLAIN SELECT a `sel__a`, b `sel__b`, sum(c) `sel__c` from ( \
2363+
select * from ( \
2364+
select * from foo.a\
2365+
) \
2366+
union all
2367+
select * from
2368+
( \
2369+
select * from foo.a1 \
2370+
union all \
2371+
select * from foo.b1 \
2372+
) \
2373+
union all
2374+
select * from foo.b \
2375+
) AS `lambda` where a = 1 group by 1, 2 order by 3 desc").await.unwrap();
2376+
match &result.get_rows()[0].values()[0] {
2377+
TableValue::String(s) => {
2378+
println!("!! s {}", s);
2379+
assert_eq!(s,
2380+
"Sort\
2381+
\n Projection, [sel__a, sel__b, sel__c]\
2382+
\n Aggregate\
2383+
\n ClusterSend, indices: [[1, 3, 4, 2]]\
2384+
\n Union\
2385+
\n Filter\
2386+
\n Scan foo.a, source: CubeTable(index: default:1:[1]:sort_on[a, b]), fields: *\
2387+
\n Filter\
2388+
\n Scan foo.a1, source: CubeTable(index: default:3:[3]:sort_on[a, b]), fields: *\
2389+
\n Filter\
2390+
\n Scan foo.b1, source: CubeTable(index: default:4:[4]:sort_on[a, b]), fields: *\
2391+
\n Filter\
2392+
\n Scan foo.b, source: CubeTable(index: default:2:[2]:sort_on[a, b]), fields: *"
2393+
2394+
);
2395+
}
2396+
_ => assert!(false),
2397+
};
2398+
let result = service.exec_query("EXPLAIN SELECT a `sel__a`, b `sel__b`, sum(c) `sel__c` from ( \
2399+
select * from ( \
2400+
select * from foo.a where 1 = 0\
2401+
) \
2402+
union all
2403+
select * from
2404+
( \
2405+
select * from foo.a1 \
2406+
union all \
2407+
select * from foo.b1 \
2408+
) \
2409+
union all
2410+
select * from foo.b \
2411+
) AS `lambda` where a = 1 group by 1, 2 order by 3 desc").await.unwrap();
2412+
match &result.get_rows()[0].values()[0] {
2413+
TableValue::String(s) => {
2414+
println!("!! s {}", s);
2415+
assert_eq!(s,
2416+
"Sort\
2417+
\n Projection, [sel__a, sel__b, sel__c]\
2418+
\n Aggregate\
2419+
\n ClusterSend, indices: [[1, 3, 4, 2]]\
2420+
\n Union\
2421+
\n Filter\
2422+
\n Filter\
2423+
\n Scan foo.a, source: CubeTable(index: default:1:[1]:sort_on[a, b]), fields: *\
2424+
\n Filter\
2425+
\n Scan foo.a1, source: CubeTable(index: default:3:[3]:sort_on[a, b]), fields: *\
2426+
\n Filter\
2427+
\n Scan foo.b1, source: CubeTable(index: default:4:[4]:sort_on[a, b]), fields: *\
2428+
\n Filter\
2429+
\n Scan foo.b, source: CubeTable(index: default:2:[2]:sort_on[a, b]), fields: *"
2430+
2431+
);
2432+
}
2433+
_ => assert!(false),
2434+
};
23612435
}).await;
23622436
}
23632437

0 commit comments

Comments
 (0)