1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::spark;

use spark::relation::RelType;
use spark::Relation;
use spark::RelationCommon;

/// Implements a struct to hold the current [Relation]
/// which represents an unresolved Logical Plan
#[derive(Clone, Debug)]
pub struct LogicalPlanBuilder {
    /// A [Relation] object that contains the unresolved
    /// logical plan
    pub relation: Relation,
}

impl LogicalPlanBuilder {
    /// Create a new Logical Plan from an initial [Relation]
    pub fn new(relation: Relation) -> Self {
        Self { relation }
    }

    pub fn relation_input(self) -> Option<Box<Relation>> {
        Some(Box::new(self.relation))
    }

    /// Build the Spark [spark::Plan] for a [Relation]
    pub fn build_plan_root(self) -> spark::Plan {
        spark::Plan {
            op_type: Some(spark::plan::OpType::Root(self.relation)),
        }
    }

    /// Build the Spark [spark::Plan] for a [spark::command::CommandType]
    pub fn build_plan_cmd(self, command_type: spark::command::CommandType) -> spark::Plan {
        let cmd = spark::Command {
            command_type: Some(command_type),
        };

        spark::Plan {
            op_type: Some(spark::plan::OpType::Command(cmd)),
        }
    }

    /// Create a relation from an existing [LogicalPlanBuilder]
    /// this will add additional actions to the [Relation]
    pub fn from(&mut self, rel_type: RelType) -> LogicalPlanBuilder {
        let relation = Relation {
            common: Some(RelationCommon {
                source_info: "NA".to_string(),
                plan_id: Some(1),
            }),
            rel_type: Some(rel_type),
        };

        LogicalPlanBuilder { relation }
    }
}