comphead commented on code in PR #7715:
URL: https://github.com/apache/datafusion-comet/pull/7715#discussion_r4762661153


##########
native/core/src/parquet/file_error_context.rs:
##########
@@ -163,71 +204,194 @@ impl FileSource for ParquetErrorContext {
 }
 
 #[derive(Debug)]
-struct FileContextMorselizer(Box<dyn Morselizer>);
+struct FileContextMorselizer {
+    inner: Box<dyn Morselizer>,
+    spark_morselizer: Option<(Arc<dyn Morselizer>, SparkReaderBatchSize)>,
+}
+
+#[derive(Debug)]
+struct SparkBatchReplan {
+    morselizer: Arc<dyn Morselizer>,
+    file: PartitionedFile,
+    footer: Arc<ReplanFooter>,
+    size: SparkReaderBatchSize,
+}
+
+#[derive(Debug)]
+struct SparkWindows {
+    size: usize,
+    row_groups: Vec<usize>,
+}
+
+impl SparkBatchReplan {
+    fn windows(&self) -> Option<Arc<SparkWindows>> {
+        if self.size.decoder_size() == self.size.spark {
+            return None;
+        }
+        let groups = self.footer.0.get()?.row_groups();
+        let mut filter = 
RowGroupAccessPlanFilter::new(ParquetAccessPlan::new_all(groups.len()));
+        if let Some(range) = &self.file.range {
+            filter.prune_by_range(groups, range);
+        }
+        let access = self.file.extensions.get::<ParquetAccessPlan>();
+        let row_groups = filter
+            .row_group_indexes()
+            .map(|i| match access.and_then(|plan| plan.inner().get(i)) {
+                Some(RowGroupAccess::Skip) => 0,
+                Some(RowGroupAccess::Selection(selection)) => 
selection.row_count(),
+                _ => groups[i].num_rows() as usize,
+            })
+            .collect();
+        Some(Arc::new(SparkWindows {
+            size: self.size.spark,
+            row_groups,
+        }))
+    }
+}
+
+impl SparkWindows {

Review Comment:
   I found no test of the new window and footer logic below the Spark SQL 
level. `git grep` at the head shows `SparkWindows`, `SparkWindows::windows`, 
`decoder_size` and `may_overflow_millis` are exercised only through the Scala 
test, which writes one row group of three rows. Not covered: a file with 
several row groups (the fresh batch per row group is the reason for 
`windows()`), a split `file.range`, a `ParquetAccessPlan` with `Skip` or 
`Selection`, sizes whose `gcd` is not the smaller size, and a footer whose 
statistics prove all values fit (the case `may_overflow_millis` exists for, 
which should keep Comet's batching). `buffer` is a pure stream transform, so a 
unit test with a synthetic stream of sub-batches would be cheap.



##########
native/core/src/parquet/parquet_support.rs:
##########
@@ -439,6 +471,71 @@ fn has_timestamp_unit(data_type: &DataType, unit: 
TimeUnit) -> bool {
     }
 }
 
+pub(crate) fn requests_nested_timestamp(schema: &Schema) -> bool {
+    schema.fields().iter().any(|field| {
+        field.data_type().is_nested()
+            && has_timestamp_unit(field.data_type(), TimeUnit::Microsecond)
+    })
+}
+
+pub(super) fn projects_nested_dictionary_timestamp_millis(

Review Comment:
   This adds another recursive walk over struct, list and map pairs next to 
`has_timestamp_unit`, `requests_nested_timestamp` and the conversion recursion 
in `parquet_convert_array_impl`. `matches_pair` re-implements the same 
container descent. This PR already made `has_timestamp_unit` dictionary-aware, 
so could the check be `has_timestamp_unit(physical_field.data_type(), 
Millisecond)` on the fields that `match_struct_fields` selects? That keeps the 
projection precision and drops the `List`/`Map`/`Struct` arms here. If the 
extra precision is needed, a comment saying which case the simpler check would 
replan unnecessarily would help.



##########
native/core/src/parquet/file_error_context.rs:
##########
@@ -163,71 +204,194 @@ impl FileSource for ParquetErrorContext {
 }
 
 #[derive(Debug)]
-struct FileContextMorselizer(Box<dyn Morselizer>);
+struct FileContextMorselizer {
+    inner: Box<dyn Morselizer>,
+    spark_morselizer: Option<(Arc<dyn Morselizer>, SparkReaderBatchSize)>,
+}
+
+#[derive(Debug)]
+struct SparkBatchReplan {

Review Comment:
   This replan machinery (`SparkBatchReplan`, `SparkWindows`, the `gcd` decoder 
size, the second morselizer, `ReplanFooter`) runs only when the footer projects 
a nested dictionary timestamp (`projects_nested_dictionary_timestamp_millis`). 
A plain nested `TIMESTAMP_MILLIS` leaf in the same scan still decodes in 
Comet's own batches, and `main` has no Spark batch alignment at all (`git grep 
SparkReaderBatchSize upstream/main` finds nothing). So I expect `SELECT s.ts 
FROM t LIMIT 1` with the first overflow in row 4097 to still raise in Comet and 
succeed in Spark when the file has no dictionary hint. I did not run that case. 
Is exact `LIMIT` parity worth roughly 250 lines for the dictionary case alone? 
Two simpler shapes: give dictionary leaves the same early-error behavior as the 
plain path (keep the dictionary arm and its tests, drop the replan, windows and 
the `FilterExec` change), or do the alignment for both paths so the behavior is 
consistent. Which one is intended?



##########
native/core/src/execution/planner.rs:
##########
@@ -1810,11 +1838,17 @@ impl PhysicalPlanner {
                 let predicate =
                     self.create_expr(filter.predicate.as_ref().unwrap(), 
child.schema())?;
 
-                // FilterExec merges its output up to its own batch size, 8192 
rows by default.
-                // Use the session batch size so it doesn't build batches 
bigger than that.
+                // Coalescing can poll a later, overflowing Parquet batch 
before a
+                // LIMIT sees its first match. With target 1 DataFusion 
forwards
+                // each nonempty filtered batch intact (bypass threshold = 
target / 2).
+                let batch_size = if 
streams_checked_nested_timestamps(child.native_plan.as_ref()) {

Review Comment:
   `streams_checked_nested_timestamps` is true for every unfiltered V1 scan 
that requests a nested microsecond timestamp, because `SparkReaderBatchSize` is 
attached at plan time before the footer is read (the PR description says so). 
That includes files with no dictionary and no `TIMESTAMP_MILLIS`. For those 
plans every `FilterExec` above the scan, including one above a join since the 
walk descends through children whose `emission_type` is not `Final`, now gets 
target batch size 1. By the comment in this diff, the coalescer then forwards 
each non-empty filtered batch as is, so a selective filter hands downstream 
operators many small batches. Can the session batch size stay unless the footer 
actually triggers the replan, or unless a limit is above the filter? If neither 
is practical, please attach a measurement of a selective filter over a scan 
with a nested timestamp column. I did not run this.



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