Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/scripts/smoke_mysql_odbc_wheel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ curl -fsSL --retry 3 --retry-all-errors -o "/tmp/$package" "https://cdn.mysql.co
curl -fsSL --retry 3 --retry-all-errors -o "/tmp/$package" "https://downloads.mysql.com/archives/get/p/10/file/$package"
echo "$MYSQL_ODBC_SHA256 /tmp/$package" | sha256sum -c --quiet -
apt-get -o Acquire::Retries=3 install -y -qq "/tmp/$package" > /dev/null
python -m venv /v && /v/bin/pip install --quiet --find-links /dist /dist/sqlload-*.whl
# This build's sqlload-api wheel by name, so pip never takes a release of the same version from PyPI.
python -m venv /v && /v/bin/pip install --quiet /dist/sqlload_api-*.whl /dist/sqlload-*.whl
python -m venv /c && /c/bin/pip install --quiet /dist/sqlload_cli-*.whl

# Load into a fresh project with SQLLOAD (and the environment before it); print what it said.
Expand Down
20 changes: 16 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ on:
- Cargo.lock
- crates/sqlload/Cargo.toml
- crates/sqlload-python/Cargo.toml
- crates/sqlload-python-host/Cargo.toml

# A new push to a pull request cancels its older wheel builds; a release run is a group of its own,
# so releases never cancel each other.
Expand Down Expand Up @@ -84,7 +85,8 @@ jobs:
with Path(path).open("rb") as file:
return tomllib.load(file)["project"]["version"]
with Path("Cargo.toml").open("rb") as file:
workspace_version = tomllib.load(file)["workspace"]["package"]["version"]
workspace = tomllib.load(file)["workspace"]
workspace_version = workspace["package"]["version"]
manifests = [
("Python package", project_version("packages/sqlload/pyproject.toml")),
("CLI package", project_version("packages/sqlload-cli/pyproject.toml")),
Expand All @@ -94,6 +96,10 @@ jobs:
with Path("packages/sqlload/pyproject.toml").open("rb") as file:
pins = [d for d in tomllib.load(file)["project"]["dependencies"] if d.startswith("sqlload-api")]
manifests.append(("Python package's sqlload-api pin", pins[0].partition("==")[2] if pins else "missing"))
# sqlload-python-host publishes with exact requirements on these, so they must name the release.
for crate in ("sqlload", "sqlload-python-host"):
pin = workspace["dependencies"][crate].get("version", "missing")
manifests.append((f"Cargo workspace's {crate} pin", pin.removeprefix("=")))
mismatches = [f"{label}={version}" for label, version in manifests if version != expected]
if mismatches:
raise SystemExit(
Expand Down Expand Up @@ -333,7 +339,8 @@ jobs:
pattern: wheel-${{ matrix.platform.name }}-*
path: dist
merge-multiple: true
# The sqlload wheel pins sqlload-api at its own version, which installs from here.
# The sqlload wheel pins sqlload-api at its own version. Both wheels are named on each install, so
# pip takes this build's sqlload-api, never a release of the same version from PyPI.
- uses: actions/download-artifact@v5
with:
name: sqlload-api
Expand All @@ -344,7 +351,7 @@ jobs:
python -m venv smoke
bin=smoke/bin
if [[ "${RUNNER_OS}" == "Windows" ]]; then bin=smoke/Scripts; fi
"${bin}/python" -m pip install --quiet --find-links dist dist/sqlload-*.whl
"${bin}/python" -m pip install --quiet dist/sqlload_api-*.whl dist/sqlload-*.whl
"${bin}/sqlload" --help
"${bin}/sqll" --help > /dev/null
"${bin}/python" -c "import sqlload, sqlload._native"
Expand All @@ -360,7 +367,7 @@ jobs:
case "${RUNNER_OS}" in
Linux)
docker run --rm -v "$PWD:/w" -w /w python:3.12-slim bash -c '
python -m venv /v && /v/bin/pip install --quiet --find-links dist dist/sqlload-*.whl &&
python -m venv /v && /v/bin/pip install --quiet dist/sqlload_api-*.whl dist/sqlload-*.whl &&
python -m venv /c && /c/bin/pip install --quiet dist/sqlload_cli-*.whl &&
/v/bin/python .github/scripts/smoke_odbc_wheel.py /v/bin/sqlload /c/bin/sqlload --default-ini /etc/odbcinst.ini --driver-folder "/usr/lib/$(uname -m)-linux-gnu/odbc"'
;;
Expand Down Expand Up @@ -491,7 +498,12 @@ jobs:
- name: Exchange the OIDC token for a crates.io token
id: crates-io-auth
uses: rust-lang/crates-io-auth-action@v1
# sqlload first: sqlload-python-host requires it at exactly this version, so crates.io must have it already.
- name: Publish sqlload
env:
CARGO_REGISTRY_TOKEN: ${{ steps.crates-io-auth.outputs.token }}
run: cargo publish --locked -p sqlload
- name: Publish sqlload-python-host
env:
CARGO_REGISTRY_TOKEN: ${{ steps.crates-io-auth.outputs.token }}
run: cargo publish --locked -p sqlload-python-host
6 changes: 4 additions & 2 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ jobs:
# Checks what crates.io would receive (files, metadata, lockfile) without compiling it again:
# the steps above already build it, and recompiling the packaged copy re-unpacks DuckDB's
# bundled sources into a cached build folder, which fails when that folder is not empty.
- name: Package the crate as crates.io would
run: cargo publish --dry-run --no-verify --locked -p sqlload
# `cargo package` with both crates resolves sqlload-python-host's exact sqlload requirement against the sqlload
# packaged beside it, which `cargo publish --dry-run` can't before that version is on crates.io.
- name: Package the crates as crates.io would
run: cargo package --no-verify --locked -p sqlload -p sqlload-python-host

containers:
name: Container tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ __pycache__/

# Native extension built in place by maturin develop
packages/sqlload/python/sqlload/*.so
tests/external_host/python/example_host/*.so
packages/sqlload/python/sqlload/*.pyd
benchmarks/results/
benchmarks/.wheels/
Expand Down
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ members = [
"crates/sqlload",
"crates/sqlload-benches",
"crates/sqlload-python",
"crates/sqlload-python-host",
"crates/sqlload-python-host-example",
"crates/sqlload-testkit",
]
resolver = "2"
Expand Down Expand Up @@ -73,7 +75,9 @@ serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["raw_value", "arbitrary_precision"] }
sha2 = "0.10"
shake = "0.1"
sqlload = { path = "crates/sqlload", default-features = false }
# Exact: sqlload-python-host publishes with this requirement, and its public types are sqlload's.
sqlload = { path = "crates/sqlload", version = "=0.85.0", default-features = false } # x-release-please-version
sqlload-python-host = { path = "crates/sqlload-python-host", version = "=0.85.0" } # x-release-please-version
sqlload-testkit = { path = "crates/sqlload-testkit" }
tempfile = "3"
testcontainers = "0.28"
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ test-python:
uv pip install --quiet --python .venv/bin/python 'maturin>=1.9,<2' pytest pytest-xdist pyarrow polars pandas orjson duckdb
uv pip install --quiet --python .venv/bin/python -e packages/sqlload-api
cd packages/sqlload && VIRTUAL_ENV=$(CURDIR)/.venv $(CURDIR)/.venv/bin/maturin develop --uv --quiet --features odbc
.venv/bin/python -m pytest packages/sqlload-api/tests packages/sqlload/tests -n auto --dist loadfile
# A host of its own on sqlload-python-host, which loads through the same engine (never published).
cd tests/external_host && VIRTUAL_ENV=$(CURDIR)/.venv $(CURDIR)/.venv/bin/maturin develop --uv --quiet
# The repository root as rootdir: otherwise pytest roots at the first argument's pyproject.toml (sqlload-api's), and names the other tests from there.
.venv/bin/python -m pytest --rootdir $(CURDIR) packages/sqlload-api/tests packages/sqlload/tests tests/external_host -n auto --dist loadfile

test: test-unit test-containers

Expand Down
23 changes: 23 additions & 0 deletions crates/sqlload-python-host-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "sqlload-python-host-example"
version = "0.0.0"
description = "A host of SQLLoad's engine with a Python runtime of its own, built on sqlload-python-host, for its tests."
edition.workspace = true
license.workspace = true
publish = false
rust-version.workspace = true

[lib]
name = "example_host"
crate-type = ["cdylib"]
# Tested from Python (tests/external_host).
test = false
doctest = false

[dependencies]
pyo3.workspace = true
sqlload = { workspace = true, default-features = true }
sqlload-python-host.workspace = true

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod run;
11 changes: 11 additions & 0 deletions crates/sqlload-python-host-example/src/bindings/_helpers/run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use pyo3::{Python, pyfunction};
use sqlload::command::main::run_args_with_host::run_args_with_host;
use sqlload_python_host::host::main::module_host::module_host;

use crate::bindings::constants::RUNTIME_MODULE;

/// Run the `sqlload` command line `argv` (the program name first) with the example host, and return its exit code.
#[pyfunction]
pub(crate) fn run(py: Python<'_>, argv: Vec<String>) -> u8 {
py.detach(|| run_args_with_host(argv, Some(module_host(RUNTIME_MODULE)), false).0)
}
2 changes: 2 additions & 0 deletions crates/sqlload-python-host-example/src/bindings/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// The example host's Python runtime module, which `sqlload-python-host`'s `ModuleHost` calls.
pub const RUNTIME_MODULE: &str = "example_host.example_runtime";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod native_module;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! The `example_host._native` module definition.

use pyo3::types::{PyModule, PyModuleMethods};
use pyo3::{Bound, PyResult, pymodule, wrap_pyfunction};

use crate::bindings::_helpers::run::run;

/// Register `example_host._native`'s functions.
#[pymodule]
#[pyo3(name = "_native")]
pub fn native_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_function(wrap_pyfunction!(run, module)?)?;
Ok(())
}
5 changes: 5 additions & 0 deletions crates/sqlload-python-host-example/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! The functions `example_host._native` exposes to Python.

pub(crate) mod _helpers;
pub mod constants;
pub mod main;
5 changes: 5 additions & 0 deletions crates/sqlload-python-host-example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! `example_host._native`: the `sqlload` command line on a host of its own, built on `sqlload-python-host`, for its tests.

#![forbid(unsafe_code)]

pub mod bindings;
38 changes: 38 additions & 0 deletions crates/sqlload-python-host/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "sqlload-python-host"
version.workspace = true
description = "The Python host of SQLLoad's engine: runs Python tables and crosses what they yield into the engine, for a pyo3 extension of your own."
edition.workspace = true
publish = true
license.workspace = true
readme = "README.md"
repository.workspace = true
rust-version.workspace = true
keywords = ["sqlload", "python", "pyo3", "arrow", "etl"]
categories = ["database", "api-bindings"]

[lib]
name = "sqlload_python_host"
# Tested from Python (packages/sqlload/tests and tests/external_host); a Rust test binary would have to link libpython.
test = false
doctest = false

[features]
# TLS with OpenSSL built in, as in the sqlload-cli wheels.
ssl-vendored = ["sqlload/ssl-vendored"]
# ODBC sources, as in the sqlload-cli wheels.
odbc = ["sqlload/odbc"]

[dependencies]
arrow-array.workspace = true
arrow-pyarrow.workspace = true
arrow-schema.workspace = true
base64.workspace = true
chrono-tz.workspace = true
pyo3.workspace = true
serde_json.workspace = true
sqlload.workspace = true
tracing.workspace = true

[lints]
workspace = true
41 changes: 41 additions & 0 deletions crates/sqlload-python-host/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# sqlload-python-host

The Python host of [SQLLoad](https://github.com/sqlstack-io/sqlload)'s engine, for a program that runs the engine in a
pyo3 extension of its own. It runs Python tables and turns what they yield into the engine's batches exactly as SQLLoad's
`sqlload` package does: dicts and lists of dicts, Arrow data (pyarrow, polars, pandas), `sqlload_api.json` / `ndjson`,
`sqlload_api.http` jobs (run in Rust, page transforms included) and `sqlload_api.checkpoint`s. The generator runs on its
own thread, and its rows are parsed on others without the GIL.

- `host::main::module_host::module_host(runtime)` is the engine's `PythonHost` over a Python runtime module of the
host's own; SQLLoad's is `sqlload._runtime`.
- `host::main::table_stream::table_stream` and `host::main::transform_stream::transform_stream` cross one table's
yields, or one transform's result, for a host that implements `PythonHost` itself.

```rust,ignore
use sqlload_python_host::host::main::module_host::module_host;

let host = module_host("my_tool._sqlload_runtime");
```

## The runtime module

| Function | What it does |
| --- | --- |
| `discover(root, python_paths) -> str` | The sources found, as the JSON list of `PythonSource`s: `name`, `module`, `settings` (TOML keys, tables under `tables`) and an optional `host` value of the host's own, which the engine copies unread into each of the source's reads. |
| `start_table(read) -> Iterable` | Call the table `read` (a `TableRead`, as JSON) names and return what it yields. |
| `current_state(table)`, `table_state(table) -> str \| None` | A logged table's `ctx.state` as JSON while its rows cross, and once its function has finished; `sqlload_api._host.current_state` and `final_state` answer both for a `Context`. |
| `rename(name, columns, context) -> (str \| None, str \| None)` | A table's `rename`: the names, or why there are none. |
| `finish_run() -> None` | The run is over: close clients. |

`sqlload_api.Context.from_read(read, **extras)` builds a table's `ctx` as SQLLoad does; a host with fields of its own
subclasses `sqlload_api.Context`. Transforms and the crossing's callbacks are `sqlload_api._host`'s, never the
runtime's, so one transform registry serves the whole process.

## Versions

- **pyo3 0.29** (any patch), with `abi3-py310` (a host may raise the floor). pyo3 types are part of this crate's
interface, so your extension uses the same pyo3. Cargo refuses a build with two pyo3 minors, as `pyo3-ffi` links the
`python` library; `sqlload_python_host::pyo3` re-exports the one in use.
- **`sqlload-api`** at exactly this crate's version in the interpreter. A different version fails the first read, naming
the version to install.
- This crate, `sqlload` and `sqlload-api` are released together, at one version.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::crossing::models::{
Crossed, CrossingOptions, CrossingStats, Dispatch, PanicSite, ParseJob, Parsed, Polled,
ReadContext, TableBatches,
};
use crate::host::constants::RUNTIME_MODULE;
use crate::host::constants::API_HOST_MODULE;

/// Where one piece's batches go, and the read's counters.
struct Output {
Expand Down Expand Up @@ -366,7 +366,7 @@ impl Output {
.map_err(|error| PyValueError::new_err(error.to_string()))?
.into_pyarrow(py)?;
let (returned, problem): (Option<Bound<'_, PyAny>>, Option<String>) = py
.import(RUNTIME_MODULE)?
.import(API_HOST_MODULE)?
.call_method1("call_transform", (function.as_str(), table, page.context))?
.extract()?;
match returned {
Expand All @@ -382,7 +382,7 @@ impl Output {
}
})?;
let items = py
.import(RUNTIME_MODULE)?
.import(API_HOST_MODULE)?
.call_method1("page_items", (feed,))?;
let snapshot =
PyCFunction::new_closure(py, None, None, |_, _| PyResult::Ok(None::<String>))?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::crossing::errors::CrossError;
use crate::crossing::models::{
Crossed, CrossingOptions, CrossingStats, DictCrossing, Dispatch, PanicSite,
};
use crate::host::constants::RUNTIME_MODULE;
use crate::host::constants::API_HOST_MODULE;

/// Where the generator's items go: dict yields gather in `pending` until a batch is full.
struct Producer<'py> {
Expand Down Expand Up @@ -253,7 +253,7 @@ fn orjson_dumps(
let options = PyDict::new(py);
options.set_item(
"default",
py.import(RUNTIME_MODULE)?.getattr(CROSSING_DEFAULT)?,
py.import(API_HOST_MODULE)?.getattr(CROSSING_DEFAULT)?,
)?;
let numpy = orjson.getattr("OPT_SERIALIZE_NUMPY")?;
let passthrough = orjson.getattr("OPT_PASSTHROUGH_DATETIME")?;
Expand Down
Loading
Loading