Skip to content

Commit 0bb42c4

Browse files
committed
refactor(proto): colocate aggregate serde tests
AggregateExec now owns its protobuf conversion, so edge-case tests and serialization rationale belong beside the implementation. Keeping these constraints local makes the self-serialization migration safer while the deprecated central serializer remains available. Refs apache#23512 Signed-off-by: Jiawei Zhao <[email protected]>
1 parent e666321 commit 0bb42c4

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

  • datafusion/physical-plan/src/aggregates

‎datafusion/physical-plan/src/aggregates/mod.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,8 @@ impl ExecutionPlan for AggregateExec {
22102210
})
22112211
})
22122212
.collect::<Result<Vec<_>>>()?;
2213+
// Match by name because the protobuf and execution enums use different
2214+
// discriminants, so a numeric cast would corrupt the wire format.
22132215
let mode = match self.mode() {
22142216
AggregateMode::Partial => protobuf::AggregateMode::Partial,
22152217
AggregateMode::Final => protobuf::AggregateMode::Final,
@@ -2257,6 +2259,8 @@ impl ExecutionPlan for AggregateExec {
22572259
}
22582260
}
22592261

2262+
/// Keep this marker byte-identical to the copy used by the deprecated
2263+
/// aggregate serializer in `datafusion-proto` until that path is removed.
22602264
#[cfg(feature = "proto")]
22612265
const HUMAN_DISPLAY_ALIAS_PREFIX: &str = "\u{1f}datafusion_human_display_alias_v1:";
22622266

@@ -2308,6 +2312,7 @@ fn encode_aggregate_expr(
23082312
})
23092313
.collect::<Result<Vec<_>>>()?;
23102314
let name = aggr_expr.fun().name().to_string();
2315+
// The context already applies `(!buf.is_empty()).then_some(buf)`.
23112316
let fun_definition = ctx.encode_udaf(aggr_expr.fun())?;
23122317
let human_display = match (aggr_expr.human_display(), aggr_expr.human_display_alias())
23132318
{
@@ -2361,6 +2366,8 @@ impl AggregateExec {
23612366
"AggregateExec",
23622367
"input",
23632368
)?;
2369+
// Match by name because the protobuf and execution enums use different
2370+
// discriminants, so a numeric cast would corrupt the wire format.
23642371
let mode = protobuf::AggregateMode::try_from(hash_agg.mode).map_err(|_| {
23652372
datafusion_common::internal_datafusion_err!(
23662373
"Received an AggregateNode message with unknown AggregateMode {}",
@@ -2378,6 +2385,7 @@ impl AggregateExec {
23782385
protobuf::AggregateMode::PartialReduce => AggregateMode::PartialReduce,
23792386
};
23802387
let num_expr = hash_agg.group_expr.len();
2388+
// Grouping expressions refer to the child plan's output schema.
23812389
let child_schema = input.schema();
23822390
let group_expr = hash_agg
23832391
.group_expr
@@ -2410,6 +2418,8 @@ impl AggregateExec {
24102418
.map(|group| group.to_vec())
24112419
.collect()
24122420
};
2421+
// Aggregate arguments, ordering, filters, and dynamic filters refer to
2422+
// the aggregate input schema carried in the protobuf node.
24132423
let input_schema = hash_agg.input_schema.as_ref().ok_or_else(|| {
24142424
datafusion_common::internal_datafusion_err!(
24152425
"input_schema in AggregateNode is missing."
@@ -2472,6 +2482,8 @@ impl AggregateExec {
24722482
"Invalid AggregateExpr, missing aggregate_function"
24732483
);
24742484
};
2485+
// The context owns the payload-to-codec and
2486+
// registry-to-codec fallback order.
24752487
let udaf = ctx.decode_udaf(
24762488
udaf_name,
24772489
aggregate.fun_definition.as_deref(),
@@ -3075,6 +3087,28 @@ mod tests {
30753087
use futures::{FutureExt, Stream, StreamExt};
30763088
use insta::{allow_duplicates, assert_snapshot};
30773089

3090+
#[cfg(feature = "proto")]
3091+
#[test]
3092+
fn split_human_display_alias_ignores_mismatched_alias() {
3093+
let encoded = encode_human_display_alias("sum(value)", "revenue");
3094+
3095+
assert_eq!(
3096+
split_human_display_alias(&encoded, "other"),
3097+
(encoded.as_str(), None)
3098+
);
3099+
}
3100+
3101+
#[cfg(feature = "proto")]
3102+
#[test]
3103+
fn split_human_display_alias_keeps_malformed_prefix_literal() {
3104+
let display = format!("{HUMAN_DISPLAY_ALIAS_PREFIX}not-an-encoding");
3105+
3106+
assert_eq!(
3107+
split_human_display_alias(&display, "agg"),
3108+
(display.as_str(), None)
3109+
);
3110+
}
3111+
30783112
// Generate a schema which consists of 5 columns (a, b, c, d, e)
30793113
fn create_test_schema() -> Result<SchemaRef> {
30803114
let a = Field::new("a", DataType::Int32, true);

0 commit comments

Comments
 (0)