Skip to content

Commit 48366b0

Browse files
moba15jungmair
authored andcommitted
expanded SQL documentation
1 parent f8867e5 commit 48366b0

File tree

7 files changed

+425
-39
lines changed

7 files changed

+425
-39
lines changed

docs/SQL/Aggregates.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

docs/SQL/CreateTables.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Create Tables
3+
weight: 100
4+
---
5+
LingoDB provides core functionality for creating tables
6+
7+
```sql
8+
create table t(
9+
-- usual column definitions
10+
a string,
11+
b int primary key -- define primary key,
12+
c decimal(4,2),
13+
d float(2) not null,
14+
-- ...
15+
-- define primary key
16+
primary key(a)
17+
)
18+
```
19+
[Supported Types](./Types.md)
20+
21+
:::info[Unsupported Features (incomplete list)]
22+
Statements like `create table as` are not supported
23+
```sql
24+
create table y as
25+
select *
26+
from z
27+
```
28+
`unique` constraint is not yet supported
29+
```sql
30+
create table t(
31+
a string unique --not supported
32+
)
33+
```
34+
:::
35+
36+

docs/SQL/Expressions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ Currently only few built-in functions are supported as others simply weren't nec
8686
| Function | Description | Example | Result |
8787
|----------------|-------------|------------|------------|
8888
| `date_part` | extract a part of a date | `date_part('year','2023-07-11')` | `2023` |
89-
| `substring` | substring of string | `substring('hello world',2,5)` | `"ello"` |
89+
| `substring` \| `substr` | substring of string | `substring('hello world',2,5)` | `"ello"` |
9090
| `abs` | absolute value of numer | `abs(-1.5)` | `1.5` |
9191
| `upper` | transform string to upper case | `upper('abc'::string)` | `"ABC"` |
9292
| `round` | found numer to number of digits | `round(1.0006,3)` | `1.0010` |
93-
| `hash` | fast hash function | `hash(42)` | `539517765266996231` |
93+
| `hash` | fast hash function | `hash(42)` | `539517765266996231` |
94+
| `date_trunc` | truncates to a specified date part | `date_trunc('year', '2021-12-08'::date)` | `1609459200000000000` ([Issue](https://github.com/lingo-db/lingo-db/issues/162)) |
95+
| `extract` | extract a part from a given date | `extract(year from '2021-12-08'::date)` | `2021` |

docs/SQL/Insert.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Insert
3+
weight: 110
4+
---
5+
6+
LingoDB supports `INSERT` statements to add new rows to a table.
7+
8+
## Insert a single row
9+
10+
You can insert a single row by providing the values for each column.
11+
12+
```sql
13+
INSERT INTO my_table (column1, column2, column3) VALUES ('value1', 123, 45.6);
14+
```
15+
16+
If you are providing values for all columns in the order they appear in the table, you can omit the column names.
17+
18+
```sql
19+
INSERT INTO my_table VALUES ('value1', 123, 45.6);
20+
```
21+
22+
## Insert multiple rows
23+
24+
You can also insert multiple rows in a single statement.
25+
26+
```sql
27+
INSERT INTO my_table (column1, column2) VALUES
28+
('valueA', 1),
29+
('valueB', 2),
30+
('valueC', 3);
31+
```
32+
33+
## Insert from a query
34+
35+
LingoDB also supports inserting data from the result of a `SELECT` query.
36+
37+
```sql
38+
INSERT INTO my_table (column1, column2)
39+
SELECT col_a, col_b FROM another_table WHERE col_c = 'some_condition';
40+
```

docs/SQL/PipeSQL.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
title: PIPE SQL Queries
3+
weight: 1
4+
---
5+
6+
LingoDB supports a subset of Google's PIPE SQL syntax, which allows you to write queries as a series of data transformations. This can make complex queries easier to read and write. A query consists of a data source (e.g., a table) followed by a series of operators, separated by `|>`.
7+
8+
## Simple Data Access
9+
10+
To start a query, specify the data source using the `from` keyword. You can then pipe this data through a series of operators.
11+
12+
```sql
13+
-- Get all information about a specific student
14+
from students
15+
|> where studnr = 12;
16+
17+
-- Selecting specific columns
18+
from students
19+
|> select studnr, name
20+
|> where studnr = 12;
21+
```
22+
23+
To create new columns or perform calculations, use the `extend` operator.
24+
25+
```sql
26+
from students
27+
|> extend extract(year from birthday) as birthyear
28+
|> select name, birthyear;
29+
```
30+
31+
:::info[Unsupported Features (incomplete list)]
32+
Expressions in the `select` clause are not yet supported. You must use `extend` to create new columns.
33+
```sql
34+
-- This is not supported:
35+
from students
36+
|> select name, extract(year from birthday) as birthyear;
37+
```
38+
:::
39+
40+
## Group By/Aggregations
41+
42+
For aggregations and groupings, the `aggregate` operator must be used. `aggregate` produces the grouping columns and then the aggregate columns. Aliases can be assigned directly to grouping expressions.
43+
44+
```sql
45+
from lectures
46+
|> aggregate min(weeklyhours) group by given_by as profNr;
47+
48+
-- Filter aggregated values using `where`
49+
from lectures
50+
|> aggregate min(weeklyhours) as minWeeklyHoours group by given_by as profNr
51+
|> where minWeeklyHoours < 5;
52+
```
53+
54+
:::info[Unsupported Features (incomplete list)]
55+
This query fails because the parser cannot resolve the reference to `min(weeklyhours)` in the where clause.
56+
It does recognizing that both refer to the same computed aggregate.
57+
```sql
58+
-- This is not supported:
59+
from lectures
60+
|> aggregate min(weeklyhours) group by given_by as profNr
61+
|> where min(weeklyhours) < 5;
62+
```
63+
:::
64+
65+
## Window Functions
66+
67+
Window functions must be used in the `extend` operator.
68+
69+
```sql
70+
from lectures
71+
|> extend row_number() over (partition by given_by order by weeklyhours desc) as rn
72+
|> where rn = 1;
73+
```
74+
75+
## Joins
76+
77+
Pipe SQL introduces a `join` operator to join the input with another table. `LEFT`, `RIGHT`, and `FULL OUTER` joins are also supported.
78+
79+
```sql
80+
-- Inner join
81+
from students s
82+
|> join attend h on h.studnr = s.studnr;
83+
84+
-- Left outer join
85+
from students s
86+
|> left join attend h on h.studnr = s.studnr;
87+
88+
-- Right outer join
89+
from students s
90+
|> right join attend h on h.studnr = s.studnr;
91+
92+
-- Full outer join
93+
from students s
94+
|> full join outer attend h on h.studnr = s.studnr;
95+
```
96+
97+
## Sorting and Limiting
98+
99+
You can sort and limit your results using the `order by` and `limit` operators.
100+
101+
```sql
102+
-- Sort students by name
103+
from students
104+
|> order by name;
105+
106+
-- Sort in descending order
107+
from students
108+
|> order by name desc;
109+
110+
-- Limit the number of results
111+
from students
112+
|> limit 10;
113+
114+
-- Combine sort and limit to get the top 10 students by matriculation number
115+
from students
116+
|> order by studnr
117+
|> limit 10;
118+
```
119+
## Set Operations
120+
121+
LingoDB supports `UNION`, `UNION ALL`, `EXCPET` and `EXCPET ALL` to combine the result sets of two or more `SELECT` statements.
122+
123+
```sql
124+
from attend
125+
|> UNION (from attend)
126+
```
127+
:::info[Unsupported Features (incomplete list)]
128+
Set operation operators currently to not support multiple queries
129+
```sql
130+
-- This is not supported:
131+
from x
132+
|> UNION (from y), (from z)
133+
```
134+
:::
135+
136+
## List of all supported pipe operators
137+
138+
The following table provides a summary of all supported pipe operators and their functions. These operators allow for a step-by-step transformation of your data.
139+
140+
| Operator | Description |
141+
|----------------|-------------|
142+
| `SELECT <expr>, ...`| Keeps only the specified columns. |
143+
| `EXTEND <expr> AS <name>, ...`| Adds new columns to the result. |
144+
| `SET <column> = <expr>`| Updates the values in existing columns. |
145+
| `DROP <column>, ...`| Removes specified columns from the result. |
146+
| `WHERE <condition>, ...`| Filters rows based on one or more conditions. |
147+
| `AGGREGATE <aggr> GROUP BY <group> [[AS] alias], ...` | Groups rows and calculates aggregate values. |
148+
| `LIMIT <n>`| Limits the number of rows in the output. |
149+
| `ORDER BY <expr> [ASC\|DESC], ...`| Sorts the result based on specified columns. |
150+
| `{UNION\|INTERSECT\|EXCEPT} [ALL\|DISTINCT] <query>, ... ` | Combines results with other queries using set operations. |
151+
| `[LEFT\|...] JOIN <table> [ON <condition>]` | Joins the current result with another table. |
152+
153+
## Compatibility with Classic SQL
154+
155+
The pipe syntax can be combined with the classic SQL syntax in any way:
156+
157+
```sql
158+
select * from students s
159+
|> where studnr=1
160+
|> join (select * from attend) h on h.studnr = s.studnr;
161+
```

0 commit comments

Comments
 (0)