alexanderbianchi commented on code in PR #113: URL: https://github.com/apache/datafusion-iceberg/pull/113#discussion_r4624433438
########## crates/datafusion/tests/file_scan_task_provider.rs: ########## @@ -0,0 +1,236 @@ +// 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::any::Any; +use std::sync::Arc; + +use datafusion::arrow::array::RecordBatch; +use datafusion::common::Result; +use datafusion::common::stats::Precision; +use datafusion::datasource::source::{DataSource, DataSourceExec}; +use datafusion::execution::TaskContext; +use datafusion::physical_plan::{ExecutionPlan, displayable}; +use futures::TryStreamExt; +use iceberg_datafusion::IcebergDataFusionConfig; +use iceberg_datafusion::read::{EagerFileScanTaskProvider, IcebergDataSource}; +use iceberg_datafusion::test_utils::IcebergTestHarness; + +#[tokio::test] +async fn lazy_scan_filters_and_projects_taxi_rows() -> Result<()> { + let harness = IcebergTestHarness::builder() + .with_config(|config| config.with_target_partitions(2)) + .build() + .await?; + + let (plan, rows) = harness + .query( + "SELECT pickup_date, COUNT(*) AS trips FROM taxi \ + WHERE pickup_date IN (DATE '2024-01-10', DATE '2024-01-11') \ + GROUP BY pickup_date ORDER BY pickup_date", + ) + .await?; + + insta::assert_snapshot!(plan, @" + SortPreservingMergeExec: [pickup_date@0 ASC NULLS LAST] + ProjectionExec: expr=[pickup_date@0 as pickup_date, count(Int64(1))@1 as trips] + SortExec: expr=[pickup_date@0 ASC NULLS LAST], preserve_partitioning=[true] + AggregateExec: mode=FinalPartitioned, gby=[pickup_date@0 as pickup_date], aggr=[count(Int64(1))] + RepartitionExec: partitioning=Hash([pickup_date@0], 2), input_partitions=2 + AggregateExec: mode=Partial, gby=[pickup_date@0 as pickup_date], aggr=[count(Int64(1))] + FilterExec: pickup_date@0 = 2024-01-10 OR pickup_date@0 = 2024-01-11 + RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 + DataSourceExec: format=iceberg, projection=[pickup_date], provider=lazy, predicate=(pickup_date = 2024-01-10) OR (pickup_date = 2024-01-11) + "); + insta::assert_snapshot!(rows, @" + +-------------+-------+ + | pickup_date | trips | + +-------------+-------+ + | 2024-01-10 | 25000 | + | 2024-01-11 | 25000 | + +-------------+-------+ + "); + Ok(()) +} + +#[tokio::test] +async fn eager_scan_plans_only_the_matching_taxi_files() -> Result<()> { + // Taxi has seven daily files; this query needs only two of them. + let harness = IcebergTestHarness::builder() + .with_config(|config| { + config + .with_target_partitions(2) + .with_option_extension(IcebergDataFusionConfig::default()) + .set_bool("iceberg.execution.eager_file_planning", true) + }) + .build() + .await?; + + let (plan, rows) = harness + .query( + "SELECT pickup_date, COUNT(*) AS trips FROM taxi \ + WHERE pickup_date IN (DATE '2024-01-10', DATE '2024-01-11') \ + GROUP BY pickup_date ORDER BY pickup_date", + ) + .await?; + + insta::assert_snapshot!(plan, @" + SortPreservingMergeExec: [pickup_date@0 ASC NULLS LAST] + ProjectionExec: expr=[pickup_date@0 as pickup_date, count(Int64(1))@1 as trips] + SortExec: expr=[pickup_date@0 ASC NULLS LAST], preserve_partitioning=[true] + AggregateExec: mode=FinalPartitioned, gby=[pickup_date@0 as pickup_date], aggr=[count(Int64(1))] + RepartitionExec: partitioning=Hash([pickup_date@0], 2), input_partitions=2 + AggregateExec: mode=Partial, gby=[pickup_date@0 as pickup_date], aggr=[count(Int64(1))] + FilterExec: pickup_date@0 = 2024-01-10 OR pickup_date@0 = 2024-01-11 + DataSourceExec: format=iceberg, projection=[pickup_date], provider=eager, planned_files=2, predicates=[(pickup_date = 2024-01-10) OR (pickup_date = 2024-01-11)] + "); + insta::assert_snapshot!(rows, @" + +-------------+-------+ + | pickup_date | trips | + +-------------+-------+ + | 2024-01-10 | 25000 | + | 2024-01-11 | 25000 | + +-------------+-------+ + "); + Ok(()) +} + +#[tokio::test] +async fn eager_scan_with_no_matching_files_is_executable() -> Result<()> { + let harness = IcebergTestHarness::builder() + .with_config(|config| { + config + .with_target_partitions(2) + .with_option_extension(IcebergDataFusionConfig::default()) + .set_bool("iceberg.execution.eager_file_planning", true) + }) + .build() + .await?; + + let (plan, rows) = harness + .query("SELECT COUNT(*) AS trips FROM taxi WHERE pickup_date = DATE '2025-01-01'") + .await?; + + insta::assert_snapshot!(plan, @" + ProjectionExec: expr=[count(Int64(1))@0 as trips] + AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))] + CoalescePartitionsExec + AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))] + FilterExec: pickup_date@0 = 2025-01-01, projection=[] + RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 + DataSourceExec: format=iceberg, projection=[pickup_date], provider=eager, planned_files=0 + "); + insta::assert_snapshot!(rows, @" + +-------+ + | trips | + +-------+ + | 0 | + +-------+ + "); + Ok(()) +} + +#[tokio::test] +async fn replacing_the_provider_preserves_the_fetch_limit() -> Result<()> { + let harness = IcebergTestHarness::builder() + .with_config(|config| { + config + .with_target_partitions(2) + .with_option_extension(IcebergDataFusionConfig::default()) + .set_bool("iceberg.execution.eager_file_planning", true) + }) + .build() + .await?; + let plan = harness.plan("SELECT pickup_date FROM taxi LIMIT 2").await?; + let source = iceberg_source(&plan).unwrap(); + let assigned_file = eager_files(source).tasks().next().unwrap().clone(); + + let assigned = source.with_file_scan_task_provider(Arc::new( + EagerFileScanTaskProvider::try_new(vec![vec![assigned_file]])?, + ))?; + let assigned_plan = DataSourceExec::from_data_source(assigned); + + insta::assert_snapshot!(displayable(assigned_plan.as_ref()).indent(true).to_string(), @"DataSourceExec: format=iceberg, projection=[pickup_date], provider=eager, planned_files=1, fetch=2"); + Ok(()) +} + +#[tokio::test] +async fn eager_snapshot_statistics_apply_fetch_per_partition() -> Result<()> { + let harness = IcebergTestHarness::builder() + .with_config(|config| { + config + .with_target_partitions(2) + .with_option_extension(IcebergDataFusionConfig::default()) + .set_bool("iceberg.execution.eager_file_planning", true) + }) + .build() + .await?; + let plan = harness.plan("SELECT pickup_date FROM taxi LIMIT 2").await?; + let source = iceberg_source(&plan).unwrap(); + + // Eager conversion retains the snapshot estimate. Fetch bounds each + // partition separately; a short partition may still return fewer rows. + assert_eq!( + source + .with_fetch(None) + .unwrap() + .partition_statistics(None)? + .num_rows, + Precision::Inexact(175000) + ); + assert_eq!(source.output_partitioning().partition_count(), 2); + assert_eq!( + source.partition_statistics(None)?.num_rows, + Precision::Inexact(4) + ); + assert_eq!( + source.partition_statistics(Some(0))?.num_rows, + Precision::Inexact(2) + ); + Ok(()) +} + +#[tokio::test] +async fn the_scan_keeps_its_construction_time_reader_settings() -> Result<()> { + let harness = IcebergTestHarness::builder() + .with_config(|config| config.with_batch_size(2000)) + .build() + .await?; + let plan = harness.plan("SELECT pickup_date FROM taxi").await?; + let source = iceberg_source(&plan).unwrap(); + + // Open with default settings, not the settings used to construct the scan. Review Comment: This deviates from the rest of the tests becuase it's specific to a future caller of `source.open(...)` -- 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]
