Skip to content

Commit

Permalink
Update documentation of Window function and order by
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Dec 18, 2024
1 parent 5018222 commit f980bdf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
6 changes: 0 additions & 6 deletions crates/gitql-engine/src/engine_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,6 @@ fn execute_where_statement(
return Ok(());
}

if gitql_object.len() > 1 {
gitql_object.flat()
}

// Perform where command only on the first group
// because group by command not executed yet
apply_filter_operation(
env,
&statement.condition,
Expand Down
36 changes: 36 additions & 0 deletions docs/expression/call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Call expression

## 1. Standard Function Calls

Standard functions call operate on individual rows and return a single value per row, with syntax similar to most programming languages for example

```sql
LEN(name)
LOWER(author_name)
```

## 2. Aggregate Function Calls

Aggregate functions call has the same syntax like standard function call but it's operate on a set of rows (a group) and return a single value for the entire group. They are often used with the `GROUP BY` clause, the value of aggregation function can be used only after group by statement.

```sql
SELECT author_name, COUNT(author_name) AS commit_num FROM commits GROUP BY author_name, author_email ORDER BY commit_num DESC LIMIT 10
```

## 3. Window functions

Window functions perform calculations across a set of rows that are related to the current row. Unlike aggregate functions with GROUP BY, window functions do not collapse rows into a single output row. Instead, they return a value for each input row based on a "window" of related rows,
in window function call you must to explicit define `OVER` clauses even if it empty, also you can use aggregation function as window function.

```sql
SELECT emp_name, dep_name, ROW_NUMBER() OVER(PARTITION BY dep_name) AS row_number FROM emp_salaries

SELECT emp_name,
dep_name,
ROW_NUMBER() OVER partition_dep_order_salary_des AS row_number_per_department,
MIN(salary) OVER partition_dep_order_salary_des AS min_salary_per_department,
MAX(salary) OVER partition_dep_order_salary_des AS max_salary_per_department
FROM emp_salaries
WINDOW partition_dep_order_salary_des AS (PARTITION BY dep_name ORDER BY salary DESC)
ORDER BY dep_name ASC NULLS LAST;
```
3 changes: 2 additions & 1 deletion docs/expression/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ GitQL Expressions
- [Case expression](case.md).
- [Cast expression](cast.md)
- [Array expression](array.md).
- [Access Member](access.md).
- [Access Member](access.md).
- [Call expression](call.md)
7 changes: 7 additions & 0 deletions docs/statement/order_by.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ The `ORDER BY` Statement with `USING <operator>` syntax inspired by PostgreSQL
```sql
SELECT author_name, author_email FROM commits ORDER BY author_email, commit_id USING <
SELECT author_name, author_email FROM commits ORDER BY author_name USING >
```

You can define nulls order policy to set if you want null value to be first or last in the order

```sql
SELECT author_name, author_email FROM commits ORDER BY author_email NULLS FIRST
SELECT author_name, author_email FROM commits ORDER BY author_name NULLS LAST
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ nav:
- Case: expression/case.md
- Cast: expression/cast.md
- Access Member: expression/access.md
- Call: expression/call.md
- STD Functions and Operators:
- "functions/index.md"
- Logical: functions/logical.md
Expand Down

0 comments on commit f980bdf

Please sign in to comment.