Skip to content

Commit ab3ba7b

Browse files
committed
Fix out of index panic when groups length is zero
1 parent 117c3d3 commit ab3ba7b

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

crates/gitql-engine/src/engine.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,28 @@ fn evaluate_select_query(
124124
);
125125

126126
let number_of_groups = gitql_object.groups.len();
127-
let main_group: &mut Group = &mut gitql_object.groups[0];
128-
129-
// If there are many groups that mean group by is executed before.
130-
// must merge each group into only one element
131-
if number_of_groups > 1 {
132-
for group in gitql_object.groups.iter_mut() {
133-
if group.len() > 1 {
134-
group.rows.drain(1..);
127+
if number_of_groups > 0 {
128+
let main_group: &mut Group = &mut gitql_object.groups[0];
129+
130+
// If there are many groups that mean group by is executed before.
131+
// must merge each group into only one element
132+
if number_of_groups > 1 {
133+
for group in gitql_object.groups.iter_mut() {
134+
if group.len() > 1 {
135+
group.rows.drain(1..);
136+
}
135137
}
138+
gitql_object.flat();
139+
}
140+
// If it a single group but it select only aggregations function,
141+
// should return only first element in the group
142+
else if number_of_groups == 1
143+
&& !select_query.has_group_by_statement
144+
&& select_query.has_aggregation_function
145+
&& main_group.len() > 1
146+
{
147+
main_group.rows.drain(1..);
136148
}
137-
gitql_object.flat();
138-
}
139-
// If it a single group but it select only aggregations function,
140-
// should return only first element in the group
141-
else if number_of_groups == 1
142-
&& !select_query.has_group_by_statement
143-
&& select_query.has_aggregation_function
144-
&& main_group.len() > 1
145-
{
146-
main_group.rows.drain(1..);
147149
}
148150

149151
// Into statement must be executed last after flatted and remove hidden selections

0 commit comments

Comments
 (0)