NoahKusaba commented on code in PR #2784:
URL:
https://github.com/apache/datafusion-ballista/pull/2784#discussion_r4647670026
##########
integrations/iceberg/src/catalog.rs:
##########
@@ -145,8 +163,132 @@ impl SchemaProvider for PinningSchema {
}
}
+/// A catalog-backed table: its catalog, by address, and its identifier.
+type TableKey = (usize, TableIdent);
+
+fn key(provider: &IcebergCatalogTableProvider) -> TableKey {
+ let catalog = Arc::as_ptr(provider.catalog()).cast::<()>() as usize;
+ (catalog, provider.table_ident().clone())
+}
+
+/// A loaded table, or the error loading it returned, shared by every scan of
it.
+type Loaded = std::result::Result<Table, Arc<DataFusionError>>;
+
+/// The version of `provider`'s table its catalog serves now, or why it
couldn't
+/// be loaded.
+async fn load(provider: &IcebergCatalogTableProvider) -> Loaded {
+ provider
+ .catalog()
+ .load_table(provider.table_ident())
+ .await
+ .map_err(|error| Arc::new(to_datafusion_error(error)))
+}
+
+/// The catalog-backed table a scan's pinned table was pinned from, if any.
+fn origin(scan: &TableScan) -> Option<&IcebergCatalogTableProvider> {
+ let source = scan.source.downcast_ref::<DefaultTableSource>()?;
+ let pinned = source.table_provider.downcast_ref::<PinnedTable>()?;
+ pinned.origin.as_ref()
+}
+
+/// Returns a view's `plan` with each catalog-backed Iceberg table it reads
+/// pinned to the version its catalog serves now, loading each table once, or
+/// `None` if it reads none. A table that can't be loaded is pinned to the
error,
+/// so the plan still knows which table to load next time. DataFusion inlines a
+/// view when it plans a scan of it, so the plan of a view over other views
+/// already holds their tables, and needs no recursion.
+async fn repin(plan: &LogicalPlan) -> Result<Option<LogicalPlan>> {
+ let mut providers = HashMap::new();
+ plan.apply_with_subqueries(|node| {
+ if let LogicalPlan::TableScan(scan) = node
+ && let Some(origin) = origin(scan)
+ {
+ providers
+ .entry(key(origin))
+ .or_insert_with(|| origin.clone());
+ }
+ Ok(TreeNodeRecursion::Continue)
+ })?;
+ if providers.is_empty() {
+ return Ok(None);
+ }
+ let loads = providers
+ .into_iter()
+ .map(|(key, provider)| async move { (key, load(&provider).await) });
+ let tables: HashMap<_, _> = join_all(loads).await.into_iter().collect();
+ let plan = plan
+ .clone()
+ .transform_up_with_subqueries(|node| {
+ let LogicalPlan::TableScan(mut scan) = node else {
+ return Ok(Transformed::no(node));
+ };
+ let Some(origin) = origin(&scan) else {
+ return Ok(Transformed::no(LogicalPlan::TableScan(scan)));
+ };
+ let table = tables[&key(origin)].clone();
Review Comment:
I think you're right :thinking: . Added it in.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]