Commit Graph

262 Commits

Author SHA1 Message Date
Kacper Madej
74e19e2148 Optimization 2025-01-09 17:16:58 +07:00
Kacper Madej
dd533414ef Implement -> and ->> operators for json 2025-01-09 15:38:32 +07:00
Jussi Saurio
925bd62cbc fix logic bug in check_index_scan() that swapped lhs/rhs but not the comparison op 2025-01-08 08:20:13 +02:00
Pekka Enberg
ba28999d05 Merge 'Add partial support for datetime() function' from Preston Thorpe
This PR adds the `datetime` function, with all the support currently
that date/time have for modifiers, and `julianday` function, as well as
some additional modifiers for date/time/datetime.
There are a couple considerations here, I left a couple comments but
essentially there is going to have to be some more work done to track
the state of the expression during the application of modifiers, to
handle a bunch of edge-cases like re-applying the same timezone modifier
to itself, or converting an integer automatically assumed to be
julianday, into epoch, or `ceiling`/`floor` which will determine
relative addition of time in cases like
```
2024-01-31 +1 month = 2024-03-02
```
which was painful enough to get working to begin with.
I couldn't get the `julianday_converter` library to get the exact same
float precision as sqlite, so function is included that matches their
output, for some reason floating point math + `.floor()` would give the
correct result. They seem to 'round' to 8 decimal places, and I was able
to get this to work with the same output as sqlite, except in cases like
`2234.5`, in which case we return `2234.5000000` because of the `fmt`
precision:
```rust
pub fn exec_julianday(time_value: &OwnedValue) -> Result<String> {
    let dt = parse_naive_date_time(time_value);
    match dt {
        // if we did something heinous like: parse::<f64>().unwrap().to_string()
        // that would solve the precision issue, but dear lord...
        Some(dt) => Ok(format!("{:.1$}", to_julian_day_exact(&dt), 8)),
        None => Ok(String::new()),
    }
}
```
Suggestions would be appreciated on the float precision issue.

Reviewed-by: Sonny <14060682+sonhmai@users.noreply.github.com>

Closes #600
2025-01-05 20:13:13 +02:00
psvri
2d84956fda Fix quote escape in literals 2025-01-05 01:35:29 +05:30
PThorpe92
ca428b3dda Julianday function and additional tests/comments 2025-01-04 10:42:34 -05:00
PThorpe92
9a635be7b8 Add tests for new modifiers and datetime func 2025-01-04 08:41:05 -05:00
psvri
18137c932e Fix integer overflow output to be same result as sqlite3 2025-01-04 18:14:09 +05:30
Pekka Enberg
fc60e544af Merge 'Fix arithmetic operations for text values' from Vrishabh
We had not implemented arithmetic operations for text values. This PR
implements this and aligns the behavior with sqlite3 .

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

Closes #605
2025-01-04 13:40:03 +02:00
Pekka Enberg
b5e95d7b5d Merge 'Remove SQLite headers dependency from compat tests' from dkaluza
* Add SQLITE_FLAGS env variable handling to compatibility tests as
SQLite does not handle `-q` flag
* Fix inconsistent SQLITE_NOTFOUND error code and add SQLITE_CANTOPEN
code
* Improve compatibility tests to display errors instead of hanging
indefinitely

Closes #599
2025-01-04 10:12:28 +02:00
Pekka Enberg
3ddbb4bdea Merge 'Add CSV import support to shell' from Vrishabh
I was trying to get limbo to a position where we can run the benchmark [
clickbench](https://github.com/ClickHouse/ClickBench/blob/main/sqlite/be
nchmark.sh)  and found that `.import` command was not supported in cli.
This PR adds that support for command `.import` which has the same
parameters as sqlite cli.
Do note that not all options from sqlite `.import` is implemented yet in
this PR.

Reviewed-by: Preston Thorpe <preston@unlockedlabs.org>

Closes #598
2025-01-04 10:11:41 +02:00
Jussi Saurio
a934ead904 Merge 'Json extract' from Kacper Madej
Implements the `json_extract` function.
In the meantime, the json path has already been implemented by
@petersooley in https://github.com/tursodatabase/limbo/pull/555 which is
a requirement for `json_extract`.
However, this PR takes a different approach and parses the JSON path
using the JSON grammar, because there are a lot of quirks in how a JSON
`key` can look (see the JSON grammar in the Pest file).
The downside is that it allocates more memory than the current
implementation, but might be easier to maintain in the long run.
I included a lot of tests with some quirky behavior of the
`json_extract` (some of them still need some work). I also noticed that
these changed between sqlite versions (had `SQLite 3.43.2` locally and
`3.45` gave different results). Due to this, I'm not sure how much value
there is in trying to be fully compatible with SQLite. Perhaps the
approach taken by @petersooley solves 99% of use-cases?

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

