alexanderbianchi commented on code in PR #113:
URL: 
https://github.com/apache/datafusion-iceberg/pull/113#discussion_r4613859298


##########
crates/datafusion/tests/file_scan_task_provider.rs:
##########
@@ -0,0 +1,497 @@
+// 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::datatypes::Schema;
+use datafusion::arrow::util::pretty::pretty_format_batches;
+use datafusion::common::Result;
+use datafusion::datasource::source::{DataSource, DataSourceExec};
+use datafusion::execution::TaskContext;
+use datafusion::physical_plan::{ExecutionPlan, collect};
+use datafusion::prelude::SessionConfig;
+use futures::TryStreamExt;
+use iceberg::arrow::ArrowReaderBuilder;
+use iceberg::io::FileIO;
+use iceberg::table::Table;
+use iceberg_datafusion::IcebergDataFusionConfig;
+use iceberg_datafusion::read::{
+    EagerFileScanTaskProvider, FileScanTaskProvider, 
FileScanTaskProviderBuilder,
+    FileScanTaskProviderContext, IcebergDataSource, LazyFileScanTaskProvider,
+};
+use iceberg_datafusion::test_utils::IcebergTestHarness;
+
+#[tokio::test]
+async fn lazy_discovery_does_not_read_manifests_until_polled() -> Result<()> {
+    let harness = IcebergTestHarness::builder()
+        .with_config(SessionConfig::new().with_target_partitions(3))
+        .build()
+        .await?;
+    let plan = harness.plan("SELECT pickup_date FROM taxi").await?;
+    let source = iceberg_source(&plan);
+    let lazy = (source.file_scan_task_provider() as &dyn Any)
+        .downcast_ref::<LazyFileScanTaskProvider>()
+        .unwrap();
+    assert_eq!(lazy.partitioning().partition_count(), 1);
+    assert_eq!(lazy.projection(), &["pickup_date"]);
+
+    // Keep the loaded metadata but deliberately remove its S3 fixture storage.
+    // LocalFs cannot read these manifest paths. Opening the lazy stream must
+    // still succeed; only polling it should attempt that I/O and fail.
+    let inaccessible_table = Table::builder()
+        .identifier(lazy.table().identifier().clone())
+        .metadata(lazy.table().metadata_ref())
+        .file_io(FileIO::new_with_fs())
+        .runtime(iceberg::Runtime::current())
+        .build()
+        .map_err(iceberg_datafusion::to_datafusion_error)?;
+    let context = harness.context().task_ctx();
+    let (_, discovery) = FileScanTaskProviderBuilder::new(
+        Arc::clone(&context),
+        inaccessible_table,
+        lazy.schema(),
+    )
+    .build()
+    .await?;
+    let mut tasks = discovery.open(
+        0,
+        FileScanTaskProviderContext {
+            task_context: &context,
+        },
+    )?;
+    assert!(tasks.try_next().await.is_err());
+    Ok(())
+}
+
+#[tokio::test]
+async fn lazy_builder_keeps_projected_schema_and_task_columns_in_order() -> 
Result<()> {
+    let harness = IcebergTestHarness::builder()
+        .with_config(SessionConfig::new().with_target_partitions(1))
+        .build()
+        .await?;
+    let plan = harness
+        .plan("SELECT * FROM taxi WHERE pickup_date = DATE '2024-01-10'")
+        .await?;
+    let source = iceberg_source(&plan);
+    let lazy = (source.file_scan_task_provider() as &dyn Any)
+        .downcast_ref::<LazyFileScanTaskProvider>()
+        .unwrap();
+    let schema = lazy.schema();
+    // Reverse the fixture's relative order: date (field 13), then vendor 
(field 1).
+    let projection = vec![
+        schema.index_of("pickup_date")?,
+        schema.index_of("vendor_id")?,
+    ];
+    let (schema, discovery) = FileScanTaskProviderBuilder::new(
+        harness.context().task_ctx(),
+        lazy.table().clone(),
+        schema,
+    )
+    .with_projection(Some(&projection))
+    .with_predicate(lazy.predicate().cloned())
+    .build()
+    .await?;
+    let columns = schema
+        .fields()
+        .iter()
+        .map(|field| field.name().clone())
+        .collect::<Vec<_>>();
+    insta::assert_debug_snapshot!(columns, @r#"
+    [
+        "pickup_date",
+        "vendor_id",
+    ]
+    "#);
+    let projected_lazy = (discovery.as_ref() as &dyn Any)
+        .downcast_ref::<LazyFileScanTaskProvider>()
+        .unwrap();
+    assert_eq!(projected_lazy.projection(), columns);
+    let context = harness.context().task_ctx();
+    let tasks = Arc::clone(&discovery)
+        .open(
+            0,
+            FileScanTaskProviderContext {
+                task_context: &context,
+            },
+        )?
+        .try_collect::<Vec<_>>()
+        .await
+        .map_err(iceberg_datafusion::to_datafusion_error)?;
+    assert_eq!(tasks.len(), 1);
+    assert_eq!(tasks[0].project_field_ids(), &[13, 1]);
+
+    let projected = IcebergDataSource::builder(
+        &harness.context().copied_config(),
+        schema,
+        source.file_io().clone(),
+        lazy.table().reader_builder(),
+    )
+    .with_reader(source.reader().clone())
+    .with_file_scan_task_provider(discovery)
+    .with_fetch(Some(1))
+    .build()?;
+    let batches = projected.open(0, context)?.try_collect::<Vec<_>>().await?;
+    insta::assert_snapshot!(pretty_format_batches(&batches)?.to_string(), @"
+    +-------------+-----------+
+    | pickup_date | vendor_id |
+    +-------------+-----------+
+    | 2024-01-10  | 2         |
+    +-------------+-----------+
+    ");
+    Ok(())
+}
+
+#[tokio::test]
+async fn lazy_builder_rejects_projection_outside_the_supplied_schema() -> 
Result<()> {
+    let harness = IcebergTestHarness::builder()
+        .with_config(SessionConfig::new().with_target_partitions(1))
+        .build()
+        .await?;
+    let plan = harness.plan("SELECT pickup_date FROM taxi").await?;
+    let source = iceberg_source(&plan);
+    let lazy = (source.file_scan_task_provider() as &dyn Any)
+        .downcast_ref::<LazyFileScanTaskProvider>()
+        .unwrap();
+    // The supplied schema has only one column, even though the table has 
thirteen.
+    let error = FileScanTaskProviderBuilder::new(
+        harness.context().task_ctx(),
+        lazy.table().clone(),
+        lazy.schema(),
+    )
+    .with_projection(Some(&vec![1]))
+    .build()
+    .await
+    .unwrap_err();
+    insta::assert_snapshot!(error.to_string(), @"Arrow error: Schema error: 
project index 1 out of bounds, max field 1");
+    Ok(())
+}
+
+#[tokio::test]
+async fn eager_table_planning_selects_files_before_execution() -> Result<()> {
+    let harness = IcebergTestHarness::builder()
+        .with_config(
+            SessionConfig::new()
+                .with_target_partitions(3)
+                .with_option_extension(IcebergDataFusionConfig::default())
+                .set_bool("iceberg.execution.eager_file_planning", true),
+        )
+        .build()
+        .await?;
+    let plan = harness
+        .plan(
+            "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?;
+    let eager = (iceberg_source(&plan).file_scan_task_provider() as &dyn Any)
+        .downcast_ref::<EagerFileScanTaskProvider>()
+        .unwrap();
+    // Only two selected files, so eager discovery caps the requested three 
partitions.
+    assert_eq!(
+        eager.groups().iter().map(Vec::len).collect::<Vec<_>>(),
+        vec![1, 1]
+    );
+    let batches = collect(plan, harness.context().task_ctx()).await?;
+    insta::assert_snapshot!(pretty_format_batches(&batches)?.to_string(), @"
+    +-------------+-------+
+    | pickup_date | trips |
+    +-------------+-------+
+    | 2024-01-10  | 25000 |
+    | 2024-01-11  | 25000 |
+    +-------------+-------+
+    ");
+    Ok(())
+}
+
+#[tokio::test]
+async fn eager_planning_consumes_the_lazy_providers_discovery() -> Result<()> {
+    let harness = IcebergTestHarness::builder()
+        .with_config(SessionConfig::new().with_target_partitions(3))
+        .build()
+        .await?;
+    let plan = harness.plan("SELECT pickup_date FROM taxi").await?;
+    let lazy = (iceberg_source(&plan).file_scan_task_provider() as &dyn Any)
+        .downcast_ref::<LazyFileScanTaskProvider>()
+        .unwrap()
+        .clone();
+    let eager = lazy.into_eager(harness.context().task_ctx()).await?;
+    assert_eq!(
+        eager.groups().iter().map(Vec::len).collect::<Vec<_>>(),
+        vec![3, 2, 2]
+    );
+    assert_eq!(eager.tasks().count(), 7);
+    Ok(())
+}
+
+#[tokio::test]
+async fn reconstruction_needs_no_table_or_current_runtime() -> Result<()> {
+    let harness = IcebergTestHarness::builder()
+        .with_config(
+            SessionConfig::new()
+                .with_target_partitions(3)
+                .with_batch_size(1)
+                .with_option_extension(IcebergDataFusionConfig::default())
+                .set_bool("iceberg.execution.eager_file_planning", true),
+        )
+        .build()
+        .await?;
+    let plan = harness
+        .plan("SELECT pickup_date FROM taxi WHERE pickup_date = DATE 
'2024-01-10'")
+        .await?;
+    let source = iceberg_source(&plan);
+    let eager = (source.file_scan_task_provider() as &dyn Any)
+        .downcast_ref::<EagerFileScanTaskProvider>()
+        .unwrap();
+    let file = eager.tasks().next().unwrap().clone();
+    // Explicit assignment: preserve an empty partition and a repeated file.
+    let groups = vec![vec![file.clone()], vec![], vec![file.clone(), file]];
+    let (schema, file_io, reader) = (
+        source.schema(),
+        source.file_io().clone(),
+        source.reader().clone(),
+    );
+    let reader_builder =
+        ArrowReaderBuilder::new(file_io.clone(), iceberg::Runtime::current());
+    // An explicit reader must override the constructor's session defaults.
+    let config = harness.context().copied_config().with_batch_size(7);
+    drop(plan);
+    drop(harness);
+
+    let source = std::thread::spawn(move || {
+        assert!(tokio::runtime::Handle::try_current().is_err());
+        IcebergDataSource::builder(&config, schema, file_io, reader_builder)
+            .with_reader(reader)
+            
.with_file_scan_task_provider(Arc::new(EagerFileScanTaskProvider::try_new(
+                groups,
+            )?))
+            .with_fetch(Some(2))
+            .build()
+    })

Review Comment:
   Tranquilo 😂



-- 
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]

Reply via email to