nushell/crates
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
..
nu-cli adjust some commansd input_output type (#11436) 2024-01-15 16:58:26 +08:00
nu-cmd-base Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-cmd-dataframe Fix incorrect handling of boolean flags for builtin commands (#11492) 2024-01-11 17:19:48 +02:00
nu-cmd-extra adjust some commansd input_output type (#11436) 2024-01-15 16:58:26 +08:00
nu-cmd-lang adjust some commansd input_output type (#11436) 2024-01-15 16:58:26 +08:00
nu-color-config Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-command Fix memory consumption of into sqlite (#10232) 2024-01-15 21:41:25 -06:00
nu-engine add type check during eval time (#11475) 2024-01-12 23:48:53 +08:00
nu-explore Fix incorrect handling of boolean flags for builtin commands (#11492) 2024-01-11 17:19:48 +02:00
nu-glob Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-json Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-lsp Fix "Char index out of bounds" Error (#11526) 2024-01-11 15:24:49 -06:00
nu-parser Fix incorrect handling of boolean flags for builtin commands (#11492) 2024-01-11 17:19:48 +02:00
nu-path Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-plugin Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu-pretty-hex Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-protocol Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu-std add type check during eval time (#11475) 2024-01-12 23:48:53 +08:00
nu-system Do not block signals for child processes (#11402) 2024-01-15 16:08:21 -06:00
nu-table Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-term-grid Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-test-support Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-utils Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_custom_values Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_example Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_formats Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_gstat Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_inc Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_python remove vectorize_over_list from python plugin (#9905) 2023-08-03 16:46:48 +02:00
nu_plugin_query Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
README.md

Nushell core libraries and plugins

These sub-crates form both the foundation for Nu and a set of plugins which extend Nu with additional functionality.

Foundational libraries are split into two kinds of crates:

  • Core crates - those crates that work together to build the Nushell language engine
  • Support crates - a set of crates that support the engine with additional features like JSON support, ANSI support, and more.

Plugins are likewise also split into two types:

  • Core plugins - plugins that provide part of the default experience of Nu, including access to the system properties, processes, and web-connectivity features.
  • Extra plugins - these plugins run a wide range of different capabilities like working with different file types, charting, viewing binary data, and more.