Skip to content

Commit c135236

Browse files
emilktimsaucer
andauthored
Improve error messages with nicer formatting of Date and Time types (#19954)
* Follow-up to: #17565 * Related: apache/arrow-rs#8290 ## Rationale for this change I believe that error messages should be as readable as possible. Aim for `rustc` more than `gcc`. `Display` is the nice, user-facing formatter. `Debug` is for… well, debugging. ## What changes are included in this PR? Change a bunch of `{:?}` format string to `{}`. I'm sure I missed a lot of them, because I know of no way to enforce this without * rust-lang/rust-clippy#8581 ## Are these changes tested? I assume CI runs `cargo test` :) ## Are there any user-facing changes? Yes! Error messages should be a bit more readable now. --------- Co-authored-by: Tim Saucer <timsaucer@gmail.com>
1 parent 05802e2 commit c135236

33 files changed

Lines changed: 206 additions & 157 deletions

File tree

datafusion/common/src/types/native.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,57 @@ pub enum NativeType {
186186

187187
impl Display for NativeType {
188188
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
189-
write!(f, "{self:?}") // TODO: nicer formatting
189+
// Match the format used by arrow::datatypes::DataType's Display impl
190+
match self {
191+
Self::Null => write!(f, "Null"),
192+
Self::Boolean => write!(f, "Boolean"),
193+
Self::Int8 => write!(f, "Int8"),
194+
Self::Int16 => write!(f, "Int16"),
195+
Self::Int32 => write!(f, "Int32"),
196+
Self::Int64 => write!(f, "Int64"),
197+
Self::UInt8 => write!(f, "UInt8"),
198+
Self::UInt16 => write!(f, "UInt16"),
199+
Self::UInt32 => write!(f, "UInt32"),
200+
Self::UInt64 => write!(f, "UInt64"),
201+
Self::Float16 => write!(f, "Float16"),
202+
Self::Float32 => write!(f, "Float32"),
203+
Self::Float64 => write!(f, "Float64"),
204+
Self::Timestamp(unit, Some(tz)) => write!(f, "Timestamp({unit}, {tz:?})"),
205+
Self::Timestamp(unit, None) => write!(f, "Timestamp({unit})"),
206+
Self::Date => write!(f, "Date"),
207+
Self::Time(unit) => write!(f, "Time({unit})"),
208+
Self::Duration(unit) => write!(f, "Duration({unit})"),
209+
Self::Interval(unit) => write!(f, "Interval({unit:?})"),
210+
Self::Binary => write!(f, "Binary"),
211+
Self::FixedSizeBinary(size) => write!(f, "FixedSizeBinary({size})"),
212+
Self::String => write!(f, "String"),
213+
Self::List(field) => write!(f, "List({})", field.logical_type),
214+
Self::FixedSizeList(field, size) => {
215+
write!(f, "FixedSizeList({size} x {})", field.logical_type)
216+
}
217+
Self::Struct(fields) => {
218+
write!(f, "Struct(")?;
219+
for (i, field) in fields.iter().enumerate() {
220+
if i > 0 {
221+
write!(f, ", ")?;
222+
}
223+
write!(f, "{:?}: {}", field.name, field.logical_type)?;
224+
}
225+
write!(f, ")")
226+
}
227+
Self::Union(fields) => {
228+
write!(f, "Union(")?;
229+
for (i, (type_id, field)) in fields.iter().enumerate() {
230+
if i > 0 {
231+
write!(f, ", ")?;
232+
}
233+
write!(f, "{type_id}: ({:?}: {})", field.name, field.logical_type)?;
234+
}
235+
write!(f, ")")
236+
}
237+
Self::Decimal(precision, scale) => write!(f, "Decimal({precision}, {scale})"),
238+
Self::Map(field) => write!(f, "Map({})", field.logical_type),
239+
}
190240
}
191241
}
192242

datafusion/core/src/datasource/file_format/avro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mod tests {
9595
.schema()
9696
.fields()
9797
.iter()
98-
.map(|f| format!("{}: {:?}", f.name(), f.data_type()))
98+
.map(|f| format!("{}: {}", f.name(), f.data_type()))
9999
.collect();
100100
assert_eq!(
101101
vec![
@@ -109,7 +109,7 @@ mod tests {
109109
"double_col: Float64",
110110
"date_string_col: Binary",
111111
"string_col: Binary",
112-
"timestamp_col: Timestamp(Microsecond, None)",
112+
"timestamp_col: Timestamp(µs)",
113113
],
114114
x
115115
);

datafusion/core/src/datasource/file_format/parquet.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ mod tests {
815815
.schema()
816816
.fields()
817817
.iter()
818-
.map(|f| format!("{}: {:?}", f.name(), f.data_type()))
818+
.map(|f| format!("{}: {}", f.name(), f.data_type()))
819819
.collect();
820820
let y = x.join("\n");
821821
assert_eq!(expected, y);
@@ -841,7 +841,7 @@ mod tests {
841841
double_col: Float64\n\
842842
date_string_col: Binary\n\
843843
string_col: Binary\n\
844-
timestamp_col: Timestamp(Nanosecond, None)";
844+
timestamp_col: Timestamp(ns)";
845845
_run_read_alltypes_plain_parquet(ForceViews::No, no_views).await?;
846846

847847
let with_views = "id: Int32\n\
@@ -854,7 +854,7 @@ mod tests {
854854
double_col: Float64\n\
855855
date_string_col: BinaryView\n\
856856
string_col: BinaryView\n\
857-
timestamp_col: Timestamp(Nanosecond, None)";
857+
timestamp_col: Timestamp(ns)";
858858
_run_read_alltypes_plain_parquet(ForceViews::Yes, with_views).await?;
859859

860860
Ok(())

datafusion/core/src/physical_planner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2753,7 +2753,7 @@ impl<'a> OptimizationInvariantChecker<'a> {
27532753
&& !is_allowed_schema_change(previous_schema.as_ref(), plan.schema().as_ref())
27542754
{
27552755
internal_err!(
2756-
"PhysicalOptimizer rule '{}' failed. Schema mismatch. Expected original schema: {:?}, got new schema: {:?}",
2756+
"PhysicalOptimizer rule '{}' failed. Schema mismatch. Expected original schema: {}, got new schema: {}",
27572757
self.rule.name(),
27582758
previous_schema,
27592759
plan.schema()

datafusion/core/tests/dataframe/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4802,7 +4802,7 @@ async fn unnest_with_redundant_columns() -> Result<()> {
48024802
@r"
48034803
Projection: shapes.shape_id [shape_id:UInt32]
48044804
Unnest: lists[shape_id2|depth=1] structs[] [shape_id:UInt32, shape_id2:UInt32;N]
4805-
Aggregate: groupBy=[[shapes.shape_id]], aggr=[[array_agg(shapes.shape_id) AS shape_id2]] [shape_id:UInt32, shape_id2:List(Field { data_type: UInt32, nullable: true });N]
4805+
Aggregate: groupBy=[[shapes.shape_id]], aggr=[[array_agg(shapes.shape_id) AS shape_id2]] [shape_id:UInt32, shape_id2:List(UInt32);N]
48064806
TableScan: shapes projection=[shape_id] [shape_id:UInt32]
48074807
"
48084808
);

datafusion/datasource/src/sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub struct DataSinkExec {
9494

9595
impl Debug for DataSinkExec {
9696
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97-
write!(f, "DataSinkExec schema: {:?}", self.count_schema)
97+
write!(f, "DataSinkExec schema: {}", self.count_schema)
9898
}
9999
}
100100

datafusion/expr/src/logical_plan/display.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,7 @@ pub fn display_schema(schema: &Schema) -> impl fmt::Display + '_ {
117117
write!(f, ", ")?;
118118
}
119119
let nullable_str = if field.is_nullable() { ";N" } else { "" };
120-
write!(
121-
f,
122-
"{}:{:?}{}",
123-
field.name(),
124-
field.data_type(),
125-
nullable_str
126-
)?;
120+
write!(f, "{}:{}{}", field.name(), field.data_type(), nullable_str)?;
127121
}
128122
write!(f, "]")
129123
}

datafusion/expr/src/type_coercion/functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ mod tests {
10681068
.unwrap_err();
10691069
assert_contains!(
10701070
got.to_string(),
1071-
"Function 'test' expects NativeType::Numeric but received NativeType::Timestamp(Second, None)"
1071+
"Function 'test' expects NativeType::Numeric but received NativeType::Timestamp(s)"
10721072
);
10731073

10741074
Ok(())

datafusion/functions-nested/src/array_has.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl<'a> TryFrom<&'a dyn Array> for ArrayWrapper<'a> {
262262
DataType::FixedSizeList(_, _) => Ok(ArrayWrapper::FixedSizeList(
263263
as_fixed_size_list_array(value)?,
264264
)),
265-
_ => exec_err!("array_has does not support type '{:?}'.", value.data_type()),
265+
_ => exec_err!("array_has does not support type '{}'.", value.data_type()),
266266
}
267267
}
268268
}

datafusion/functions-nested/src/flatten.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ fn flatten_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
208208
}
209209
Null => Ok(Arc::clone(array)),
210210
_ => {
211-
exec_err!("flatten does not support type '{:?}'", array.data_type())
211+
exec_err!("flatten does not support type '{}'", array.data_type())
212212
}
213213
}
214214
}

0 commit comments

Comments
 (0)