|
| 1 | +//! Definition of physical expressions / operators in the Cascades query optimization framework. |
| 2 | +//! |
| 3 | +//! FIXME: All fields are placeholders, and group IDs are just represented as i32 for now. |
| 4 | +//! |
| 5 | +//! TODO figure out if each operator should be in a different submodule. |
| 6 | +//! TODO This entire file is a WIP. |
| 7 | +
|
| 8 | +use crate::entities::*; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | + |
| 11 | +#[derive(Clone, Debug)] |
| 12 | +pub enum PhysicalExpression { |
| 13 | + TableScan(TableScan), |
| 14 | + Filter(PhysicalFilter), |
| 15 | + HashJoin(HashJoin), |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
| 19 | +pub struct TableScan { |
| 20 | + table_schema: String, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
| 24 | +pub struct PhysicalFilter { |
| 25 | + child: i32, |
| 26 | + expression: String, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
| 30 | +pub struct HashJoin { |
| 31 | + left: i32, |
| 32 | + right: i32, |
| 33 | + expression: String, |
| 34 | +} |
| 35 | + |
| 36 | +/// TODO Use a macro instead. |
| 37 | +impl From<physical_expression::Model> for PhysicalExpression { |
| 38 | + fn from(value: physical_expression::Model) -> Self { |
| 39 | + match value.kind { |
| 40 | + 0 => Self::TableScan( |
| 41 | + serde_json::from_value(value.data) |
| 42 | + .expect("unable to deserialize data into a physical `TableScan`"), |
| 43 | + ), |
| 44 | + 1 => Self::Filter( |
| 45 | + serde_json::from_value(value.data) |
| 46 | + .expect("Unable to deserialize data into a physical `Filter`"), |
| 47 | + ), |
| 48 | + 2 => Self::HashJoin( |
| 49 | + serde_json::from_value(value.data) |
| 50 | + .expect("Unable to deserialize data into a physical `HashJoin`"), |
| 51 | + ), |
| 52 | + _ => panic!(), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// TODO Use a macro instead. |
| 58 | +impl From<PhysicalExpression> for physical_expression::Model { |
| 59 | + fn from(value: PhysicalExpression) -> physical_expression::Model { |
| 60 | + fn create_physical_expression( |
| 61 | + kind: i16, |
| 62 | + data: serde_json::Value, |
| 63 | + ) -> physical_expression::Model { |
| 64 | + physical_expression::Model { |
| 65 | + id: -1, |
| 66 | + group_id: -1, |
| 67 | + kind, |
| 68 | + data, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + match value { |
| 73 | + PhysicalExpression::TableScan(scan) => create_physical_expression( |
| 74 | + 0, |
| 75 | + serde_json::to_value(scan).expect("unable to serialize physical `TableScan`"), |
| 76 | + ), |
| 77 | + PhysicalExpression::Filter(filter) => create_physical_expression( |
| 78 | + 1, |
| 79 | + serde_json::to_value(filter).expect("unable to serialize physical `Filter`"), |
| 80 | + ), |
| 81 | + PhysicalExpression::HashJoin(join) => create_physical_expression( |
| 82 | + 2, |
| 83 | + serde_json::to_value(join).expect("unable to serialize physical `HashJoin`"), |
| 84 | + ), |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +pub use build::*; |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod build { |
| 94 | + use super::*; |
| 95 | + use crate::expression::Expression; |
| 96 | + |
| 97 | + pub fn table_scan(table_schema: String) -> Expression { |
| 98 | + Expression::Physical(PhysicalExpression::TableScan(TableScan { table_schema })) |
| 99 | + } |
| 100 | + |
| 101 | + pub fn filter(child_group: i32, expression: String) -> Expression { |
| 102 | + Expression::Physical(PhysicalExpression::Filter(PhysicalFilter { |
| 103 | + child: child_group, |
| 104 | + expression, |
| 105 | + })) |
| 106 | + } |
| 107 | + |
| 108 | + pub fn hash_join(left_group: i32, right_group: i32, expression: String) -> Expression { |
| 109 | + Expression::Physical(PhysicalExpression::HashJoin(HashJoin { |
| 110 | + left: left_group, |
| 111 | + right: right_group, |
| 112 | + expression, |
| 113 | + })) |
| 114 | + } |
| 115 | +} |
0 commit comments