MCPcopy Create free account
hub / github.com/apache/datafusion / rewrite

Method rewrite

datafusion/optimizer/src/optimize_unions.rs:54–99  ·  view source on GitHub ↗
(
        &self,
        plan: LogicalPlan,
        _config: &dyn OptimizerConfig,
    )

Source from the content-addressed store, hash-verified

52 }
53
54 fn rewrite(
55 &self,
56 plan: LogicalPlan,
57 _config: &dyn OptimizerConfig,
58 ) -> Result<Transformed<LogicalPlan>> {
59 match plan {
60 LogicalPlan::Union(Union { mut inputs, .. }) if inputs.len() == 1 => Ok(
61 Transformed::yes(Arc::unwrap_or_clone(inputs.pop().unwrap())),
62 ),
63 LogicalPlan::Union(Union { inputs, schema }) => {
64 let inputs = inputs
65 .into_iter()
66 .flat_map(extract_plans_from_union)
67 .map(|plan| Ok(Arc::new(coerce_plan_expr_for_schema(plan, &schema)?)))
68 .collect::<Result<Vec<_>>>()?;
69
70 Ok(Transformed::yes(LogicalPlan::Union(Union {
71 inputs,
72 schema,
73 })))
74 }
75 LogicalPlan::Distinct(Distinct::All(nested_plan)) => {
76 match Arc::unwrap_or_clone(nested_plan) {
77 LogicalPlan::Union(Union { inputs, schema }) => {
78 let inputs = inputs
79 .into_iter()
80 .map(extract_plan_from_distinct)
81 .flat_map(extract_plans_from_union)
82 .map(|plan| coerce_plan_expr_for_schema(plan, &schema))
83 .collect::<Result<Vec<_>>>()?;
84
85 Ok(Transformed::yes(LogicalPlan::Distinct(Distinct::All(
86 Arc::new(LogicalPlan::Union(Union {
87 inputs: inputs.into_iter().map(Arc::new).collect_vec(),
88 schema: Arc::clone(&schema),
89 })),
90 ))))
91 }
92 nested_plan => Ok(Transformed::no(LogicalPlan::Distinct(
93 Distinct::All(Arc::new(nested_plan)),
94 ))),
95 }
96 }
97 _ => Ok(Transformed::no(plan)),
98 }
99 }
100}
101
102fn extract_plans_from_union(plan: Arc<LogicalPlan>) -> Vec<LogicalPlan> {

Callers

nothing calls this directly

Calls 8

newFunction · 0.85
DistinctEnum · 0.85
UnionClass · 0.50
lenMethod · 0.45
popMethod · 0.45
mapMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected