nushell/crates/nu-command/tests/commands
Skyler Hawthorne 7ac3e97bfe
Fix memory consumption of into sqlite (#10232)
# Description

Currently, the `into sqlite` command collects the entire input stream
into a single Value, which soaks up the entire input into memory, before
it ever tries to write anything to the DB. This is very problematic for
large inputs; for example, I tried transforming a multi-gigabyte CSV
file into SQLite, and before I knew what was happening, my system's
memory was completely exhausted, and I had to hard reboot to recover.

This PR fixes this problem by working directly with the pipeline stream,
inserting into the DB as values are read from the stream.

In order to facilitate working with the stream directly, I introduced a
new `Table` struct to store the connection and a few configuration
parameters, as well as to make it easier to lazily create the table on
the first read value.

In addition to the purely functional fixes, a few other changes were
made to the serialization and user facing behavior.

### Serialization

Much of the preexisting code was focused on generating the exact text
needed for a SQL statement. This is unneeded and less safe than using
the `rusqlite` crate's serialization for native Rust types along with
prepared statements.

### User-Facing Changes

Currently, the command is very liberal in the input types it accepts.
The strategy is basically if it is a record, try to follow its structure
and make an analogous SQL row, which is pretty reasonable. However, when
it's not a record, it basically tries to guess what the user wanted and
just makes a single column table and serializes the value into that one
column, whatever type it may be.

This has been changed so that it only accepts records as input. If the
user wants to serialize non-record types into SQL, then they must
explicitly opt into doing this by constructing a record or table with it
first. For a utility for inserting data into SQL, I think it makes more
sense to let the user choose how to convert their data, rather than make
a choice for them that may surprise them.

However, I understand this may be a controversial change. If the
maintainers don't agree, I can change this back.

#### Long switch names

The `file_name` and `table_name` long form switches are currently
snake_case and expect to be as such at the command line. These have been
changed to kebab-case to be more conventional.

# Tests + Formatting

To test the memory consumption, I used [this publicly available index of
all Wikipedia articles](https://dumps.wikimedia.org/enwiki/20230820/),
using the first 10,000, 100,000, and 1,000,000 entries, in that order. I
ran the following script to benchmark the changes against the current
stable release:

```nu
#!/usr/bin/nu

# let shellbin = $"($env.HOME)/src/nushell/target/aarch64-linux-android/release/nu"
let shellbin = "nu"
const dbpath = 'enwiki-index.db'

[10000, 100000, 1000000]
  | each {|rows|
      rm -f $dbpath;
      do { time -f '%M %e %U %S' $shellbin -c (
        $"bzip2 -cdk ~/enwiki-20230820-pages-articles-multistream-index.txt.bz2
            | head -n ($rows)
            | lines
            | parse '{offset}:{id}:{title}'
            | update cells -c [offset, id] { into int }
            | into sqlite ($dbpath)"
        )
      }
      | complete
      | get stderr
      | str trim
      | parse '{rss_max} {real} {user} {kernel}'
      | update cells -c [rss_max] { $"($in)kb" | into filesize }
      | update cells -c [real, user, kernel] { $"($in)sec" | into duration }
      | insert rows $rows
      | roll right
    }
  | flatten
  | to nuon
```

This yields the following results

Current stable release:

|rows|rss_max|real|user|kernel|
|-|-|-|-|-|
|10000|53.6 MiB|770ms|460ms|420ms|
|100000|209.6 MiB|6sec 940ms|3sec 740ms|4sec 380ms|
|1000000|1.7 GiB|1min 8sec 810ms|38sec 690ms|42sec 550ms|

This PR:

|rows|rss_max|real|user|kernel|
|-|-|-|-|-|
|10000|38.2 MiB|780ms|440ms|410ms|
|100000|39.8 MiB|6sec 450ms|3sec 530ms|4sec 160ms|
|1000000|39.8 MiB|1min 3sec 230ms|37sec 440ms|40sec 180ms|

# Note

I started this branch kind of at the same time as my others, but I
understand the feedback that smaller PRs are preferred. Let me know if
it would be better to split this up.

I do think the scope of the changes are on the bigger side even without
the behavior changes I mentioned, so I'm not sure if that will help this
particular PR very much, but I'm happy to oblige on request.
2024-01-15 21:41:25 -06:00
..
assignment Match ++= capabilities with ++ (#11130) 2023-12-07 05:46:37 +08:00
conversions Fix 9156 endian consistency (#9873) 2023-08-24 07:08:58 -05:00
database Fix memory consumption of into sqlite (#10232) 2024-01-15 21:41:25 -06:00
date rename from date format to format date (#9902) 2023-08-04 06:06:00 +12:00
hash_ Clean up tests containing unnecessary cwd: tokens (#9692) 2023-07-17 18:43:51 +02:00
math Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
move_ Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
network Allow http commands' automatic redirect-following to be disabled (#11329) 2023-12-28 15:26:34 +08:00
path check existance w/o traversing symlinks (#10872) 2024-01-14 07:33:33 +08:00
platform Add long options for platform and random (#10776) 2023-10-19 22:04:33 +02:00
query Feature cleanup (#7182) 2022-11-22 16:58:11 -08:00
random remove size command in favor of str stats (#10784) 2023-11-17 06:49:19 +08:00
skip Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
str_ use to_lowercase in str downcase (#10850) 2023-10-27 19:16:17 +02:00
take Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
url Add url decode command (#10611) 2023-10-05 18:43:58 +02:00
alias.rs feat: Add default docs for aliases, generated from the command they point to (#10825) 2023-12-04 20:56:46 +02:00
all.rs Removes unnecessary cwd and pipeline from various tests (#9202) 2023-05-17 18:55:26 -05:00
any.rs Removes unnecessary cwd and pipeline from various tests (#9202) 2023-05-17 18:55:26 -05:00
append.rs fix(nu-command/tests): further remove unnecessary pipeline() and cwd() (#8793) 2023-04-07 14:09:55 -07:00
break_.rs fix(nu-command/tests): further remove unnecessary pipeline() and cwd() (#8793) 2023-04-07 14:09:55 -07:00
cal.rs fix(nu-command/tests): further remove unnecessary pipeline() and cwd() (#8793) 2023-04-07 14:09:55 -07:00
cd.rs bump rust-toolchain to 1.72.1 (#11079) 2023-11-16 15:14:45 -06:00
compact.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
complete.rs Fix the test which fails on windows (#11478) 2024-01-03 07:22:43 -06:00
config_env_default.rs Command: Add config env/nu --default to print defaults (#10480) 2023-09-25 08:00:59 -05:00
config_nu_default.rs Command: Add config env/nu --default to print defaults (#10480) 2023-09-25 08:00:59 -05:00
continue_.rs fix(nu-command/tests): further remove unnecessary pipeline() and cwd() (#8793) 2023-04-07 14:09:55 -07:00
cp.rs Send only absolute paths to uu_cp (#11080) 2023-11-17 07:30:57 +08:00
debug_info.rs Make debug info lazy (#10728) 2023-10-24 12:48:05 -05:00
def.rs Refactor tests (using cococo instead of ^echo) (#11479) 2024-01-05 11:40:56 +08:00
default.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
detect_columns.rs Use long options for string (#10777) 2023-10-19 22:08:09 +02:00
do_.rs Replace bash with POSIX sh in tests (#11293) 2023-12-15 14:53:19 +08:00
drop.rs Refactor drop columns to fix issues (#10903) 2023-11-09 13:51:46 +01:00
each.rs REFACTOR: move the 0% commands to nu-cmd-extra (#9404) 2023-07-06 08:31:31 -07:00
echo.rs Change echo to print when not redirected (#10338) 2023-09-13 06:35:01 +12:00
empty.rs fix(nu-command/tests): further remove unnecessary pipeline() and cwd() (#8793) 2023-04-07 14:09:55 -07:00
error_make.rs Refactor error make (#10923) 2023-11-03 10:09:33 -05:00
every.rs fix(nu-command/tests): further remove unnecessary pipeline() and cwd() (#8793) 2023-04-07 14:09:55 -07:00
exec.rs Allow spreading arguments to commands (#11289) 2023-12-28 15:43:20 +08:00
export_def.rs Removes unnecessary cwd and pipeline from various tests (#9202) 2023-05-17 18:55:26 -05:00
fill.rs Removes unnecessary cwd and pipeline from various tests (#9202) 2023-05-17 18:55:26 -05:00
find.rs Fix find puts extra cols into record (#9397) 2023-06-10 16:57:26 -05:00
first.rs remove --column from length command and remove record processing (#10091) 2023-08-23 16:03:26 -05:00
flatten.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
for_.rs Change echo to print when not redirected (#10338) 2023-09-13 06:35:01 +12:00
format.rs update format signature to allow record to be passed in (#9898) 2023-08-02 10:57:58 -05:00
generate.rs rename unfold to generate (#10770) 2023-10-19 09:30:34 -05:00
get.rs Fix get -i ignoring errors for only the first cellpath (#11213) 2023-12-02 11:01:08 -06:00
glob.rs fix clippy (#10659) 2023-10-10 03:31:15 +13:00
group_by.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
headers.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
help.rs Tests: clean up unnecessary use of pipeline() (#10170) 2023-08-31 23:10:29 +02:00
histogram.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
insert.rs Fix replacement closures for update, insert, and upsert (#11258) 2023-12-09 15:22:45 -06:00
inspect.rs throw an error instead of a panic if no input is provided to inspect (#9259) 2023-05-22 13:54:04 -05:00
into_datetime.rs add table -> table to into datetime (#9775) 2023-07-23 20:14:51 +02:00
into_filesize.rs Update internal use of decimal to float (#10333) 2023-09-13 23:53:55 +02:00
into_int.rs fix some new chrono warnings (#10384) 2023-09-15 15:46:25 -05:00
join.rs Removes unnecessary cwd and pipeline from various tests (#9202) 2023-05-17 18:55:26 -05:00
last.rs remove --column from length command and remove record processing (#10091) 2023-08-23 16:03:26 -05:00
length.rs Spanned Value step 1: span all value cases (#10042) 2023-08-25 08:48:05 +12:00
let_.rs fix 'let' to properly redirect (#10360) 2023-09-14 10:18:29 +12:00
lines.rs fix panic with lines on an error (#9967) 2023-08-09 14:12:58 +02:00
loop_.rs Change echo to print when not redirected (#10338) 2023-09-13 06:35:01 +12:00
ls.rs Fix incorrect handling of boolean flags for builtin commands (#11492) 2024-01-11 17:19:48 +02:00
match_.rs Support pattern matching null literals (#10829) 2023-10-25 06:30:45 +08:00
merge.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
mkdir.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
mktemp.rs Add mktemp command (#11005) 2023-11-17 19:30:53 -06:00
mod.rs Fix memory consumption of into sqlite (#10232) 2024-01-15 21:41:25 -06:00
mut_.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
nu_check.rs Use long options for string (#10777) 2023-10-19 22:08:09 +02:00
open.rs allow multiple extensions (#10593) 2023-10-13 13:45:36 -05:00
par_each.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
parse.rs Clippy in tests (#10394) 2023-09-16 21:49:10 +02:00
prepend.rs Fix warnings and old names (#8457) 2023-03-15 18:54:55 +13:00
print.rs Clean up tests containing unnecessary cwd: tokens (#9692) 2023-07-17 18:43:51 +02:00
range.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
redirection.rs Fix sandboxing of redirection tests (#11407) 2023-12-23 20:01:20 +01:00
reduce.rs Add long options for filters (#10641) 2023-10-08 13:12:46 +02:00
reject.rs Disallow duplicated columns in table literals (#10875) 2023-11-01 21:25:35 +01:00
rename.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
return_.rs allow early return outside of main (#10514) 2023-09-28 18:49:42 +02:00
reverse.rs Clean up tests containing unnecessary cwd: tokens (#9692) 2023-07-17 18:43:51 +02:00
rm.rs Fix rm for symlinks pointing to directory on windows (issue #11461) (#11463) 2024-01-02 21:27:03 +08:00
roll.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
rotate.rs Clean up tests containing unnecessary cwd: tokens (#9692) 2023-07-17 18:43:51 +02:00
run_external.rs Refactor tests (using cococo instead of ^echo) (#11479) 2024-01-05 11:40:56 +08:00
save.rs Fix an infinite loop if the input stream and output stream are the same (#11384) 2023-12-24 23:29:23 +08:00
select.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
semicolon.rs Slim down tests (#9021) 2023-04-28 13:25:44 +02:00
seq_char.rs Slim down tests (#9021) 2023-04-28 13:25:44 +02:00
seq.rs Slim down tests (#9021) 2023-04-28 13:25:44 +02:00
sort_by.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
sort.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
source_env.rs Tests: clean up unnecessary use of pipeline() (#10170) 2023-08-31 23:10:29 +02:00
split_by.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
split_column.rs Use long options for string (#10777) 2023-10-19 22:08:09 +02:00
split_row.rs Simplify rawstrings in tests (#10180) 2023-09-01 00:08:27 +02:00
table.rs Fix #11047 (#11054) 2023-11-16 05:28:54 -06:00
terminal.rs Add is-terminal to determine if stdin/out/err are a terminal (#10970) 2023-11-21 20:48:39 -06:00
to_text.rs Make to text stream ListStreams (#7577) 2022-12-22 16:38:07 -08:00
touch.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00
transpose.rs Slim down tests (#9021) 2023-04-28 13:25:44 +02:00
try_.rs Clean up tests containing unnecessary cwd: tokens (#9692) 2023-07-17 18:43:51 +02:00
ucp.rs Add file attribute handling flag to cp (#11491) 2024-01-12 12:02:55 -06:00
ulimit.rs Fix build for BSDs (#11372) 2023-12-19 08:58:45 -06:00
umkdir.rs Add umkdir command (#10785) 2023-10-30 07:59:48 -05:00
uniq_by.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
uniq.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
update.rs Fix replacement closures for update, insert, and upsert (#11258) 2023-12-09 15:22:45 -06:00
upsert.rs Fix replacement closures for update, insert, and upsert (#11258) 2023-12-09 15:22:45 -06:00
use_.rs Error on use path item1 item2, if item1 is not a module (#11183) 2023-12-05 11:38:45 +01:00
where_.rs remove warnings in nu_command tests (#10145) 2023-08-29 13:18:52 -07:00
which.rs change the output of which to be more explicit (#9646) 2023-07-20 19:10:53 -05:00
while_.rs Change echo to print when not redirected (#10338) 2023-09-13 06:35:01 +12:00
with_env.rs Clean up tests containing unnecessary cwd: tokens (#9692) 2023-07-17 18:43:51 +02:00
wrap.rs Remove file I/O from tests that don't need it (#11182) 2023-11-29 23:21:34 +01:00
zip.rs Fix: remove unnecessary r#"..."# (#8670) (#9764) 2023-07-21 17:32:37 +02:00