What is Myval?
Mýval (pronounced as [m'ival]) is translated from Czech as raccoon.
Why not a bear-name?
The common name for raccoon in Czech is "medvídek mýval" which can be translated as "little bear".
But there is Polars?
Myval is not a competitor of Polars. Myval is a lightweight Arrow data frame which is focused on data transformation and IPC.
Because Arrow has got the standardized data layout, data frames can be converted to Polars and vice-versa with zero-copy:
let polars_df = from;
let myval_df = from;
As well as Polars, Myval is based on arrow2.
Some tricks
IPC
Consider there is an Arrow stream block (Schema+Chunk) received from e.g. RPC or Pub/Sub. Convert the block into a Myval data frame:
let df = from_ipc_block.unwrap;
Need to send a data frame back? Convert it to Arrow stream block with a single line of code:
let buf = df.into_ipc_block.unwrap;
Need to send sliced? No problem, there are methods which can easily return sliced series, sliced data frames or IPC chunks.
Overriding data types
Consider there is an i64-column "time" which contains nanosecond timestamps. Let us override its data type:
use ;
df.set_data_type.unwrap;
Parsing numbers from strings
Consider there is a utf8-column "value" which should be parsed to floats:
df..unwrap;
Basic in-place math
df.add.unwrap;
df.sub.unwrap;
df.mul.unwrap;
df.div.unwrap;
Custom in-place transformations
df.apply.unwrap;
Set column ordering
Consider there is a Myval data frame with columns "voltage", "temp1", "temp2", "temp3" which has received data from a server column-by-column in random ordering. Let us correct the ordering back to normal:
df.set_ordering;
Others
Check the documentation: https://docs.rs/myval
Working with databases
Arrow provides several ways to work with databases. Myval additionally provides tools to work with PostgreSQL databases in the easy way via the popular sqlx crate ("postgres" feature must be enabled):
Fetching data from a database
use TryStreamExt;
let pool = new;
let max_size = 100_000;
let mut stream = fetch;
// the stream returns data frames one by one with max data frame size (in
// bytes) = max_size
while let Some = stream.try_next.await.unwrap
Why does the stream object require Arc<PgPool>? There is one important reason: such stream objects are static and can be stored anywhere, e.g. used as cursors in a client-server architecture.
Pushing data into a database
Server
let df = from_ipc_block.unwrap;
// The first received data frame must have "database" field in its schema
// metadata. Next data frames can go without it.
if let Some = df.metadata.get
Client
Let us push Polars data frame into a PostgreSQL database:
use json;
let mut df = from;
df.metadata_mut.insert;
// send the data frame to the server in a single or multiple chunks/blocks
PostgreSQL types supported
-
BOOL, INT2 (16-bit int), INT4 (32-bit int), INT8 (64-bit int), FLOAT4 (32-bit float), FLOAT8 (64-bit float)
-
TIMESTAMP, TIMESTAMPTZ (time zone information is discarded as Arrow arrays can not have different time zones for individual records)
-
CHAR, VARCHAR
-
JSON/JSONB (encoded to strings as LargeUtf8 when fetched)
General limitations
-
Myval is not designed for data engineering. Use Polars.
-
Myval series can contain a single chunk only and there are no plans to extend this. When a Polars data frame with multiple chunks is converted to Myval, the chunks are automatically aggregated.
-
Some features (conversion to Polars, PostgreSQL) are experimental, use at your own risk.