Skip to content

Commit

Permalink
feat: Implement GROUP_CONCAT Aggregation function
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed May 23, 2024
1 parent 6be1152 commit c42c9e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/gitql-ast/src/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn aggregation_functions() -> &'static HashMap<&'static str, Aggregation> {
map.insert("sum", aggregation_sum);
map.insert("avg", aggregation_average);
map.insert("count", aggregation_count);
map.insert("group_concat", aggregation_group_concat);
map
})
}
Expand Down Expand Up @@ -76,6 +77,13 @@ pub fn aggregation_function_signatures() -> &'static HashMap<&'static str, Signa
return_type: DataType::Integer,
},
);
map.insert(
"group_concat",
Signature {
parameters: vec![DataType::Any],
return_type: DataType::Text,
},
);
map
})
}
Expand Down Expand Up @@ -129,3 +137,17 @@ fn aggregation_average(field_name: &str, titles: &[String], objects: &Group) ->
fn aggregation_count(_field_name: &str, _titles: &[String], objects: &Group) -> Value {
Value::Integer(objects.len() as i64)
}

fn aggregation_group_concat(field_name: &str, titles: &[String], objects: &Group) -> Value {
let column_index = titles.iter().position(|r| r.eq(&field_name)).unwrap();
let rows_count = objects.rows.len();
let mut string_values: Vec<String> = Vec::with_capacity(rows_count);
for row in &objects.rows {
let field_value = &row.values.get(column_index).unwrap();
if field_value.data_type().is_null() {
continue;
}
string_values.push(field_value.to_string());
}
Value::Text(string_values.concat())
}
7 changes: 7 additions & 0 deletions docs/function/aggregations.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ The function count() is an aggregate function that returns the number of items i

```sql
SELECT name, max(name) FROM commits GROUP BY name
```

### Aggregation `group_concat`
The function group_concat() is an aggregate function that returns a string with concatenated non-NULL value from a group

```sql
SELECT GROUP_CONCAT(email) FROM commits GROUP BY name
```

0 comments on commit c42c9e4

Please sign in to comment.