Skip to content

Commit

Permalink
[CHORE] Cleanup display impls - follow-up PR (#2820)
Browse files Browse the repository at this point in the history
# Overview
Follow up PR for #2794.
  • Loading branch information
raunakab authored Sep 10, 2024
1 parent ee0b973 commit 2a89d0d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
24 changes: 12 additions & 12 deletions daft/expressions/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,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 @@ -1512,7 +1512,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 @@ -1547,7 +1547,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 @@ -1582,7 +1582,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 @@ -1617,7 +1617,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 @@ -1652,7 +1652,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 @@ -1685,7 +1685,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 @@ -1718,7 +1718,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 @@ -1752,7 +1752,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 @@ -1785,7 +1785,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 @@ -2721,7 +2721,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 @@ -2740,7 +2740,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
10 changes: 4 additions & 6 deletions src/common/display/src/table_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ fn create_table_cell(value: &str) -> comfy_table::Cell {
cell
}

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

Expand All @@ -38,9 +37,8 @@ pub fn make_schema_vertical_table<S1: ToString, S2: ToString>(

let header = vec![create_table_cell("Column Name"), create_table_cell("Type")];
table.set_header(header);
assert_eq!(names.len(), dtypes.len());
for (name, dtype) in names.iter().zip(dtypes.iter()) {
table.add_row(vec![name.to_string(), dtype.to_string()]);
for (name, dtype) in fields {
table.add_row(vec![name.clone(), dtype]);
}
table
}
Expand Down
8 changes: 4 additions & 4 deletions src/daft-schema/src/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub enum DataType {
///
/// When the timezone is not specified, the timestamp is considered to have no timezone
/// and is represented _as is_
#[display("Timestamp[{_0}, {_1:?}]")]
#[display("Timestamp({_0}, {_1:?})")]
Timestamp(TimeUnit, Option<String>),

/// An [`i32`] representing the elapsed time since UNIX epoch (1970-01-01)
Expand All @@ -79,7 +79,7 @@ pub enum DataType {

/// A 64-bit time representing the elapsed time since midnight in the unit of `TimeUnit`.
/// Only [`TimeUnit::Microsecond`] and [`TimeUnit::Nanosecond`] are supported on this variant.
#[display("Time[{_0}]")]
#[display("Time({_0})")]
Time(TimeUnit),

/// Measure of elapsed time. This elapsed time is a physical duration (i.e. 1s as defined in S.I.)
Expand Down Expand Up @@ -130,7 +130,7 @@ pub enum DataType {
FixedShapeImage(ImageMode, u32, u32),

/// A logical type for tensors with variable shapes.
#[display("Tensor[{_0}]")]
#[display("Tensor({_0})")]
Tensor(Box<DataType>),

/// A logical type for tensors with the same shape.
Expand All @@ -150,7 +150,7 @@ fn format_struct(fields: &[Field]) -> std::result::Result<String, std::fmt::Erro
write!(&mut f, ", ")?;
}
if !(field.name.is_empty() && field.dtype.is_null()) {
write!(&mut f, "{}", field)?;
write!(&mut f, "{}: {}", field.name, field.dtype)?;
}
}
Ok(f)
Expand Down
2 changes: 1 addition & 1 deletion src/daft-schema/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
pub type Metadata = std::collections::BTreeMap<String, String>;

#[derive(Clone, Display, Debug, Eq, Deserialize, Serialize)]
#[display("{name}: {dtype}")]
#[display("{name}#{dtype}")]
pub struct Field {
pub name: String,
pub dtype: DataType,
Expand Down
3 changes: 1 addition & 2 deletions src/daft-schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ pub type SchemaRef = Arc<Schema>;
#[derive(Debug, Display, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
#[display("{}\n", make_schema_vertical_table(
fields.keys().collect::<Vec<_>>().as_slice(),
fields.values().map(|field| field.dtype.to_string()).collect::<Vec<_>>().as_slice(),
fields.iter().map(|(name, field)| (name.clone(), field.dtype.to_string()))
))]
pub struct Schema {
#[serde(with = "indexmap::map::serde_seq")]
Expand Down
2 changes: 1 addition & 1 deletion tests/series/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_tensor_repr():
== """╭───────────────────────╮
│ list_series │
│ --- │
│ Tensor[Int64]
│ Tensor(Int64)
╞═══════════════════════╡
│ <Tensor shape=(2, 2)> │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
Expand Down

0 comments on commit 2a89d0d

Please sign in to comment.