Skip to content

Commit 14d6706

Browse files
committed
refactor(proto): migrate symmetric hash join serde
SymmetricHashJoinExec serialization still depends on centralized plan dispatch, keeping its join, streaming-mode, and ordering wire logic outside the plan that owns the state. The retained compatibility helpers also duplicate that logic and can drift from normal dispatch. Move encoding and decoding behind the plan's ExecutionPlan hooks, route central dispatch through them, and reduce the deprecated helpers to thin adapters. Preserve explicit enum and optional-ordering representations, and expand round-trip coverage across the serialized join state. Closes apache#23509 Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 1763417 commit 14d6706

3 files changed

Lines changed: 427 additions & 272 deletions

File tree

‎datafusion/physical-plan/src/joins/symmetric_hash_join.rs‎

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,309 @@ impl ExecutionPlan for SymmetricHashJoinExec {
627627
Ok(None)
628628
}
629629
}
630+
631+
#[cfg(feature = "proto")]
632+
fn try_to_proto(
633+
&self,
634+
ctx: &crate::proto::ExecutionPlanEncodeCtx<'_>,
635+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalPlanNode>> {
636+
use datafusion_proto_models::protobuf;
637+
638+
let left = ctx.encode_child(self.left())?;
639+
let right = ctx.encode_child(self.right())?;
640+
let on = self
641+
.on()
642+
.iter()
643+
.map(|(left, right)| {
644+
Ok(protobuf::JoinOn {
645+
left: Some(ctx.encode_expr(left)?),
646+
right: Some(ctx.encode_expr(right)?),
647+
})
648+
})
649+
.collect::<Result<Vec<_>>>()?;
650+
651+
let join_type = match self.join_type() {
652+
JoinType::Inner => protobuf::JoinType::Inner,
653+
JoinType::Left => protobuf::JoinType::Left,
654+
JoinType::Right => protobuf::JoinType::Right,
655+
JoinType::Full => protobuf::JoinType::Full,
656+
JoinType::LeftSemi => protobuf::JoinType::Leftsemi,
657+
JoinType::RightSemi => protobuf::JoinType::Rightsemi,
658+
JoinType::LeftAnti => protobuf::JoinType::Leftanti,
659+
JoinType::RightAnti => protobuf::JoinType::Rightanti,
660+
JoinType::LeftMark => protobuf::JoinType::Leftmark,
661+
JoinType::RightMark => protobuf::JoinType::Rightmark,
662+
};
663+
let null_equality = match self.null_equality() {
664+
NullEquality::NullEqualsNothing => protobuf::NullEquality::NullEqualsNothing,
665+
NullEquality::NullEqualsNull => protobuf::NullEquality::NullEqualsNull,
666+
};
667+
let partition_mode = match self.partition_mode() {
668+
StreamJoinPartitionMode::SinglePartition => {
669+
protobuf::StreamPartitionMode::SinglePartition
670+
}
671+
StreamJoinPartitionMode::Partitioned => {
672+
protobuf::StreamPartitionMode::PartitionedExec
673+
}
674+
};
675+
let filter = self
676+
.filter()
677+
.map(|filter| -> Result<protobuf::JoinFilter> {
678+
let expression = ctx.encode_expr(filter.expression())?;
679+
let column_indices = filter
680+
.column_indices()
681+
.iter()
682+
.map(|column_index| {
683+
let side = match column_index.side {
684+
JoinSide::Left => protobuf::JoinSide::LeftSide,
685+
JoinSide::Right => protobuf::JoinSide::RightSide,
686+
JoinSide::None => protobuf::JoinSide::None,
687+
};
688+
protobuf::ColumnIndex {
689+
index: column_index.index as u32,
690+
side: side.into(),
691+
}
692+
})
693+
.collect();
694+
Ok(protobuf::JoinFilter {
695+
expression: Some(expression),
696+
column_indices,
697+
schema: Some(filter.schema().as_ref().try_into()?),
698+
})
699+
})
700+
.transpose()?;
701+
let encode_sort_exprs =
702+
|exprs: Option<&LexOrdering>| -> Result<Vec<protobuf::PhysicalSortExprNode>> {
703+
exprs
704+
.map(|exprs| {
705+
exprs
706+
.iter()
707+
.map(|expr| {
708+
Ok(protobuf::PhysicalSortExprNode {
709+
expr: Some(Box::new(ctx.encode_expr(&expr.expr)?)),
710+
asc: !expr.options.descending,
711+
nulls_first: expr.options.nulls_first,
712+
})
713+
})
714+
.collect::<Result<Vec<_>>>()
715+
})
716+
.transpose()
717+
.map(Option::unwrap_or_default)
718+
};
719+
let left_sort_exprs = encode_sort_exprs(self.left_sort_exprs())?;
720+
let right_sort_exprs = encode_sort_exprs(self.right_sort_exprs())?;
721+
722+
Ok(Some(protobuf::PhysicalPlanNode {
723+
physical_plan_type: Some(
724+
protobuf::physical_plan_node::PhysicalPlanType::SymmetricHashJoin(
725+
Box::new(protobuf::SymmetricHashJoinExecNode {
726+
left: Some(Box::new(left)),
727+
right: Some(Box::new(right)),
728+
on,
729+
join_type: join_type.into(),
730+
partition_mode: partition_mode.into(),
731+
null_equality: null_equality.into(),
732+
filter,
733+
left_sort_exprs,
734+
right_sort_exprs,
735+
}),
736+
),
737+
),
738+
}))
739+
}
740+
}
741+
742+
#[cfg(feature = "proto")]
743+
impl SymmetricHashJoinExec {
744+
/// Reconstruct a [`SymmetricHashJoinExec`] from its protobuf representation.
745+
///
746+
/// The exact inverse of [`ExecutionPlan::try_to_proto`].
747+
///
748+
/// [`ExecutionPlan::try_to_proto`]: crate::ExecutionPlan::try_to_proto
749+
pub fn try_from_proto(
750+
node: &datafusion_proto_models::protobuf::PhysicalPlanNode,
751+
ctx: &crate::proto::ExecutionPlanDecodeCtx<'_>,
752+
) -> Result<Arc<dyn ExecutionPlan>> {
753+
use datafusion_common::internal_datafusion_err;
754+
use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr;
755+
use datafusion_proto_models::protobuf;
756+
757+
let sym_join = crate::expect_plan_variant!(
758+
node,
759+
protobuf::physical_plan_node::PhysicalPlanType::SymmetricHashJoin,
760+
"SymmetricHashJoinExec",
761+
);
762+
let left = ctx.decode_required_child(
763+
sym_join.left.as_deref(),
764+
"SymmetricHashJoinExec",
765+
"left",
766+
)?;
767+
let right = ctx.decode_required_child(
768+
sym_join.right.as_deref(),
769+
"SymmetricHashJoinExec",
770+
"right",
771+
)?;
772+
let left_schema = left.schema();
773+
let right_schema = right.schema();
774+
let on = sym_join
775+
.on
776+
.iter()
777+
.map(|columns| {
778+
let left = ctx.decode_required_expr(
779+
columns.left.as_ref(),
780+
left_schema.as_ref(),
781+
"SymmetricHashJoinExec",
782+
"on.left",
783+
)?;
784+
let right = ctx.decode_required_expr(
785+
columns.right.as_ref(),
786+
right_schema.as_ref(),
787+
"SymmetricHashJoinExec",
788+
"on.right",
789+
)?;
790+
Ok((left, right))
791+
})
792+
.collect::<Result<JoinOn>>()?;
793+
794+
let join_type =
795+
match protobuf::JoinType::try_from(sym_join.join_type).map_err(|_| {
796+
internal_datafusion_err!(
797+
"SymmetricHashJoinExec: unknown JoinType {}",
798+
sym_join.join_type
799+
)
800+
})? {
801+
protobuf::JoinType::Inner => JoinType::Inner,
802+
protobuf::JoinType::Left => JoinType::Left,
803+
protobuf::JoinType::Right => JoinType::Right,
804+
protobuf::JoinType::Full => JoinType::Full,
805+
protobuf::JoinType::Leftsemi => JoinType::LeftSemi,
806+
protobuf::JoinType::Rightsemi => JoinType::RightSemi,
807+
protobuf::JoinType::Leftanti => JoinType::LeftAnti,
808+
protobuf::JoinType::Rightanti => JoinType::RightAnti,
809+
protobuf::JoinType::Leftmark => JoinType::LeftMark,
810+
protobuf::JoinType::Rightmark => JoinType::RightMark,
811+
};
812+
let null_equality = match protobuf::NullEquality::try_from(sym_join.null_equality)
813+
.map_err(|_| {
814+
internal_datafusion_err!(
815+
"SymmetricHashJoinExec: unknown NullEquality {}",
816+
sym_join.null_equality
817+
)
818+
})? {
819+
protobuf::NullEquality::NullEqualsNothing => NullEquality::NullEqualsNothing,
820+
protobuf::NullEquality::NullEqualsNull => NullEquality::NullEqualsNull,
821+
};
822+
let partition_mode =
823+
match protobuf::StreamPartitionMode::try_from(sym_join.partition_mode)
824+
.map_err(|_| {
825+
internal_datafusion_err!(
826+
"SymmetricHashJoinExec: unknown StreamPartitionMode {}",
827+
sym_join.partition_mode
828+
)
829+
})? {
830+
protobuf::StreamPartitionMode::SinglePartition => {
831+
StreamJoinPartitionMode::SinglePartition
832+
}
833+
protobuf::StreamPartitionMode::PartitionedExec => {
834+
StreamJoinPartitionMode::Partitioned
835+
}
836+
};
837+
let filter = sym_join
838+
.filter
839+
.as_ref()
840+
.map(|filter| -> Result<JoinFilter> {
841+
let schema: Schema = filter
842+
.schema
843+
.as_ref()
844+
.ok_or_else(|| {
845+
internal_datafusion_err!(
846+
"SymmetricHashJoinExec: JoinFilter missing schema"
847+
)
848+
})?
849+
.try_into()?;
850+
let expression = ctx.decode_required_expr(
851+
filter.expression.as_ref(),
852+
&schema,
853+
"SymmetricHashJoinExec",
854+
"filter.expression",
855+
)?;
856+
let column_indices = filter
857+
.column_indices
858+
.iter()
859+
.map(|column_index| {
860+
let side = protobuf::JoinSide::try_from(column_index.side)
861+
.map_err(|_| {
862+
internal_datafusion_err!(
863+
"SymmetricHashJoinExec: unknown JoinSide {}",
864+
column_index.side
865+
)
866+
})?;
867+
let side = match side {
868+
protobuf::JoinSide::LeftSide => JoinSide::Left,
869+
protobuf::JoinSide::RightSide => JoinSide::Right,
870+
protobuf::JoinSide::None => JoinSide::None,
871+
};
872+
Ok(ColumnIndex {
873+
index: column_index.index as usize,
874+
side,
875+
})
876+
})
877+
.collect::<Result<Vec<_>>>()?;
878+
Ok(JoinFilter::new(
879+
expression,
880+
column_indices,
881+
Arc::new(schema),
882+
))
883+
})
884+
.transpose()?;
885+
let decode_sort_exprs = |sort_exprs: &[protobuf::PhysicalSortExprNode],
886+
schema: &Schema,
887+
field: &str|
888+
-> Result<Option<LexOrdering>> {
889+
let sort_exprs = sort_exprs
890+
.iter()
891+
.map(|sort_expr| {
892+
let expr = ctx.decode_required_expr(
893+
sort_expr.expr.as_deref(),
894+
schema,
895+
"SymmetricHashJoinExec",
896+
field,
897+
)?;
898+
Ok(PhysicalSortExpr {
899+
expr,
900+
options: arrow::compute::SortOptions {
901+
descending: !sort_expr.asc,
902+
nulls_first: sort_expr.nulls_first,
903+
},
904+
})
905+
})
906+
.collect::<Result<Vec<_>>>()?;
907+
Ok(LexOrdering::new(sort_exprs))
908+
};
909+
let left_sort_exprs = decode_sort_exprs(
910+
&sym_join.left_sort_exprs,
911+
left_schema.as_ref(),
912+
"left_sort_exprs",
913+
)?;
914+
let right_sort_exprs = decode_sort_exprs(
915+
&sym_join.right_sort_exprs,
916+
right_schema.as_ref(),
917+
"right_sort_exprs",
918+
)?;
919+
920+
Self::try_new(
921+
left,
922+
right,
923+
on,
924+
filter,
925+
&join_type,
926+
null_equality,
927+
left_sort_exprs,
928+
right_sort_exprs,
929+
partition_mode,
930+
)
931+
.map(|exec| Arc::new(exec) as _)
932+
}
630933
}
631934

632935
/// A stream that issues [RecordBatch]es as they arrive from the right of the join.

0 commit comments

Comments
 (0)