Closes #524
2025-01-03 23:53:29 +02:00
Jussi Saurio
0fefffbbcb Merge 'fix: index seek wrong on SeekOp::LT\SeekOp::GT' from Kould
data does not match predicate when using index, e.g: `select id, age
from users where age > 90 limit 1;` will return data with age  90
the reason is that the current index seek directly uses record for
comparison, but the record of the index itself is longer than the record
of the key (because it contains the primary key), so Gt is invalid.
since only single-column indexes are currently supported:
https://github.com/tursodatabase/limbo/pull/350, only the first value of
the record is currently used for comparison.

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

Closes #593
2025-01-03 23:27:24 +02:00
psvri
2b879a4f40 Fix arithmetic operations for text values 2025-01-04 00:34:04 +05:30
Daniel Kaluza
9ab0e90807 Fix compatibility tests not showing errors and hanging indefinitely in case of error 2025-01-03 11:19:21 +01:00
psvri
1f21cf6a71 Feat: Import csv support 2025-01-03 15:20:22 +05:30
Kacper Madej
f27f873804 PR remarks #2 2025-01-02 15:09:16 +07:00
Kacper Madej
7d7d202ffe PR remarks 2025-01-02 14:41:47 +07:00
Kould
e5d0ad044e fix: index seek wrong 2025-01-02 11:11:44 +08:00
Jussi Saurio
2066475e03 feat: subqueries in FROM clause 2024-12-31 14:18:29 +02:00
Kacper Madej
719eda7cf7 Mark tests to fix for newer SQLite version 2024-12-31 15:56:35 +07:00
Kacper Madej
692301e72c Merge branch 'main' into json-extract 2024-12-31 15:53:08 +07:00
Kacper Madej
2e730ead25 Merge branch 'main' into json-extract 2024-12-31 15:41:18 +07:00
Kacper Madej
ad9acf7400 Add support for json_extract 2024-12-31 15:11:36 +07:00
psvri
3ac3fdf0a2 Fix glob 2024-12-30 17:02:31 +05:30
Pekka Enberg
f87dc7cacc Merge 'Support like function with escape' from Vrishabh
I have added support for like function with escape i.e like(X,Y,Z) .
There is good opportunity to refactor/cleanup the like operations which
can be done in another PR, as I wanted to keep the changes small .

Closes #568
2024-12-29 16:58:06 +02:00
psvri
1922b8ea38 Support like function with escape 2024-12-28 13:55:12 +05:30
PThorpe92
82de59dd88 Add compatability tests for mod operator 2024-12-27 15:39:02 -05:00
Pekka Enberg
5b8f00cb8d Merge 'Fixes like function when pattern has regex meta chars' from Vrishabh
Fixes #552
In our construct regex function, we were not escaping the required
characters properly which was causing the failure.
Limbo output with this branch
```
limbo> select like('\%A', '\A');
1
limbo> select like('A$%', 'A$');
1
limbo> select like('%a.a', 'aaaa');
0
```

Closes #553
2024-12-27 18:35:47 +02:00
Pekka Enberg
8352dcd582 Merge 'Fix sqlite_version() out of bound panics' from Diego Reis
#560
Changes to `translate_expr` function:
* [`core/translate/expr.rs`](diffhunk://#diff-
371865d5d7b8bcaed649413c687492e61e94f21387cd9b2c47d989a033888c8bL1558-
R1560): Changed the `amount` parameter in the `Insn::Copy` instruction
from `1` to `0`.
Enhancements to the testing framework:
* [`testing/scalar-functions.test`](diffhunk://#diff-
a046d58ab24eee8207f0ce3199f8d0a609edcef9c24b8ed7f242f7a60e6c1e61R812-
R815): Added a new test `do_execsql_test_regex` to validate that the
`sqlite_version` function returns a valid output.
* [`testing/tester.tcl`](diffhunk://#diff-
316cca92d85df3f78558cc3e60d7420c1fd19a23ecf2bbea534db93ab08ea3ecR29-
R45): Introduced a new procedure `do_execsql_test_regex` to support
regex-based validation of SQL outputs.

