Skip to content

feat(datafusion): Implement insert_into for IcebergTableProvider #1600

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
wants to merge 6 commits into
base: main
Choose a base branch
from
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
69 changes: 66 additions & 3 deletions crates/integrations/datafusion/src/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ use std::sync::Arc;
use async_trait::async_trait;
use datafusion::arrow::datatypes::SchemaRef as ArrowSchemaRef;
use datafusion::catalog::Session;
use datafusion::common::DataFusionError;
use datafusion::datasource::{TableProvider, TableType};
use datafusion::error::Result as DFResult;
use datafusion::logical_expr::dml::InsertOp;
use datafusion::logical_expr::{Expr, TableProviderFilterPushDown};
use datafusion::physical_plan::ExecutionPlan;
use datafusion::physical_plan::coalesce_partitions::CoalescePartitionsExec;
use iceberg::arrow::schema_to_arrow_schema;
use iceberg::inspect::MetadataTableType;
use iceberg::table::Table;
use iceberg::{Catalog, Error, ErrorKind, NamespaceIdent, Result, TableIdent};
use metadata_table::IcebergMetadataTableProvider;

use crate::physical_plan::commit::IcebergCommitExec;
use crate::physical_plan::scan::IcebergTableScan;
use crate::physical_plan::write::IcebergWriteExec;
use crate::to_datafusion_error;

/// Represents a [`TableProvider`] for the Iceberg [`Catalog`],
/// managing access to a [`Table`].
Expand All @@ -46,6 +52,8 @@ pub struct IcebergTableProvider {
snapshot_id: Option<i64>,
/// A reference-counted arrow `Schema`.
schema: ArrowSchemaRef,
/// The catalog that the table belongs to.
catalog: Option<Arc<dyn Catalog>>,
}

impl IcebergTableProvider {
Expand All @@ -54,6 +62,7 @@ impl IcebergTableProvider {
table,
snapshot_id: None,
schema,
catalog: None,
}
}
/// Asynchronously tries to construct a new [`IcebergTableProvider`]
Expand All @@ -73,6 +82,7 @@ impl IcebergTableProvider {
table,
snapshot_id: None,
schema,
catalog: Some(client),
})
}

Expand All @@ -84,6 +94,7 @@ impl IcebergTableProvider {
table,
snapshot_id: None,
schema,
catalog: None,
})
}

Expand All @@ -108,6 +119,7 @@ impl IcebergTableProvider {
table,
snapshot_id: Some(snapshot_id),
schema,
catalog: None,
})
}

Expand Down Expand Up @@ -140,8 +152,18 @@ impl TableProvider for IcebergTableProvider {
filters: &[Expr],
_limit: Option<usize>,
) -> DFResult<Arc<dyn ExecutionPlan>> {
// Refresh table if catalog is available
let table = if let Some(catalog) = &self.catalog {
catalog
.load_table(self.table.identifier())
.await
.map_err(to_datafusion_error)?
} else {
self.table.clone()
};

Copy link
Contributor Author

@CTTY CTTY Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder if storing table directly in the IcebergTableProvider is correct... We could get a stale table if the provider doesn't have a catalog table.

Iceberg-java has a refresh() interface which uses TableOperation to refresh metadata. In iceberg-rs we don't have TableOperation and need to rely on catalog to refresh

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a PR to add that functionality (including the refresh): #1297

Ok(Arc::new(IcebergTableScan::new(
self.table.clone(),
table,
self.snapshot_id,
self.schema.clone(),
projection,
Expand All @@ -152,11 +174,52 @@ impl TableProvider for IcebergTableProvider {
fn supports_filters_pushdown(
&self,
filters: &[&Expr],
) -> std::result::Result<Vec<TableProviderFilterPushDown>, datafusion::error::DataFusionError>
{
) -> DFResult<Vec<TableProviderFilterPushDown>> {
// Push down all filters, as a single source of truth, the scanner will drop the filters which couldn't be push down
Ok(vec![TableProviderFilterPushDown::Inexact; filters.len()])
}

async fn insert_into(
&self,
_state: &dyn Session,
input: Arc<dyn ExecutionPlan>,
_insert_op: InsertOp,
) -> DFResult<Arc<dyn ExecutionPlan>> {
if !self
.table
Copy link
Contributor Author

@CTTY CTTY Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should refresh the table here and every otherself.table usages in IcebergTableProvider, but I think we should fix that in a separate PR if needed

.metadata()
.default_partition_spec()
.is_unpartitioned()
{
// TODO add insert into support for partitioned tables
return Err(DataFusionError::NotImplemented(
"IcebergTableProvider::insert_into does not support partitioned tables yet"
.to_string(),
));
}

let Some(catalog) = self.catalog.clone() else {
return Err(DataFusionError::Execution(
"Catalog cannot be none for insert_into".to_string(),
));
};

let write_plan = Arc::new(IcebergWriteExec::new(
self.table.clone(),
input,
self.schema.clone(),
));

// Merge the outputs of write_plan into one so we can commit all files together
let coalesce_partitions = Arc::new(CoalescePartitionsExec::new(write_plan));

Ok(Arc::new(IcebergCommitExec::new(
self.table.clone(),
catalog,
coalesce_partitions,
self.schema.clone(),
)))
}
}

#[cfg(test)]
Expand Down
Loading
Loading