mirror of
https://github.com/nushell/nushell.git
synced 2024-12-26 08:59:12 +01:00
7ac3e97bfe
# 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. |
||
---|---|---|
.. | ||
assignment | ||
conversions | ||
database | ||
date | ||
hash_ | ||
math | ||
move_ | ||
network | ||
path | ||
platform | ||
query | ||
random | ||
skip | ||
str_ | ||
take | ||
url | ||
alias.rs | ||
all.rs | ||
any.rs | ||
append.rs | ||
break_.rs | ||
cal.rs | ||
cd.rs | ||
compact.rs | ||
complete.rs | ||
config_env_default.rs | ||
config_nu_default.rs | ||
continue_.rs | ||
cp.rs | ||
debug_info.rs | ||
def.rs | ||
default.rs | ||
detect_columns.rs | ||
do_.rs | ||
drop.rs | ||
each.rs | ||
echo.rs | ||
empty.rs | ||
error_make.rs | ||
every.rs | ||
exec.rs | ||
export_def.rs | ||
fill.rs | ||
find.rs | ||
first.rs | ||
flatten.rs | ||
for_.rs | ||
format.rs | ||
generate.rs | ||
get.rs | ||
glob.rs | ||
group_by.rs | ||
headers.rs | ||
help.rs | ||
histogram.rs | ||
insert.rs | ||
inspect.rs | ||
into_datetime.rs | ||
into_filesize.rs | ||
into_int.rs | ||
join.rs | ||
last.rs | ||
length.rs | ||
let_.rs | ||
lines.rs | ||
loop_.rs | ||
ls.rs | ||
match_.rs | ||
merge.rs | ||
mkdir.rs | ||
mktemp.rs | ||
mod.rs | ||
mut_.rs | ||
nu_check.rs | ||
open.rs | ||
par_each.rs | ||
parse.rs | ||
prepend.rs | ||
print.rs | ||
range.rs | ||
redirection.rs | ||
reduce.rs | ||
reject.rs | ||
rename.rs | ||
return_.rs | ||
reverse.rs | ||
rm.rs | ||
roll.rs | ||
rotate.rs | ||
run_external.rs | ||
save.rs | ||
select.rs | ||
semicolon.rs | ||
seq_char.rs | ||
seq.rs | ||
sort_by.rs | ||
sort.rs | ||
source_env.rs | ||
split_by.rs | ||
split_column.rs | ||
split_row.rs | ||
table.rs | ||
terminal.rs | ||
to_text.rs | ||
touch.rs | ||
transpose.rs | ||
try_.rs | ||
ucp.rs | ||
ulimit.rs | ||
umkdir.rs | ||
uniq_by.rs | ||
uniq.rs | ||
update.rs | ||
upsert.rs | ||
use_.rs | ||
where_.rs | ||
which.rs | ||
while_.rs | ||
with_env.rs | ||
wrap.rs | ||
zip.rs |