|
| 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 | +//! Regression tests for aggregating batches whose data types are *stricter* |
| 19 | +//! than the table's declared schema. See |
| 20 | +//! <https://github.com/apache/datafusion/issues/24069>. |
| 21 | +//! |
| 22 | +//! Builds on the end-to-end reproducer from #24278 by @alamb. |
| 23 | +//! |
| 24 | +//! A `RecordBatch` is a valid instance of a schema that is a superset of its |
| 25 | +//! own (see [`Schema::contains`] / `Field::contains`): most commonly the |
| 26 | +//! schema declares a (possibly nested) field as nullable while the batch's |
| 27 | +//! arrays mark it non-nullable. `MemTable::try_new` accepts such batches via |
| 28 | +//! exactly that check, and engines embedding DataFusion (e.g. Comet) feed |
| 29 | +//! such batches over FFI. Aggregations must therefore not fail when the |
| 30 | +//! runtime arrays are stricter than the planned schema. |
| 31 | +//! |
| 32 | +//! [`Schema::contains`]: arrow::datatypes::Schema::contains |
| 33 | +
|
| 34 | +use std::sync::Arc; |
| 35 | + |
| 36 | +use arrow::array::{BooleanArray, RecordBatch, StructArray, UInt32Array}; |
| 37 | +use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef}; |
| 38 | +use datafusion::datasource::MemTable; |
| 39 | +use datafusion::datasource::memory::MemorySourceConfig; |
| 40 | +use datafusion::physical_expr::aggregate::AggregateExprBuilder; |
| 41 | +use datafusion::physical_plan::ExecutionPlan; |
| 42 | +use datafusion::physical_plan::aggregates::{ |
| 43 | + AggregateExec, AggregateMode, PhysicalGroupBy, |
| 44 | +}; |
| 45 | +use datafusion::physical_plan::collect; |
| 46 | +use datafusion::physical_plan::expressions::col; |
| 47 | +use datafusion::prelude::*; |
| 48 | +use datafusion_common::Result; |
| 49 | +use datafusion_execution::TaskContext; |
| 50 | +use datafusion_execution::memory_pool::FairSpillPool; |
| 51 | +use datafusion_execution::runtime_env::RuntimeEnvBuilder; |
| 52 | +use datafusion_functions_aggregate::array_agg::array_agg_udaf; |
| 53 | + |
| 54 | +/// Returns the fields of the struct column `b`: a single `colA Boolean`. |
| 55 | +/// |
| 56 | +/// `col_a_nullable` controls whether `colA` is declared nullable — the only |
| 57 | +/// difference between the table's declared schema (`true`) and the actual |
| 58 | +/// batches (`false`). |
| 59 | +fn make_struct_fields(col_a_nullable: bool) -> Fields { |
| 60 | + Fields::from(vec![Field::new("colA", DataType::Boolean, col_a_nullable)]) |
| 61 | +} |
| 62 | + |
| 63 | +/// Returns the schema `(a UInt32 NOT NULL, b Struct("colA" Boolean) NOT NULL)` |
| 64 | +/// with the nested field `b.colA` nullable per `col_a_nullable`. |
| 65 | +/// |
| 66 | +/// See [`make_struct_fields`]. |
| 67 | +fn make_schema(col_a_nullable: bool) -> SchemaRef { |
| 68 | + Arc::new(Schema::new(vec![ |
| 69 | + Field::new("a", DataType::UInt32, false), |
| 70 | + Field::new( |
| 71 | + "b", |
| 72 | + DataType::Struct(make_struct_fields(col_a_nullable)), |
| 73 | + false, |
| 74 | + ), |
| 75 | + ])) |
| 76 | +} |
| 77 | + |
| 78 | +/// Runs a SQL aggregation over a table whose batches are stricter than its |
| 79 | +/// declared schema. |
| 80 | +/// |
| 81 | +/// [`Self::run`] registers table `t(a UInt32, b Struct("colA" Boolean))` |
| 82 | +/// where the declared schema marks the nested field `colA` as nullable, but |
| 83 | +/// the batches carry a stricter, non-nullable `colA`, then runs the query |
| 84 | +/// and returns the collected result. |
| 85 | +struct AggregateBatchesTest { |
| 86 | + /// Number of rows in the table. `a` is `0..num_rows` (so also the number |
| 87 | + /// of groups for `GROUP BY a`) and `b.colA` alternates `true` / `false`. |
| 88 | + num_rows: u32, |
| 89 | + /// If set, the context uses a [`FairSpillPool`] of this size (and a small |
| 90 | + /// batch size) so the aggregation is forced to spill. |
| 91 | + memory_limit: Option<usize>, |
| 92 | +} |
| 93 | + |
| 94 | +impl AggregateBatchesTest { |
| 95 | + fn new() -> Self { |
| 96 | + Self { |
| 97 | + num_rows: 100, |
| 98 | + memory_limit: None, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fn with_num_rows(mut self, num_rows: u32) -> Self { |
| 103 | + self.num_rows = num_rows; |
| 104 | + self |
| 105 | + } |
| 106 | + |
| 107 | + fn with_memory_limit(mut self, memory_limit: usize) -> Self { |
| 108 | + self.memory_limit = Some(memory_limit); |
| 109 | + self |
| 110 | + } |
| 111 | + |
| 112 | + /// Runs `sql` against the table described above and asserts the result |
| 113 | + /// has one output row per group (i.e. [`Self::num_rows`] rows in total). |
| 114 | + async fn run(self, sql: &str) -> Result<()> { |
| 115 | + // The table's declared schema: the nested field `b.colA` is |
| 116 | + // nullable ... |
| 117 | + let declared_schema = make_schema(true); |
| 118 | + |
| 119 | + // ... while the batches are stricter: `b.colA` is non-nullable. |
| 120 | + // `MemTable::try_new` accepts this combination via |
| 121 | + // `Schema::contains`. |
| 122 | + let batch_struct_fields = make_struct_fields(false); |
| 123 | + let batch = RecordBatch::try_new( |
| 124 | + make_schema(false), |
| 125 | + vec![ |
| 126 | + Arc::new(UInt32Array::from_iter_values(0..self.num_rows)), |
| 127 | + Arc::new(StructArray::new( |
| 128 | + batch_struct_fields, |
| 129 | + vec![Arc::new(BooleanArray::from_iter( |
| 130 | + (0..self.num_rows).map(|i| Some(i % 2 == 0)), |
| 131 | + ))], |
| 132 | + None, |
| 133 | + )), |
| 134 | + ], |
| 135 | + )?; |
| 136 | + |
| 137 | + let table = MemTable::try_new(declared_schema, vec![vec![batch]])?; |
| 138 | + |
| 139 | + let ctx = match self.memory_limit { |
| 140 | + Some(limit) => { |
| 141 | + let runtime = RuntimeEnvBuilder::new() |
| 142 | + .with_memory_pool(Arc::new(FairSpillPool::new(limit))) |
| 143 | + .build_arc()?; |
| 144 | + SessionContext::new_with_config_rt( |
| 145 | + SessionConfig::new().with_batch_size(100), |
| 146 | + runtime, |
| 147 | + ) |
| 148 | + } |
| 149 | + None => SessionContext::new(), |
| 150 | + }; |
| 151 | + ctx.register_table("t", Arc::new(table))?; |
| 152 | + |
| 153 | + let result = ctx.sql(sql).await?.collect().await?; |
| 154 | + |
| 155 | + let total_rows: usize = result.iter().map(|batch| batch.num_rows()).sum(); |
| 156 | + assert_eq!(total_rows, self.num_rows as usize); |
| 157 | + Ok(()) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +#[tokio::test] |
| 162 | +async fn array_agg_struct_from_stricter_batches() -> Result<()> { |
| 163 | + AggregateBatchesTest::new() |
| 164 | + .run("SELECT a, array_agg(b) FROM t GROUP BY a") |
| 165 | + .await |
| 166 | +} |
| 167 | + |
| 168 | +#[tokio::test] |
| 169 | +async fn array_agg_distinct_struct_from_stricter_batches() -> Result<()> { |
| 170 | + AggregateBatchesTest::new() |
| 171 | + .run("SELECT a, array_agg(DISTINCT b) FROM t GROUP BY a") |
| 172 | + .await |
| 173 | +} |
| 174 | + |
| 175 | +#[tokio::test] |
| 176 | +async fn array_agg_struct_from_stricter_batches_with_spilling() -> Result<()> { |
| 177 | + AggregateBatchesTest::new() |
| 178 | + .with_num_rows(10_000) |
| 179 | + .with_memory_limit(4_000_000) |
| 180 | + .run("SELECT a, array_agg(b) FROM t GROUP BY a") |
| 181 | + .await |
| 182 | +} |
| 183 | + |
| 184 | +#[tokio::test] |
| 185 | +async fn array_agg_distinct_struct_from_stricter_batches_with_spilling() -> Result<()> { |
| 186 | + AggregateBatchesTest::new() |
| 187 | + .with_num_rows(10_000) |
| 188 | + .with_memory_limit(4_000_000) |
| 189 | + .run("SELECT a, array_agg(DISTINCT b) FROM t GROUP BY a") |
| 190 | + .await |
| 191 | +} |
| 192 | + |
| 193 | +/// Direct unit test for `AggregateExec` boundary adaptation: |
| 194 | +/// Feeds `AggregateExec` directly from a `MemorySourceConfig` whose batches carry |
| 195 | +/// a stricter nested struct nullability than the plan schema without going |
| 196 | +/// through `MemTable`. |
| 197 | +#[tokio::test] |
| 198 | +async fn test_aggregate_exec_direct_input_adaptation() -> Result<()> { |
| 199 | + let declared_schema = make_schema(true); |
| 200 | + let batch_struct_fields = make_struct_fields(false); |
| 201 | + let num_rows = 100_u32; |
| 202 | + let stricter_batch = RecordBatch::try_new( |
| 203 | + make_schema(false), |
| 204 | + vec![ |
| 205 | + Arc::new(UInt32Array::from_iter_values(0..num_rows)), |
| 206 | + Arc::new(StructArray::new( |
| 207 | + batch_struct_fields, |
| 208 | + vec![Arc::new(BooleanArray::from_iter( |
| 209 | + (0..num_rows).map(|i| Some(i % 2 == 0)), |
| 210 | + ))], |
| 211 | + None, |
| 212 | + )), |
| 213 | + ], |
| 214 | + )?; |
| 215 | + |
| 216 | + let input_plan: Arc<dyn ExecutionPlan> = MemorySourceConfig::try_new_exec( |
| 217 | + &[vec![stricter_batch]], |
| 218 | + Arc::clone(&declared_schema), |
| 219 | + None, |
| 220 | + )?; |
| 221 | + |
| 222 | + let grouping_set = |
| 223 | + PhysicalGroupBy::new_single(vec![(col("a", &declared_schema)?, "a".to_string())]); |
| 224 | + let aggregates = vec![Arc::new( |
| 225 | + AggregateExprBuilder::new(array_agg_udaf(), vec![col("b", &declared_schema)?]) |
| 226 | + .schema(Arc::clone(&declared_schema)) |
| 227 | + .alias("array_agg(b)") |
| 228 | + .build()?, |
| 229 | + )]; |
| 230 | + |
| 231 | + let agg_exec = Arc::new(AggregateExec::try_new( |
| 232 | + AggregateMode::Single, |
| 233 | + grouping_set, |
| 234 | + aggregates, |
| 235 | + vec![None], |
| 236 | + input_plan, |
| 237 | + Arc::clone(&declared_schema), |
| 238 | + )?); |
| 239 | + |
| 240 | + let task_ctx = Arc::new(TaskContext::default()); |
| 241 | + let results = collect(agg_exec, task_ctx).await?; |
| 242 | + |
| 243 | + let total_rows: usize = results.iter().map(|b| b.num_rows()).sum(); |
| 244 | + assert_eq!(total_rows, num_rows as usize); |
| 245 | + Ok(()) |
| 246 | +} |
0 commit comments