Commit Graph

519 Commits

Author SHA1 Message Date
jussisaurio
a108dea825 GROUP BY 2024-09-14 16:14:45 +03:00
jussisaurio
0839211a49 Pass FuncCtx to Insn::Function to keep track of arg count 2024-09-14 12:38:14 +03:00
Pekka Enberg
f67b915855 Merge 'Allocate the right-hand-side register for a binary expression after translating the left-hand-side' from RJ Barman
I found a bug in queries using a like function in the where clause, ex:
`SELECT first_name, last_name FROM users WHERE like('Jas%', first_name)
= 1`.  That panicked with the message:
```
thread 'main' panicked at core/vdbe/mod.rs:1226:33:
internal error: entered unreachable code: Like on non-text registers
```
This was caused by an off-by-one error in the vdbe code for executing a
`ScalarFunc::Like`. However, this only happened in where-like-fn
queries. Queries using the like operator (ex: `SELECT first_name,
last_name FROM users WHERE first_name LIKE 'Jas%'`) did not have this
problem.
I did some digging around, looked at the explains for these queries from
both limbo and sqlite, and it turns out, for binary expressions, limbo
positions the arguments in the register differently, which is the
ultimate root cause of this problem.
For the where-like-fn query, before execution limbo's registers look
like this:
```
[Null, Null, Integer(1), Text("Jas%"), Text("Jason"), Null, Null]
                  ^the rhs 1  ^pattern         ^haystack str
```
Sqlite's look look something like this:
```
[Null, Null, Text("Jas%"), Text("Jason"), Integer(1), Null, Null]
                   ^pattern        ^haystack str  ^the rhs 1
```
Ultimately limbo's execution of scalar like function was looking in
positions 2 and 3 always, but because we stored the right-hand-side
before the like-fn arguments, there was an off-by-one error, and the
non-text register it was finding was the `Integer(1)`.
This PR changes the binary expression translation to allocate the right-
hand-side register *after* translating the left-hand-side, fixing the
off-by-one and matching sqlite's register layout.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #321
2024-09-14 08:29:43 +03:00
Pekka Enberg
59baa209f9 Merge 'core: core btree splitting' from Pere Diaz Bou
This pr adds a new crate to increase expressivity of tests with complex
things like btree node splitting.
To run tests:
```bash
cd core_tester
cargo test test_sequential_write -- --nocapture
```
This prs improves btree balancing with simple page splitting and some
minor refactors.

Closes #316
2024-09-13 10:29:43 +03:00
Pere Diaz Bou
9d1bb4d4ea get_mem_page 2024-09-13 07:24:34 +02:00
rjhallsted
dc3d3a492a Fix register positions of binary expression arguments to fix where-like-fn case 2024-09-12 12:03:27 -07:00
Pekka Enberg
b7926dfe7c Merge 'Cache LIKE regexes' from RJ Barman
This PR adds a regex cache to `ProgramState` so that we ca re-use
already constructed regexes while processing LIKE expressions. I didn't
find anywhere else that seemed like a good fit to put an execution-time
only cache like this, so let me know if there's a better spot.
To best match sqlite, I added the constant mask into the `Function`
instruction (this indicates whether the first argument to the function
was determined to be constant at compile time), and decide whether to
use the cache based on its value. I've left the value for
`constant_mask` as 0 on every other kind of `Function` instruction. That
seemed to be the safest choice, as that appears to be what has been
implicitly done up to this point. Happy to change that if you'd advise
otherwise.

Fixes #168
Closes #320
2024-09-12 16:57:57 +03:00
rjhallsted
e67f1e910e Functionally meaningless change to get cargo fmt to play nice in ci 2024-09-10 14:40:57 -07:00
rjhallsted
960560dffc Use the same comment for Expr::Like case 2024-09-10 14:26:21 -07:00
rjhallsted
a038d1f700 Add comment on why constant_mask is 1 in ScalarFunc::Like case 2024-09-10 14:24:32 -07:00
rjhallsted
e1b134dd88 Fix the spot that constant_mask was being set to 1 2024-09-10 14:08:15 -07:00
rjhallsted
6ac78dfb03 Cache constructed LIKE regexes if FUNCTION P1 is set 2024-09-10 13:54:52 -07:00
rjhallsted
9f18fdbfd2 Remove unecessary clone when executing LIKE function 2024-09-10 11:25:52 -07:00
Lauri Virtanen
8048b4e655 Add missing assertion to substring() test 2024-09-08 19:31:27 +03:00
Pere Diaz Bou
d87f9c9774 core: multiple level btree page split
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
2024-09-05 20:50:30 +02:00
Pekka Enberg
4ff705868a Merge 'core: support modifiers in date function' from Sonny
solves #211. related to #158, #305

## Before
date(...) does not support modifiers

## After
date(...) supports modifiers

```
limbo> explain SELECT date('2023-05-18', '+1 days', '-1 days', '+10 days');
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     8     0                    0   Start at 8
1     String8            0     2     0     2023-05-18     0   r[2]='2023-05-18'
2     String8            0     3     0     +1 days        0   r[3]='+1 days'
3     String8            0     4     0     -1 days        0   r[4]='-1 days'
4     String8            0     5     0     +10 days       0   r[5]='+10 days'
5     Function           1     2     1     date           0   r[1]=func(r[2..])
6     ResultRow          1     1     0                    0   output=r[1]
7     Halt               0     0     0                    0
8     Transaction        0     0     0                    0
9     Goto               0     1     0                    0
```

Closes #315
2024-09-03 13:10:04 +03:00
sonhmai
9cc965186f core: support modifiers in date function 2024-09-03 14:28:07 +07:00
Ajaya Agrawal
84a1d29586 fix 2024-09-03 12:20:38 +05:30
Ajaya Agrawal
5861684d1c fix 2024-09-03 00:46:53 +05:30
Ajaya Agrawal
dcc99148d2 fix 2024-09-03 00:46:53 +05:30
Ajaya Agrawal
cb275feaa2 Index lookup support
Adds support for parsing index structure
2024-09-03 00:46:53 +05:30
Rajiv Harlalka
0b5b0a426d remove redundant comment
Signed-off-by: Rajiv Harlalka <rajivharlalka009@gmail.com>
2024-08-30 20:28:37 +05:30
Rajiv Harlalka
1de974253b add support for custom date time value as first parameter
Signed-off-by: Rajiv Harlalka <rajivharlalka009@gmail.com>
2024-08-30 20:27:59 +05:30
Rajiv Harlalka
6684fb46e4 FEAT: Implement the unixepoch function
Signed-off-by: Rajiv Harlalka <rajivharlalka009@gmail.com>
2024-08-30 20:27:58 +05:30
Pekka Enberg
63c91b83ed Merge 'Add time querying to I/O trait for deterministic simulation' from mason
Follow-up PR to this [issue](https://github.com/penberg/limbo/issues/272).

Closes #307
2024-08-30 17:33:23 +03:00
mazchew
671b61ba19 add time querying to io trait 2024-08-25 22:55:15 +08:00
Pekka Enberg
93964c6655 Merge 'core: simplify page buffer handling' from Pere Diaz Bou
Closes #308
2024-08-25 14:31:42 +03:00
Pere Díaz
19f000238c core: simplify page buffer handling 2024-08-25 12:48:37 +02:00
Pekka Enberg
596abed93b Merge 'core: support modifiers in time function' from Sonny
this PR is a continue of #252 and #277 to resolve #158.

- supports modifiers in `time(..)` functions

```
limbo> select time('now');
11:35:22

limbo> select time('now', '+2 hours', '-3 hours');
10:35:02

limbo> explain select time('now', '+2 hours', '-3 hours');
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     7     0                    0   Start at 7
1     String8            0     2     0     now            0   r[2]='now'
2     String8            0     3     0     +2 hours       0   r[3]='+2 hours'
3     String8            0     4     0     -3 hours       0   r[4]='-3 hours'
4     Function           1     2     1     time           0   r[1]=func(r[2..])
5     ResultRow          1     1     0                    0   output=r[1]
6     Halt               0     0     0                    0
7     Transaction        0     0     0                    0
8     Goto               0     1     0                    0

```

Closes #305
2024-08-25 13:24:43 +03:00
Pekka Enberg
5da38aa11d Merge 'core: fix concat function wrong start_register' from Sonny
## What does PR do?
- fix concat function wrong start_register

## Current behavior
- addr 4 `r[1]=func(r[1..])`
- `r[1]` is target register to store the result, should not be the start reg of the function arguments
- it should be `r[1]=func(r[2..])` where
  - `r[2]` is the start of arguments
  - `r[1]` is target register to store the result

```
limbo> explain SELECT concat('SQLite',' ','Concat') result;
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     7     0                    0   Start at 7
1     String8            0     2     0     SQLite         0   r[2]='SQLite'
2     String8            0     3     0                    0   r[3]=' '
3     String8            0     4     0     Concat         0   r[4]='Concat'
4     Function           1     1     1     concat         0   r[1]=func(r[1..])
5     ResultRow          1     1     0                    0   output=r[1]
6     Halt               0     0     0                    0
7     Transaction        0     0     0                    0
8     Goto               0     1     0                    0
```

## Expected behavior after the fix
- `r[1]=func(r[2..])` where `r[2]` is the start of arguments

```
limbo> explain SELECT concat('SQLite',' ','Concat') result;

addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     7     0                    0   Start at 7
1     String8            0     2     0     SQLite         0   r[2]='SQLite'
2     String8            0     3     0                    0   r[3]=' '
3     String8            0     4     0     Concat         0   r[4]='Concat'
4     Function           1     2     1     concat         0   r[1]=func(r[2..])
5     ResultRow          1     1     0                    0   output=r[1]
6     Halt               0     0     0                    0
7     Transaction        0     0     0                    0
8     Goto               0     1     0                    0
```

Closes #304
2024-08-25 13:24:22 +03:00
jussisaurio
d1059da9f1 Return enum instead of bool for better documentation 2024-08-25 11:15:47 +03:00
jussisaurio
d653f550e2 Don't do any I/O if top level operator is Nothing 2024-08-25 10:43:52 +03:00
sonhmai
c32bb91dd5 core: support modifiers in time function 2024-08-24 18:31:34 +07:00
sonhmai
9af811233b core: fix concat function wrong start_register 2024-08-24 16:14:28 +07:00
mazchew
156005694a added rng to io trait for simulation 2024-08-22 23:24:02 +08:00
Pekka Enberg
51eebb8ea5 Merge 'Add support for nullif scalar function' from Kim Seon Woo
### EXPLAIN nullif(1,2)
<img width="1339" alt="image" src="https://github.com/user-attachments/assets/08230797-914d-4922-b52c-5b2b2b4c2a12">

### Issue
https://github.com/penberg/limbo/issues/144

Closes #299
2024-08-20 20:47:35 +03:00
Kim Seon Woo
8f617dd394 Apply fmt 2024-08-20 18:44:06 +02:00
Kim Seon Woo
8bb2a48cb6 Add support for nullif scalar function 2024-08-20 18:36:06 +02:00
Kim Seon Woo
78817d3804 Fix insert error 2024-08-18 21:00:03 +02:00
Kim Seon Woo
f406481849 Explain why it fails 2024-08-18 21:00:03 +02:00
Piotr Sarna
d8002ff03c core: pretty-print EXPLAIN QUERY PLAN
Mimics SQLite's EXPLAIN QUERY PLAN pretty printer.

Example run on a preexisting db:
```
$ cargo run /tmp/srn "explain query plan select * from t natural join t join t2 on t.id = 2*t2.id where t.id in (select * from t);"
QUERY PLAN
`--JOIN ON t.id = 2 * t2.id
   |--JOIN
   |  |--SCAN t FILTER t.id IN (SELECT * FROM t)
   |  `--SCAN t
   `--SCAN t2

```

Fixes https://github.com/penberg/limbo/issues/295
2024-08-18 20:11:19 +02:00
Pekka Enberg
edf0f754f6 Merge 'More structured query planner' from Jussi Saurio
Reader's guide to this PR:

The aim is to have a more structured and maintainable approach to generating bytecode from the query AST so that different parts of the query processing pipeline have clearer responsibilities, so that developing new functionality is easier. E.g.:

- If you want to implement join reordering -> you do it in `Optimizer`
- If you want to implement `GROUP BY` -> you change `QueryPlanNode::Aggregate` to include it, parse it in `Planner` and handle the code generation for it in `Emitter`

The pipeline is:

`SQL text -> Parser -> Planner -> Optimizer -> Emitter`

and this pipeline generates:

`SQL text -> AST -> Logical Plan -> Optimized Logical Plan -> SQLite Bytecode`

---

Module structure:

`plan.rs`: defines the `Operator` enum. An `Operator` is a tree of other `Operators`, e.g. an `Operator::Join` has `left` and `right` children, etc.

`planner.rs`: Parses an `ast::Select` into a `Plan` which is mainly a wrapper for a root `Operator`

`optimizer.rs`: Makes a new `Plan` from an input `Plan` - does predicate pushdown, constant elimination and turns `Scan` nodes into `SeekRowId` nodes where applicable

`emitter.rs`: Generates bytecode instructions from an input `Plan`.

---

Adds feature `EXPLAIN QUERY PLAN <stmt>` which shows the logical query plan instead of the bytecode plan

---

Other changes:

- Almost everything from `select.rs` removed; things like `translate_aggregation()` moved to `expr.rs`
- `where_clause.rs` removed, some things from it like `translate_condition_expr()` moved to `expr.rs`
- i.e.: there is nothing _new_ in `expr.rs`, stuff just moved there

---

Concerns:

- Perf impact: there's a lot more indirection than before (`Operator`s are very "traditional" trees where they refer to other operators via Boxes etc)

Closes #281
2024-08-18 16:36:51 +03:00
jussisaurio
a79c0c5b34 BytecodeGenerator struct was unnecessary 2024-08-17 14:35:44 +03:00
jussisaurio
2b71a5802d tweak 2024-08-17 14:24:20 +03:00
jussisaurio
b7fbe57ca7 Move translate_table_columns to expr.rs 2024-08-17 14:16:56 +03:00
jussisaurio
05a6616803 BytecodeGenerator struct 2024-08-17 14:12:57 +03:00
jussisaurio
d7d195a618 Cleanup and improve emitter.rs docs 2024-08-17 13:56:59 +03:00
jussisaurio
e8c894e532 More flexible Emitter via stateful operators 2024-08-17 12:55:16 +03:00
JeanArhancet
b6c720c90a feat: add quote function 2024-08-17 09:28:14 +02:00
jussisaurio
d70eb6b3d7 fix seekrowid operator not emitting result when root 2024-08-17 09:16:24 +03:00