Skip to content

Commit 0359a3c

Browse files
authored
fix[physical-plan/aggregates]: fix grouping by Ree<Dict> (#21195)
There was a match arm missing, so the inner dictionary was not reconstructed, resulting in a schema mismatch downstream. ## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #21194 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Fix a bug ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Match arm + memtable test verifying the fix ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> Queries that would previously error now do not Signed-off-by: Alfonso Subiotto Marques <alfonso.subiotto@polarsignals.com>
1 parent 1e3b956 commit 0359a3c

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

  • datafusion
    • core/tests/sql/aggregates
    • physical-plan/src/aggregates/group_values

datafusion/core/tests/sql/aggregates/basic.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,58 @@ async fn count_distinct_dictionary_mixed_values() -> Result<()> {
441441

442442
Ok(())
443443
}
444+
445+
#[tokio::test]
446+
async fn group_by_ree_dict_column() -> Result<()> {
447+
let ctx = SessionContext::new();
448+
449+
let run_ends = Int32Array::from(vec![2, 4, 5]);
450+
let dict = DictionaryArray::new(
451+
UInt32Array::from(vec![0, 1, 2]),
452+
Arc::new(StringArray::from(vec!["alpha", "beta", "gamma"])),
453+
);
454+
let ree_col = RunArray::<Int32Type>::try_new(&run_ends, &dict).unwrap();
455+
let value_col = Int32Array::from(vec![1, 2, 3, 4, 5]);
456+
457+
let dict_type =
458+
DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8));
459+
let schema = Arc::new(Schema::new(vec![
460+
Field::new(
461+
"group_col",
462+
DataType::RunEndEncoded(
463+
Arc::new(Field::new("run_ends", DataType::Int32, false)),
464+
Arc::new(Field::new("values", dict_type, true)),
465+
),
466+
true,
467+
),
468+
Field::new("value", DataType::Int32, false),
469+
]));
470+
471+
let batch = RecordBatch::try_new(
472+
schema.clone(),
473+
vec![Arc::new(ree_col), Arc::new(value_col)],
474+
)?;
475+
let table = MemTable::try_new(schema, vec![vec![batch]])?;
476+
ctx.register_table("t", Arc::new(table))?;
477+
478+
let results = ctx
479+
.sql("SELECT group_col, SUM(value) as total FROM t GROUP BY group_col ORDER BY group_col")
480+
.await?
481+
.collect()
482+
.await?;
483+
484+
assert_snapshot!(
485+
batches_to_string(&results),
486+
@r"
487+
+-----------+-------+
488+
| group_col | total |
489+
+-----------+-------+
490+
| alpha | 3 |
491+
| beta | 7 |
492+
| gamma | 5 |
493+
+-----------+-------+
494+
"
495+
);
496+
497+
Ok(())
498+
}

datafusion/physical-plan/src/aggregates/group_values/row.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
// under the License.
1717

1818
use crate::aggregates::group_values::GroupValues;
19-
use arrow::array::{Array, ArrayRef, ListArray, StructArray};
19+
use arrow::array::{
20+
Array, ArrayRef, ListArray, PrimitiveArray, RunArray, StructArray,
21+
downcast_run_end_index,
22+
};
2023
use arrow::compute::cast;
2124
use arrow::datatypes::{DataType, SchemaRef};
2225
use arrow::row::{RowConverter, Rows, SortField};
@@ -291,6 +294,33 @@ fn dictionary_encode_if_necessary(
291294
)?))
292295
}
293296
(DataType::Dictionary(_, _), _) => Ok(cast(array.as_ref(), expected)?),
297+
(
298+
DataType::RunEndEncoded(run_ends_field, expected_values_field),
299+
&DataType::RunEndEncoded(_, _),
300+
) => {
301+
macro_rules! reencode_ree {
302+
($run_end_type:ty) => {{
303+
let run_array = array
304+
.as_any()
305+
.downcast_ref::<RunArray<$run_end_type>>()
306+
.unwrap();
307+
let values = dictionary_encode_if_necessary(
308+
&(Arc::clone(run_array.values()) as ArrayRef),
309+
expected_values_field.data_type(),
310+
)?;
311+
let run_ends = PrimitiveArray::<$run_end_type>::new(
312+
run_array.run_ends().inner().clone(),
313+
None,
314+
);
315+
Ok(Arc::new(RunArray::try_new(&run_ends, &values)?))
316+
}};
317+
}
318+
downcast_run_end_index! {
319+
run_ends_field.data_type() => (reencode_ree),
320+
_ => unreachable!("unsupported run end type: {}", run_ends_field.data_type()),
321+
}
322+
}
323+
(DataType::RunEndEncoded(_, _), _) => Ok(cast(array.as_ref(), expected)?),
294324
(_, _) => Ok(Arc::<dyn Array>::clone(array)),
295325
}
296326
}

0 commit comments

Comments
 (0)