Skip to content

Commit 41dbbc0

Browse files
authored
fix(cubestore): Panic: index out of bounds: the len is 0 but the index is 0 (#6311)
1 parent b45def4 commit 41dbbc0

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

rust/cubestore/cubestore-sql-tests/src/tests.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub fn sql_tests() -> Vec<(&'static str, TestFn)> {
5959
t("cast_timestamp_to_utf8", cast_timestamp_to_utf8),
6060
t("numbers_to_bool", numbers_to_bool),
6161
t("union", union),
62+
t("nested_union_empty_tables", nested_union_empty_tables),
6263
t("timestamp_select", timestamp_select),
6364
t("timestamp_seconds_frac", timestamp_seconds_frac),
6465
t("column_escaping", column_escaping),
@@ -1186,6 +1187,76 @@ async fn union(service: Box<dyn SqlClient>) {
11861187
);
11871188
}
11881189

1190+
async fn nested_union_empty_tables(service: Box<dyn SqlClient>) {
1191+
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
1192+
1193+
let _ = service
1194+
.exec_query("CREATE TABLE foo.un_1 (a int, b int, c int)")
1195+
.await
1196+
.unwrap();
1197+
1198+
let _ = service
1199+
.exec_query("CREATE TABLE foo.un (a int, b int, c int)")
1200+
.await
1201+
.unwrap();
1202+
1203+
let _ = service
1204+
.exec_query("CREATE TABLE foo.un_2 (a int, b int, c int)")
1205+
.await
1206+
.unwrap();
1207+
1208+
service
1209+
.exec_query(
1210+
"INSERT INTO foo.un (a, b, c) VALUES
1211+
(1, 2, 3),
1212+
(2, 3, 4),
1213+
(5, 6, 7),
1214+
(8, 9, 10)",
1215+
)
1216+
.await
1217+
.unwrap();
1218+
1219+
let result = service
1220+
.exec_query(
1221+
"
1222+
SELECT aa, bb FROM
1223+
(
1224+
SELECT
1225+
a aa,
1226+
b bb
1227+
FROM (
1228+
SELECT * FROM foo.un
1229+
)
1230+
UNION ALL
1231+
SELECT
1232+
a aa,
1233+
b bb
1234+
FROM
1235+
(
1236+
SELECT * FROM foo.un_1
1237+
UNION ALL
1238+
SELECT * FROM foo.un_2
1239+
1240+
)
1241+
)
1242+
GROUP BY 1, 2 ORDER BY 2 LIMIT 2
1243+
",
1244+
)
1245+
.await
1246+
.unwrap();
1247+
1248+
assert_eq!(result.get_rows().len(), 2);
1249+
assert_eq!(
1250+
result.get_rows()[0],
1251+
Row::new(vec![TableValue::Int(1), TableValue::Int(2),])
1252+
);
1253+
1254+
assert_eq!(
1255+
result.get_rows()[1],
1256+
Row::new(vec![TableValue::Int(2), TableValue::Int(3),])
1257+
);
1258+
}
1259+
11891260
async fn timestamp_select(service: Box<dyn SqlClient>) {
11901261
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
11911262

rust/cubestore/cubestore/src/queryplanner/serialized_plan.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,17 @@ impl SerializedLogicalPlan {
592592
})
593593
.collect::<Vec<_>>();
594594

595-
SerializedLogicalPlan::Union {
596-
inputs,
597-
schema: schema.clone(),
598-
alias: alias.clone(),
595+
if inputs.is_empty() {
596+
SerializedLogicalPlan::EmptyRelation {
597+
produce_one_row: false,
598+
schema: schema.clone(),
599+
}
600+
} else {
601+
SerializedLogicalPlan::Union {
602+
inputs,
603+
schema: schema.clone(),
604+
alias: alias.clone(),
605+
}
599606
}
600607
}
601608
SerializedLogicalPlan::TableScan {

0 commit comments

Comments
 (0)