|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::HashMap; |
| 19 | + |
| 20 | +use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; |
| 21 | +use datafusion_comet_proto::spark_expression::{ |
| 22 | + data_type::DataTypeId, expr::ExprStruct, literal::Value, DataType as SparkDataType, Expr, |
| 23 | + Literal, |
| 24 | +}; |
| 25 | +use datafusion_comet_proto::spark_operator::{ |
| 26 | + operator::OpStruct, NativeScan, NativeScanCommon, Operator, SparkFilePartition, |
| 27 | + SparkPartitionedFile, SparkStructField, |
| 28 | +}; |
| 29 | + |
| 30 | +use crate::execution::planner::PhysicalPlanner; |
| 31 | + |
| 32 | +fn spark_field( |
| 33 | + name: &str, |
| 34 | + data_type: DataTypeId, |
| 35 | + nullable: bool, |
| 36 | + field_id: &str, |
| 37 | +) -> SparkStructField { |
| 38 | + SparkStructField { |
| 39 | + name: name.to_string(), |
| 40 | + data_type: Some(SparkDataType { |
| 41 | + type_id: data_type as i32, |
| 42 | + type_info: None, |
| 43 | + }), |
| 44 | + nullable, |
| 45 | + metadata: HashMap::from([("parquet.field.id".to_string(), field_id.to_string())]), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fn common(required_data_indices: &[usize], projection: Vec<i64>) -> NativeScanCommon { |
| 50 | + let data_schema = vec![ |
| 51 | + spark_field("id", DataTypeId::Int32, false, "1"), |
| 52 | + spark_field("unused", DataTypeId::String, true, "2"), |
| 53 | + spark_field("payload", DataTypeId::Int64, true, "3"), |
| 54 | + ]; |
| 55 | + NativeScanCommon { |
| 56 | + required_schema: required_data_indices |
| 57 | + .iter() |
| 58 | + .map(|&index| data_schema[index].clone()) |
| 59 | + .collect(), |
| 60 | + data_schema, |
| 61 | + // Constant file metadata is appended alongside partition values by the JVM scan. |
| 62 | + partition_schema: vec![ |
| 63 | + spark_field("part", DataTypeId::Int32, false, "4"), |
| 64 | + spark_field("_metadata.file_size", DataTypeId::Int64, false, "5"), |
| 65 | + ], |
| 66 | + projection_vector: projection, |
| 67 | + session_timezone: "UTC".to_string(), |
| 68 | + ..Default::default() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn literal(data_type: DataTypeId, value: Value) -> Expr { |
| 73 | + Expr { |
| 74 | + expr_struct: Some(ExprStruct::Literal(Literal { |
| 75 | + datatype: Some(SparkDataType { |
| 76 | + type_id: data_type as i32, |
| 77 | + type_info: None, |
| 78 | + }), |
| 79 | + value: Some(value), |
| 80 | + is_null: false, |
| 81 | + })), |
| 82 | + ..Default::default() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn plan_schema(common: &NativeScanCommon, empty: bool) -> SchemaRef { |
| 87 | + let files = if empty { |
| 88 | + vec![] |
| 89 | + } else { |
| 90 | + // Planning does not read the file. Only its presence selects the nonempty path. |
| 91 | + vec![SparkPartitionedFile { |
| 92 | + file_path: "file:///tmp/comet-empty-native-scan-schema.parquet".to_string(), |
| 93 | + start: 0, |
| 94 | + length: 16, |
| 95 | + file_size: 16, |
| 96 | + partition_values: vec![ |
| 97 | + literal(DataTypeId::Int32, Value::IntVal(7)), |
| 98 | + literal(DataTypeId::Int64, Value::LongVal(16)), |
| 99 | + ], |
| 100 | + }] |
| 101 | + }; |
| 102 | + let operator = Operator { |
| 103 | + op_struct: Some(OpStruct::NativeScan(NativeScan { |
| 104 | + common: Some(common.clone()), |
| 105 | + file_partition: Some(SparkFilePartition { |
| 106 | + partitioned_file: files, |
| 107 | + }), |
| 108 | + ..Default::default() |
| 109 | + })), |
| 110 | + ..Default::default() |
| 111 | + }; |
| 112 | + let (_, _, plan) = PhysicalPlanner::default() |
| 113 | + .create_plan(&operator, &mut vec![], 1) |
| 114 | + .unwrap(); |
| 115 | + plan.schema() |
| 116 | +} |
| 117 | + |
| 118 | +fn assert_schema_matches_nonempty(common: NativeScanCommon, expected: Vec<Field>) { |
| 119 | + let nonempty_schema = plan_schema(&common, false); |
| 120 | + let empty_schema = plan_schema(&common, true); |
| 121 | + assert_eq!(nonempty_schema.as_ref(), &Schema::new(expected)); |
| 122 | + assert_eq!(empty_schema, nonempty_schema); |
| 123 | +} |
| 124 | + |
| 125 | +fn id_field() -> Field { |
| 126 | + Field::new("id", DataType::Int32, false).with_metadata(HashMap::from([( |
| 127 | + "parquet.field.id".to_string(), |
| 128 | + "1".to_string(), |
| 129 | + )])) |
| 130 | +} |
| 131 | + |
| 132 | +fn payload_field() -> Field { |
| 133 | + Field::new("payload", DataType::Int64, true).with_metadata(HashMap::from([( |
| 134 | + "parquet.field.id".to_string(), |
| 135 | + "3".to_string(), |
| 136 | + )])) |
| 137 | +} |
| 138 | + |
| 139 | +#[test] |
| 140 | +fn empty_scan_preserves_reordered_and_pruned_data_columns() { |
| 141 | + assert_schema_matches_nonempty( |
| 142 | + common(&[0, 2], vec![2, 0]), |
| 143 | + vec![payload_field(), id_field()], |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +#[test] |
| 148 | +fn empty_scan_preserves_partition_and_constant_metadata_projection() { |
| 149 | + // Match the nonempty path: data-field metadata is preserved, while partition |
| 150 | + // and constant metadata fields retain their names, types and nullability only. |
| 151 | + assert_schema_matches_nonempty( |
| 152 | + common(&[0, 2], vec![3, 2, 4, 0]), |
| 153 | + vec![ |
| 154 | + Field::new("part", DataType::Int32, false), |
| 155 | + payload_field(), |
| 156 | + Field::new("_metadata.file_size", DataType::Int64, false), |
| 157 | + id_field(), |
| 158 | + ], |
| 159 | + ); |
| 160 | +} |
| 161 | + |
| 162 | +#[test] |
| 163 | +fn empty_scan_preserves_partition_only_projection() { |
| 164 | + assert_schema_matches_nonempty( |
| 165 | + common(&[], vec![3]), |
| 166 | + vec![Field::new("part", DataType::Int32, false)], |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +#[test] |
| 171 | +fn empty_scan_preserves_zero_column_projection() { |
| 172 | + assert_schema_matches_nonempty(common(&[], vec![]), vec![]); |
| 173 | +} |
0 commit comments