Metadata-Version: 2.4
Name: beaver-box
Version: 1.4.2
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Database
License-File: LICENSE
Summary: Stream CSV, JSON and NDJSON into PostgreSQL, MySQL or SQLite SQL, with type inference, validation, batching and upserts. Available as a CLI and Python library.
Keywords: sql,csv,json,database,cli,etl
Home-Page: https://gitlab.com/cjggarcia.dev/beaver
Author-email: "Christian Joyce G. Garcia" <cjggarcia.dev@gmail.com>
License-Expression: Apache-2.0
Requires-Python: >=3.8
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Issues, https://gitlab.com/cjggarcia.dev/beaver/-/issues
Project-URL: Repository, https://gitlab.com/cjggarcia.dev/beaver

<p align="center">
    <img src="https://gitlab.com/cjggarcia.dev/beaver/-/raw/main/assets/banner.png?ref_type=heads" alt="beaver banner">
</p>

<p align="center">
    <a href="https://pypi.org/project/beaver-box/"><img src="https://img.shields.io/pypi/v/beaver-box?style=flat-square&color=blue" alt="PyPI Version"></a>
    <a href="https://pypistats.com/packages/beaver-box"><img src="https://pypistats.com/api/badges/beaver-box?period=total" alt="PyPI Stats"></a>
    <a href="https://pypi.org/project/beaver-box/"><img src="https://img.shields.io/badge/python-%3E%3D3.8-blue?style=flat-square&logo=python&logoColor=white" alt="Python Versions"></a>
    <a href="https://www.rust-lang.org/"><img src="https://img.shields.io/badge/rust-1.70%2B-orange?style=flat-square&logo=rust" alt="Rust Version"></a>
    <a href="https://gitlab.com/cjggarcia.dev/beaver"><img src="https://img.shields.io/badge/GitLab-repo-fc6d26?style=flat-square&logo=gitlab&logoColor=white" alt="GitLab"></a>
    <a href="https://pypi.org/project/beaver-box/"><img src="https://img.shields.io/pypi/implementation/beaver-box?style=flat-square" alt="Python Implementation"></a>
    <a href="https://gitlab.com/cjggarcia.dev/beaver/-/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0-green?style=flat-square" alt="License"></a>
</p>

# Beaver 

**Turn raw data into ready-to-run SQL in one command.**

