-
Notifications
You must be signed in to change notification settings - Fork 297
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
CTTY
wants to merge
6
commits into
apache:main
Choose a base branch
from
CTTY:ctty/df-insert-final
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.
+434
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b72ca68
big insertion
CTTY fbc61b6
Merge branch 'main' into ctty/df-insert-final
CTTY 063c4b8
added basic insert into test
CTTY e4d39a4
complex nested complicated convoluted compounded
CTTY 8277069
minor and simple
CTTY ad02313
i fix fmt everyday
CTTY 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 |
---|---|---|
|
@@ -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`]. | ||
|
@@ -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 { | ||
|
@@ -54,6 +62,7 @@ impl IcebergTableProvider { | |
table, | ||
snapshot_id: None, | ||
schema, | ||
catalog: None, | ||
} | ||
} | ||
/// Asynchronously tries to construct a new [`IcebergTableProvider`] | ||
|
@@ -73,6 +82,7 @@ impl IcebergTableProvider { | |
table, | ||
snapshot_id: None, | ||
schema, | ||
catalog: Some(client), | ||
}) | ||
} | ||
|
||
|
@@ -84,6 +94,7 @@ impl IcebergTableProvider { | |
table, | ||
snapshot_id: None, | ||
schema, | ||
catalog: None, | ||
}) | ||
} | ||
|
||
|
@@ -108,6 +119,7 @@ impl IcebergTableProvider { | |
table, | ||
snapshot_id: Some(snapshot_id), | ||
schema, | ||
catalog: None, | ||
}) | ||
} | ||
|
||
|
@@ -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() | ||
}; | ||
|
||
Ok(Arc::new(IcebergTableScan::new( | ||
self.table.clone(), | ||
table, | ||
self.snapshot_id, | ||
self.schema.clone(), | ||
projection, | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we should refresh the table here and every other |
||
.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)] | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
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 usesTableOperation
to refresh metadata. In iceberg-rs we don't haveTableOperation
and need to rely on catalog to refreshThere 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.
I have a PR to add that functionality (including the refresh): #1297