Skip to content
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

Improve API docs, README, and examples for configuring context #321

Merged
merged 5 commits into from
Apr 12, 2023
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ This produces the following chart:

![Chart](examples/chart.png)

## Configuration

It is possible to configure runtime (memory and disk settings) and configuration settings when creating a context.

```python
runtime = (
RuntimeConfig()
.with_disk_manager_os()
.with_fair_spill_pool(10000000)
)
config = (
SessionConfig()
.with_create_default_catalog_and_schema(True)
.with_default_catalog_and_schema("foo", "bar")
.with_target_partitions(8)
.with_information_schema(True)
.with_repartition_joins(False)
.with_repartition_aggregations(False)
.with_repartition_windows(False)
.with_parquet_pruning(False)
.set("datafusion.execution.parquet.pushdown_filters", "true")
)
ctx = SessionContext(config, runtime)
```

Refer to the [API documentation](https://arrow.apache.org/datafusion-python/#api-reference) for more information.

Printing the context will show the current configuration settings.

```python
print(ctx)
```

## More Examples

See [examples](examples/README.md) for more information.
Expand Down
2 changes: 1 addition & 1 deletion dev/release/rat_exclude_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Cargo.lock
.history
*rat.txt
*/.git
docs.yaml
.github/*
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# DataFusion Documentation

This folder contains the source content of the [python api](./source/api).
This is published to https://arrow.apache.org/datafusion-python/ by a GitHub action
This is published to https://arrow.apache.org/datafusion-python/ by a GitHub action
when changes are merged to the main branch.

## Dependencies
Expand Down Expand Up @@ -61,4 +61,4 @@ version of the docs, follow these steps:
- `cp -rT ./build/html/ ../../arrow-site/datafusion/` (doesn't work on mac)
- `rsync -avzr ./build/html/ ../../arrow-site/datafusion/`

5. Commit changes in `arrow-site` and send a PR.
5. Commit changes in `arrow-site` and send a PR.
27 changes: 0 additions & 27 deletions docs/source/api/config.rst

This file was deleted.

4 changes: 3 additions & 1 deletion docs/source/api/execution_context.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
.. currentmodule:: datafusion

SessionContext
================
==============

.. autosummary::
:toctree: ../generated/

SessionConfig
RuntimeConfig
SessionContext
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Here is a direct link to the file used in the examples:

- https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2021-01.parquet

### Creating a SessionContext

- [Creating a SessionContext](./create-context.py)

### Executing Queries with DataFusion

- [Query a Parquet file using SQL](./sql-parquet.py)
Expand Down
39 changes: 39 additions & 0 deletions examples/create-context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from datafusion import RuntimeConfig, SessionConfig, SessionContext

# create a session context with default settings
ctx = SessionContext()
print(ctx)

# create a session context with explicit runtime and config settings
runtime = RuntimeConfig().with_disk_manager_os().with_fair_spill_pool(10000000)
config = (
SessionConfig()
.with_create_default_catalog_and_schema(True)
.with_default_catalog_and_schema("foo", "bar")
.with_target_partitions(8)
.with_information_schema(True)
.with_repartition_joins(False)
.with_repartition_aggregations(False)
.with_repartition_windows(False)
.with_parquet_pruning(False)
.set("datafusion.execution.parquet.pushdown_filters", "true")
)
ctx = SessionContext(config, runtime)
print(ctx)
29 changes: 22 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use pyo3::types::PyTuple;
use tokio::runtime::Runtime;
use tokio::task::JoinHandle;

/// Configuration options for a SessionContext
#[pyclass(name = "SessionConfig", module = "datafusion", subclass, unsendable)]
#[derive(Clone, Default)]
pub(crate) struct PySessionConfig {
Expand Down Expand Up @@ -141,8 +142,13 @@ impl PySessionConfig {
fn with_parquet_pruning(&self, enabled: bool) -> Self {
Self::from(self.config.clone().with_parquet_pruning(enabled))
}

fn set(&self, key: &str, value: &str) -> Self {
Self::from(self.config.clone().set_str(key, value))
}
}

/// Runtime options for a SessionContext
#[pyclass(name = "RuntimeConfig", module = "datafusion", subclass, unsendable)]
#[derive(Clone)]
pub(crate) struct PyRuntimeConfig {
Expand Down Expand Up @@ -549,8 +555,8 @@ impl PySessionContext {
Ok(PyDataFrame::new(self.ctx.read_empty()?))
}

fn session_id(&self) -> PyResult<String> {
Ok(self.ctx.session_id())
fn session_id(&self) -> String {
self.ctx.session_id()
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -684,11 +690,20 @@ impl PySessionContext {
}

fn __repr__(&self) -> PyResult<String> {
let id = self.session_id();
match id {
Ok(value) => Ok(format!("SessionContext(session_id={value})")),
Err(err) => Ok(format!("Error: {:?}", err.to_string())),
}
let config = self.ctx.copied_config();
let mut config_entries = config
.options()
.entries()
.iter()
.filter(|e| e.value.is_some())
.map(|e| format!("{} = {}", e.key, e.value.as_ref().unwrap()))
.collect::<Vec<_>>();
config_entries.sort();
Ok(format!(
"SessionContext: id={}; configs=[\n\t{}]",
self.session_id(),
config_entries.join("\n\t")
))
}

/// Execute a partition of an execution plan and return a stream of record batches
Expand Down