Commit Graph

76 Commits

Author SHA1 Message Date
Bennett Clement
a85f47310c Move where clause literal out of loop 2024-07-14 14:00:25 +08:00
Bennett Clement
e9647bf425 Fix comments display 2024-07-14 13:50:21 +08:00
Pekka Enberg
0f0b88cbe7 Merge pull request #140 from benclmnt/feat/improve-explain-comments 2024-07-14 08:10:37 +03:00
Bennett Clement
d64733c0b9 Improve explain comments
- Resolve cursor ID to table name and get column name from index
- Since we change the type of BranchOffset to i64, add assertions in
  Program.step() function
- opcode generation compatibility with sqlite: change register number to start from 1
- Improve Column,Rowid comment, Add DecrJumpZero comment, Fix Integer
  comment
- Fix typos in code comments
2024-07-14 11:35:22 +08:00
jussisaurio
b288ac2b89 Optimize where clause bytecode 2024-07-14 01:30:09 +03:00
jussisaurio
851aea212d add coalesce(), refactor/rename add_label() 2024-07-13 23:03:56 +03:00
Pekka Enberg
9458522164 Merge pull request #134 from Ramkarthik/add-group-concat 2024-07-13 20:28:11 +03:00
Pere Diaz Bou
be387eb31c core: labels system and limit refactor
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-13 18:54:33 +02:00
Ramkarthik Krishnamurthy
59c6429555 Fix invalid column names in aggregate function 2024-07-13 11:47:59 +05:30
Ramkarthik Krishnamurthy
dddc0be88f Some refactoring 2024-07-13 02:46:37 +05:30
Ramkarthik Krishnamurthy
a303e6ad96 Implements string_agg and extends group_concat to work with column delimiters 2024-07-13 02:13:28 +05:30
Ramkarthik Krishnamurthy
9268560a51 Implements group concat aggregate function 2024-07-13 00:55:40 +05:30
jussisaurio
1b0aa568a4 Basic where clause support 2024-07-12 00:26:32 +03:00
Bennett Clement
8a9eb74f9b Implement total() aggregation function
- Returns 0.0 when called on non integer / non float columns
- Always returns floating point number
- fix: default for sum() should be NULL when there is no non-NULL row
  per docs
2024-07-11 23:40:55 +08:00
Pekka Enberg
d8af1aa73b Format source code with cargo fmt 2024-07-11 10:43:34 +03:00
Pekka Enberg
d8078d5e19 Simplify LIMIT 0 handling in translate_select()
It's a special-case so let's make it special like SQLite does.
Simplifies the logic in `translate_select()` quite a bit.
2024-07-11 09:42:24 +03:00
Pekka Enberg
46e34daccd Merge pull request #115 from pereman2/real-affinity 2024-07-11 08:11:18 +03:00
Pere Diaz Bou
538d624770 core: apply Real affinity on columns stored as int
Values in sqlite3, as expected, can be stored in different formats to
optimize disk usage. In this case, a 79.0 float will be transformed to a
u8.

sqlite3 deals with this by adding a RealAffinity op after each column
that might need it. Therefore, in this pr we do exactly that :).

Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-10 21:21:49 +02:00
Pekka Enberg
5e47dfabaf Drop Select::from member field
It's not used anywhere.
2024-07-10 15:17:04 +03:00
Pekka Enberg
0316385150 Fold SelectContext to Select
We can use Select to keep track of loops and stuff. Having a separate
SelectContext just means we need to pass around more objects to
functions.
2024-07-10 15:09:30 +03:00
Pekka Enberg
f2b1971def Simplify AST type paths
The `sqlite3_parser` prefix is redundant.
2024-07-10 11:08:07 +03:00
Pere Diaz Bou
e557b7b9ee core: add comment for src_tables
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-09 18:48:26 +02:00
Pere Diaz Bou
61d0f877e3 core: create list of src tables
Instead of having divided "from table" and "join tables", we merge them
in to a list of `src_tables`.

Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-09 18:46:35 +02:00
Pere Diaz Bou
46592e7b34 core: fix translate_expr args from new agg functions
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-09 18:10:18 +02:00
Pere Diaz Bou
110df3cff1 core: fix resolve table name from join
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-09 18:08:16 +02:00
Pere Diaz Bou
0b0885325c core: refactor generation of table row read
In sqlite3 generating the loop to read multiple joined tables follows
the pattern:

```c
sqlite3WhereBegin();
sqlite3WhereEnd();
```

