alamb commented on code in PR #20174:
URL: https://github.com/apache/datafusion/pull/20174#discussion_r2771204474


##########
datafusion/datasource/src/file_scan_config.rs:
##########
@@ -685,11 +685,10 @@ impl DataSource for FileScanConfig {
 
     fn eq_properties(&self) -> EquivalenceProperties {
         let schema = self.file_source.table_schema().table_schema();
-        let mut eq_properties = EquivalenceProperties::new_with_orderings(
-            Arc::clone(schema),
-            self.output_ordering.clone(),
-        )
-        .with_constraints(self.constraints.clone());
+        let orderings = project_orderings(&self.output_ordering, schema);

Review Comment:
   After some more thought I am not sure if this is correct 🤔 I need to do some 
more research into when the projection is applied



##########
datafusion/datasource/src/file_scan_config.rs:
##########
@@ -1957,22 +1956,103 @@ mod tests {
         }
     }
 
+    #[test]
+    fn equivalence_properties_projection_reorders_schema() {
+        // This test ensures `project_orderings` is applied even when there 
are no
+        // partition columns: a projection reorders the schema, and the output 
ordering
+        // is specified in projected schema indices.
+        let file_schema = Arc::new(Schema::new(vec![
+            Field::new("a", DataType::Int32, false),
+            Field::new("b", DataType::Int64, false),
+            Field::new("c", DataType::Utf8, true),
+        ]));
+        let object_store_url = ObjectStoreUrl::parse("test:///").unwrap();
+        let table_schema = TableSchema::from(file_schema);
+
+        let file_source = mock_source(table_schema);
+
+        let config = FileScanConfigBuilder::new(object_store_url.clone(), 
file_source)
+            .with_projection_indices(Some(vec![2, 0]))
+            .unwrap()
+            // Indices are in the projected schema: [c, a] -> [0, 1].
+            .with_output_ordering(vec![[sort_expr("c", 0), sort_expr("a", 
1)].into()])
+            .build();
+
+        let eq_properties = config.eq_properties();
+        let ordering = eq_properties
+            .output_ordering()
+            .expect("expected output ordering");
+
+        let first_col = 
ordering[0].expr.as_any().downcast_ref::<Column>().unwrap();
+        assert_eq!(first_col.name(), "c");
+        assert_eq!(first_col.index(), 0);
+
+        let second_col = 
ordering[1].expr.as_any().downcast_ref::<Column>().unwrap();
+        assert_eq!(second_col.name(), "a");
+        assert_eq!(second_col.index(), 1);
+    }
+
+    #[test]
+    fn equivalence_properties_reindex_output_ordering_for_partition_cols() {
+        // This test exercises `project_orderings` via `eq_properties()` when 
a projection
+        // reorders columns and includes a partition column.
+        let file_schema = Arc::new(Schema::new(vec![
+            Field::new("f", DataType::Float64, true),
+            Field::new(
+                "time",
+                DataType::Timestamp(TimeUnit::Nanosecond, None),
+                false,
+            ),
+        ]));
+        let object_store_url = ObjectStoreUrl::parse("test:///").unwrap();
+
+        let table_schema = TableSchema::new(
+            file_schema,
+            vec![Arc::new(Field::new(
+                "tag1",
+                wrap_partition_type_in_dict(DataType::Utf8),
+                true,
+            ))],
+        );
+
+        let file_source = mock_source(table_schema);
+
+        let config = FileScanConfigBuilder::new(object_store_url.clone(), 
file_source)
+            .with_projection_indices(Some(vec![2, 0, 1]))
+            .unwrap()
+            // Output ordering is defined on unprojected (base) column 
indices. We expect
+            // `project_orderings` to remap these to the projected schema 
indices.
+            .with_output_ordering(vec![
+                [sort_expr("tag1", 0), sort_expr("time", 1)].into(),
+            ])
+            .build();
+
+        let eq_properties = config.eq_properties();
+        let ordering = eq_properties
+            .output_ordering()
+            .expect("expected output ordering");
+
+        let first_col = 
ordering[0].expr.as_any().downcast_ref::<Column>().unwrap();
+        assert_eq!(first_col.name(), "tag1");
+        assert_eq!(first_col.index(), 0);
+
+        let second_col = 
ordering[1].expr.as_any().downcast_ref::<Column>().unwrap();
+        assert_eq!(second_col.name(), "time");
+        assert_eq!(second_col.index(), 2);
+    }
+
     #[test]
     fn test_file_scan_config_builder_defaults() {
         let file_schema = aggr_test_schema();
         let object_store_url = ObjectStoreUrl::parse("test:///").unwrap();
 
-        let table_schema = TableSchema::new(Arc::clone(&file_schema), vec![]);
+        let table_schema = TableSchema::from(Arc::clone(&file_schema));

Review Comment:
   this is a driveby cleanup to reduce test duplication (codex actually copied 
a lot of this extra bloat into the new tests, so instead I went and cleaned it 
up at the source)
   
   I can break it into a new PR if you prefer



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