Commit Graph

495 Commits

Author SHA1 Message Date
Nikita Sivukhin
a4a80f37bc rewrite unary expressions too - in order to support "NOT FALSE" expressions 2025-02-03 11:25:14 +04:00
Pekka Enberg
7257fb8aae Merge 'core: move pragma statement bytecode generator to its own file.' from Sonny
What?
- no logic change
- refactored and moved pragma statement bytecode generation to its own
package to better structure.

Closes #871
2025-02-03 09:10:33 +02:00
Pekka Enberg
662d629666 Rename JoinAwareConditionExpr to WhereTerm
We transform all JOIN conditions into WHERE clause terms in the query
planner. The JoinAwareConditionExpr name tries to make that point, but I
think it makes things more confusing. Let's call it WhereTerm (suggested
by Jussi).
2025-02-03 07:46:51 +02:00
sonhmai
2d4bf2eb62 core: move pragma statement bytecode generator to its own file. 2025-02-03 09:21:14 +07:00
Jussi Saurio
c18c6ad64d Marginal changes to use new data structures and field names 2025-02-02 10:18:13 +02:00
Jussi Saurio
82a2850de9 subquery.rs: use iteration instead of recursion and simplify 2025-02-02 10:18:13 +02:00
Jussi Saurio
98439cd936 optimizer.rs: refactor to use new data structures and remove unnecessary stuff
We don't need `push_predicates()` because that never REALLY was a predicate
pushdown optimization -- it just pushed WHERE clause condition expressions
into the correct SourceOperator nodes in the tree.

Now that we don't have a SourceOperator tree anymore and we keep the conditions
in the WHERE clause instead, we don't need to "push" anything anymore. Leaves
room for ACTUAL predicate pushdown optimizations later :)

We also don't need any weird bitmask stuff anymore, and perhaps we never did,
to determine where conditions should be evaluated.
2025-02-02 10:18:13 +02:00
Jussi Saurio
89fba9305a main_loop.rs: use iteration instead of recursion
Now that we do not have a tree of SourceOperators but rather
a Vec of TableReferences, we can just use loops instead of
recursion for handling the main query loop.
2025-02-02 10:18:13 +02:00
Jussi Saurio
09b6bad0af delete.rs: use new data structures when parsing delete 2025-02-02 10:18:13 +02:00
Jussi Saurio
2ddac4bf21 select.rs: use new data structures when parsing select 2025-02-02 10:18:13 +02:00
Jussi Saurio
16a97d3b98 planner.rs: refactor from/join + where parsing logic
- use new TableReference and JoinAwareConditionExpr
- add utilities for determining at which loop depth a
  WHERE condition should be evaluated, now that "operators"
  do not carry condition expressions inside them anymore.
2025-02-02 10:18:13 +02:00
Jussi Saurio
e63256f657 Change Display implementation of Plan to work with new data structures 2025-02-02 10:18:13 +02:00
Jussi Saurio
390d0e673f plan.rs: refactor data structures
- Get rid of SourceOperator tree
- Make plan have a Vec of TableReference, and TableReference now
  contains the information from the old SourceOperator.
- Remove `predicates` (conditions) from Table References -- put
  everything in the WHERE clause like SQLite, and attach metadata
  to the where clause expressions with JoinAwareConditionExpr struct.
- Refactor select_star() to be simpler now that we use a vec, not a tree
2025-02-02 10:18:13 +02:00
Pekka Enberg
dbb7d1a6ba Merge 'Pagecount' from Glauber Costa
This PR implements the Pagecount pragma, as well as its associated
bytecode opcode

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #819
2025-02-02 09:32:18 +02:00
Glauber Costa
a3387cfd5f implement the pragma page_count
To do that, we also have to implement the vdbe opcode Pagecount.
2025-02-01 19:39:46 -05:00
Nikita Sivukhin
1bd8b4ef7a pass null_eq flag for instructions generated for expressions (not in the conditions) 2025-02-02 02:51:51 +04:00
Nikita Sivukhin
c7aed22e39 null_eq flag disable effect of jump_if_null flag - so it makes no sense to set them both 2025-02-02 02:29:02 +04:00
Nikita Sivukhin
478ee6be8d remove null optimization which didn't check for jump_if_condition_is_true flag
- limbo already store constants only once and more clever optimizations
  better to do with generic optimizator and not manually
