manuzhang commented on code in PR #6246:
URL: https://github.com/apache/datafusion-comet/pull/6246#discussion_r4771296527


##########
native/core/src/parquet/parquet_exec.rs:
##########
@@ -506,6 +744,510 @@ mod tests {
         assert!(!calls_jvm(&native));
     }
 
+    /// How many rows of a scan hold each decoded value of `s`.
+    type DecodedCounts = BTreeMap<Option<String>, usize>;
+
+    /// Writes three 500-row row groups with `a` counting from 0, 1,000 and 
2,000, and `s` holding
+    /// `values[i]` in every row of group `i` as `s_type`. A scan's values 
then show which groups a
+    /// filter pruned.
+    fn write_binary_as_string_fixture(
+        s_type: DataType,
+        values: [Option<&[u8]>; 3],
+    ) -> PartitionedFile {
+        let file_schema = Arc::new(Schema::new(vec![
+            Field::new("a", DataType::Int32, false),
+            Field::new("s", s_type.clone(), true),
+        ]));
+        let filename = get_temp_filename()
+            .as_path()
+            .as_os_str()
+            .to_str()
+            .unwrap()
+            .to_string();
+        let props = WriterProperties::builder()
+            .set_max_row_group_row_count(Some(500))
+            .build();
+        let file = File::create(&filename).unwrap();
+        let mut writer = ArrowWriter::try_new(file, Arc::clone(&file_schema), 
Some(props)).unwrap();
+        for (group, value) in values.into_iter().enumerate() {
+            let start = group as i32 * 1_000;
+            let s = arrow::compute::cast(&BinaryArray::from(vec![value; 500]), 
&s_type).unwrap();
+            let a = Arc::new(Int32Array::from((start..start + 
500).collect::<Vec<i32>>()));
+            let batch = RecordBatch::try_new(Arc::clone(&file_schema), vec![a, 
s]).unwrap();
+            writer.write(&batch).unwrap();
+        }
+        writer.close().unwrap();
+        PartitionedFile::from_path(filename).unwrap()
+    }
+
+    fn binary_as_string_required_schema() -> SchemaRef {
+        Arc::new(Schema::new(vec![
+            Field::new("a", DataType::Int32, false),
+            Field::new("s", DataType::Utf8, true),
+        ]))
+    }
+
+    /// A session whose scans evaluate pushed filters on every row when 
`row_filter` is set, and
+    /// otherwise use them only to prune row groups.
+    fn binary_as_string_session(row_filter: bool) -> Arc<SessionContext> {
+        let mut config = SessionConfig::new();
+        config.options_mut().execution.parquet.pushdown_filters = row_filter;
+        Arc::new(SessionContext::new_with_config(config))
+    }
+
+    /// A scan of `file` with `binaryAsString` on, reading `s` as a string.
+    fn binary_as_string_scan(
+        file: &PartitionedFile,
+        filters: Vec<Arc<dyn PhysicalExpr>>,
+        session_ctx: &Arc<SessionContext>,
+    ) -> Arc<DataSourceExec> {
+        let required_schema = binary_as_string_required_schema();
+        init_datasource_exec(
+            Arc::clone(&required_schema),
+            Some(required_schema),
+            None,
+            ObjectStoreUrl::local_filesystem(),
+            ObjectStoreBackend::Local,
+            vec![vec![file.clone()]],
+            Some(vec![0, 1]),
+            Some(filters),
+            None,
+            "UTC",
+            true,
+            false,
+            false,
+            false,
+            true,
+            session_ctx,
+            false,
+            false,
+            false,
+            false,
+            "",
+            "",
+        )
+        .unwrap()
+    }
+
+    /// Counts the decoded values of `s` that `scan` returns. The scan has no 
residual filter, so
+    /// it returns every row of each row group its filters keep, or with row 
filters only the rows
+    /// they match.
+    async fn decoded_counts(
+        scan: &Arc<DataSourceExec>,
+        session_ctx: &Arc<SessionContext>,
+    ) -> DecodedCounts {
+        let batches: Vec<RecordBatch> = scan
+            .execute(0, session_ctx.task_ctx())
+            .unwrap()
+            .map(|batch| batch.unwrap())
+            .collect()
+            .await;
+        let mut counts = BTreeMap::new();
+        for batch in &batches {
+            assert_eq!(batch.schema().field(1).data_type(), &DataType::Utf8);
+            for value in batch.column(1).as_string::<i32>() {
+                *counts.entry(value.map(str::to_string)).or_insert(0) += 1;
+            }
+        }
+        counts
+    }
+
+    async fn scan_binary_as_string(
+        file: &PartitionedFile,
+        filters: Vec<Arc<dyn PhysicalExpr>>,
+        row_filter: bool,
+    ) -> DecodedCounts {
+        let session_ctx = binary_as_string_session(row_filter);
+        decoded_counts(
+            &binary_as_string_scan(file, filters, &session_ctx),
+            &session_ctx,
+        )
+        .await
+    }
+
+    fn compare_s(op: Operator, value: &str) -> Arc<dyn PhysicalExpr> {
+        let literal = Literal::new(ScalarValue::Utf8(Some(value.to_string())));
+        Arc::new(BinaryExpr::new(
+            Arc::new(Column::new("s", 1)),
+            op,
+            Arc::new(literal),
+        ))
+    }
+
+    fn s_in(values: &[&str], negated: bool) -> Arc<dyn PhysicalExpr> {
+        let values = values
+            .iter()
+            .map(|value| {
+                let literal = 
Literal::new(ScalarValue::Utf8(Some(value.to_string())));
+                Arc::new(literal) as Arc<dyn PhysicalExpr>
+            })
+            .collect();
+        let schema = binary_as_string_required_schema();
+        in_list(Arc::new(Column::new("s", 1)), values, &negated, 
&schema).unwrap()
+    }
+
+    fn groups(values: &[Option<&str>]) -> DecodedCounts {
+        values
+            .iter()
+            .map(|value| (value.map(str::to_string), 500))
+            .collect()
+    }
+
+    /// A `binaryAsString` scan that projects no string column has nothing to 
read as Binary, so
+    /// it builds the same plan as with the setting off, with the projection 
as indices.
+    #[test]
+    fn binary_as_string_leaves_scan_without_string_columns_unchanged() {

Review Comment:
   This is fixed now.



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