Repository navigation
Rust SDK uploads and ingests local files #2195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yorkhellen
wants to merge
1
commit into
ClickHouse:main
Choose a base branch
from
yorkhellen:feat/rust-file-ingestion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,785
−21
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| name: Rust file path validation | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main, "release/**"] | ||
| paths: | ||
| - "sdk/rust/**" | ||
| - ".github/workflows/rust-file-paths.yml" | ||
| push: | ||
| branches: [main, "release/**"] | ||
| paths: | ||
| - "sdk/rust/**" | ||
| - ".github/workflows/rust-file-paths.yml" | ||
| workflow_dispatch: | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.ref || github.sha }} | ||
| cancel-in-progress: ${{ github.event_name == 'pull_request' }} | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| native-paths: | ||
| name: Native paths (${{ matrix.os }}, Rust ${{ matrix.rust }}, CPython 3.14) | ||
| runs-on: ${{ matrix.os }} | ||
| timeout-minutes: 10 | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-24.04, windows-2025] | ||
| rust: ["1.85.1", stable] | ||
| env: | ||
| RUSTUP_TOOLCHAIN: ${{ matrix.rust }} | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 | ||
| with: | ||
| python-version: "3.14" | ||
| - name: Install the Rust toolchain | ||
| shell: bash | ||
| run: rustup toolchain install "$RUSTUP_TOOLCHAIN" --profile minimal --no-self-update | ||
| - name: Compare native paths with CPython and run all resolver tests | ||
| # The harness enables SeCreateSymbolicLinkPrivilege on Windows. Missing | ||
| # privileges, junction support or long-path support fail this job. | ||
| shell: bash | ||
| run: python sdk/rust/tests/native_paths/check.py --toolchain "$RUSTUP_TOOLCHAIN" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| use clickhouse_loom::{Client, FileIngestOptions, IngestWaitOptions}; | ||
| use std::{ | ||
| env, | ||
| error::Error, | ||
| fs::OpenOptions, | ||
| io::{self, Write}, | ||
| path::Path, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| fn save_id(path: &Path, id: &str) -> io::Result<()> { | ||
| let mut saved = OpenOptions::new().write(true).create_new(true).open(path)?; | ||
| writeln!(saved, "{id}") | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn Error>> { | ||
| let path = env::args().nth(1).ok_or("usage: ingest_file <path>")?; | ||
| let client = Client::builder( | ||
| env::var("LOOM_ORG")?, | ||
| env::var("LOOM_NAMESPACE")?, | ||
| "file-example", | ||
| ) | ||
| .base_url(env::var("LOOM_BASE_URL").unwrap_or_else(|_| "http://localhost:7777".into())) | ||
| .api_key(env::var("LOOM_TOKEN").unwrap_or_default()) | ||
| .build()?; | ||
| let job = client | ||
| .knowledge_start_file_ingest_with_options( | ||
| &path, | ||
| FileIngestOptions::default().tags(["example"]), | ||
| ) | ||
| .await? | ||
| .data; | ||
| save_id(Path::new("ingestion-id.txt"), &job.ingestion_id).map_err(|error| { | ||
| format!( | ||
| "job {} started; cannot save its ID: {error}", | ||
| job.ingestion_id | ||
| ) | ||
| })?; | ||
| let status = client | ||
| .knowledge_wait_for_ingest_with_options( | ||
| &job.ingestion_id, | ||
| IngestWaitOptions::default().timeout(Duration::from_secs(120)), | ||
| ) | ||
| .await? | ||
| .data; | ||
| if !status.found | ||
| || !matches!(status.state.as_str(), "ready" | "partial") | ||
| || status.retrieval != "ready" | ||
| { | ||
| return Err(format!( | ||
| "ingestion {}: {} ({})", | ||
| job.ingestion_id, status.state, status.error_message | ||
| ) | ||
| .into()); | ||
| } | ||
| let bytes = client.knowledge_download(&job.source_uri).await?; | ||
| println!( | ||
| "{}: {} bytes, revision {:?}", | ||
| job.source_uri, | ||
| bytes.len(), | ||
| status.revision | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn saving_a_job_id_never_overwrites_the_input_or_an_earlier_id() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let path = dir.path().join("ingestion-id.txt"); | ||
| std::fs::write(&path, b"original file").unwrap(); | ||
| assert_eq!( | ||
| save_id(&path, "job").unwrap_err().kind(), | ||
| io::ErrorKind::AlreadyExists | ||
| ); | ||
| assert_eq!(std::fs::read(&path).unwrap(), b"original file"); | ||
| let new_path = dir.path().join("new-id.txt"); | ||
| save_id(&new_path, "job").unwrap(); | ||
| assert_eq!(std::fs::read(new_path).unwrap(), b"job\n"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2 — With an existing
ingestion-id.txt, the example uploads the file and queues another job before failing to save the new ID. The first ID remains on disk. Reserve the destination before sending either request, or resume the saved job.