2025-02-02 02:28:07 +04:00
Pekka Enberg
20d3399c71 Merge 'implement is and is not where constraints' from Glauber Costa
The main difference between = and != is how null values are handled.
SQLite passes a flag "NULLEQ" to Eq and Ne to disambiguate that.
In the presence of that flag, NULL = NULL.
Some prep work is done to make sure we can pass a flag instead of a
boolean to Eq and Ne. I looked into the bitflags crate but got a bit
scared with the list of dependencies.
Warning:
The following query produces a different result for Limbo:
```
select * from demo where value is null or id == 2;
```
I strongly suspect the issue is with the OR implementation, though. The
bytecode generated is quite different.

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

Closes #847
2025-02-01 17:24:11 +02:00
Glauber Costa
c04260ab54 rename Flags to a less ambiguous name
Those Flags in SQLite are global, but it doesn't mean it has to be
the case for us as well.
2025-02-01 08:09:06 -05:00
Glauber Costa
96987db6ca implement is and is not where constraints
The main difference between = and != is how null values are handled.
SQLite passes a flag "NULLEQ" to Eq and Ne to disambiguate that.
In the presence of that flag, NULL = NULL.

Some prep work is done to make sure we can pass a flag instead of a
boolean to Eq and Ne. I looked into the bitflags crate but got a bit
scared with the list of dependencies.
2025-01-31 23:01:49 -05:00
Glauber Costa
f300d2c8e8 rename register for IsNull opcode
Now it has the same name as NotNull, so it is easier to write macros
2025-01-31 19:09:01 -05:00
Glauber Costa
7aa3cc26ad simplify the writing of bytecode programs
Instead of always having the caller specify all instructions, this
work introduces convenience functions into the program builder,
making the code a lot cleaner.
2025-01-31 11:35:51 -05:00
Glauber Costa
b37317f68b avoid allocations during pragma_list
If we keep the pragma list sorted when declaring it, we can avoid
a vector allocation when printing the pragma_list.
2025-01-31 11:35:51 -05:00
Pekka Enberg
d8a9c57d3a Merge 'Fix table with single column PRIMARY KEY to not create extra btree' from Krishna Vishal
The error is due to comparing the PRIMARY KEY's name to INTEGER when in
it was all in lowercase. This was causing `needs_auto_index` to be set
to `true`.
After the fix:
```
/limbo /tmp/sc2-limbo.db
Limbo v0.0.13
Enter ".help" for usage hints.
limbo> CREATE TABLE temp (t1 integer, primary key (t1));

hexdump -s 28 -n 4 /tmp/sc2-limbo.db
000001c 0000 0200 -- matches SQLite
0000020
```
Closes https://github.com/tursodatabase/limbo/issues/824

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

Closes #830
2025-01-31 18:33:28 +02:00
Glauber Costa
a7cc367c1f implement pragma pragma_list
List all available pragmas (Except pragma_list)
2025-01-31 06:44:56 -05:00
Glauber Costa
62efbde661 use strum package to simplify PragmaName enum management
The pragma list will only grow. The strum crate can be used to:
* automatically convert to string from enum
* automatically convert to enum from string
* implement an iterator over all elements of the enum
2025-01-31 06:44:56 -05:00
krishvishal
8b2393fcef Check for if a column is in descending order to add an automatic primary key index. 2025-01-31 08:25:54 +05:30
Glauber Costa
016b815b59 implement pragma table_info
Both () and = variants covered. It is important to make sure that
the transaction is a read transaction, so we cannot hide all that logic
inside update_pragma, and have to make our decision before that.
2025-01-30 20:00:20 -05:00
Glauber Costa
249a8cf8d2 keep type information as a string in column metadata
SQLite holds on to it deeply, for example:

sqlite> create table a(a int);
sqlite> create table b(b integer);
sqlite> create table c(c glauber);

sqlite> pragma table_info=a;
0|a|INT|0||0
sqlite> pragma table_info=b;
0|b|INTEGER|0||0
sqlite> pragma table_info=c;
0|c|glauber|0||0