Beaver is a high-performance data conversion tool written in [Rust](https://rust-lang.org/). It streams structured CSV, JSON and NDJSON data from files, standard input or pipeline streams and outputs validated, dialect-accurate SQL statements.

Designed for low overhead and speed, Beaver operates as a zero-dependency CLI binary or as a native [Python](https://www.python.org/) extension compiled via [PyO3](https://pyo3.rs/v0.29.3/).


## Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Command-Line Usage](#command-line-usage)
    - [CLI Flags Reference](#cli-flags-reference)
    - [Example Output](#example-output)
    - [Pipe into a Database](#pipe-into-a-database)
- [Import Options](#import-options)
- [Python Usage](#python-usage)
    - [Error Handling](#error-handling)
- [Behavior Reference](#behavior-reference)
    - [Type Inference](#type-inference)
    - [Schema Inference and Sampling](#schema-inference-and-sampling)
    - [SQL Type Mapping](#sql-type-mapping)
    - [Value Escaping](#value-escaping)
    - [Primary Keys](#primary-keys)
    - [Streaming and Memory](#streaming-and-memory)
- [Testing](#testing)
- [Changelog](#changelog)
- [License](#license)
- [Author](#author)


## Features

- **Streaming Processing:** Streams continuous inputs (CSV, JSON arrays, NDJSON) with minimal footprint.
- **Multi-Dialect Support:** Supports *PostgreSQL*, *MySQL* and *SQLite* identifier quoting and value escaping rules.
- **Automatic Type Inference:** Infers column types from a sample of rows (`--sample`), so one empty cell no longer turns a number column into text.
- **Safe Values:** Rows with leading zeros stay text, `nan` and `inf` are quoted as text and MySQL backslashes are escaped.
- **DDL & DML Generation:** Generates schema definitions alongside value-escaped statements.
- **Primary Keys:** Mark existing columns (including composite keys) as the primary key, or add an auto-increment `id` column.
- **Upsert:** Update or skip rows that conflict with existing unique keys.
- **Batch Inserts:** Set the number of rows per statement.
- **Custom Types:** Set column types, including exact decimal literals.
- **Validation:** Check data without SQL output and stream errors to a JSON report.
- **CSV Options:** Set a delimiter, read headerless files and add null markers.
- **Column Mapping:** Select, reorder and rename columns.
- **Dual Runtime Interface:** Available as a standalone CLI tool and a C-extension Python Library [beaver-box](https://pypi.org/project/beaver-box/).


## Installation

```bash
pip install beaver-box
```

Verify installation:

```bash
beaver --version
```


## Command-Line Usage

```bash
beaver [OPTIONS]
```

Basic Conversion

```bash
# Convert CSV to SQL
beaver -i data.csv -o data.sql -t users --create-table

# Convert JSON to SQL
beaver -i data.json -f json -o data.sql -t users --create-table
```

**Stream from Standard Input (Pipelines)**

Pipe JSON or CSV data directly into Beaver:

```bash
cat data.ndjson | beaver -f json -t users -d postgres
```

**Generate Table Schema (DDL) and Inserts**
Include the `--create-table` flag to prepend a `CREATE TABLE IF NOT EXISTS` definition:

```bash 
cat data.csv | beaver -f csv -t users -d mysql --create-table
```

**Convert File and Write Output Directly to File**

```bash 
beaver -i data.csv -o user_data.sql -t users -d sqlite --create-table
```

**Create a Primary Key**

Use `-k` / `--primary-key` to make one or more existing columns the primary key. Separate column names with commas for a composite key:

```bash
# Single column
beaver -i data.csv -t users --create-table -k id

# Composite key
beaver -i book.csv -t orders --create-table -k user_id,book_id
```

If your data has no suitable column, use `--auto-id` to add a new auto-increment `id` column that the database fills in:

```bash
beaver -i data.csv -t people --create-table --auto-id
```

> `--primary-key` and `--auto-id` require `--create-table` and cannot be used together. An existing primary key also supplies the default conflict key for upserts.

**Scan More Rows to Detect Column Types**

With `--create-table`, Beaver reads the first 100 rows to decide each column's type. If a column only shows its real type further down the file, raise the number with `-s` / `--sample`:

```bash
beaver -i data.csv -t users --create-table --sample 1000
```

If values after the sampled rows do not fit the inferred types, Beaver still writes the SQL and prints a warning to standard error:

```text
beaver: warning: 2 value(s) did not match the column types inferred from the first 100 row(s); use a larger --sample so the schema covers them
```

### CLI Flags Reference

| Option | Short | Description | Default |
| :--- | :--- | :--- | :--- |
| `--input` | `-i` | Input file path (reads from stdin if omitted) | `stdin` |
| `--output` | `-o` | Output file path (writes to stdout if omitted) | `stdout` |
| `--table` | `-t` | Target database table name | `beaver_table` |
| `--format` | `-f` | Input format (`csv`, `json`) | `csv` |
| `--dialect` | `-d` | SQL dialect (`postgres`, `sqlite`, `mysql`) | `postgres` |
| `--create-table` | | Generate `CREATE TABLE IF NOT EXISTS` DDL | `false` |
| `--primary-key` | `-k` | Existing column(s) to use as primary key, comma-separated. Requires `--create-table` | none |
| `--auto-id` | | Add an auto-increment `id` primary key column. Requires `--create-table` | `false` |
| `--sample` | `-s` | Number of rows scanned to infer column types (minimum `1`). Used for DDL, custom types and validation | `100` |
| `--conflict` | | Duplicate handling: `error`, `ignore`, `update` | `error` |
| `--key` | | Conflict columns, comma-separated; defaults to `--primary-key` | none |
| `--batch` | | Maximum rows per INSERT (minimum `1`) | `1` |
| `--type` | | Set `column=type`; repeat for more columns | none |
| `--validate` | | Check data without SQL output; cannot use `--output` | `false` |
| `--report` | | JSON error report; requires `--validate` | none |
| `--delimiter` | | CSV separator: one ASCII character or `tab` | `,` |
| `--no-headers` | | CSV has no header; use `col_1`, `col_2`, ... | `false` |
| `--null` | | Extra CSV null marker; repeat for more markers | none |
| `--columns` | | Keep source columns in this order, comma-separated | all |
| `--rename` | | Set `old=new`; repeat for more columns | none |
| `--help` | `-h` | Print help information | |
| `--version` | `-V` | Print version information | |


> The `json` format accepts both a JSON array of objects and newline-delimited JSON (NDJSON). Beaver detects which one it is from the first character of the input.
>
> Columns keep the order of the keys in the JSON file.
 

### Example Output

Given `data.csv`:

```csv
id,name,active,joined
1,Karen,true,2024-03-01
2,G'Christian,false,2024-04-12
```

Running `beaver -i data.csv -t users -d postgres --create-table` produces:

```sql
CREATE TABLE IF NOT EXISTS "users" (
  "id" BIGINT,
  "name" TEXT,
  "active" BOOLEAN,
  "joined" DATE
);
 
INSERT INTO "users" ("id", "name", "active", "joined") VALUES (1, 'Karen', TRUE, '2024-03-01');
INSERT INTO "users" ("id", "name", "active", "joined") VALUES (2, 'G''Christian', FALSE, '2024-04-12');
```

**With a primary key**

Running `beaver -i data.csv -t users -d postgres --create-table -k id` marks the `id` column as the primary key. Key columns become `NOT NULL`, and a `PRIMARY KEY` clause is added:

```sql
CREATE TABLE IF NOT EXISTS "users" (
  "id" BIGINT NOT NULL,
  "name" TEXT,
  "active" BOOLEAN,
  "joined" DATE,
  PRIMARY KEY ("id")
);
```

**With an auto-increment id**

Given `data.csv`:

```csv
name,email
Karen,karen@example.com
Christian,christian@example.com
```

Running `beaver -i data.csv -t users -d postgres --create-table --auto-id` produces:

```sql
CREATE TABLE IF NOT EXISTS "users" (
  "id" BIGSERIAL PRIMARY KEY,
  "name" TEXT,
  "email" TEXT
);

INSERT INTO "users" ("name", "email") VALUES ('Karen', 'karen@example.com');
INSERT INTO "users" ("name", "email") VALUES ('Christian', 'christian@example.com');
```

> The `INSERT` statements leave out `id`, so the database generates the values.

**With values that only look like numbers**

Given `places.csv`, where the first row has an empty `score`:

```csv
zip,score,note
01234,,nan
10001,4.5,ok
```

Running `beaver -i places.csv -t places -d postgres --create-table` keeps the zero in `zip` and still detects `score` as a number, because Beaver looks at more than the first row:
 
```sql
CREATE TABLE IF NOT EXISTS "places" (
  "zip" TEXT,
  "score" DOUBLE PRECISION,
  "note" TEXT
);
 
INSERT INTO "places" ("zip", "score", "note") VALUES ('01234', NULL, 'nan');
INSERT INTO "places" ("zip", "score", "note") VALUES ('10001', 4.5, 'ok');
```


### Pipe into a Database

Without `-o`, Beaver writes SQL to standard output, so you can pipe it straight into the command-line client of your database. Set `-d` to match the target database.


**SQLite** *(creates the `.db` file if it does not exist)*

```bash
beaver -i data.csv -t users -d sqlite --create-table | sqlite3 user_data.db
```

**PostgreSQL**

```bash
beaver -i data.csv -t users -d postgres --create-table \
    | psql "postgresql://user:pass@localhost:5432/mydb" -v ON_ERROR_STOP=1 --single-transaction -f -
```

> `ON_ERROR_STOP` stops at the first error, and `--single-transaction` rolls everything back if a statement fails.

**MySQL**

```bash
beaver -i data.csv -t users -d mysql --create-table | MYSQL_PWD=secret mysql -u root mydb
```

**Databases running in Docker**
 
```bash
# PostgreSQL
beaver -i data.csv -t users -d postgres --create-table | docker exec -i my-postgres psql -U postgres -d mydb
 
# MySQL
beaver -i data.csv -t users -d mysql --create-table | docker exec -i my-mysql mysql -u root -psecret mydb
```

> - Without `--create-table`, the target table must already exist.
> - By default, running the same command twice inserts the rows twice. With `-k`, the second run fails on duplicate keys. Use `--conflict` to change this.


## Import Options

### Upsert

Update existing rows by key:

```bash
beaver -i users.csv -t users --conflict update --key id
```

Skip duplicates:

```bash
beaver -i users.csv -t users --conflict ignore --key id
```

- `--key` supports composite keys (`--key user_id,book_id`) and defaults to `--primary-key` when provided.
- `--conflict update` requires a key and updates supplied non-key columns. If there are no non-key columns, it skips the conflict.
- `--key` uses existing database constraints; it does not create a key. With `--create-table`, use `--primary-key` to declare one.
- PostgreSQL and SQLite use `ON CONFLICT`. A supplied key must match a database unique constraint. Without a key, `ignore` skips any unique conflict.
- MySQL uses `ON DUPLICATE KEY UPDATE` and resolves **any** unique conflict, regardless of `--key`. Updates use row aliases and require MySQL 8.0.19 or later. Ignore uses a no-op update, which can fire update triggers; unrelated data errors are not suppressed.

### Batch Inserts

```bash
beaver -i users.csv -t users --batch 1000
```

Each INSERT contains up to 1,000 rows. The final batch may be smaller. Batches flush when JSON column names or order change. Update batches also flush on repeated identical input key values. Database comparisons can treat different input values as equal (for example, through collations); such conflicts within one PostgreSQL batch may still fail. Use `--batch 1` for those inputs.

### Custom Types

```bash
beaver -i orders.csv -t orders --create-table \
    --type code=text --type price=decimal:12,2
```

Supported types: `text`, `int`, `float`, `bool`, `uuid`, `date`, `timestamp`, and `decimal:P,S`. Aliases: `integer`, `bigint`, `double`, `real`, `boolean`. Decimal precision is 1–65; scale is 0–30 and cannot exceed precision.

Types apply with or without `--create-table`. Explicit types are checked for every row; incompatible values fail conversion. Decimal values must be plain decimals (no exponent or leading `+`) and fit the declared precision and scale. Their SQL literals are written directly without floating-point conversion. SQLite's numeric affinity can still round decimals when storing them.

### Validation

```bash
beaver -i users.csv --validate --sample 1000 --report errors.json
```

Validation emits no SQL. It checks parsing, selected columns, type overrides, values against the sampled schema, unexpected JSON columns after sampling, and null values in declared primary keys. It returns exit code `0` on success or `1` on failure. Python returns `None` on success and raises `ValueError` on failure.

Reports are streamed as a JSON array, including on malformed input. Each entry has `row` (1-based data row, excluding the CSV header), `column`, `value`, `expected`, and `message`. Parser errors use the next data row and null column details. An empty report is `[]`.

```json
[
  {"row": 101, "column": "id", "value": "oops", "expected": "BIGINT", "message": "type mismatch"}
]
```

Row-level validation errors are collected through the stream. Unrecoverable parser or configuration errors stop processing. Validation uses Beaver's type inference rules; it does not connect to the database, verify database constraints, or detect duplicate keys. Use explicit types when a mixed sample would otherwise infer `TEXT`.

### CSV Options

```bash
beaver -i export.tsv --delimiter tab --null NA --null N/A
beaver -i export.csv --delimiter ';' --no-headers
```

Headerless columns are named `col_1`, `col_2`, and so on; the first row is data. Extra null markers match trimmed CSV values exactly and supplement the default empty/`null` handling. CSV options do not affect JSON input. Duplicate CSV headers are rejected.

### Column Mapping

```bash
beaver -i users.csv --columns id,name --rename name=full_name
```

Selection happens first, then renaming. `--columns` uses source names; `--type`, `--key`, and `--primary-key` use output names. Requested source columns must exist in every row, including JSON rows. Missing columns, repeated selections and duplicate output names are errors.

## Python Usage

Beaver exposes C-extension functions for high-speed string and file conversion inside Python workflows. Each function returns the generated SQL as `str`.

| Function | Input |
| :--- | :--- |
| `convert_csv_to_sql(csv_data, ...)` | CSV **contents** as a string |
| `convert_json_to_sql(json_data, ...)` | JSON / NDJSON **contents** as a string |
| `convert_csv_file_to_sql(path, ...)` | Path to a CSV **file** |
| `convert_json_file_to_sql(path, ...)` | Path to a JSON / NDJSON **file** |
| `convert_dataframe_to_sql(df, ...)` | A **pandas DataFrame** (pandas is not required by Beaver) |

All five accept the same arguments:

| Argument | Description | Default |
| :--- | :--- | :--- |
| `table_name` | Target database table name | `"beaver_table"` |
| `create_table` | Prepend `CREATE TABLE IF NOT EXISTS` DDL | `False` |
| `dialect` | `"postgres"`, `"mysql"` or `"sqlite"` | `"postgres"` |
| `primary_key` | Existing column(s) to use as primary key, comma-separated (for example `"id"` or `"user_id,book_id"`). Requires `create_table=True` | `None` |
| `auto_id` | Add an auto-increment `id` primary key column. Requires `create_table=True` | `False` |
| `sample` | Number of rows scanned to infer column types (minimum `1`). Used for DDL, custom types and validation | `100` |
| `output` | Path of a `.sql` file to write to. The SQL is streamed into the file and the function returns `None`. If omitted, the SQL is returned as a string | `None` |


The new options are keyword-only and available on all five functions:

| Argument | Description | Default |
| :--- | :--- | :--- |
| `conflict` | `"error"`, `"ignore"`, `"update"` | `"error"` |
| `key` | Conflict columns, comma-separated; defaults to `primary_key` | `None` |
| `batch` | Maximum rows per INSERT, minimum 1 | `1` |
| `types` | List of `"column=type"` strings | `None` |
| `validate` | Check input without SQL output | `False` |
| `report` | JSON error report path; requires `validate=True` | `None` |
| `delimiter` | CSV separator or `"tab"` | `","` |
| `no_headers` | Generate `col_1`, `col_2`, ... for CSV | `False` |
| `nulls` | List of extra CSV null markers | `None` |
| `columns` | Source columns, comma-separated | `None` |
| `rename` | List of `"old=new"` strings | `None` |

```python
import beaver

beaver.convert_csv_file_to_sql(
    'users.csv',
    table_name='users',
    create_table=True,
    primary_key='id',
    conflict='update',
    batch=1000,
    columns='id,name,balance',
    rename=['name=full_name'],
    types=['balance=decimal:12,2'],
    nulls=['NA'],
    output='users.sql',
)

# Returns None on success; raises ValueError if validation fails.
beaver.convert_csv_file_to_sql(
    'users.csv', validate=True, types=['id=int'], report='errors.json'
)
```

`validate=True` cannot be combined with `output`. Input, output and report paths must be different files. Reports remain available after validation fails.


**Convert a File to SQL**
 
1. CSV

```python
import beaver

sql = beaver.convert_csv_file_to_sql(
    'data/data.csv',
    table_name='users',
    create_table=True,
    dialect='postgres',
)

print(sql)
```
 
2. JSON/NDJSON

```python
import beaver

sql = beaver.convert_json_file_to_sql(
    'data/data.json',
    table_name='users',
    create_table=True,
    dialect='postgres',
)

print(sql)
```

3. Write Output Directly to a `.sql` File

Pass `output` and Beaver streams the SQL straight into that file instead of building one big string in Python, which keeps memory low for large inputs. The function then returns `None`.

```python
import beaver

beaver.convert_csv_file_to_sql(
    'data/data.csv',
    table_name='users',
    create_table=True,
    output='user_db.sql'
)
```

> New in [v1.3.0](https://gitlab.com/cjggarcia.dev/beaver/-/tags/v1.3.0): the `output` keyword argument, available on every conversion function.
> 
> If the conversion fails, the partly written file is deleted and the error is raised. `output` cannot be the same file as the input.

You can also build the SQL as a string and save it yourself. This holds the whole result in memory, so it suits small inputs:

```python
import beaver

sql = beaver.convert_csv_file_to_sql(
    'data/data.csv',
    table_name='users',
    create_table=True,
    dialect='postgres',
)


with open('user_db.sql', 'w') as f:
    f.write(sql)

print('Successfully generated user_db.sql')
```


**Convert a String to SQL**
 
The `*_to_sql` functions take the data itself, not a file path:
 
```python
import beaver

csv_data = 'id,name\n1,Karen\n2,Christian\n'

sql = beaver.convert_csv_to_sql(
    csv_data,
    table_name='users',
    create_table=True
)

print(sql)
```

**Convert a pandas DataFrame to SQL**
 
Beaver works with plain strings, so a DataFrame goes in as CSV text. Use `df.to_csv(index=False)` and pass the result to `convert_csv_to_sql`:

```python
import pandas as pd
import beaver

df = pd.DataFrame({
    'id': [1, 2],
    'name': ['Karen', 'Christian'],
})

sql = beaver.convert_dataframe_to_sql(
    df,
    table_name='users',
    create_table=True,
    primary_key='id',
)

print(sql)
```

> pandas is not a dependency of Beaver. Install it separately with `pip install pandas`.



**Create a Primary Key**

Use an existing column *(or several, comma-separated, for a composite key)*:

```python
import beaver

sql = beaver.convert_csv_file_to_sql(
    'data/data.csv',
    table_name='users',
    create_table=True,
    primary_key='id',
)

print(sql)
```

Or let the database generate the key with an auto-increment `id` column:

```python
import beaver

sql = beaver.convert_csv_file_to_sql(
    'data/data.csv',
    table_name='users',
    create_table=True,
    auto_id=True,
)

print(sql)
```

**Scan More Rows to Detect Column Types**


Raise `sample` when a column only shows its real type further down the file:

```python
import beaver

sql = beaver.convert_csv_file_to_sql(
    'data/data.csv',
    table_name='users',
    create_table=True,
    sample=1000,
)

print(sql)
```

> The Python functions do not print the type-mismatch warning that the CLI shows. Use `validate=True` for strict checks, explicit `types` for known columns, or a larger `sample`.

 
### Error Handling

| Exception | Raised when |
| :--- | :--- |
| `ValueError` | The input is malformed: unbalanced quotes, a row with a different number of fields than the header, invalid `UTF-8`, or invalid `JSON` |
| `ValueError` | `primary_key` names a column that is not in the data |
| `ValueError` | `primary_key` and `auto_id` are used together |
| `ValueError` | `auto_id` is used and the data already has an `id` column |
| `ValueError` | `primary_key` or `auto_id` is used without `create_table=True` |
| `ValueError` | `sample` is less than `1` |
| `ValueError` | `output` is the same file as the input of a `*_file_to_sql` function |
| `ValueError` | Invalid new options, missing selected/renamed/type/key columns, duplicate CSV headers or output names, incompatible explicit types, or failed validation |
| `ValueError` | `report` is used without validation, validation is combined with `output`, or a report path aliases the input |
| `TypeError` | The object passed to `convert_dataframe_to_sql` has no `to_csv` method |
| `TypeError` | An argument has the wrong type (for example, a number passed as `table_name`) |
| `OSError` | The input file passed to a `*_file_to_sql` function cannot be opened |
| `OSError` | The `output` file cannot be created |

> - If a conversion fails while writing to `output`, the partly written file is deleted before the exception is raised.
> - Errors raised by pandas itself (for example, while converting the DataFrame) are passed through unchanged.
> - An unrecognized `dialect` string does not raise an error. It falls back to `"postgres"`.


## Behavior Reference
 

### Type Inference
 
Each value is classified in this order. The first match wins.
 
| Detected as | Rule |
| :--- | :--- |
| Null | Empty value or the text `null` (case-insensitive) |
| Boolean | `true` or `false` (case-insensitive) |
| Integer | A whole number that fits in 64 bits and prints back as the exact same text |
| Float | A plain decimal or scientific number such as `3.14`, `-0.5` or `1e10` |
| UUID | 36 characters in `8-4-4-4-12` hexadecimal form |
| Date | `YYYY-MM-DD` with a month from `01` to `12` and a day from `01` to `31` |
| Timestamp | `YYYY-MM-DD`, then `T` or a space, then `HH:MM:SS` (anything after that is allowed) |
| Text | Everything else |

Numbers have to survive a round trip, so values that only look numeric stay text and are quoted:

| Value | Detected as | Why |
| :--- | :--- | :--- |
| `01234`, `007` | Text | A leading zero would be lost |
| `+639171234567`, `+5` | Text | The `+` would be lost |
| `123456789012345678901234` | Text | Too large for a 64-bit integer |
| `nan`, `inf`, `infinity` | Text | Not valid SQL number literals |
| `.5`, `5.` | Text | Not written as a plain decimal |
| `2024-13-45` | Text | Not a real date |
| `0`, `-15`, `0.5`, `1e10` | Integer / Float | Exact round trip |


### Schema Inference and Sampling
 
When DDL, custom types or validation are requested, Beaver reads the first `--sample` rows *(default `100`)*, merges the type of every value seen in each column and, with `--create-table`, writes the `CREATE TABLE` statement before the first `INSERT`. Explicit types override the inferred types. Columns are listed in the order they first appear and a column that only shows up in later sampled rows is still included.

| Values seen in a column | Column type |
| :--- | :--- |
| All the same type | Type |
| Nulls and one other type | Type |
| `Integer` and `Float` | `Float` |
| `Date` and `Timestamp` | `Timestamp` |
| Any other mix, or only nulls | `Text` |

> Values are then written to match the column. A number in a `TEXT` column is quoted (`'5'`), and values in numeric or date columns are written unquoted or quoted as usual.

- Input shorter than `--sample` rows is read completely before anything is written.
- By default, a value after the sampled rows that does not fit its column (for example `abc` in an integer column) is still written, and the CLI prints a warning. Validation reports these mismatches as errors. An incompatible explicit type always fails conversion.
- Without DDL, custom types or validation, each value is written from its own inferred type.

 
### SQL Type Mapping
 
| Detected as | PostgreSQL | MySQL | SQLite |
| :--- | :--- | :--- | :--- |
| Integer | `BIGINT` | `BIGINT` | `INTEGER` |
| Float | `DOUBLE PRECISION` | `DOUBLE` | `REAL` |
| Boolean | `BOOLEAN` | `BOOLEAN` | `INTEGER` |
| UUID | `UUID` | `VARCHAR(36)` | `TEXT` |
| Date | `DATE` | `DATE` | `TEXT` |
| Timestamp | `TIMESTAMPTZ` | `DATETIME` | `TEXT` |
| Text / Null | `TEXT` | `TEXT` | `TEXT` |


### Value Escaping

Text values are wrapped in single quotes, and leading and trailing whitespace is trimmed.

| Dialect | Escaping inside text values |
| :--- | :--- |
| PostgreSQL | `'` becomes `''` |
| SQLite | `'` becomes `''` |
| MySQL | `\` becomes `\\` and `'` becomes `''` |


> *MySQL* treats a backslash as an escape character by default. If your server runs with the `NO_BACKSLASH_ESCAPES` SQL mode, backslashes in values will be stored doubled.


### Primary Keys

**Existing columns** (`--primary-key` / `primary_key`) are marked `NOT NULL` and listed in a `PRIMARY KEY (...)` clause at the end of the table definition. Composite keys keep the column order you give.

**Auto-increment id** (`--auto-id` / `auto_id`) adds an `id` column as the first column of the table. Its definition depends on the dialect:

| Dialect | Generated `id` column |
| :--- | :--- |
| PostgreSQL | `"id" BIGSERIAL PRIMARY KEY` |
| MySQL | `` `id` BIGINT AUTO_INCREMENT PRIMARY KEY `` |
| SQLite | `"id" INTEGER PRIMARY KEY AUTOINCREMENT` |


### Streaming and Memory

- CSV is read in chunks, and one row at a time is converted and written.
- A top-level JSON array is read one object at a time, and NDJSON one line at a time. Nothing is loaded as a whole.
- Sampled rows are kept only for DDL, custom types or validation. SQL generation also keeps up to `--batch` rows in memory. Validation reports stream directly to disk.
- Unbalanced quotes in a CSV are detected when the end of the input is reached. Rows converted before that point have already been written, so check the exit code (`1` on error) before using the output.


## Testing

All tests are in [`tests/`](tests/README.md), grouped by component. Run them from the repository root:

```bash
cargo test --all-targets --all-features
cargo build --features python
python3 -m unittest discover -s tests -p 'test_*.py' -v
```

See [tests/README.md](tests/README.md) for the test layout and lint command.

## Changelog
 
See [CHANGELOG.md](https://gitlab.com/cjggarcia.dev/beaver/-/blob/main/CHANGELOG.md?ref_type=heads) for the list of changes in each release.


## LICENSE

This project is licensed under the [Apache License 2.0](https://gitlab.com/cjggarcia.dev/beaver/-/raw/main/LICENSE?ref_type=heads)

## Author

Created and maintained by [cjggarcia.dev](https://gitlab.com/cjggarcia.dev)
