-
Notifications
You must be signed in to change notification settings - Fork 215
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
feat: support projection pushdown for datafusion iceberg #594
Changes from 4 commits
0d412b9
e3a58be
3e7426d
a2cbd15
59fff25
78fc2fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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. | ||
|
||
use std::sync::Arc; | ||
|
||
use datafusion::prelude::SessionContext; | ||
use iceberg_datafusion::IcebergCatalogProvider; | ||
|
||
mod utils; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
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. A tool to read Iceberg data using datafusion SQL |
||
let iceberg_catalog = utils::get_rest_catalog(); | ||
|
||
let client = Arc::new(iceberg_catalog); | ||
let catalog = Arc::new(IcebergCatalogProvider::try_new(client).await.unwrap()); | ||
|
||
let ctx = SessionContext::new(); | ||
ctx.register_catalog("catalog", catalog); | ||
let df = ctx.sql("select * from catalog.ns.table1").await.unwrap(); | ||
let data = df.collect().await.unwrap(); | ||
println!("{:?}", data); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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. | ||
|
||
use std::env; | ||
|
||
use iceberg_catalog_rest::{RestCatalog, RestCatalogConfig}; | ||
|
||
fn get_catalog_uri_from_env() -> String { | ||
env::var("CATALOG_URI").unwrap_or("http://localhost:8080".to_string()) | ||
} | ||
|
||
pub fn get_rest_catalog() -> RestCatalog { | ||
let config = RestCatalogConfig::builder() | ||
.uri(get_catalog_uri_from_env()) | ||
.build(); | ||
RestCatalog::new(config) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,6 +18,7 @@ | |||||
use std::any::Any; | ||||||
use std::pin::Pin; | ||||||
use std::sync::Arc; | ||||||
use std::vec; | ||||||
|
||||||
use datafusion::arrow::array::RecordBatch; | ||||||
use datafusion::arrow::datatypes::SchemaRef as ArrowSchemaRef; | ||||||
|
@@ -44,17 +45,25 @@ pub(crate) struct IcebergTableScan { | |||||
/// Stores certain, often expensive to compute, | ||||||
/// plan properties used in query optimization. | ||||||
plan_properties: PlanProperties, | ||||||
/// Projection column names | ||||||
projection: Vec<String>, | ||||||
} | ||||||
|
||||||
impl IcebergTableScan { | ||||||
/// Creates a new [`IcebergTableScan`] object. | ||||||
pub(crate) fn new(table: Table, schema: ArrowSchemaRef) -> Self { | ||||||
pub(crate) fn new( | ||||||
table: Table, | ||||||
schema: ArrowSchemaRef, | ||||||
projection: Option<&Vec<usize>>, | ||||||
) -> Self { | ||||||
let plan_properties = Self::compute_properties(schema.clone()); | ||||||
let projection = get_column_names(schema.clone(), projection); | ||||||
|
||||||
Self { | ||||||
table, | ||||||
schema, | ||||||
plan_properties, | ||||||
projection, | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -100,7 +109,7 @@ impl ExecutionPlan for IcebergTableScan { | |||||
_partition: usize, | ||||||
_context: Arc<TaskContext>, | ||||||
) -> DFResult<SendableRecordBatchStream> { | ||||||
let fut = get_batch_stream(self.table.clone()); | ||||||
let fut = get_batch_stream(self.table.clone(), self.projection.clone()); | ||||||
let stream = futures::stream::once(fut).try_flatten(); | ||||||
|
||||||
Ok(Box::pin(RecordBatchStreamAdapter::new( | ||||||
|
@@ -116,7 +125,11 @@ impl DisplayAs for IcebergTableScan { | |||||
_t: datafusion::physical_plan::DisplayFormatType, | ||||||
f: &mut std::fmt::Formatter, | ||||||
) -> std::fmt::Result { | ||||||
write!(f, "IcebergTableScan") | ||||||
write!( | ||||||
f, | ||||||
"IcebergTableScan projection:[{}]", | ||||||
self.projection.join(" ") | ||||||
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.
Suggested change
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. done |
||||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -127,8 +140,13 @@ impl DisplayAs for IcebergTableScan { | |||||
/// and then converts it into a stream of Arrow [`RecordBatch`]es. | ||||||
async fn get_batch_stream( | ||||||
table: Table, | ||||||
column_names: Vec<String>, | ||||||
) -> DFResult<Pin<Box<dyn Stream<Item = DFResult<RecordBatch>> + Send>>> { | ||||||
let table_scan = table.scan().build().map_err(to_datafusion_error)?; | ||||||
let table_scan = table | ||||||
.scan() | ||||||
.select(column_names) | ||||||
.build() | ||||||
.map_err(to_datafusion_error)?; | ||||||
|
||||||
let stream = table_scan | ||||||
.to_arrow() | ||||||
|
@@ -138,3 +156,18 @@ async fn get_batch_stream( | |||||
|
||||||
Ok(Box::pin(stream)) | ||||||
} | ||||||
|
||||||
fn get_column_names(schema: ArrowSchemaRef, projection: Option<&Vec<usize>>) -> Vec<String> { | ||||||
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. Why not return 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. done |
||||||
if let Some(projection) = projection { | ||||||
projection | ||||||
.iter() | ||||||
.map(|p| schema.field(*p).name().clone()) | ||||||
.collect::<Vec<String>>() | ||||||
} else { | ||||||
schema | ||||||
.fields() | ||||||
.iter() | ||||||
.map(|f| f.name().clone()) | ||||||
.collect::<Vec<String>>() | ||||||
} | ||||||
} |
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.
I would suggest to move this example to another pr.
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.
done