Skip to content
Draft
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ The workspace uses a pinned `iceberg-rust` Git revision. Applications that
also depend on Iceberg crates should use the same revision shown in
[`Cargo.toml`](Cargo.toml) so Cargo uses one Iceberg crate source.

## Scan runtime configuration

Use `IcebergTableProviderOptions { runtime: Some(runtime) }` to override the
Iceberg runtime used by scans. Install these options with:

- `IcebergTableProvider::with_options` for a directly registered table;
- `with_table_options` on the live or static catalog builder;
- `IcebergTableProviderFactory::with_table_options` for SQL external tables.

Eager discovery is polled on the configured I/O runtime, and readers receive its
I/O and CPU handles. Dropping scan planning cancels its spawned discovery future.
Catalog/metadata lookup and consumption of lazy discovery streams stay on the
caller. This setting does not move all query execution onto the Iceberg runtime,
and does not change write behavior. Keep the underlying Tokio runtimes alive
while using the provider. Without an override, existing runtime behavior is
unchanged.

## Development

The repository's [`rust-toolchain.toml`](rust-toolchain.toml) selects the Rust
Expand Down
2 changes: 2 additions & 0 deletions crates/datafusion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ name = "lazy_scan"
harness = false

[dev-dependencies]
serde = { version = "1", features = ["derive"] }
typetag = "0.2"
criterion = "0.8"
iceberg-datafusion-test-tools = { workspace = true }
insta = "1"
Expand Down
45 changes: 36 additions & 9 deletions crates/datafusion/src/catalog/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use std::sync::Arc;

use datafusion::catalog::{CatalogProvider, SchemaProvider};
use datafusion::common::config_err;
use datafusion::error::Result;
use datafusion::error::{DataFusionError, Result};
use futures::future::try_join_all;
use iceberg::table::Table;
use iceberg::{Catalog, TableIdent};

use super::schema::IcebergSchemaProvider;
use crate::to_datafusion_error;
use crate::{IcebergTableProviderOptions, to_datafusion_error};

/// Exposes Iceberg namespaces as DataFusion schemas.
#[derive(Debug)]
Expand All @@ -41,6 +41,7 @@ impl IcebergCatalogProvider {
IcebergCatalogProviderBuilder {
catalog,
fixed_tables: None,
table_options: IcebergTableProviderOptions::default(),
}
}

Expand Down Expand Up @@ -83,9 +84,16 @@ impl CatalogProvider for IcebergCatalogProvider {
pub struct IcebergCatalogProviderBuilder {
catalog: Arc<dyn Catalog>,
fixed_tables: Option<Vec<TableIdent>>,
table_options: IcebergTableProviderOptions,
}

impl IcebergCatalogProviderBuilder {
/// Applies scan settings to tables resolved through this catalog.
pub fn with_table_options(mut self, options: IcebergTableProviderOptions) -> Self {
self.table_options = options;
self
}

/// Exposes only these live tables, without listing the catalog. Tables can
/// receive appends, but SQL cannot change the configured membership.
pub fn with_fixed_tables(
Expand All @@ -104,7 +112,10 @@ impl IcebergCatalogProviderBuilder {
for id in tables {
schemas
.entry(id.namespace)
.or_insert_with_key(|ns| IcebergSchemaProvider::fixed(ns.clone()))
.or_insert_with_key(|ns| {
IcebergSchemaProvider::fixed(ns.clone())
.with_table_options(self.table_options.clone())
})
.add_live(id.name, catalog.clone());
}
return IcebergCatalogProvider::from_schemas(schemas.into_values());
Expand All @@ -113,11 +124,17 @@ impl IcebergCatalogProviderBuilder {
.list_namespaces(None)
.await
.map_err(to_datafusion_error)?;
let schemas = try_join_all(
namespaces
.into_iter()
.map(|ns| IcebergSchemaProvider::try_new(catalog.clone(), ns)),
)
let schemas = try_join_all(namespaces.into_iter().map(|ns| {
let catalog = catalog.clone();
let options = self.table_options.clone();
async move {
Ok::<_, DataFusionError>(
IcebergSchemaProvider::try_new(catalog, ns)
.await?
.with_table_options(options),
)
}
}))
.await?;
IcebergCatalogProvider::from_schemas(schemas)
}
Expand All @@ -127,9 +144,16 @@ impl IcebergCatalogProviderBuilder {
#[derive(Debug, Default)]
pub struct IcebergStaticCatalogBuilder {
tables: Vec<(Table, Option<i64>)>,
table_options: IcebergTableProviderOptions,
}

impl IcebergStaticCatalogBuilder {
/// Applies scan settings to every detached table in this catalog.
pub fn with_table_options(mut self, options: IcebergTableProviderOptions) -> Self {
self.table_options = options;
self
}

/// Adds a table under its Iceberg identifier using the supplied metadata.
pub fn add_static_table(mut self, table: Table) -> Self {
self.tables.push((table, None));
Expand All @@ -148,7 +172,10 @@ impl IcebergStaticCatalogBuilder {
for (table, snapshot) in self.tables {
schemas
.entry(table.identifier().namespace().clone())
.or_insert_with_key(|ns| IcebergSchemaProvider::fixed(ns.clone()))
.or_insert_with_key(|ns| {
IcebergSchemaProvider::fixed(ns.clone())
.with_table_options(self.table_options.clone())
})
.add_static(table, snapshot)?;
}
IcebergCatalogProvider::from_schemas(schemas.into_values())
Expand Down
33 changes: 26 additions & 7 deletions crates/datafusion/src/catalog/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use iceberg::table::Table;
use iceberg::{Catalog, NamespaceIdent, TableCreation, TableIdent};

use crate::metadata::IcebergMetadataTableProvider;
use crate::table_provider::IcebergTableProvider;
use crate::table_provider::{IcebergTableProvider, IcebergTableProviderOptions};
use crate::to_datafusion_error;

/// An Iceberg namespace. Each live lookup loads current metadata from its catalog.
Expand All @@ -45,6 +45,7 @@ pub(crate) struct IcebergSchemaProvider {
pub(super) namespace: NamespaceIdent,
ddl_catalog: Option<Arc<dyn Catalog>>,
tables: Arc<DashMap<String, Registration>>,
table_options: IcebergTableProviderOptions,
}

#[derive(Clone, Debug)]
Expand All @@ -60,9 +61,18 @@ impl IcebergSchemaProvider {
namespace,
ddl_catalog: None,
tables: Arc::default(),
table_options: IcebergTableProviderOptions::default(),
}
}

pub(super) fn with_table_options(
mut self,
options: IcebergTableProviderOptions,
) -> Self {
self.table_options = options;
self
}

pub(super) async fn try_new(
catalog: Arc<dyn Catalog>,
namespace: NamespaceIdent,
Expand All @@ -87,7 +97,8 @@ impl IcebergSchemaProvider {

pub(super) fn add_static(&self, table: Table, snapshot: Option<i64>) -> Result<()> {
let name = table.identifier().name.clone();
let provider = IcebergTableProvider::from_table(table, snapshot)?;
let provider = IcebergTableProvider::from_table(table, snapshot)?
.with_options(self.table_options.clone());
if self
.tables
.insert(name.clone(), Registration::Static(Arc::new(provider)))
Expand Down Expand Up @@ -154,19 +165,27 @@ impl SchemaProvider for IcebergSchemaProvider {
let provider: Arc<dyn TableProvider> = match entry {
Registration::Live(catalog) => {
let id = TableIdent::new(self.namespace.clone(), table_name.into());
let table = catalog.load_table(&id).await.map_err(to_datafusion_error)?;
let mut table =
catalog.load_table(&id).await.map_err(to_datafusion_error)?;
if let Some(runtime) = &self.table_options.runtime {
table = table.with_runtime(runtime.clone());
}
match metadata_table_type {
Some(kind) => {
Arc::new(IcebergMetadataTableProvider::try_new(table, kind)?)
}
None => {
Arc::new(IcebergTableProvider::from_loaded(catalog, id, &table)?)
}
None => Arc::new(
IcebergTableProvider::from_loaded(catalog, id, &table)?
.with_options(self.table_options.clone()),
),
}
}
Registration::Static(provider) => match metadata_table_type {
Some(kind) => {
let (table, _) = provider.resolve().await?;
let (mut table, _) = provider.resolve().await?;
if let Some(runtime) = &self.table_options.runtime {
table = table.with_runtime(runtime.clone());
}
Arc::new(IcebergMetadataTableProvider::try_new(table, kind)?)
}
None => provider,
Expand Down
3 changes: 2 additions & 1 deletion crates/datafusion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod metadata;
pub mod read;
pub mod table_provider;
pub use table_provider::{
IcebergTableProvider, IcebergTableProviderFactory, IcebergTableSource,
IcebergTableProvider, IcebergTableProviderFactory, IcebergTableProviderOptions,
IcebergTableSource,
};
pub mod write;
17 changes: 12 additions & 5 deletions crates/datafusion/src/table_provider/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use iceberg::TableIdent;
use iceberg::io::{FileIOBuilder, LocalFsStorageFactory, StorageFactory};
use iceberg::table::StaticTable;

use super::IcebergTableProvider;
use super::{IcebergTableProvider, IcebergTableProviderOptions};
use crate::to_datafusion_error;

/// A factory that implements DataFusion's `TableProviderFactory` to create [`IcebergTableProvider`] instances.
Expand Down Expand Up @@ -99,21 +99,27 @@ use crate::to_datafusion_error;
#[derive(Debug, Default)]
pub struct IcebergTableProviderFactory {
storage_factory: Option<Arc<dyn StorageFactory>>,
table_options: IcebergTableProviderOptions,
}

impl IcebergTableProviderFactory {
pub fn new() -> Self {
Self {
storage_factory: None,
}
Self::default()
}

/// Create a new factory with a custom storage factory for creating FileIO instances.
pub fn new_with_storage_factory(storage_factory: Arc<dyn StorageFactory>) -> Self {
Self {
storage_factory: Some(storage_factory),
..Self::default()
}
}

/// Applies process-local settings to every table created by this factory.
pub fn with_table_options(mut self, options: IcebergTableProviderOptions) -> Self {
self.table_options = options;
self
}
}

#[async_trait]
Expand Down Expand Up @@ -144,7 +150,8 @@ impl TableProviderFactory for IcebergTableProviderFactory {
.await?
.into_table();

let provider = IcebergTableProvider::from_table(table, None)?;
let provider = IcebergTableProvider::from_table(table, None)?
.with_options(self.table_options.clone());

Ok(Arc::new(provider))
}
Expand Down
2 changes: 2 additions & 0 deletions crates/datafusion/src/table_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
//! DataFusion table access for live and detached Iceberg tables.

mod factory;
mod options;
mod provider;

pub use factory::IcebergTableProviderFactory;
pub use options::IcebergTableProviderOptions;
pub use provider::{IcebergTableProvider, IcebergTableSource};
28 changes: 28 additions & 0 deletions crates/datafusion/src/table_provider/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.

/// Process-local settings shared by table providers created directly, through a
/// catalog, or by the SQL table factory. Query reader settings remain in the
/// session's Iceberg configuration and ArrowReaderOptions extension.
#[derive(Debug, Clone, Default)]
pub struct IcebergTableProviderOptions {
/// Override the table's scan runtime. Eager discovery is polled on its I/O
/// runtime and readers receive its I/O/CPU handles. Metadata lookup and
/// consumption of lazy task streams remain on the caller. The application
/// must keep the underlying runtimes alive.
pub runtime: Option<iceberg::Runtime>,
}
Loading