So we'll keep it as well so we can produce the same responses.
2025-01-30 19:53:36 -05:00
Glauber Costa
69d3fbc797 keep track of notnull constraint on column creation 2025-01-30 17:04:12 -05:00
Glauber Costa
42f93e9bea add default type to Column definition 2025-01-30 16:45:57 -05:00
Glauber Costa
7a972318a8 Make query_pragma use enum instead of &str
Fixes #823
2025-01-30 14:06:17 -05:00
krishvishal
6f32344efb Make comparison of type_name case insensitive by converting to uppercase 2025-01-30 17:05:14 +05:30
Pekka Enberg
e66648beb8 Merge 'Add support for offset in select queries' from Ben Li
#739
Started adding support for `LIMIT...OFFSET...`
- New `OffsetLimit` opcode
- `OFFSET` is now supported for:
    - `SELECT...LIMIT...OFFSET`
    - `SELECT...GROUP BY...LIMIT...OFFSET`
    - `SELECT...ORDER BY...LIMIT...OFFSET`
    - Subqueries for `SELECT` statements
**In progress/todo**
- [x] Testing
- [x] Handle negative offset value
- **(will make in separate PR)** Add support for
`DELETE...LIMIT...OFFSET`
- **(will make in separate PR)** Use `limit + offset` sum register from
`OffsetLimit` to constrain number of records inserted into sorter

Closes #779
2025-01-30 13:29:49 +02:00
Pekka Enberg
5614a7751c Merge 'implement isnull / not null for filter expressions' from Glauber Costa
Allow us to write queries like:
        SELECT name, type, sql FROM sqlite_schema where sql isnull
and
        SELECT name, type, sql FROM sqlite_schema where sql not null

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

Closes #829
2025-01-30 13:28:53 +02:00
Pekka Enberg
4673ac969e Merge 'Fix SELECT -9223372036854775808 result differs from SQLite' from Krishna Vishal
Closes #812
`-9223372036854775808` is `MIN_INT64`. So when we extract out the minus
and try to parse the remainder it becomes greater than MAX_INT64
(9223372036854775807) and will trigger overflow, which converts the
literal into `Real`. So we have to handle it as a special case.

Reviewed-by: Kim Seon Woo (@seonWKim)

Closes #814
2025-01-30 13:25:27 +02:00
krishvishal
cab0625017 Fixes limbo creating an extra btree, when table has single column PRIMARy KEY.
The error is due to comparing the PRIMARY KEY's name to INTEGER when in it was all in lowercase.
2025-01-30 15:04:50 +05:30
Glauber Costa
effde1cc04 implement isnull / not null for filter expressions
Allow us to write queries like:

	SELECT name, type, sql FROM sqlite_schema where sql isnull

and

	SELECT name, type, sql FROM sqlite_schema where sql not null
2025-01-29 20:58:04 -05:00
Ihor Andrianov
d57d9bef6f add function definition 2025-01-29 22:37:04 +02:00
Pekka Enberg
06edf33878 Merge 'json_patch() function implementation' from Ihor Andrianov
First review #820
The function follows RFC 7386 JSON Merge Patch semantics:
* If the patch is null, the target is replaced with null
* If the patch contains a scalar value, the target is replaced with that
value
* If both target and patch are objects, the patch is recursively applied
* null values in the patch result in property removal from the target

Closes #821
2025-01-29 19:54:12 +02:00
Ihor Andrianov
98be735f5a add json_patch to expr and vm 2025-01-29 18:05:39 +02:00
krishvishal
39b4122ddd chore: cargo fmt 2025-01-29 15:19:05 +05:30
krishvishal
a4a1ff2b6d Handle special case -9223372036854775808 and prevent conversion to Real 2025-01-29 15:13:57 +05:30
Glauber Costa
8f24d18ad8 implement sqlite_source_id function 2025-01-28 14:55:38 -05:00
Krishna Vishal
61d60cf4b7 Fix panic when double quoted strings are used for column names in SQL statements. Double quoted strings are not supported in SQLite. Fixes https://github.com/tursodatabase/limbo/issues/800 2025-01-27 23:15:04 +05:30
ben594
54a6505b33 Handle negative limits and offsets 2025-01-26 16:40:30 -05:00
ben594
983fe4c151 Emit Integer, OffsetLimit instructions, and emit IfPos instruction to
skip rows

Emit Integer, OffsetLimit instructions for offset, and define function to emit IfPosinstruction to skip rows

Emit IfPos instructions to handle offset for simple select

Emit IfPos to handle offset for select with order by

Moved repeated emit_offset function call into emit_select_result
2025-01-26 16:40:30 -05:00
ben594
0ff4389eea Add registers for offset and sum of limit and offset 2025-01-26 16:40:30 -05:00
ben594
d03a0dbd39 Added parsing of offset clause 2025-01-26 16:40:30 -05:00