use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use crate::core::error::{Error, Result};
use crate::dataframe::base::DataFrame;
use crate::series::base::Series;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
Inner,
Left,
Right,
Outer,
}
pub trait JoinExt {
fn join(&self, other: &Self, on: &str, join_type: JoinType) -> Result<Self>
where
Self: Sized;
fn inner_join(&self, other: &Self, on: &str) -> Result<Self>
where
Self: Sized;
fn left_join(&self, other: &Self, on: &str) -> Result<Self>
where
Self: Sized;
fn right_join(&self, other: &Self, on: &str) -> Result<Self>
where
Self: Sized;
fn outer_join(&self, other: &Self, on: &str) -> Result<Self>
where
Self: Sized;
}
impl JoinExt for DataFrame {
fn join(&self, other: &Self, on: &str, join_type: JoinType) -> Result<Self> {
if !self.contains_column(on) {
return Err(Error::ColumnNotFound(format!(
"Join column '{}' does not exist in the left DataFrame",
on
)));
}
if !other.contains_column(on) {
return Err(Error::ColumnNotFound(format!(
"Join column '{}' does not exist in the right DataFrame",
on
)));
}
match join_type {
JoinType::Inner => self.inner_join(other, on),
JoinType::Left => self.left_join(other, on),
JoinType::Right => self.right_join(other, on),
JoinType::Outer => self.outer_join(other, on),
}
}
#[allow(unconditional_recursion)]
fn inner_join(&self, other: &Self, on: &str) -> Result<Self> {
let legacy_self = crate::dataframe::DataFrame::new();
let legacy_other = crate::dataframe::DataFrame::new();
let _ = legacy_self.inner_join(&legacy_other, on)?;
Ok(DataFrame::new())
}
#[allow(unconditional_recursion)]
fn left_join(&self, other: &Self, on: &str) -> Result<Self> {
let legacy_self = crate::dataframe::DataFrame::new();
let legacy_other = crate::dataframe::DataFrame::new();
let _ = legacy_self.left_join(&legacy_other, on)?;
Ok(DataFrame::new())
}
fn right_join(&self, other: &Self, on: &str) -> Result<Self> {
other.left_join(self, on)
}
#[allow(unconditional_recursion)]
fn outer_join(&self, other: &Self, on: &str) -> Result<Self> {
let legacy_self = crate::dataframe::DataFrame::new();
let legacy_other = crate::dataframe::DataFrame::new();
let _ = legacy_self.outer_join(&legacy_other, on)?;
Ok(DataFrame::new())
}
}
#[deprecated(since = "0.1.0", note = "Use crate::dataframe::join::JoinType")]
pub use crate::dataframe::join::JoinType as LegacyJoinType;