ella_engine/
plan.rs

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{fmt::Debug, sync::Arc};

use arrow_schema::SchemaRef;
use datafusion::{common::DFSchema, logical_expr::LogicalPlan, prelude::SessionContext};
use datafusion_proto::bytes::{
    logical_plan_from_bytes_with_extension_codec, logical_plan_to_bytes_with_extension_codec,
};

use crate::{codec::RemoteExtensionCodec, engine::EllaState};

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Plan {
    inner: PlanInner,
    definition: Option<String>,
}

impl Plan {
    pub fn definition(&self) -> Option<String> {
        self.definition.clone()
    }

    pub fn with_definition(mut self, definition: String) -> Self {
        self.definition = Some(definition);
        self
    }

    pub fn resolve(&self, state: &EllaState) -> crate::Result<LogicalPlan> {
        self.inner.resolve(state)
    }

    pub fn stub(&self) -> &LogicalPlan {
        self.inner.stub()
    }

    pub fn as_resolved(&self) -> Option<LogicalPlan> {
        match &self.inner {
            PlanInner::Resolved(plan) => Some(plan.clone()),
            PlanInner::Stub(_) => None,
        }
    }

    pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
        Ok(Self {
            inner: PlanInner::from_raw(bytes)?,
            definition: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.inner.to_raw()
    }

    pub fn from_plan(plan: LogicalPlan) -> Self {
        Self {
            inner: PlanInner::Resolved(plan),
            definition: None,
        }
    }

    pub fn from_stub(stub: LogicalPlan) -> Self {
        Self {
            inner: PlanInner::Stub(stub),
            definition: None,
        }
    }

    pub fn arrow_schema(&self) -> SchemaRef {
        (**self.inner.stub().schema()).clone().into()
    }

    pub fn df_schema(&self) -> &Arc<DFSchema> {
        self.inner.stub().schema()
    }

    pub fn map<F>(mut self, f: F) -> Self
    where
        F: FnOnce(LogicalPlan) -> LogicalPlan,
    {
        self.inner = match self.inner {
            PlanInner::Resolved(plan) => PlanInner::Resolved(f(plan)),
            PlanInner::Stub(plan) => PlanInner::Stub(f(plan)),
        };
        self
    }

    pub fn try_map<F, E>(mut self, f: F) -> crate::Result<Self>
    where
        F: FnOnce(LogicalPlan) -> Result<LogicalPlan, E>,
        crate::Error: From<E>,
    {
        self.inner = match self.inner {
            PlanInner::Resolved(plan) => PlanInner::Resolved(f(plan)?),
            PlanInner::Stub(plan) => PlanInner::Stub(f(plan)?),
        };
        Ok(self)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum PlanInner {
    Resolved(LogicalPlan),
    Stub(LogicalPlan),
}

impl PlanInner {
    fn resolve(&self, state: &EllaState) -> crate::Result<LogicalPlan> {
        Ok(match self {
            Self::Resolved(plan) => plan.clone(),
            Self::Stub(plan) => {
                let codec = state.codec();
                let ctx = SessionContext::with_state(state.session().clone());
                let raw = logical_plan_to_bytes_with_extension_codec(plan, &codec)?;
                logical_plan_from_bytes_with_extension_codec(&raw, &ctx, &codec)?
            }
        })
    }

    fn stub(&self) -> &LogicalPlan {
        match self {
            Self::Stub(stub) => stub,
            Self::Resolved(plan) => plan,
        }
    }

    fn to_raw(&self) -> Vec<u8> {
        match self {
            Self::Resolved(plan) | Self::Stub(plan) => {
                let codec = RemoteExtensionCodec {};
                logical_plan_to_bytes_with_extension_codec(plan, &codec)
                    .unwrap()
                    .into()
            }
        }
    }

    fn from_raw(raw: &[u8]) -> crate::Result<Self> {
        let ctx = SessionContext::new();
        let codec = RemoteExtensionCodec {};
        Ok(Self::Stub(logical_plan_from_bytes_with_extension_codec(
            raw, &ctx, &codec,
        )?))
    }
}

impl serde::Serialize for PlanInner {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_bytes(&self.to_raw())
    }
}

impl<'de> serde::Deserialize<'de> for PlanInner {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = <Vec<u8> as serde::Deserialize<'de>>::deserialize(deserializer)?;
        Self::from_raw(&raw).map_err(<D::Error as serde::de::Error>::custom)
    }
}