Skip to content

Commit

Permalink
feat: Implement TRIM_ARRAY array function
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Sep 25, 2024
1 parent b8f6b87 commit 8c6f813
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/gitql-std/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn register_std_array_functions(map: &mut HashMap<&'static str, Function>) {
map.insert("array_positions", array_positions);
map.insert("array_dims", array_dims);
map.insert("array_replace", array_replace);
map.insert("trim_array", array_trim);
}

#[inline(always)]
Expand Down Expand Up @@ -109,6 +110,13 @@ pub fn register_std_array_function_signatures(map: &mut HashMap<&'static str, Si
return_type: DataType::Dynamic(first_element_type),
},
);
map.insert(
"trim_array",
Signature {
parameters: vec![DataType::Array(Box::new(DataType::Any)), DataType::Integer],
return_type: DataType::Dynamic(first_element_type),
},
);
}

pub fn array_append(inputs: &[Value]) -> Value {
Expand Down Expand Up @@ -198,3 +206,12 @@ pub fn array_replace(inputs: &[Value]) -> Value {
}
Value::Array(array_type, array_values)
}

pub fn array_trim(inputs: &[Value]) -> Value {
let mut array = inputs[0].as_array();
let array_type = inputs[0].data_type();
let array_len = array.len();
let n = i64::min(array.len().try_into().unwrap(), inputs[1].as_int());
array.truncate(array_len - n as usize);
Value::Array(array_type, array)
}
3 changes: 2 additions & 1 deletion docs/functions/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
| ARRAY_POSITION | Array, Any | Integer | Return the position of element in array or NULL if not found. |
| ARRAY_POSITIONS | Array, Any | Array<Integer> | Return the an array of positions of element in array. |
| ARRAY_DIMS | Array | Text | Returns a text representation of the array's dimensions. |
| ARRAY_REPLACE | Array, Any, Any | Array | Replaces each array element equal to the second argument with the third argument. |
| ARRAY_REPLACE | Array, Any, Any | Array | Replaces each array element equal to the second argument with the third argument. |
| TRIM_ARRAY | Array, Integer | Array | Remove the last n elements from the array. |

0 comments on commit 8c6f813

Please sign in to comment.