Skip to content

add special handling for sqlite dbs with last, first, select, length - #17415

Merged
fdncred merged 6 commits into
nushell:mainfrom
fdncred:history_last_first_select_speed
Jan 27, 2026
Merged

fdncred merged 6 commits into
nushell:mainfrom
fdncred:history_last_first_select_speed

Conversation

@fdncred

@fdncred fdncred commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

This PR introduces some pushdown optimizations with last, first, select, and length when used with the history command and sqlite databases.

Before

image

After

image

Note the improvement of history | last going from 90,000us to 219us.

Release notes summary - What our users need to know

This PR introduces some pushdown optimizations with last, first, select, and length when used with the history command and sqlite databases.

Tasks after submitting

N/A

@fdncred fdncred added performance Work to make nushell quicker and use less resources A:history Related to the history notes:mention Noted in "Hall of Fame" section notes:ready Indicates Ready for Release notes A:database-SQL Our interactions with sqlite and any other work to interface with databases. labels Jan 24, 2026
@NotTheDr01ds

Copy link
Copy Markdown
Contributor

Very, very nice.

Hate to get greedy, but would it make sense to extend this to slice as well?

@fdncred

fdncred commented Jan 26, 2026

Copy link
Copy Markdown
Contributor Author

what would be the equivalent sqlite sql for slice?

@weirdan

weirdan commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

LIMIT <N> OFFSET <M> (https://www.sqlite.org/lang_select.html#the_limit_clause). But it won't work with three-element ranges (I'm not sure slice does either).

@NotTheDr01ds

Copy link
Copy Markdown
Contributor

Probably not worth the extra effort.

And yes, slice does handle 3 element ranges.

@weirdan

weirdan commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

And yes, slice does handle 3 element ranges.

image

It handles them by ignoring the step element, so it can be translated into limit .... offset .... (not saying that it should be).

@fdncred

fdncred commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

ignoring the step element sounds like a bug

@fdncred

fdncred commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

😰 hope this goes well, let's give 'er a try.

@fdncred
fdncred merged commit dd536a0 into nushell:main Jan 27, 2026
17 checks passed
@fdncred
fdncred deleted the history_last_first_select_speed branch January 27, 2026 18:22
@github-actions github-actions Bot added this to the v0.111.0 milestone Jan 27, 2026
@NotTheDr01ds

NotTheDr01ds commented Jan 28, 2026 •

Copy link
Copy Markdown
Contributor

It handles them by ignoring the step element

(total tangent, since I agree we shouldn't worry about it for this PR)

Ah yeah, I missed that until @fdncred mentioned it today in Open Mic.

Just in case you ever need it, though, the following works, and is similar to what I use in std-rfc/tables

ls | select ...(0..4..10 | into list)

Using the std-rfc helpers, that becomes:

use std-rfc/tables *
ls | select slices 0..4..10

The only "gotcha" is that enumerate is used inside select slices so that the user can see/reference the correct indices.

Note that select slices even supports (as the name implies) multiple slices:

ls | select slices 0..2..6 7..8

@NotTheDr01ds NotTheDr01ds mentioned this pull request Feb 24, 2026
2 tasks done
NotTheDr01ds pushed a commit that referenced this pull request Mar 7, 2026
This PR is a follow-on of these PRs to hopefully finish the last fixing of the sqlite pushdown. The biggest part of this fix is creating mini-sqlite-column-projection code for when sqlite column names are aliased in rust code like `command_line as command`. It's not a fully sql parser but it looks for column aliasing so it knows how to select the data properly. I've tried to comment the code verbosely to help us all remember what it's doing.

- #17415
- #17645
- #17680
- #17668

I also took the opportunity to refactor the push down calls from filter commands so that they're easier to understand by calling a new pipeline function that does the same thing but in a little cleaner way.

There's also a bunch of new tests around `open some_sqlite_file.db`.

Before this PR you would get this with `history | select command`
because `command` is an alias to `command_line`.
```nushell
❯ history | select command
Error: nu::shell::error

  × Failed to execute query
   ╭─[repl_entry #1:1:11]
 1 │ history | select command
   ·           ───┬──
   ·              ╰── no such column: command in SELECT command FROM [history] at offset 7
   ╰────
```
After, it just works (tm)

## Release notes summary - What our users need to know

Refactor sqlite push down and apply to (nearly) all filter commands.

## Tasks after submitting

N/A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:database-SQL Our interactions with sqlite and any other work to interface with databases. A:history Related to the history notes:mention Noted in "Hall of Fame" section notes:ready Indicates Ready for Release notes performance Work to make nushell quicker and use less resources

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants