Apache DataFusion SQL Query Engine
support inter leave node (#8460)
Kun Liue2986f135890
diff --git a/datafusion/proto/proto/datafusion.proto b/datafusion/proto/proto/datafusion.protoindex de7afd5c7b..55fb080423 100644--- a/datafusion/proto/proto/datafusion.proto+++ b/datafusion/proto/proto/datafusion.proto@@ -1163,6 +1163,7 @@ message PhysicalPlanNode {AnalyzeExecNode analyze = 23;JsonSinkExecNode json_sink = 24;SymmetricHashJoinExecNode symmetric_hash_join = 25;+ InterleaveExecNode interleave = 26;}}@@ -1456,6 +1457,10 @@ message SymmetricHashJoinExecNode {JoinFilter filter = 8;}+message InterleaveExecNode {+ repeated PhysicalPlanNode inputs = 1;+}+message UnionExecNode {repeated PhysicalPlanNode inputs = 1;}diff --git a/datafusion/proto/src/generated/pbjson.rs b/datafusion/proto/src/generated/pbjson.rsindex 3001a9c095..dea329cbea 100644--- a/datafusion/proto/src/generated/pbjson.rs+++ b/datafusion/proto/src/generated/pbjson.rs@@ -9244,6 +9244,97 @@ impl<'de> serde::Deserialize<'de> for InListNode {deserializer.deserialize_struct("datafusion.InListNode", FIELDS, GeneratedVisitor)}}+impl serde::Serialize for InterleaveExecNode {+ #[allow(deprecated)]+ fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>+ where+ S: serde::Serializer,+ {+ use serde::ser::SerializeStruct;+ let mut len = 0;+ if !self.inputs.is_empty() {+ len += 1;+ }+ let mut struct_ser = serializer.serialize_struct("datafusion.InterleaveExecNode", len)?;+ if !self.inputs.is_empty() {+ struct_ser.serialize_field("inputs", &self.inputs)?;+ }+ struct_ser.end()+ }+}+impl<'de> serde::Deserialize<'de> for InterleaveExecNode {+ #[allow(deprecated)]+ fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>+ where+ D: serde::Deserializer<'de>,+ {+ const FIELDS: &[&str] = &[+ "inputs",+ ];++ #[allow(clippy::enum_variant_names)]+ enum GeneratedField {+ Inputs,+ }+ impl<'de> serde::Deserialize<'de> for GeneratedField {+ fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>+ where+ D: serde::Deserializer<'de>,+ {+ struct GeneratedVisitor;++ impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {+ type Value = GeneratedField;++ fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(formatter, "expected one of: {:?}", &FIELDS)+ }++ #[allow(unused_variables)]+ fn visit_str<E>(self, value: &str) -> std::result::Result<GeneratedField, E>+ where+ E: serde::de::Error,+ {+ match value {+ "inputs" => Ok(GeneratedField::Inputs),+ _ => Err(serde::de::Error::unknown_field(value, FIELDS)),+ }+ }+ }+ deserializer.deserialize_identifier(GeneratedVisitor)+ }+ }+ struct GeneratedVisitor;+ impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {+ type Value = InterleaveExecNode;++ fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ formatter.write_str("struct datafusion.InterleaveExecNode")+ }++ fn visit_map<V>(self, mut map_: V) -> std::result::Result<InterleaveExecNode, V::Error>+ where+ V: serde::de::MapAccess<'de>,+ {+ let mut inputs__ = None;+ while let Some(k) = map_.next_key()? {+ match k {+ GeneratedField::Inputs => {+ if inputs__.is_some() {+ return Err(serde::de::Error::duplicate_field("inputs"));+ }+ inputs__ = Some(map_.next_value()?);+ }+ }+ }+ Ok(InterleaveExecNode {+ inputs: inputs__.unwrap_or_default(),+ })+ }+ }+ deserializer.deserialize_struct("datafusion.InterleaveExecNode", FIELDS, GeneratedVisitor)+ }+}impl serde::Serialize for IntervalMonthDayNanoValue {#[allow(deprecated)]fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>@@ -17926,6 +18017,9 @@ impl serde::Serialize for PhysicalPlanNode {physical_plan_node::PhysicalPlanType::SymmetricHashJoin(v) => {struct_ser.serialize_field("symmetricHashJoin", v)?;}+ physical_plan_node::PhysicalPlanType::Interleave(v) => {+ struct_ser.serialize_field("interleave", v)?;+ }}}struct_ser.end()@@ -17974,6 +18068,7 @@ impl<'de> serde::Deserialize<'de> for PhysicalPlanNode {"jsonSink","symmetric_hash_join","symmetricHashJoin",+ "interleave",];#[allow(clippy::enum_variant_names)]@@ -18002,6 +18097,7 @@ impl<'de> serde::Deserialize<'de> for PhysicalPlanNode {Analyze,JsonSink,SymmetricHashJoin,+ Interleave,}impl<'de> serde::Deserialize<'de> for GeneratedField {fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>@@ -18047,6 +18143,7 @@ impl<'de> serde::Deserialize<'de> for PhysicalPlanNode {"analyze" => Ok(GeneratedField::Analyze),"jsonSink" | "json_sink" => Ok(GeneratedField::JsonSink),"symmetricHashJoin" | "symmetric_hash_join" => Ok(GeneratedField::SymmetricHashJoin),+ "interleave" => Ok(GeneratedField::Interleave),_ => Err(serde::de::Error::unknown_field(value, FIELDS)),}}@@ -18235,6 +18332,13 @@ impl<'de> serde::Deserialize<'de> for PhysicalPlanNode {return Err(serde::de::Error::duplicate_field("symmetricHashJoin"));}physical_plan_type__ = map_.next_value::<::std::option::Option<_>>()?.map(physical_plan_node::PhysicalPlanType::SymmetricHashJoin)+;+ }+ GeneratedField::Interleave => {+ if physical_plan_type__.is_some() {+ return Err(serde::de::Error::duplicate_field("interleave"));+ }+ physical_plan_type__ = map_.next_value::<::std::option::Option<_>>()?.map(physical_plan_node::PhysicalPlanType::Interleave);}}diff --git a/datafusion/proto/src/generated/prost.rs b/datafusion/proto/src/generated/prost.rsindex cfa424e444..41b94a2a39 100644--- a/datafusion/proto/src/generated/prost.rs+++ b/datafusion/proto/src/generated/prost.rs@@ -1525,7 +1525,7 @@ pub mod owned_table_reference {pub struct PhysicalPlanNode {#[prost(oneof = "physical_plan_node::PhysicalPlanType",- tags = "1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25"+ tags = "1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26")]pub physical_plan_type: ::core::option::Option<physical_plan_node::PhysicalPlanType>,}@@ -1584,6 +1584,8 @@ pub mod physical_plan_node {JsonSink(::prost::alloc::boxed::Box<super::JsonSinkExecNode>),#[prost(message, tag = "25")]SymmetricHashJoin(::prost::alloc::boxed::Box<super::SymmetricHashJoinExecNode>),+ #[prost(message, tag = "26")]+ Interleave(super::InterleaveExecNode),}}#[allow(clippy::derive_partial_eq_without_eq)]@@ -2042,6 +2044,12 @@ pub struct SymmetricHashJoinExecNode {}#[allow(clippy::derive_partial_eq_without_eq)]#[derive(Clone, PartialEq, ::prost::Message)]+pub struct InterleaveExecNode {+ #[prost(message, repeated, tag = "1")]+ pub inputs: ::prost::alloc::vec::Vec<PhysicalPlanNode>,+}+#[allow(clippy::derive_partial_eq_without_eq)]+#[derive(Clone, PartialEq, ::prost::Message)]pub struct UnionExecNode {#[prost(message, repeated, tag = "1")]pub inputs: ::prost::alloc::vec::Vec<PhysicalPlanNode>,diff --git a/datafusion/proto/src/physical_plan/mod.rs b/datafusion/proto/src/physical_plan/mod.rsindex 74c8ec894f..878a5bcb7f 100644--- a/datafusion/proto/src/physical_plan/mod.rs+++ b/datafusion/proto/src/physical_plan/mod.rs@@ -48,7 +48,7 @@ use datafusion::physical_plan::projection::ProjectionExec;use datafusion::physical_plan::repartition::RepartitionExec;use datafusion::physical_plan::sorts::sort::SortExec;use datafusion::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;-use datafusion::physical_plan::union::UnionExec;+use datafusion::physical_plan::union::{InterleaveExec, UnionExec};use datafusion::physical_plan::windows::{BoundedWindowAggExec, WindowAggExec};use datafusion::physical_plan::{udaf, AggregateExpr, ExecutionPlan, InputOrderMode, Partitioning, PhysicalExpr,@@ -545,7 +545,7 @@ impl AsExecutionPlan for PhysicalPlanNode {f.expression.as_ref().ok_or_else(|| {proto_error("Unexpected empty filter expression")})?,- registry, &schema+ registry, &schema,)?;let column_indices = f.column_indices.iter()@@ -556,7 +556,7 @@ impl AsExecutionPlan for PhysicalPlanNode {i.side)))?;- Ok(ColumnIndex{+ Ok(ColumnIndex {index: i.index as usize,side: side.into(),})@@ -634,7 +634,7 @@ impl AsExecutionPlan for PhysicalPlanNode {f.expression.as_ref().ok_or_else(|| {proto_error("Unexpected empty filter expression")})?,- registry, &schema+ registry, &schema,)?;let column_indices = f.column_indices.iter()@@ -645,7 +645,7 @@ impl AsExecutionPlan for PhysicalPlanNode {i.side)))?;- Ok(ColumnIndex{+ Ok(ColumnIndex {index: i.index as usize,side: side.into(),})@@ -693,6 +693,17 @@ impl AsExecutionPlan for PhysicalPlanNode {}Ok(Arc::new(UnionExec::new(inputs)))}+ PhysicalPlanType::Interleave(interleave) => {+ let mut inputs: Vec<Arc<dyn ExecutionPlan>> = vec![];+ for input in &interleave.inputs {+ inputs.push(input.try_into_physical_plan(+ registry,+ runtime,+ extension_codec,+ )?);+ }+ Ok(Arc::new(InterleaveExec::try_new(inputs)?))+ }PhysicalPlanType::CrossJoin(crossjoin) => {let left: Arc<dyn ExecutionPlan> = into_physical_plan(&crossjoin.left,@@ -735,7 +746,7 @@ impl AsExecutionPlan for PhysicalPlanNode {})?.as_ref();Ok(PhysicalSortExpr {- expr: parse_physical_expr(expr,registry, input.schema().as_ref())?,+ expr: parse_physical_expr(expr, registry, input.schema().as_ref())?,options: SortOptions {descending: !sort_expr.asc,nulls_first: sort_expr.nulls_first,@@ -782,7 +793,7 @@ impl AsExecutionPlan for PhysicalPlanNode {})?.as_ref();Ok(PhysicalSortExpr {- expr: parse_physical_expr(expr,registry, input.schema().as_ref())?,+ expr: parse_physical_expr(expr, registry, input.schema().as_ref())?,options: SortOptions {descending: !sort_expr.asc,nulls_first: sort_expr.nulls_first,@@ -845,7 +856,7 @@ impl AsExecutionPlan for PhysicalPlanNode {f.expression.as_ref().ok_or_else(|| {proto_error("Unexpected empty filter expression")})?,- registry, &schema+ registry, &schema,)?;let column_indices = f.column_indices.iter()@@ -856,7 +867,7 @@ impl AsExecutionPlan for PhysicalPlanNode {i.side)))?;- Ok(ColumnIndex{+ Ok(ColumnIndex {index: i.index as usize,side: side.into(),})@@ -1463,6 +1474,21 @@ impl AsExecutionPlan for PhysicalPlanNode {});}+ if let Some(interleave) = plan.downcast_ref::<InterleaveExec>() {+ let mut inputs: Vec<PhysicalPlanNode> = vec![];+ for input in interleave.inputs() {+ inputs.push(protobuf::PhysicalPlanNode::try_from_physical_plan(+ input.to_owned(),+ extension_codec,+ )?);+ }+ return Ok(protobuf::PhysicalPlanNode {+ physical_plan_type: Some(PhysicalPlanType::Interleave(+ protobuf::InterleaveExecNode { inputs },+ )),+ });+ }+if let Some(exec) = plan.downcast_ref::<SortPreservingMergeExec>() {let input = protobuf::PhysicalPlanNode::try_from_physical_plan(exec.input().to_owned(),diff --git a/datafusion/proto/tests/cases/roundtrip_physical_plan.rs b/datafusion/proto/tests/cases/roundtrip_physical_plan.rsindex 287207bae5..f46a29447d 100644--- a/datafusion/proto/tests/cases/roundtrip_physical_plan.rs+++ b/datafusion/proto/tests/cases/roundtrip_physical_plan.rs@@ -50,12 +50,14 @@ use datafusion::physical_plan::joins::{};use datafusion::physical_plan::limit::{GlobalLimitExec, LocalLimitExec};use datafusion::physical_plan::projection::ProjectionExec;+use datafusion::physical_plan::repartition::RepartitionExec;use datafusion::physical_plan::sorts::sort::SortExec;+use datafusion::physical_plan::union::{InterleaveExec, UnionExec};use datafusion::physical_plan::windows::{BuiltInWindowExpr, PlainAggregateWindowExpr, WindowAggExec,};use datafusion::physical_plan::{- functions, udaf, AggregateExpr, ExecutionPlan, PhysicalExpr, Statistics,+ functions, udaf, AggregateExpr, ExecutionPlan, Partitioning, PhysicalExpr, Statistics,};use datafusion::prelude::SessionContext;use datafusion::scalar::ScalarValue;@@ -231,21 +233,21 @@ fn roundtrip_window() -> Result<()> {};let builtin_window_expr = Arc::new(BuiltInWindowExpr::new(- Arc::new(NthValue::first(- "FIRST_VALUE(a) PARTITION BY [b] ORDER BY [a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW",- col("a", &schema)?,- DataType::Int64,- )),- &[col("b", &schema)?],- &[PhysicalSortExpr {- expr: col("a", &schema)?,- options: SortOptions {- descending: false,- nulls_first: false,- },- }],- Arc::new(window_frame),- ));+ Arc::new(NthValue::first(+ "FIRST_VALUE(a) PARTITION BY [b] ORDER BY [a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW",+ col("a", &schema)?,+ DataType::Int64,+ )),+ &[col("b", &schema)?],+ &[PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: SortOptions {+ descending: false,+ nulls_first: false,+ },+ }],+ Arc::new(window_frame),+ ));let plain_aggr_window_expr = Arc::new(PlainAggregateWindowExpr::new(Arc::new(Avg::new(@@ -798,3 +800,34 @@ fn roundtrip_sym_hash_join() -> Result<()> {}Ok(())}++#[test]+fn roundtrip_union() -> Result<()> {+ let field_a = Field::new("col", DataType::Int64, false);+ let schema_left = Schema::new(vec![field_a.clone()]);+ let schema_right = Schema::new(vec![field_a]);+ let left = EmptyExec::new(false, Arc::new(schema_left));+ let right = EmptyExec::new(false, Arc::new(schema_right));+ let inputs: Vec<Arc<dyn ExecutionPlan>> = vec![Arc::new(left), Arc::new(right)];+ let union = UnionExec::new(inputs);+ roundtrip_test(Arc::new(union))+}++#[test]+fn roundtrip_interleave() -> Result<()> {+ let field_a = Field::new("col", DataType::Int64, false);+ let schema_left = Schema::new(vec![field_a.clone()]);+ let schema_right = Schema::new(vec![field_a]);+ let partition = Partitioning::Hash(vec![], 3);+ let left = RepartitionExec::try_new(+ Arc::new(EmptyExec::new(false, Arc::new(schema_left))),+ partition.clone(),+ )?;+ let right = RepartitionExec::try_new(+ Arc::new(EmptyExec::new(false, Arc::new(schema_right))),+ partition.clone(),+ )?;+ let inputs: Vec<Arc<dyn ExecutionPlan>> = vec![Arc::new(left), Arc::new(right)];+ let interleave = InterleaveExec::try_new(inputs)?;+ roundtrip_test(Arc::new(interleave))+}