viirya commented on code in PR #26035:
URL: https://github.com/apache/datafusion/pull/26035#discussion_r4250029286


##########
datafusion/substrait/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -320,6 +320,189 @@ async fn select_without_limit() -> Result<()> {
     roundtrip_fill_na("SELECT * FROM data OFFSET 5+7-2").await
 }
 
+#[tokio::test]
+async fn roundtrip_table_scan_offset_and_fetch() -> Result<()> {
+    use datafusion::arrow::array::Int64Array;
+    use datafusion::dataframe::DataFrame;
+    use datafusion::datasource::provider_as_source;
+    use datafusion::logical_expr::TableScanBuilder;
+
+    // A single CSV partition preserves input order and supports exact skip 
pushdown.
+    let temp = tempfile::tempdir()?;
+    let path = temp.path().join("data.csv");
+    std::fs::write(&path, "x\n0\n1\n2\n3\n4\n5\n")?;
+    let ctx = SessionContext::new();
+    ctx.register_csv("t", path.to_str().unwrap(), CsvReadOptions::new())
+        .await?;
+    let source = provider_as_source(ctx.table_provider("t").await?);
+
+    for skip in [Some(2), None, Some(0), Some(8)] {
+        for fetch in [None, Some(0), Some(2)] {
+            for project in [false, true] {
+                for parent in [
+                    None,
+                    Some((0, Some(2))),
+                    Some((1, Some(3))),
+                    Some((1, None)),
+                ] {
+                    let scan = TableScanBuilder::new("t", source.clone())
+                        .with_skip(skip)
+                        .with_fetch(fetch)
+                        .build()?;
+                    let mut builder =
+                        LogicalPlanBuilder::from(LogicalPlan::TableScan(scan));
+                    if project {
+                        builder = builder.project(vec![col("x")])?;
+                    }
+                    if let Some((skip, fetch)) = parent {
+                        builder = builder.limit(skip, fetch)?;
+                    }
+                    let plan = builder.build()?;
+                    let proto = to_substrait_plan(&plan, &ctx.state())?;
+                    let restored = from_substrait_plan(&ctx.state(), 
&proto).await?;
+                    let mut expected = (0_i64..6)
+                        .skip(skip.unwrap_or(0))
+                        .take(fetch.unwrap_or(usize::MAX))
+                        .collect::<Vec<_>>();
+                    if let Some((skip, fetch)) = parent {
+                        expected = expected
+                            .into_iter()
+                            .skip(skip)
+                            .take(fetch.unwrap_or(usize::MAX))
+                            .collect();
+                    }
+                    for (stage, plan) in [("original", plan), ("roundtrip", 
restored)] {
+                        let batches = DataFrame::new(ctx.state(), 
plan).collect().await?;
+                        let actual = batches
+                            .iter()
+                            .flat_map(|batch| {
+                                batch
+                                    .column(0)
+                                    .as_any()
+                                    .downcast_ref::<Int64Array>()
+                                    .unwrap()
+                                    .values()
+                                    .iter()
+                                    .copied()
+                            })
+                            .collect::<Vec<_>>();
+                        assert_eq!(
+                            actual, expected,
+                            "{stage}: skip={skip:?}, fetch={fetch:?}, 
project={project}, parent={parent:?}"
+                        );
+                    }
+                }
+            }
+        }
+    }
+    Ok(())
+}
+
+// Limit expressions use Int64, but limit pushdown can produce a larger usize 
fetch.
+#[tokio::test]
+#[cfg(target_pointer_width = "64")]
+async fn reject_table_scan_fetch_above_i64_max() -> Result<()> {
+    use datafusion::arrow::array::Int64Array;
+    use datafusion::arrow::record_batch::RecordBatch;
+
+    let ctx = SessionContext::new();
+    ctx.register_batch(
+        "t",
+        RecordBatch::try_from_iter(vec![(
+            "x",
+            Arc::new(Int64Array::from(vec![0, 1, 2])) as ArrayRef,
+        )])?,
+    )?;
+    let plan = ctx
+        .sql("SELECT * FROM t LIMIT 9223372036854775807 OFFSET 1")
+        .await?
+        .into_optimized_plan()?;
+    let LogicalPlan::Limit(limit) = &plan else {
+        panic!("expected Limit")
+    };
+    let LogicalPlan::TableScan(scan) = limit.input.as_ref() else {
+        panic!("expected TableScan")
+    };
+    assert_eq!(scan.fetch, Some(i64::MAX as usize + 1));
+    let error = to_substrait_plan(&plan, &ctx.state())
+        .unwrap_err()
+        .to_string();
+    assert!(
+        error.contains("TableScan fetch exceeds the supported Int64 range"),
+        "{error}"
+    );
+    Ok(())
+}
+
+#[tokio::test]
+#[cfg(target_pointer_width = "64")]

Review Comment:
   Removed it. Thanks.



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