Skip to content

Commit

Permalink
[CHORE] Add derive_more to get rid of manual Display impls (#2794)
Browse files Browse the repository at this point in the history
# Overview
- Implemented `derive_more::Display` for a couple structs to remove some
boilerplate.
- Still potentially more things to derive-macro away

# Note
This has the potential of increasing build times due to increasing the
amount of codegen `rustc` will have to do internally. I can try and put
up some build time metrics in order to compare the differences in build
times.
  • Loading branch information
raunakab authored Sep 9, 2024
1 parent d30e62a commit 08ca9a4
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 272 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ path = "src/arrow2"
[workspace.dependencies.bincode]
version = "1.3.3"

[workspace.dependencies.derive_more]
features = ["display"]
version = "1.0.0"

[workspace.dependencies.lazy_static]
version = "1.4.0"

Expand Down
24 changes: 12 additions & 12 deletions daft/expressions/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ def date(self) -> Expression:
╭───────────────────────────────┬────────────╮
│ x ┆ date │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ Date │
│ Timestamp[Microseconds, None] ┆ Date │
╞═══════════════════════════════╪════════════╡
│ 2021-01-01 05:01:01 ┆ 2021-01-01 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1502,7 +1502,7 @@ def day(self) -> Expression:
╭───────────────────────────────┬────────╮
│ x ┆ day │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ UInt32 │
│ Timestamp[Microseconds, None] ┆ UInt32 │
╞═══════════════════════════════╪════════╡
│ 2021-01-01 05:01:01 ┆ 1 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1537,7 +1537,7 @@ def hour(self) -> Expression:
╭───────────────────────────────┬────────╮
│ x ┆ hour │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ UInt32 │
│ Timestamp[Microseconds, None] ┆ UInt32 │
╞═══════════════════════════════╪════════╡
│ 2021-01-01 05:01:01 ┆ 5 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1572,7 +1572,7 @@ def minute(self) -> Expression:
╭───────────────────────────────┬────────╮
│ x ┆ minute │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ UInt32 │
│ Timestamp[Microseconds, None] ┆ UInt32 │
╞═══════════════════════════════╪════════╡
│ 2021-01-01 05:01:01 ┆ 1 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1607,7 +1607,7 @@ def second(self) -> Expression:
╭───────────────────────────────┬────────╮
│ x ┆ second │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ UInt32 │
│ Timestamp[Microseconds, None] ┆ UInt32 │
╞═══════════════════════════════╪════════╡
│ 2021-01-01 00:01:01 ┆ 1 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1642,7 +1642,7 @@ def time(self) -> Expression:
╭───────────────────────────────┬────────────────────╮
│ x ┆ time │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ Time(Microseconds)
│ Timestamp[Microseconds, None] ┆ Time[Microseconds]
╞═══════════════════════════════╪════════════════════╡
│ 2021-01-01 00:01:01 ┆ 00:01:01 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1675,7 +1675,7 @@ def month(self) -> Expression:
╭───────────────────────────────┬────────╮
│ datetime ┆ month │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ UInt32 │
│ Timestamp[Microseconds, None] ┆ UInt32 │
╞═══════════════════════════════╪════════╡
│ 2024-07-03 00:00:00 ┆ 7 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1708,7 +1708,7 @@ def year(self) -> Expression:
╭───────────────────────────────┬───────╮
│ datetime ┆ year │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ Int32 │
│ Timestamp[Microseconds, None] ┆ Int32 │
╞═══════════════════════════════╪═══════╡
│ 2024-07-03 00:00:00 ┆ 2024 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1742,7 +1742,7 @@ def day_of_week(self) -> Expression:
╭───────────────────────────────┬─────────────╮
│ datetime ┆ day_of_week │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ UInt32 │
│ Timestamp[Microseconds, None] ┆ UInt32 │
╞═══════════════════════════════╪═════════════╡
│ 2024-07-03 00:00:00 ┆ 2 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -1775,7 +1775,7 @@ def truncate(self, interval: str, relative_to: Expression | None = None) -> Expr
╭───────────────────────────────┬───────────────────────────────╮
│ datetime ┆ truncated │
│ --- ┆ --- │
│ Timestamp(Microseconds, None) ┆ Timestamp(Microseconds, None)
│ Timestamp[Microseconds, None] ┆ Timestamp[Microseconds, None]
╞═══════════════════════════════╪═══════════════════════════════╡
│ 2021-01-01 00:01:01 ┆ 2021-01-01 00:01:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
Expand Down Expand Up @@ -2711,7 +2711,7 @@ def to_datetime(self, format: str, timezone: str | None = None) -> Expression:
╭─────────────────────────┬───────────────────────────────╮
│ x ┆ datetime │
│ --- ┆ --- │
│ Utf8 ┆ Timestamp(Milliseconds, None)
│ Utf8 ┆ Timestamp[Milliseconds, None]
╞═════════════════════════╪═══════════════════════════════╡
│ 2021-01-01 00:00:00.123 ┆ 2021-01-01 00:00:00.123 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
Expand All @@ -2730,7 +2730,7 @@ def to_datetime(self, format: str, timezone: str | None = None) -> Expression:
╭───────────────────────────────┬────────────────────────────────────────────────╮
│ x ┆ datetime │
│ --- ┆ --- │
│ Utf8 ┆ Timestamp(Milliseconds, Some("Asia/Shanghai"))
│ Utf8 ┆ Timestamp[Milliseconds, Some("Asia/Shanghai")]
╞═══════════════════════════════╪════════════════════════════════════════════════╡
│ 2021-01-01 00:00:00.123 +0800 ┆ 2021-01-01 00:00:00.123 CST │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
Expand Down
5 changes: 4 additions & 1 deletion src/common/display/src/table_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ fn create_table_cell(value: &str) -> comfy_table::Cell {
cell
}

pub fn make_schema_vertical_table<S: ToString>(names: &[S], dtypes: &[S]) -> comfy_table::Table {
pub fn make_schema_vertical_table<S1: ToString, S2: ToString>(
names: &[S1],
dtypes: &[S2],
) -> comfy_table::Table {
let mut table = comfy_table::Table::new();

let default_width_if_no_tty = 120usize;
Expand Down
1 change: 1 addition & 0 deletions src/daft-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ common-py-serde = {path = "../common/py-serde", default-features = false}
daft-minhash = {path = "../daft-minhash", default-features = false}
daft-schema = {path = "../daft-schema", default-features = false}
daft-sketch = {path = "../daft-sketch", default-features = false}
derive_more = {workspace = true}
fastrand = "2.1.0"
fnv = "1.0.7"
html-escape = {workspace = true}
Expand Down
12 changes: 3 additions & 9 deletions src/daft-core/src/count_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use common_py_serde::impl_bincode_py_state_serialization;
#[cfg(feature = "python")]
use pyo3::{exceptions::PyValueError, prelude::*};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result};
use std::str::FromStr;

use derive_more::Display;

use common_error::{DaftError, DaftResult};

/// Supported count modes for Daft's count aggregation.
Expand All @@ -13,7 +14,7 @@ use common_error::{DaftError, DaftResult};
/// | Valid - Count only valid values.
/// | Null - Count only null values.
#[allow(clippy::upper_case_acronyms)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[cfg_attr(feature = "python", pyclass(module = "daft.daft"))]
pub enum CountMode {
All = 1,
Expand Down Expand Up @@ -66,10 +67,3 @@ impl FromStr for CountMode {
}
}
}

impl Display for CountMode {
fn fmt(&self, f: &mut Formatter) -> Result {
// Leverage Debug trait implementation, which will already return the enum variant as a string.
write!(f, "{:?}", self)
}
}
24 changes: 4 additions & 20 deletions src/daft-core/src/join.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::{
fmt::{Display, Formatter, Result},
str::FromStr,
};
use std::str::FromStr;

use common_error::{DaftError, DaftResult};
use common_py_serde::impl_bincode_py_state_serialization;
use derive_more::Display;
#[cfg(feature = "python")]
use pyo3::{exceptions::PyValueError, pyclass, pymethods, PyObject, PyResult, Python};

use serde::{Deserialize, Serialize};

/// Type of a join operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[cfg_attr(feature = "python", pyclass(module = "daft.daft"))]
pub enum JoinType {
Inner,
Expand Down Expand Up @@ -71,14 +69,7 @@ impl FromStr for JoinType {
}
}

impl Display for JoinType {
fn fmt(&self, f: &mut Formatter) -> Result {
// Leverage Debug trait implementation, which will already return the enum variant as a string.
write!(f, "{:?}", self)
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[cfg_attr(feature = "python", pyclass(module = "daft.daft"))]
pub enum JoinStrategy {
Hash,
Expand Down Expand Up @@ -131,10 +122,3 @@ impl FromStr for JoinStrategy {
}
}
}

impl Display for JoinStrategy {
fn fmt(&self, f: &mut Formatter) -> Result {
// Leverage Debug trait implementation, which will already return the enum variant as a string.
write!(f, "{:?}", self)
}
}
19 changes: 4 additions & 15 deletions src/daft-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ mod from;
mod ops;
mod serdes;
mod series_like;
use std::{
fmt::{Display, Formatter, Result},
sync::Arc,
};
use derive_more::Display;
use std::sync::Arc;

use crate::{
array::{
Expand All @@ -24,7 +22,8 @@ pub use ops::cast_series_to_supertype;

pub(crate) use self::series_like::SeriesLike;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Display)]
#[display("{}\n", self.to_comfy_table())]
pub struct Series {
pub inner: Arc<dyn SeriesLike>,
}
Expand Down Expand Up @@ -136,13 +135,3 @@ impl Series {
Ok(data.as_slice())
}
}
impl Display for Series {
// `f` is a buffer, and this method must write the formatted string into it
fn fmt(&self, f: &mut Formatter) -> Result {
let table = self.to_comfy_table();
writeln!(f, "{table}")
}
}

#[cfg(test)]
mod tests {}
1 change: 1 addition & 0 deletions src/daft-dsl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ common-resource-request = {path = "../common/resource-request", default-features
common-treenode = {path = "../common/treenode", default-features = false}
daft-core = {path = "../daft-core", default-features = false}
daft-sketch = {path = "../daft-sketch", default-features = false}
derive_more = {workspace = true}
indexmap = {workspace = true}
itertools = {workspace = true}
log = {workspace = true}
Expand Down
Loading

0 comments on commit 08ca9a4

Please sign in to comment.