Closes #561
2024-12-27 18:34:47 +02:00
psvri
5470ea2344 Add tests in like.test 2024-12-27 21:49:26 +05:30
Diego Reis
2d0c16c428 Fix sqlite_version() out of bound 2024-12-27 11:39:33 -03:00
Diego Reis
9dea335a0a Add test function with regex 2024-12-27 11:39:02 -03:00
Peter Sooley
28244b10d6 implement json_array_length 2024-12-26 15:08:11 -08:00
jussisaurio
82bc9501fd Merge 'feat(optimizer): eliminate between statement' from KaguraMilet
Rewrite `Y BETWEEN X AND Z` as `X <= Y AND Y <= Z`. And due to the
support of this optimization rule, limbo should now be able to execute
the `BETWEEN AND` statement.

Closes #490
2024-12-20 17:23:42 +02:00
KaguraMilet
d5d7185995 add between expr tests 2024-12-20 22:49:44 +08:00
jussisaurio
5b4ef4412b Merge 'Implement json_array' from Kacper Madej
Implements [json_array](https://sqlite.org/json1.html#jarray).
As a side quest, this PR also fixes an issue with the `CHAR` function
which didn't work properly if the parameters were  non-leaf AST nodes.
The PR is quite big, because as I mentioned in https://github.com/tursod
atabase/limbo/issues/127#issuecomment-2541307979 we had to modify
`OwnedValue::Text` to support a `subtype` parameter, which is what
SQLite does.

Closes #504
2024-12-20 16:35:40 +02:00
Kacper Madej
cdb24d3de1 Handle issues with nested arguments 2024-12-20 11:32:57 +01:00
Kacper Madej
19ae42dfa3 Implement json_array 2024-12-20 11:15:48 +01:00
jussisaurio
80d438ba58 Merge 'Handle all SQLite quoting syntax' from Kacper Madej
This PR adds missing SQLite quoting syntax:
https://sqlite.org/lang_keywords.html
Closes #483
Closes #497
```bash
➜  limbo git:(missing-quoting) ✗ cargo run --package limbo --bin limbo home.db
Limbo v0.0.9
Enter ".help" for usage hints.
limbo> select * from home limit 1;
Documents|dir|280|2024-10-13 06:26:05.605048960+02:00
limbo> select * from [home] limit 1;
Documents|dir|280|2024-10-13 06:26:05.605048960+02:00
limbo> select * from `home` limit 1;
Documents|dir|280|2024-10-13 06:26:05.605048960+02:00
limbo> select * from "home" limit 1;
Documents|dir|280|2024-10-13 06:26:05.605048960+02:00
```

Closes #492
2024-12-19 17:00:23 +02:00
Kacper Madej
9e01c22a5e Handle quoting identifiers properly 2024-12-18 19:45:06 +01:00
Raul Ferrando
a6d1a7cb56 pragma: inital approach to handle pragma statements
This change refactors how PRAGMA statements are handled, introducing a
more organized and extensible structure to simplify adding new PRAGMA
properties in the future.

Previously, only the `cache_size` PRAGMA was supported. With this
update, support for the `journal_mode` PRAGMA has been added.
2024-12-18 17:26:16 +01:00
Konstantinos Artopoulos
fb2908b3e9 refactor(testing): move .table tests to shelltests.py 2024-12-18 09:10:37 +02:00
Konstantinos Artopoulos
969ab244c2 feat(cli): added .tables command 2024-12-18 00:00:29 +02:00
jussisaurio
783ec65c77 Merge 'Expression equality checking, some optimizations' from Preston Thorpe
This PR mainly adds custom logic to check equality in ast expressions.
Not sure if this belongs in the `vendored` parser or not, let me know
and I'll bring it out. Also replaces `Vec` arguments with slice refs
where possible, as well as some clippy warnings in the same `emitter`
file.
I'll write some more tests tomorrow to make sure this is as thorough as
possible.
EDIT: failed test same issue referenced in #484. Marking as draft until
more tests + cases added

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

Closes #488
2024-12-17 22:09:20 +02:00
PThorpe92
1833dcb618 Shrink shell help msg and replace hardcoded path for shell tests 2024-12-16 20:14:04 -05:00
Lauri Virtanen
a1c77af8a8 Limit sin and mod tests rows 2024-12-17 00:14:26 +02:00
Lauri Virtanen
ca418c2674 Run do_execsql_test_tolerance for each database 2024-12-17 00:14:26 +02:00
Lauri Virtanen
aa82164717 Add FIXME comments about floating point comparison tolerance 2024-12-17 00:14:25 +02:00
Lauri Virtanen
e69ee80fac Support log(X) and log(B,X) math functions 2024-12-17 00:14:25 +02:00