Skip to content
Open
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
35 changes: 18 additions & 17 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ reqwest = { version = "0.13", default-features = false, features = ["json", "que
# The ring provider that `sqlbuild_orchestration::tls` installs before any client is built.
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
# The loading engine behind loads, loaders and sinks: its library, without its CLI or its own DuckDB.
sqlload = { version = "=0.91.1", default-features = false }
sqlload-python-host = { version = "=0.91.1", default-features = false }
sqlload = { version = "=0.92.0", default-features = false }
sqlload-python-host = { version = "=0.92.0", default-features = false }
arrow-array = { version = "60", default-features = false }
arrow-schema = { version = "60", default-features = false }
serde = { version = "1", features = ["derive"] }
Expand Down Expand Up @@ -299,6 +299,7 @@ tokio-stream = { version = "0.1", default-features = false }
tokio-util = { version = "0.7", default-features = false }
toml_writer = { version = "1.1", default-features = false }
windows-sys-v052 = { package = "windows-sys", version = "0.52", default-features = false }
windows-sys-v059 = { package = "windows-sys", version = "0.59", default-features = false }
windows-sys-v060 = { package = "windows-sys", version = "0.60", default-features = false }
zerocopy = { version = "0.8", default-features = false }
zeroize = { version = "1.9", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/sqlbuild-build-features/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ nix = { workspace = true, features = ["default", "fs", "mman", "resource"] }
tokio = { workspace = true, features = ["windows-sys"] }
windows-sys = { workspace = true, features = ["Wdk", "Wdk_Foundation", "Wdk_Storage", "Wdk_Storage_FileSystem", "Wdk_System", "Wdk_System_IO", "Win32", "Win32_Foundation", "Win32_Networking", "Win32_Networking_WinSock", "Win32_Security", "Win32_Security_Authentication", "Win32_Security_Authentication_Identity", "Win32_Security_Authorization", "Win32_Security_Credentials", "Win32_Security_Cryptography", "Win32_Storage", "Win32_Storage_FileSystem", "Win32_System", "Win32_System_Com", "Win32_System_Console", "Win32_System_Diagnostics", "Win32_System_Diagnostics_Debug", "Win32_System_Diagnostics_ToolHelp", "Win32_System_IO", "Win32_System_Ioctl", "Win32_System_JobObjects", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Pipes", "Win32_System_ProcessStatus", "Win32_System_SystemInformation", "Win32_System_SystemServices", "Win32_System_Threading", "Win32_System_WindowsProgramming", "Win32_UI", "Win32_UI_Shell"] }
windows-sys-v052 = { workspace = true, features = ["Win32_System", "Win32_System_Diagnostics", "Win32_System_Diagnostics_Debug"] }
windows-sys-v059 = { workspace = true, features = ["Win32_System", "Win32_System_Diagnostics", "Win32_System_Diagnostics_Debug"] }
windows-sys-v060 = { workspace = true, features = ["Win32_Security_Authorization", "Win32_Storage", "Win32_Storage_FileSystem"] }

[target.'cfg(any(target_os = "macos", windows))'.dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
use std::sync::Arc;

use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult, pyfunction};
use pyo3::prelude::{Bound, Py, PyAny, PyAnyMethods, PyResult, pyfunction};
use pyo3::types::PyDict;
use sqlload::committed_rows::types::CommittedRows;
use sqlload::loader::models::{PlanOptions, TypedCursors};
use sqlload::python_sources::models::PythonSource;
use sqlload::python_sources::types::PythonHost;

use crate::bindings::_helpers::run_binding::{run_report, session_connections};
use crate::bindings::_helpers::run_binding::{committed_rows, run_report, session_connections};
use crate::runs::main::run_project::run_project;
use crate::runs::models::LoaderHost;

/// Load `loader` (an `EngineLoaderRun`: its project text, Python source, reload and cursor window, typed by family or in the cursor's own type) through `sessions`; the report `run_sqlload_project` gives.
/// Load `loader` (an `EngineLoaderRun`: its project text, Python source, reload and cursor window, typed by family or in the cursor's own type) through `sessions`; the report `run_sqlload_project` gives, calling `on_rows` with the rows committed so far after each commit.
#[pyfunction]
#[pyo3(signature = (*, loader, sessions))]
#[pyo3(signature = (*, loader, sessions, on_rows = None))]
pub(crate) fn run_sqlload_loader(
loader: &Bound<'_, PyAny>,
sessions: &Bound<'_, PyDict>,
on_rows: Option<Py<PyAny>>,
) -> PyResult<String> {
let text = |name: &str| -> PyResult<Option<String>> { loader.getattr(name)?.extract() };
let source: PythonSource =
Expand All @@ -39,6 +41,7 @@ pub(crate) fn run_sqlload_loader(
};
let project: String = loader.getattr("project")?.extract()?;
let connections = session_connections(sessions)?;
let committed: Option<CommittedRows> = on_rows.map(committed_rows);
let host: Arc<dyn PythonHost> = Arc::new(LoaderHost {
run: loader.clone().unbind(),
});
Expand All @@ -49,6 +52,7 @@ pub(crate) fn run_sqlload_loader(
(&project, vec![source], Some(host)),
connections,
(options, Vec::new()),
committed,
)
})
.map_err(PyRuntimeError::new_err)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use crate::runs::models::PlannedProject;

/// Plan `project` (SQLLoad TOML), its targets writing through `sessions`; `{lines, ready, tables, missing}` as JSON.
#[pyfunction]
#[pyo3(signature = (*, project, sessions, tables, reload = false))]
#[pyo3(signature = (*, project, sessions, plan))]
pub(crate) fn plan_sqlload_project(
project: &str,
sessions: &Bound<'_, PyDict>,
tables: (Vec<String>, Vec<String>),
reload: bool,
plan: (Vec<String>, Vec<String>, bool),
) -> PyResult<String> {
let planned: PlannedProject = detached_load(
(project, sessions),
(tables, reload),
plan,
|project, (connections, options, require)| {
plan_project(&project, connections, (options, require))
},
Expand Down
31 changes: 22 additions & 9 deletions crates/sqlbuild-python-load/src/bindings/_helpers/run_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,37 @@
use std::collections::BTreeMap;

use pyo3::exceptions::{PyKeyError, PyRuntimeError};
use pyo3::prelude::{Bound, Py, PyAny, PyResult, pyfunction};
use pyo3::prelude::{Bound, Py, PyAny, PyResult, Python, pyfunction};
use pyo3::types::{PyAnyMethods, PyDict, PyDictMethods};
use serde_json::{Value, json};
use sqlload::adapters::session::models::SessionConnection;
use sqlload::committed_rows::types::CommittedRows;
use sqlload::loader::models::PlanOptions;

use crate::bindings::constants::{ADAPTER, DATABASE, DESCRIPTION, OPENER};
use crate::runs::main::run_project::run_project;
use crate::runs::models::ProjectRun;
use crate::sessions::main::session_connection::session_connection;

/// Load `project` (SQLLoad TOML) through `sessions`; `{tables, planned, missing}` JSON, nothing loaded when `require` misses.
/// Load `project` (SQLLoad TOML) through `sessions`, calling `on_rows` with the rows committed so far after each commit; `{tables, planned, missing}` JSON, nothing loaded when `plan`'s required tables miss.
#[pyfunction]
#[pyo3(signature = (*, project, sessions, tables, reload = false))]
#[pyo3(signature = (*, project, sessions, plan, on_rows = None))]
pub(crate) fn run_sqlload_project(
project: &str,
sessions: &Bound<'_, PyDict>,
tables: (Vec<String>, Vec<String>),
reload: bool,
plan: (Vec<String>, Vec<String>, bool),
on_rows: Option<Py<PyAny>>,
) -> PyResult<String> {
let committed: Option<CommittedRows> = on_rows.map(committed_rows);
let run: ProjectRun = detached_load(
(project, sessions),
(tables, reload),
plan,
|project, (connections, options, require)| {
run_project(
(&project, Vec::new(), None),
connections,
(options, require),
committed,
)
},
)?;
Expand All @@ -56,20 +59,30 @@ pub(crate) fn run_report(run: ProjectRun) -> String {
json!({"tables": rows, "planned": run.planned, "missing": run.missing}).to_string()
}

/// `on_rows` as SQLLoad's committed-rows callback, called from the committing table's thread with the interpreter attached only for the call; an error it raises is reported as unraisable and the load goes on.
pub(crate) fn committed_rows(on_rows: Py<PyAny>) -> CommittedRows {
std::sync::Arc::new(move |total: u64| {
Python::attach(|py| {
if let Err(error) = on_rows.call1(py, (total,)) {
error.write_unraisable(py, None);
}
});
})
}

/// SQLLoad's connections, plan options and required tables for one plan or run from Python.
pub(crate) type LoadRequest = (
BTreeMap<String, SessionConnection>,
PlanOptions,
Vec<String>,
);

/// `work` on `project` and its load request (`sessions`, `tables`, `reload`), with the GIL released.
/// `work` on `project` and its load request (`sessions` and the `(select, require, reload)` plan), with the GIL released.
pub(crate) fn detached_load<T: Send>(
(project, sessions): (&str, &Bound<'_, PyDict>),
(tables, reload): ((Vec<String>, Vec<String>), bool),
(select, require, reload): (Vec<String>, Vec<String>, bool),
work: impl FnOnce(String, LoadRequest) -> Result<T, String> + Send,
) -> PyResult<T> {
let (select, require): (Vec<String>, Vec<String>) = tables;
let options = PlanOptions {
select,
reload,
Expand Down
6 changes: 4 additions & 2 deletions crates/sqlbuild-python-load/src/runs/main/run_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::collections::BTreeMap;

use sqlload::adapters::session::models::SessionConnection;
use sqlload::committed_rows::types::CommittedRows;
use sqlload::loader::main::apply_project::apply_project;
use sqlload::loader::main::connect_targets::connect_targets;
use sqlload::loader::main::load_tables::load_tables;
Expand All @@ -20,11 +21,12 @@ use crate::runs::_helpers::tables::{
};
use crate::runs::models::{LoadedTable, ProjectRun};

/// Each selected table's load of the project `text` with the `python` sources `host` runs; nothing loads when the plan lacks a `required` table.
/// Each selected table's load of the project `text` with the `python` sources `host` runs, `committed` hearing the rows it has committed after each commit; nothing loads when the plan lacks a `required` table.
pub fn run_project(
(text, python, host): (&str, Vec<PythonSource>, Option<Arc<dyn PythonHost>>),
sessions: BTreeMap<String, SessionConnection>,
(options, required): (PlanOptions, Vec<String>),
committed: Option<CommittedRows>,
) -> Result<ProjectRun, String> {
let warehouse = sessions.values().next().map(|session| session.warehouse);
let mut project = parse_python_project(text, &|_| None, (&python, sessions))
Expand Down Expand Up @@ -59,7 +61,7 @@ pub fn run_project(
options: &options,
};
let mut tables: Vec<LoadedTable> = Vec::new();
for outcome in load_tables(&run, &plan, None).await {
for outcome in load_tables(&run, &plan, None, committed.clone()).await {
let (loaded, changes, warnings) = match outcome.loaded {
Ok(loaded) => (Ok(loaded.rows), loaded.changes, loaded.warnings),
Err(error) => (
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies = [
"PyYAML>=6.0.2",
"sqlbuild-cli==0.309.0", # x-release-please-version
# Release Please rewrites the pin above; this line keeps its bumps from conflicting with edits below.
"sqlload-api==0.91.1",
"sqlload-api==0.92.0",
"sqlbabel==0.41.0",
]

Expand Down
14 changes: 9 additions & 5 deletions src/sqlbuild/_native.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4124,8 +4124,8 @@ def run_sqlload_project(
*,
project: str,
sessions: dict[str, dict[str, object]],
tables: tuple[list[str], list[str]],
reload: bool = False,
plan: tuple[list[str], list[str], bool],
on_rows: Callable[[int], None] | None = None,
) -> str: ...
def loader_projects(
*,
Expand All @@ -4134,13 +4134,17 @@ def loader_projects(
target: str | None = None,
schema: str | None = None,
) -> dict[str, tuple[str, str]]: ...
def run_sqlload_loader(*, loader: object, sessions: dict[str, dict[str, object]]) -> str: ...
def run_sqlload_loader(
*,
loader: object,
sessions: dict[str, dict[str, object]],
on_rows: Callable[[int], None] | None = None,
) -> str: ...
def plan_sqlload_project(
*,
project: str,
sessions: dict[str, dict[str, object]],
tables: tuple[list[str], list[str]],
reload: bool = False,
plan: tuple[list[str], list[str], bool],
) -> str: ...
def resolve_adapter(*, adapter_name: str, project_dir: Path | None = None) -> BaseAdapter: ...

Expand Down
Loading
Loading