cloud
polars_pylance.cloud
¶
Helpers for running Lance scans and writes on Polars Cloud.
Install the client with the cloud extra (pip install polars-pylance[cloud]),
which brings polars-cloud>=0.14, the release that runs on polars 2.2. See "The
polars pin" below for why the extra exists rather than a hard dependency.
What works and what does not, as of polars-cloud 0.14:
Reading¶
A scan_lance plan serializes to ~2-6 kB and can be shipped with
LazyFrame.remote(), but the remote workers must be able to import lance and to
reach the dataset's storage. Install the dependency with
ComputeContext(requirements=...); see
requirements_txt.
The scan survives prepare_cloud_plan, on its own and under pl.concat of
scan_lance_fragments shards. It is a
LazyFrameResolver, so the workers resolve it and read the version that is
current when the query runs there.
Writing¶
Possible remotely since 0.10, via
sink_lance_remote. Polars Cloud's native
sink destinations are still Parquet, CSV, IPC and Iceberg, but sink_batches hands
each result batch to a Python callable that is cloudpickled into the query plan and
therefore runs on the workers, so the workers write Lance data files directly, and
a single client-side commit publishes them. polars_pylance._remote documents the
arrangement.
The polars pin¶
polars-cloud pins the polars runtime with == (0.14 installs
polars-runtime==2.2.0 through polars-nanook, which 0.14 is a shim over), and
this package requires polars>=2.2, the first release with the resolver and
external-reader interface scan_lance is built on in working order. The
cloud extra is an extra rather than a hard dependency so a plain
pip install polars-pylance stays usable without a Cloud workspace.
StagedLanceSink
dataclass
¶
A remote Lance write in progress: a callback to ship, and a commit to run.
Returned by stage_lance_sink. Hand
callback to sink_batches, wait for the query, then call
commit.
Source code in src/polars_pylance/_remote.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
staged_fragments
¶
Every fragment the workers staged, in a deterministic order.
Ordered by staging key so that two commits of the same staged output produce the same fragment layout.
Source code in src/polars_pylance/_remote.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
commit
¶
commit(*, cleanup: bool = True) -> LanceDataset
Make every staged fragment one dataset version, in a single commit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cleanup
|
bool
|
Remove the staging prefix once the commit lands. The data files a
retried batch orphaned are not touched: they live inside the dataset and
are unreferenced by any manifest, so reclaim them with
|
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If nothing was staged. An empty commit would replace the dataset with nothing, which is never what a failed remote query meant. |
Source code in src/polars_pylance/_remote.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
cleanup
¶
cleanup() -> None
Delete the staging prefix. Safe to call when it was never created.
Source code in src/polars_pylance/_remote.py
308 309 310 311 312 | |
sink_lance_remote
¶
sink_lance_remote(remote: LazyFrameRemote | ExecuteRemote, target: str | Path | LanceDataset, *, schema: Schema | Schema | None = None, mode: RemoteWriteMode = 'create', chunk_size: int | None = None, maintain_order: bool = False, storage_options: dict[str, str] | None = None, staging_uri: str | None = None, staging_filesystem: FileSystem | None = None, staging_storage_options: dict[str, str] | None = None, fragment_key: Callable[[DataFrame], str] | None = None, cleanup: bool = True, **lance_write_kwargs: Any) -> LanceDataset
Run a Polars Cloud query and write its output to Lance from the workers.
The staging parameters are documented on
stage_lance_sink, which receives them
unchanged.
Submits remote with a worker-side fragment writer, waits for it, and
commits every staged fragment as one dataset version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote
|
LazyFrameRemote | ExecuteRemote
|
A |
required |
target
|
str | Path | LanceDataset
|
Destination URI, path, or an existing |
required |
schema
|
Schema | Schema | None
|
The query's output schema. Resolved from the LazyFrame behind |
None
|
chunk_size
|
int | None
|
Rows buffered before the callback runs; the remote counterpart of
|
None
|
maintain_order
|
bool
|
Call the writer serially rather than in parallel across workers. Costs the parallelism that makes this a distributed write; the fragment order at commit is deterministic either way. |
False
|
cleanup
|
bool
|
Remove the staging prefix after a successful commit. On failure it is always left in place, so a re-run can be diagnosed or the fragments committed by hand. |
True
|
Returns:
| Type | Description |
|---|---|
LanceDataset
|
lance.LanceDataset: The committed dataset. |
Examples:
>>> ctx = pc.ComputeContext(
... cpus=8, memory=32, requirements=requirements_txt().encode()
... )
>>> lf = scan_lance("s3://bucket/in.lance").filter(
... pl.col("score") > 0.9
... )
>>> sink_lance_remote(
... lf.remote(ctx).distributed(), "s3://bucket/out.lance", mode="overwrite"
... )
Source code in src/polars_pylance/_remote.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
stage_lance_sink
¶
stage_lance_sink(target: str | Path | LanceDataset, schema: Schema | Schema | LazyFrame, *, mode: RemoteWriteMode = 'create', storage_options: dict[str, str] | None = None, staging_uri: str | None = None, staging_filesystem: FileSystem | None = None, staging_storage_options: dict[str, str] | None = None, fragment_key: Callable[[DataFrame], str] | None = None, **lance_write_kwargs: Any) -> StagedLanceSink
Build a worker-side Lance writer for sink_batches.
Use this when you want to drive the remote query yourself: to pick a planner, set
maintain_order, or inspect the query handle.
sink_lance_remote is the same thing with
the query submitted and awaited for you.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str | Path | LanceDataset
|
Destination URI, path, or an existing |
required |
schema
|
Schema | Schema | LazyFrame
|
The query's output schema. A LazyFrame is accepted and resolved with
|
required |
mode
|
RemoteWriteMode
|
|
'create'
|
storage_options
|
dict[str, str] | None
|
Passed to Lance for the data files, and (for |
None
|
staging_uri
|
str | None
|
Where fragment metadata is staged. Defaults to the dataset URI plus
|
None
|
staging_filesystem
|
FileSystem | None
|
An explicit |
None
|
staging_storage_options
|
dict[str, str] | None
|
Staging credentials, when they differ from
|
None
|
fragment_key
|
Callable[[DataFrame], str] | None
|
|
None
|
**lance_write_kwargs
|
Any
|
Passed to |
{}
|
Examples:
>>> staged = stage_lance_sink(
... "s3://bucket/out.lance", lf, mode="overwrite"
... )
>>> query = (
... lf.remote(ctx).distributed().sink_batches(staged.callback)
... )
>>> query.await_result()
>>> staged.commit()
Source code in src/polars_pylance/_remote.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
requirements_txt
¶
Render a requirements file pinning the versions a cloud worker needs.
Polars Cloud rejects a compute context whose polars version differs from the
client's, so both pins are exact. polars-pylance itself is on the list because a
sink_lance_remote callback is pickled by
reference: the worker imports it rather than receiving its code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extra
|
list[str] | None
|
Further requirement lines to append, for whatever else the query needs on the worker. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The file contents, newline-terminated. |
Examples:
>>> import polars_cloud as pc
>>> ctx = pc.ComputeContext(
... cpus=8, memory=32, requirements=requirements_txt().encode()
... )
Source code in src/polars_pylance/cloud.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |