-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datafusion): support metadata tables for Datafusion
Signed-off-by: xxchan <[email protected]>
- Loading branch information
Showing
13 changed files
with
473 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -83,6 +83,7 @@ pub mod transform; | |
mod runtime; | ||
|
||
pub mod arrow; | ||
pub mod test_utils; | ||
mod utils; | ||
pub mod writer; | ||
|
||
|
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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. | ||
|
||
//! Test utilities. | ||
//! This module is pub just for internal testing. | ||
//! It is subject to change and is not intended to be used by external users. | ||
use arrow_array::RecordBatch; | ||
use expect_test::Expect; | ||
use itertools::Itertools; | ||
|
||
/// Snapshot testing to check the resulting record batch. | ||
/// | ||
/// - `expected_schema/data`: put `expect![[""]]` as a placeholder, | ||
/// and then run test with `UPDATE_EXPECT=1 cargo test` to automatically update the result, | ||
/// or use rust-analyzer (see [video](https://github.com/rust-analyzer/expect-test)). | ||
/// Check the doc of [`expect_test`] for more details. | ||
/// - `ignore_check_columns`: Some columns are not stable, so we can skip them. | ||
/// - `sort_column`: The order of the data might be non-deterministic, so we can sort it by a column. | ||
pub fn check_record_batch( | ||
record_batch: RecordBatch, | ||
expected_schema: Expect, | ||
expected_data: Expect, | ||
ignore_check_columns: &[&str], | ||
sort_column: Option<&str>, | ||
) { | ||
let mut columns = record_batch.columns().to_vec(); | ||
if let Some(sort_column) = sort_column { | ||
let column = record_batch.column_by_name(sort_column).unwrap(); | ||
let indices = arrow_ord::sort::sort_to_indices(column, None, None).unwrap(); | ||
columns = columns | ||
.iter() | ||
.map(|column| arrow_select::take::take(column.as_ref(), &indices, None).unwrap()) | ||
.collect_vec(); | ||
} | ||
|
||
expected_schema.assert_eq(&format!( | ||
"{}", | ||
record_batch.schema().fields().iter().format(",\n") | ||
)); | ||
expected_data.assert_eq(&format!( | ||
"{}", | ||
record_batch | ||
.schema() | ||
.fields() | ||
.iter() | ||
.zip_eq(columns) | ||
.map(|(field, column)| { | ||
if ignore_check_columns.contains(&field.name().as_str()) { | ||
format!("{}: (skipped)", field.name()) | ||
} else { | ||
format!("{}: {:?}", field.name(), column) | ||
} | ||
}) | ||
.format(",\n") | ||
)); | ||
} |
This file contains 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
91 changes: 91 additions & 0 deletions
91
crates/integrations/datafusion/src/physical_plan/metadata_scan.rs
This file contains 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 datafusion::catalog::TableProvider; | ||
use datafusion::physical_expr::EquivalenceProperties; | ||
use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType}; | ||
use datafusion::physical_plan::stream::RecordBatchStreamAdapter; | ||
use datafusion::physical_plan::{DisplayAs, ExecutionPlan, Partitioning, PlanProperties}; | ||
|
||
use crate::metadata_table::IcebergMetadataTableProvider; | ||
|
||
#[derive(Debug)] | ||
pub struct IcebergMetadataScan { | ||
provider: IcebergMetadataTableProvider, | ||
properties: PlanProperties, | ||
} | ||
|
||
impl IcebergMetadataScan { | ||
pub fn new(provider: IcebergMetadataTableProvider) -> Self { | ||
let properties = PlanProperties::new( | ||
EquivalenceProperties::new(provider.schema()), | ||
Partitioning::UnknownPartitioning(1), | ||
EmissionType::Incremental, | ||
Boundedness::Bounded, | ||
); | ||
Self { | ||
provider, | ||
properties, | ||
} | ||
} | ||
} | ||
|
||
impl DisplayAs for IcebergMetadataScan { | ||
fn fmt_as( | ||
&self, | ||
_t: datafusion::physical_plan::DisplayFormatType, | ||
f: &mut std::fmt::Formatter, | ||
) -> std::fmt::Result { | ||
write!(f, "IcebergMetadataScan") | ||
} | ||
} | ||
|
||
impl ExecutionPlan for IcebergMetadataScan { | ||
fn name(&self) -> &str { | ||
"IcebergMetadataScan" | ||
} | ||
|
||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
fn properties(&self) -> &PlanProperties { | ||
&self.properties | ||
} | ||
|
||
fn children(&self) -> Vec<&std::sync::Arc<dyn ExecutionPlan>> { | ||
vec![] | ||
} | ||
|
||
fn with_new_children( | ||
self: std::sync::Arc<Self>, | ||
_children: Vec<std::sync::Arc<dyn ExecutionPlan>>, | ||
) -> datafusion::error::Result<std::sync::Arc<dyn ExecutionPlan>> { | ||
Ok(self) | ||
} | ||
|
||
fn execute( | ||
&self, | ||
_partition: usize, | ||
_context: std::sync::Arc<datafusion::execution::TaskContext>, | ||
) -> datafusion::error::Result<datafusion::execution::SendableRecordBatchStream> { | ||
let batch_fut = self.provider.clone().scan(); | ||
let schema = self.provider.schema(); | ||
let stream = futures::stream::once(batch_fut); | ||
Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream))) | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -16,4 +16,5 @@ | |
// under the License. | ||
|
||
pub(crate) mod expr_to_predicate; | ||
pub(crate) mod metadata_scan; | ||
pub(crate) mod scan; |
Oops, something went wrong.