Skip to content
Open
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
94 changes: 51 additions & 43 deletions datafusion/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::sync::Arc;
/// in the [`ConfigOptions`] configuration tree.
///
/// `transform` is used to normalize values before parsing.
/// `validate` can reject a value before it is stored.
///
/// For example,
///
Expand Down Expand Up @@ -123,6 +124,7 @@ macro_rules! config_namespace {
$field_vis:vis $field_name:ident : $field_type:ty,
$(warn = $warn:expr,)?
$(transform = $transform:expr,)?
$(validate = $validate:expr,)?
default = $default:expr
)*$(,)*
}
Expand Down Expand Up @@ -150,6 +152,7 @@ macro_rules! config_namespace {
// $(#[allow(deprecated)])?
{
$(let value = $transform(value);)? // Apply transformation if specified
$(($validate)(value.as_ref())?;)?
let ret = self.$field_name.set(rem, value.as_ref());

$(if !$warn.is_empty() {
Expand Down Expand Up @@ -2168,14 +2171,60 @@ config_namespace! {
/// Timestamp format for timestamp with timezone arrays. When `None`, ISO 8601 format is used.
pub timestamp_tz_format: Option<String>, default = None
/// Time format for time arrays
pub time_format: Option<String>, default = Some("%H:%M:%S%.f".to_string())
pub time_format: Option<String>,
validate = |value| validate_temporal_format("time_format", value),
default = Some("%H:%M:%S%.f".to_string())
/// Duration format. Can be either `"pretty"` or `"ISO8601"`
pub duration_format: ConfigDurationFormat, default = ConfigDurationFormat::Pretty
/// Show types in visual representation batches
pub types_info: bool, default = false
}
}

fn validate_temporal_format(name: &str, format: &str) -> Result<()> {
let items = chrono::format::StrftimeItems::new(format)
.parse()
.map_err(|error| {
DataFusionError::Configuration(format!(
"Invalid datafusion.format.{name}: {error}"
))
})?;

// Parsing validates directives, but some directives require fields
// that are absent from a particular temporal type (for example,
// `%H` for a date). Chrono reports those errors while formatting.
let mut rendered = String::new();
let result = match name {
"date_format" => chrono::NaiveDate::from_ymd_opt(2001, 2, 3)
.expect("valid sample date")
.format_with_items(items.iter())
.write_to(&mut rendered),
"datetime_format" | "timestamp_format" => {
chrono::NaiveDate::from_ymd_opt(2001, 2, 3)
.expect("valid sample date")
.and_hms_opt(4, 5, 6)
.expect("valid sample time")
.format_with_items(items.iter())
.write_to(&mut rendered)
}
"timestamp_tz_format" => chrono::DateTime::from_timestamp(981_173_106, 0)
.expect("valid sample timestamp")
.format_with_items(items.iter())
.write_to(&mut rendered),
"time_format" => chrono::NaiveTime::from_hms_opt(4, 5, 6)
.expect("valid sample time")
.format_with_items(items.iter())
.write_to(&mut rendered),
_ => unreachable!("all temporal format names are handled above"),
};
if result.is_err() {
return Err(DataFusionError::Configuration(format!(
"Invalid datafusion.format.{name}: format is incompatible with its temporal type"
)));
}
Ok(())
}

impl<'a> TryFrom<&'a FormatOptions> for arrow::util::display::FormatOptions<'a> {
type Error = DataFusionError;
fn try_from(options: &'a FormatOptions) -> Result<Self> {
Expand All @@ -2190,48 +2239,7 @@ impl<'a> TryFrom<&'a FormatOptions> for arrow::util::display::FormatOptions<'a>
("time_format", options.time_format.as_deref()),
] {
if let Some(format) = format {
let items = chrono::format::StrftimeItems::new(format).parse().map_err(
|error| {
DataFusionError::Configuration(format!(
"Invalid datafusion.format.{name}: {error}"
))
},
)?;

// Parsing validates directives, but some directives require fields
// that are absent from a particular temporal type (for example,
// `%H` for a date). Chrono reports those errors while formatting.
let mut rendered = String::new();
let result = match name {
"date_format" => chrono::NaiveDate::from_ymd_opt(2001, 2, 3)
.expect("valid sample date")
.format_with_items(items.iter())
.write_to(&mut rendered),
"datetime_format" | "timestamp_format" => {
chrono::NaiveDate::from_ymd_opt(2001, 2, 3)
.expect("valid sample date")
.and_hms_opt(4, 5, 6)
.expect("valid sample time")
.format_with_items(items.iter())
.write_to(&mut rendered)
}
"timestamp_tz_format" => {
chrono::DateTime::from_timestamp(981_173_106, 0)
.expect("valid sample timestamp")
.format_with_items(items.iter())
.write_to(&mut rendered)
}
"time_format" => chrono::NaiveTime::from_hms_opt(4, 5, 6)
.expect("valid sample time")
.format_with_items(items.iter())
.write_to(&mut rendered),
_ => unreachable!("all temporal format names are handled above"),
};
if result.is_err() {
return Err(DataFusionError::Configuration(format!(
"Invalid datafusion.format.{name}: format is incompatible with its temporal type"
)));
}
validate_temporal_format(name, format)?;
}
}

Expand Down
11 changes: 11 additions & 0 deletions datafusion/sqllogictest/test_files/set_variable.slt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ SHOW datafusion.format.time_format
----
datafusion.format.time_format %H-%M-%S

statement error datafusion\.format\.time_format
SET datafusion.format.time_format = '%Y'

statement error datafusion\.format\.time_format
SET datafusion.format.time_format = '%'

query TT
SHOW datafusion.format.time_format
----
datafusion.format.time_format %H-%M-%S

statement ok
RESET datafusion.format.time_format

Expand Down