use std::fmt;
use std::fmt::{Error, Formatter};
use std::rc::Rc;
use super::types::*;
use arrow::datatypes::*;
#[derive(Debug, Clone)]
pub enum FunctionType {
Scalar,
Aggregate,
}
#[derive(Debug, Clone)]
pub struct FunctionMeta {
name: String,
args: Vec<Field>,
return_type: DataType,
function_type: FunctionType,
}
impl FunctionMeta {
pub fn new(
name: String,
args: Vec<Field>,
return_type: DataType,
function_type: FunctionType,
) -> Self {
FunctionMeta {
name,
args,
return_type,
function_type,
}
}
pub fn name(&self) -> &String {
&self.name
}
pub fn args(&self) -> &Vec<Field> {
&self.args
}
pub fn return_type(&self) -> &DataType {
&self.return_type
}
pub fn function_type(&self) -> &FunctionType {
&self.function_type
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Operator {
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
Plus,
Minus,
Multiply,
Divide,
Modulus,
And,
Or,
}
impl Operator {
pub fn get_datatype(&self, l: &Expr, _r: &Expr, schema: &Schema) -> DataType {
l.get_type(schema).clone()
}
}
#[derive(Clone, PartialEq)]
pub enum Expr {
Column(usize),
Literal(ScalarValue),
BinaryExpr {
left: Rc<Expr>,
op: Operator,
right: Rc<Expr>,
},
IsNotNull(Rc<Expr>),
IsNull(Rc<Expr>),
Cast { expr: Rc<Expr>, data_type: DataType },
Sort { expr: Rc<Expr>, asc: bool },
ScalarFunction {
name: String,
args: Vec<Expr>,
return_type: DataType,
},
AggregateFunction {
name: String,
args: Vec<Expr>,
return_type: DataType,
},
}
impl Expr {
pub fn get_type(&self, schema: &Schema) -> DataType {
match self {
Expr::Column(n) => schema.column(*n).data_type().clone(),
Expr::Literal(l) => l.get_datatype(),
Expr::Cast { data_type, .. } => data_type.clone(),
Expr::ScalarFunction { return_type, .. } => return_type.clone(),
Expr::AggregateFunction { return_type, .. } => return_type.clone(),
Expr::IsNull(_) => DataType::Boolean,
Expr::IsNotNull(_) => DataType::Boolean,
Expr::BinaryExpr {
ref left,
ref right,
ref op,
} => {
match op {
Operator::Eq | Operator::NotEq => DataType::Boolean,
Operator::Lt | Operator::LtEq => DataType::Boolean,
Operator::Gt | Operator::GtEq => DataType::Boolean,
Operator::And | Operator::Or => DataType::Boolean,
_ => {
let left_type = left.get_type(schema);
let right_type = right.get_type(schema);
get_supertype(&left_type, &right_type).unwrap_or(DataType::Utf8) }
}
}
Expr::Sort { ref expr, .. } => expr.get_type(schema),
}
}
pub fn cast_to(&self, cast_to_type: &DataType, schema: &Schema) -> Result<Expr, String> {
let this_type = self.get_type(schema);
if this_type == *cast_to_type {
Ok(self.clone())
} else if can_coerce_from(cast_to_type, &this_type) {
Ok(Expr::Cast {
expr: Rc::new(self.clone()),
data_type: cast_to_type.clone(),
})
} else {
Err(format!(
"Cannot automatically convert {:?} to {:?}",
this_type, cast_to_type
))
}
}
pub fn eq(&self, other: &Expr) -> Expr {
Expr::BinaryExpr {
left: Rc::new(self.clone()),
op: Operator::Eq,
right: Rc::new(other.clone()),
}
}
pub fn not_eq(&self, other: &Expr) -> Expr {
Expr::BinaryExpr {
left: Rc::new(self.clone()),
op: Operator::NotEq,
right: Rc::new(other.clone()),
}
}
pub fn gt(&self, other: &Expr) -> Expr {
Expr::BinaryExpr {
left: Rc::new(self.clone()),
op: Operator::Gt,
right: Rc::new(other.clone()),
}
}
pub fn gt_eq(&self, other: &Expr) -> Expr {
Expr::BinaryExpr {
left: Rc::new(self.clone()),
op: Operator::GtEq,
right: Rc::new(other.clone()),
}
}
pub fn lt(&self, other: &Expr) -> Expr {
Expr::BinaryExpr {
left: Rc::new(self.clone()),
op: Operator::Lt,
right: Rc::new(other.clone()),
}
}
pub fn lt_eq(&self, other: &Expr) -> Expr {
Expr::BinaryExpr {
left: Rc::new(self.clone()),
op: Operator::LtEq,
right: Rc::new(other.clone()),
}
}
}
impl fmt::Debug for Expr {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
Expr::Column(i) => write!(f, "#{}", i),
Expr::Literal(v) => write!(f, "{:?}", v),
Expr::Cast { expr, data_type } => write!(f, "CAST({:?} AS {:?})", expr, data_type),
Expr::IsNull(expr) => write!(f, "{:?} IS NULL", expr),
Expr::IsNotNull(expr) => write!(f, "{:?} IS NOT NULL", expr),
Expr::BinaryExpr { left, op, right } => write!(f, "{:?} {:?} {:?}", left, op, right),
Expr::Sort { expr, asc } => if *asc {
write!(f, "{:?} ASC", expr)
} else {
write!(f, "{:?} DESC", expr)
},
Expr::ScalarFunction { name, ref args, .. } => {
write!(f, "{}(", name)?;
for i in 0..args.len() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", args[i])?;
}
write!(f, ")")
}
Expr::AggregateFunction { name, ref args, .. } => {
write!(f, "{}(", name)?;
for i in 0..args.len() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", args[i])?;
}
write!(f, ")")
}
}
}
}
#[derive(Clone)]
pub enum LogicalPlan {
Limit {
limit: usize,
input: Rc<LogicalPlan>,
schema: Rc<Schema>,
},
Projection {
expr: Vec<Expr>,
input: Rc<LogicalPlan>,
schema: Rc<Schema>,
},
Selection { expr: Expr, input: Rc<LogicalPlan> },
Aggregate {
input: Rc<LogicalPlan>,
group_expr: Vec<Expr>,
aggr_expr: Vec<Expr>,
schema: Rc<Schema>,
},
Sort {
expr: Vec<Expr>,
input: Rc<LogicalPlan>,
schema: Rc<Schema>,
},
TableScan {
schema_name: String,
table_name: String,
schema: Rc<Schema>,
projection: Option<Vec<usize>>,
},
CsvFile {
filename: String,
schema: Rc<Schema>,
has_header: bool,
projection: Option<Vec<usize>>,
},
NdJsonFile {
filename: String,
schema: Rc<Schema>,
projection: Option<Vec<usize>>,
},
ParquetFile {
filename: String,
schema: Rc<Schema>,
projection: Option<Vec<usize>>,
},
EmptyRelation { schema: Rc<Schema> },
}
impl LogicalPlan {
pub fn schema(&self) -> &Rc<Schema> {
match self {
LogicalPlan::EmptyRelation { schema } => &schema,
LogicalPlan::TableScan { schema, .. } => &schema,
LogicalPlan::CsvFile { schema, .. } => &schema,
LogicalPlan::NdJsonFile { schema, .. } => &schema,
LogicalPlan::ParquetFile { schema, .. } => &schema,
LogicalPlan::Projection { schema, .. } => &schema,
LogicalPlan::Selection { input, .. } => input.schema(),
LogicalPlan::Aggregate { schema, .. } => &schema,
LogicalPlan::Sort { schema, .. } => &schema,
LogicalPlan::Limit { schema, .. } => &schema,
}
}
}
impl LogicalPlan {
fn fmt_with_indent(&self, f: &mut Formatter, indent: usize) -> Result<(), Error> {
if indent > 0 {
writeln!(f)?;
for _ in 0..indent {
write!(f, " ")?;
}
}
match *self {
LogicalPlan::EmptyRelation { .. } => write!(f, "EmptyRelation"),
LogicalPlan::TableScan {
ref table_name,
ref projection,
..
} => write!(f, "TableScan: {} projection={:?}", table_name, projection),
LogicalPlan::CsvFile {
ref filename,
ref schema,
..
} => write!(f, "CsvFile: file={}, schema={:?}", filename, schema),
LogicalPlan::NdJsonFile {
ref filename,
ref schema,
..
} => write!(f, "NdJsonFile: file={}, schema={:?}", filename, schema),
LogicalPlan::ParquetFile { .. } => write!(f, "ParquetFile:"),
LogicalPlan::Projection {
ref expr,
ref input,
..
} => {
write!(f, "Projection: ")?;
for i in 0..expr.len() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", expr[i])?;
}
input.fmt_with_indent(f, indent + 1)
}
LogicalPlan::Selection {
ref expr,
ref input,
..
} => {
write!(f, "Selection: {:?}", expr)?;
input.fmt_with_indent(f, indent + 1)
}
LogicalPlan::Aggregate {
ref input,
ref group_expr,
ref aggr_expr,
..
} => {
write!(
f,
"Aggregate: groupBy=[{:?}], aggr=[{:?}]",
group_expr, aggr_expr
)?;
input.fmt_with_indent(f, indent + 1)
}
LogicalPlan::Sort {
ref input,
ref expr,
..
} => {
write!(f, "Sort: ")?;
for i in 0..expr.len() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", expr[i])?;
}
input.fmt_with_indent(f, indent + 1)
}
LogicalPlan::Limit {
ref input, limit, ..
} => {
write!(f, "Limit: {}", limit)?;
input.fmt_with_indent(f, indent + 1)
}
}
}
}
impl fmt::Debug for LogicalPlan {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
self.fmt_with_indent(f, 0)
}
}