Skip to content

Instantly share code, notes, and snippets.

@ice1000
Last active September 6, 2022 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ice1000/9bfe2596f3cefd0e8304d7df6f4933eb to your computer and use it in GitHub Desktop.
Save ice1000/9bfe2596f3cefd0e8304d7df6f4933eb to your computer and use it in GitHub Desktop.
PlanNode refactor demo
#![allow(dead_code)]
#[derive(Default)]
pub struct Schema {
fields: Vec<String>,
}
pub struct Order();
macro_rules! impl_node {
($base:ident, $($t:ident),*) => {
pub enum Node {
$($t(Box<$t>),)*
}
$(
impl Into<Node> for $t {
fn into(self) -> Node {
Node::$t(Box::new(self))
}
}
)*
pub type PlanRef = std::rc::Rc<($base, Node)>;
};
}
pub mod batch {
use crate::{Order, Schema};
pub struct Base {
pub id: u32,
pub schema: Schema,
pub order: Order,
}
impl Base {
pub fn test_new() -> Self {
Self {
id: 114,
schema: Default::default(),
order: Order(),
}
}
}
pub struct Scan {
pub scan: crate::logical::Scan,
pub range: Vec<(usize, usize)>,
}
impl_node!(Base, Scan);
}
pub mod stream {
use crate::Schema;
pub struct Base {
pub id: u32,
pub schema: Schema,
pub append_only: bool,
pub logical_pk: Vec<usize>,
}
impl Base {
pub fn test_new() -> Self {
Self {
id: 114,
schema: Default::default(),
append_only: false,
logical_pk: Default::default(),
}
}
}
pub struct Scan {
pub scan: crate::logical::Scan,
pub id: u32,
}
impl_node!(Base, Scan);
}
/// Logical nodes
pub mod logical {
use std::rc::Rc;
use crate::Schema;
pub struct Base {
pub id: u32,
pub schema: Schema,
pub logical_pk: Vec<usize>,
}
impl Base {
pub fn test_new() -> Self {
Self {
id: 514,
schema: Default::default(),
logical_pk: Default::default(),
}
}
}
pub struct Delete {
pub table_source_name: String,
pub source_id: u32,
pub associated_mview_id: u32,
pub input: PlanRef,
}
pub struct Scan {
pub table_name: String,
is_sys_table: bool,
/// Include `output_col_idx` and columns required in `predicate`
required_col_idx: Vec<usize>,
output_col_idx: Vec<usize>,
// ...
}
impl_node!(Base, Delete, Scan);
pub fn demo1() {
let tt: PlanRef = Rc::new((
Base::test_new(),
Scan {
table_name: "test".to_string(),
is_sys_table: false,
required_col_idx: Default::default(),
output_col_idx: Default::default(),
}
.into(),
));
let tt: PlanRef = Rc::new((
Base::test_new(),
Delete {
table_source_name: "test".to_string(),
source_id: 114,
associated_mview_id: 514,
input: tt,
}
.into(),
));
match &tt.1 {
Node::Delete(d) => assert_eq!(d.source_id, 114),
_ => panic!("wrong type"),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment