Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add egg optimizer #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qurious/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ connectorx = { optional = true, workspace = true, features = [
] }
postgres = { version = "0.19.8", optional = true }
rayon = { version = "1.10.0", optional = true }
egg = "0.9.5"


[features]
Expand Down
10 changes: 10 additions & 0 deletions qurious/src/common/table_relation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::error::{Error, Result};
use std::{
fmt::Display,
hash::{DefaultHasher, Hash, Hasher},
str::FromStr,
sync::Arc,
};

Expand Down Expand Up @@ -112,6 +114,14 @@ impl From<&str> for TableRelation {
}
}

impl FromStr for TableRelation {
type Err = Error;

fn from_str(s: &str) -> Result<Self> {
Ok(TableRelation::parse_str(s))
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ResolvedTableReference {
/// An unqualified table reference, e.g. "table"
Expand Down
42 changes: 42 additions & 0 deletions qurious/src/datatypes/float_ord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::cmp::Ordering;

#[derive(PartialEq, PartialOrd)]
pub struct F32Ord(pub f32);

#[derive(PartialEq, PartialOrd)]
pub struct F64Ord(pub f64);

impl Eq for F32Ord {}
impl Eq for F64Ord {}

impl Ord for F32Ord {
fn cmp(&self, other: &Self) -> Ordering {
self.0.partial_cmp(&other.0).unwrap_or_else(|| {
if self.0.is_nan() {
if other.0.is_nan() {
Ordering::Equal
} else {
Ordering::Greater // Place NaN values at the end
}
} else {
Ordering::Less
}
})
}
}

impl Ord for F64Ord {
fn cmp(&self, other: &Self) -> Ordering {
self.0.partial_cmp(&other.0).unwrap_or_else(|| {
if self.0.is_nan() {
if other.0.is_nan() {
Ordering::Equal
} else {
Ordering::Greater // Place NaN values at the end
}
} else {
Ordering::Less
}
})
}
}
2 changes: 2 additions & 0 deletions qurious/src/datatypes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod operator;
pub mod scalar;

mod float_ord;
36 changes: 34 additions & 2 deletions qurious/src/datatypes/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use super::float_ord::{F32Ord, F64Ord};
use crate::error::{Error, Result};
use arrow::{
array::{
new_null_array, Array, ArrayRef, ArrowPrimitiveType, BooleanArray, Decimal128Array, Decimal256Array, Float32Array, Float64Array, Int16Array, Int32Array, Int64Array, Int8Array, LargeStringArray, PrimitiveArray, StringArray, UInt16Array, UInt32Array, UInt64Array, UInt8Array
new_null_array, Array, ArrayRef, ArrowPrimitiveType, BooleanArray, Decimal128Array, Decimal256Array,
Float32Array, Float64Array, Int16Array, Int32Array, Int64Array, Int8Array, LargeStringArray, PrimitiveArray,
StringArray, UInt16Array, UInt32Array, UInt64Array, UInt8Array,
},
datatypes::{i256, DataType, Field},
};
use std::any::type_name;
use std::cmp::Ordering;
use std::{any::type_name, str::FromStr};
use std::{fmt::Display, sync::Arc};

macro_rules! typed_cast {
Expand Down Expand Up @@ -276,3 +280,31 @@ impl Display for ScalarValue {
}
}
}

impl FromStr for ScalarValue {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(ScalarValue::Utf8(Some(s.to_string())))
}
}

impl Ord for ScalarValue {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(ScalarValue::Float32(a), ScalarValue::Float32(b)) => match (a, b) {
(Some(a), Some(b)) => F32Ord(*a).cmp(&F32Ord(*b)),
(None, None) => Ordering::Equal,
(None, _) => Ordering::Less,
(_, None) => Ordering::Greater,
},
(ScalarValue::Float64(a), ScalarValue::Float64(b)) => match (a, b) {
(Some(a), Some(b)) => F64Ord(*a).cmp(&F64Ord(*b)),
(None, None) => Ordering::Equal,
(None, _) => Ordering::Less,
(_, None) => Ordering::Greater,
},
_ => self.cmp(other),
}
}
}
12 changes: 9 additions & 3 deletions qurious/src/logical/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;

use super::{
expr::{LogicalExpr, SortExpr},
plan::{Aggregate, CrossJoin, EmptyRelation, Filter, Join, Limit, LogicalPlan, Projection, Sort, TableScan},
plan::{Aggregate, EmptyRelation, Filter, Join, Limit, LogicalPlan, Projection, Sort, TableScan},
};
use crate::{common::join_type::JoinType, provider::table::TableProvider};
use crate::{common::table_relation::TableRelation, error::Result};
Expand Down Expand Up @@ -74,7 +74,13 @@ impl LogicalPlanBuilder {
);

Ok(LogicalPlanBuilder {
plan: LogicalPlan::CrossJoin(CrossJoin::new(Arc::new(self.plan), Arc::new(right), Arc::new(schema))),
plan: LogicalPlan::Join(Join {
left: Arc::new(self.plan),
right: Arc::new(right),
join_type: JoinType::Inner,
filter: None,
schema: Arc::new(schema),
}),
})
}

Expand All @@ -96,7 +102,7 @@ impl LogicalPlanBuilder {
left: Arc::new(self.plan),
right: Arc::new(right),
join_type,
filter: on,
filter: Some(on),
schema: Arc::new(schema),
}),
})
Expand Down
35 changes: 6 additions & 29 deletions qurious/src/logical/plan/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,12 @@ use crate::{
use arrow::datatypes::SchemaRef;
use std::{fmt::Display, sync::Arc};

#[derive(Debug, Clone)]
pub struct CrossJoin {
pub left: Arc<LogicalPlan>,
pub right: Arc<LogicalPlan>,
pub schema: SchemaRef,
}

impl CrossJoin {
pub fn new(left: Arc<LogicalPlan>, right: Arc<LogicalPlan>, schema: SchemaRef) -> Self {
Self { left, right, schema }
}

pub fn schema(&self) -> SchemaRef {
self.schema.clone()
}

pub fn children(&self) -> Option<Vec<&LogicalPlan>> {
Some(vec![&self.left, &self.right])
}
}

impl Display for CrossJoin {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "CrossJoin")
}
}

#[derive(Debug, Clone)]
pub struct Join {
pub left: Arc<LogicalPlan>,
pub right: Arc<LogicalPlan>,
pub join_type: JoinType,
pub filter: LogicalExpr,
pub filter: Option<LogicalExpr>,
pub schema: SchemaRef,
}

Expand All @@ -53,6 +26,10 @@ impl Join {

impl Display for Join {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: Filter: {}", self.join_type, self.filter)
if let Some(filter) = &self.filter {
write!(f, "{}: Filter: {}", self.join_type, filter)
} else {
write!(f, "{}", self.join_type)
}
}
}
5 changes: 0 additions & 5 deletions qurious/src/logical/plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ macro_rules! impl_logical_plan {

#[derive(Debug, Clone)]
pub enum LogicalPlan {
/// Apply Cross Join to two logical plans.
CrossJoin(CrossJoin),
Join(Join),
Projection(Projection),
Filter(Filter),
Expand Down Expand Up @@ -84,7 +82,6 @@ impl LogicalPlan {
LogicalPlan::Aggregate(a) => a.schema(),
LogicalPlan::TableScan(t) => t.schema(),
LogicalPlan::EmptyRelation(e) => e.schema.clone(),
LogicalPlan::CrossJoin(s) => s.schema(),
LogicalPlan::SubqueryAlias(s) => s.schema(),
LogicalPlan::Join(j) => j.schema(),
LogicalPlan::Sort(s) => s.schema(),
Expand All @@ -102,7 +99,6 @@ impl LogicalPlan {
LogicalPlan::Filter(f) => f.children(),
LogicalPlan::Aggregate(a) => a.children(),
LogicalPlan::TableScan(t) => t.children(),
LogicalPlan::CrossJoin(s) => s.children(),
LogicalPlan::SubqueryAlias(s) => s.children(),
LogicalPlan::Join(j) => j.children(),
LogicalPlan::Sort(s) => s.children(),
Expand Down Expand Up @@ -205,7 +201,6 @@ impl std::fmt::Display for LogicalPlan {
LogicalPlan::Aggregate(a) => write!(f, "{}", a),
LogicalPlan::TableScan(t) => write!(f, "{}", t),
LogicalPlan::EmptyRelation(_) => write!(f, "Empty Relation"),
LogicalPlan::CrossJoin(s) => write!(f, "{}", s),
LogicalPlan::SubqueryAlias(s) => write!(f, "{}", s),
LogicalPlan::Join(j) => write!(f, "{}", j),
LogicalPlan::Sort(s) => write!(f, "{}", s),
Expand Down
Loading
Loading