and this generates:
```
sqlite> explain select * from users, products;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     23    0                    0   Start at 23
1     OpenRead       0     2     0     10             0   root=2 iDb=0; users
2     OpenRead       1     3     0     3              0   root=3 iDb=0; products
3     Rewind         0     22    0                    0
4       Rewind         1     22    0                    0
5         Rowid          0     1     0                    0   r[1]=users.rowid
6         Column         0     1     2                    0   r[2]= cursor 0 column 1
7         Column         0     2     3                    0   r[3]= cursor 0 column 2
8         Column         0     3     4                    0   r[4]= cursor 0 column 3
9         Column         0     4     5                    0   r[5]= cursor 0 column 4
10        Column         0     5     6                    0   r[6]= cursor 0 column 5
11        Column         0     6     7                    0   r[7]= cursor 0 column 6
12        Column         0     7     8                    0   r[8]= cursor 0 column 7
13        Column         0     8     9                    0   r[9]= cursor 0 column 8
14        Column         0     9     10                   0   r[10]= cursor 0 column 9
15        Rowid          1     11    0                    0   r[11]=products.rowid
16        Column         1     1     12                   0   r[12]= cursor 1 column 1
17        Column         1     2     13                   0   r[13]= cursor 1 column 2
18        RealAffinity   13    0     0                    0
19        ResultRow      1     13    0                    0   output=r[1..13]
20      Next           1     5     0                    1
21    Next           0     4     0                    1
22    Halt           0     0     0                    0
23    Transaction    0     0     2     0              1   usesStmtJournal=0
24    Goto           0     1     0                    0
```

`sqlite3WhereBegin()` as the name represents, mainly does stuff with
`WHERE` expressions + loop generation. This is why I decided to change
the name to `translate_tables_begin` to try improve the naming.

In our case:
```rust
    translate_table_open_cursor(program, &mut context, select.from.as_ref().unwrap());
    translate_table_open_loop(program, &mut context, loop_index);
```

translates into:
```sql
> explain select * from users, products;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------
0     Init           0     28    0       0   Start at 28
1     OpenReadAsync  0     2     0       0   root=2
2     OpenReadAwait  0     0     0       0
3     OpenReadAsync  1     3     0       0   root=3
4     OpenReadAwait  0     0     0       0
5     RewindAsync    0     0     0       0
6     RewindAwait    0     27    0       0
7     RewindAsync    1     0     0       0
8     RewindAwait    1     25    0       0
9     RowId          0     0     0       0
10    Column         0     1     1       0   r[1]= cursor 0 column 1
11    Column         0     2     2       0   r[2]= cursor 0 column 2
12    Column         0     3     3       0   r[3]= cursor 0 column 3
13    Column         0     4     4       0   r[4]= cursor 0 column 4
14    Column         0     5     5       0   r[5]= cursor 0 column 5
15    Column         0     6     6       0   r[6]= cursor 0 column 6
16    Column         0     7     7       0   r[7]= cursor 0 column 7
17    Column         0     8     8       0   r[8]= cursor 0 column 8
18    Column         0     9     9       0   r[9]= cursor 0 column 9
19    RowId          1     10    0       0
20    Column         1     1     11      0   r[11]= cursor 1 column 1
21    Column         1     2     12      0   r[12]= cursor 1 column 2
22    ResultRow      0     13    0       0   output=r[0..13]
23    NextAsync      1     0     0       0
24    NextAwait      1     8     0       0
25    NextAsync      0     0     0       0
26    NextAwait      0     6     0       0
27    Halt           0     0     0       0
28    Transaction    0     0     0       0
29    Goto           0     1     0       0
```

This works on as many joined tables but... it is ready to extend for
further join operations.

Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-09 18:08:16 +02:00
mazchew
fc0c3d539a add min aggregate function
add changes for all.test
2024-07-09 03:14:02 +08:00
mazchew
3f339d07d3 add max aggregate function 2024-07-09 02:45:34 +08:00
Kunal Singh
00c26286ce fix: lint warnings 2024-07-08 22:43:11 +05:30
Kunal Singh
fab8f1777a fix: unhandled empty args in aggregate function 2024-07-08 19:13:53 +05:30
Vivek Khatri
fe7c1b5b1d Merge branch 'main' into add-agg-fn-count 2024-07-08 13:58:14 +05:30
Vivek
94358dc665 Add aggregate fn: count 2024-07-08 13:55:06 +05:30
Pekka Enberg
30ec86a81e Add sorter utility functions and opcodes
This adds basic in-memory sorting utility functions, similar to SQLite's
src/vdbesort.c. We need to improve this later with external sorting so
to support large data sets.

This also adds sorting functionality to the VDBE. Note that none of this
is wired to SQL translation yet so it's unused for now.
2024-07-07 13:56:55 +03:00
Pekka Enberg
519e6b141f Add Table::column_is_rowid_alias() helper
We need to check for rowid alias elsewhere too with ORDER BY, for
example, so let's extract a small helper for that.
2024-07-07 12:27:08 +03:00
Piotr Jastrzebski
fdbd010d89 Remove incorrect Column::is_rowid_alias
Fixes #83

Signed-off-by: Piotr Jastrzebski <haaawk@gmail.com>
2024-07-07 10:06:45 +02:00
Piotr Sarna
5bd3d283d1 translate: special case for LIMIT 0
Fixes #55
2024-07-06 10:58:19 +02:00
Pekka Enberg
c7a67a1bf4 Fix ResultRow operands
Fix ResultRow operands to follow SQLite bytecode format for consistency.
2024-07-05 18:13:06 +03:00
Pekka Enberg
4a089e6e4e Merge pull request #67 from pereman2/fix-float-parse 2024-07-05 08:43:11 +03:00
Pere Diaz Bou
ae524a07e2 core: Insn::Real support
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-04 17:19:05 +02:00
Pere Diaz Bou
6bebfccd99 core: fix evaluating columns a part from agg
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-07-04 17:02:54 +02:00
Pekka Enberg
f4369c873f Simplify AggFinal emission in translate_select() 2024-07-04 12:21:27 +03:00
Pekka Enberg
e988ca0129 Consolidate AggregateFunction and AggFunc enums 2024-07-04 12:19:17 +03:00
Pekka Enberg
0f9f178746 Emit DecrJumpZero for aggregations too
SQLite emits a DecrJumpZero instruction after ResultRow even when there
are aggregation functions:

```
sqlite> EXPLAIN SELECT avg(age) FROM users LIMIT 1;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     13    0                    0   Start at 13
1     Integer        1     1     0                    0   r[1]=1; LIMIT counter
2     Null           0     2     3                    0   r[2..3]=NULL
3     OpenRead       0     2     0     10             0   root=2 iDb=0; users
4     Rewind         0     8     0                    0
5       Column         0     9     4                    0   r[4]= cursor 0 column 9
6       AggStep        0     4     3     avg(1)         1   accum=r[3] step(r[4])
7     Next           0     5     0                    1
8     AggFinal       3     1     0     avg(1)         0   accum=r[3] N=1
9     Copy           3     5     0                    0   r[5]=r[3]
10    ResultRow      5     1     0                    0   output=r[5]
11    DecrJumpZero   1     12    0                    0   if (--r[1])==0 goto 12
12    Halt           0     0     0                    0
13    Transaction    0     0     1     0              1   usesStmtJournal=0
14    Goto           0     1     0                    0
```

This does not seem to have any user-visible difference in semantics
because we always jump to Halt regardless of the limit. Howwever, to
keep generated code consistent with SQLite and avoid special-case paths,
let's just emit the instruction.
2024-07-04 11:55:10 +03:00
Pekka Enberg
e3031c2594 Simplify translate_select() 2024-07-04 11:37:34 +03:00
Pekka Enberg
3b297dd05b Fix analyze_column() to use RustDoc format 2024-07-04 11:25:37 +03:00
Pekka Enberg
c4e3cce8a2 Introduce Select struct
This introduces an intermediate `Select` struct, which hopefully makes
the codegen a bit simpler by transforming the complext AST to something
more straight-forward.
2024-07-03 22:09:21 +03:00
Pekka Enberg
883e494ac5 Don't import AST types directly
Instead, use the `ast::<type>` qualifier in the code to make it more
explicit where we're dealing with AST and where we're dealing with our
own data types. Paves the way for a `Select` struct.
2024-07-03 20:23:55 +03:00
Pekka Enberg
f9647a58d3 Use Vec::with_capacity() in analyze_columns() 2024-07-03 16:25:00 +03:00
Pekka Enberg
4474d067fe Code cleanups to make Clippy happy 2024-07-03 11:39:29 +03:00
Pekka Enberg
32f72e91fe Merge pull request #60 from pereman2/agg-uppercase
core: fix agg function uppercase parsing
2024-07-03 11:37:48 +03:00