Skip to content

Commit 3319220

Browse files
add time_zone into ConfigOptions (#3485)
* add time_zone into ConfigOptions * fix debug leftover * Update datafusion/core/src/config.rs Co-authored-by: Kun Liu <liukun@apache.org> Co-authored-by: Kun Liu <liukun@apache.org>
1 parent 6ffda68 commit 3319220

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

datafusion/core/src/config.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub const OPT_COALESCE_TARGET_BATCH_SIZE: &str =
4747
pub const OPT_OPTIMIZER_SKIP_FAILED_RULES: &str =
4848
"datafusion.optimizer.skip_failed_rules";
4949

50+
/// Configuration option "datafusion.execution.time_zone"
51+
pub const OPT_TIME_ZONE: &str = "datafusion.execution.time_zone";
52+
5053
/// Definition of a configuration option
5154
pub struct ConfigDefinition {
5255
/// key used to identifier this configuration option
@@ -102,6 +105,20 @@ impl ConfigDefinition {
102105
ScalarValue::UInt64(Some(default_value)),
103106
)
104107
}
108+
109+
/// Create a configuration option definition with a string value
110+
pub fn new_string(
111+
key: impl Into<String>,
112+
description: impl Into<String>,
113+
default_value: String,
114+
) -> Self {
115+
Self::new(
116+
key,
117+
description,
118+
DataType::Utf8,
119+
ScalarValue::Utf8(Some(default_value)),
120+
)
121+
}
105122
}
106123

107124
/// Contains definitions for all built-in configuration options
@@ -167,7 +184,14 @@ impl BuiltInConfigs {
167184
messages if any optimization rules produce errors and then proceed to the next \
168185
rule. When set to false, any rules that produce errors will cause the query to fail.",
169186
true
170-
)],
187+
),
188+
ConfigDefinition::new_string(
189+
OPT_TIME_ZONE,
190+
"The session time zone which some function require \
191+
e.g. EXTRACT(HOUR from SOME_TIME) shift the underline datetime according to the time zone,
192+
then extract the hour",
193+
"UTC".into()
194+
)]
171195
}
172196
}
173197

@@ -279,6 +303,14 @@ impl ConfigOptions {
279303
}
280304
}
281305

306+
/// get a string configuration option
307+
pub fn get_string(&self, key: &str) -> String {
308+
match self.get(key) {
309+
Some(ScalarValue::Utf8(Some(s))) => s,
310+
_ => "".into(),
311+
}
312+
}
313+
282314
/// Access the underlying hashmap
283315
pub fn options(&self) -> &HashMap<String, ScalarValue> {
284316
&self.options

datafusion/core/tests/sql/information_schema.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,41 @@ async fn show_all() {
703703
.len();
704704
assert_eq!(expected_length, results[0].num_rows());
705705
}
706+
707+
#[tokio::test]
708+
async fn show_time_zone_default_utc() {
709+
// https://github.com/apache/arrow-datafusion/issues/3255
710+
let ctx =
711+
SessionContext::with_config(SessionConfig::new().with_information_schema(true));
712+
let sql = "SHOW TIME ZONE";
713+
let results = plan_and_collect(&ctx, sql).await.unwrap();
714+
715+
let expected = vec![
716+
"+--------------------------------+---------+",
717+
"| name | setting |",
718+
"+--------------------------------+---------+",
719+
"| datafusion.execution.time_zone | UTC |",
720+
"+--------------------------------+---------+",
721+
];
722+
723+
assert_batches_eq!(expected, &results);
724+
}
725+
726+
#[tokio::test]
727+
async fn show_timezone_default_utc() {
728+
// https://github.com/apache/arrow-datafusion/issues/3255
729+
let ctx =
730+
SessionContext::with_config(SessionConfig::new().with_information_schema(true));
731+
let sql = "SHOW TIMEZONE";
732+
let results = plan_and_collect(&ctx, sql).await.unwrap();
733+
734+
let expected = vec![
735+
"+--------------------------------+---------+",
736+
"| name | setting |",
737+
"+--------------------------------+---------+",
738+
"| datafusion.execution.time_zone | UTC |",
739+
"+--------------------------------+---------+",
740+
];
741+
742+
assert_batches_eq!(expected, &results);
743+
}

datafusion/sql/src/planner.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2386,8 +2386,13 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
23862386
));
23872387
}
23882388

2389-
let query = if variable.to_lowercase() == "all" {
2389+
let variable_lower = variable.to_lowercase();
2390+
2391+
let query = if variable_lower == "all" {
23902392
String::from("SELECT name, setting FROM information_schema.df_settings")
2393+
} else if variable_lower == "timezone" || variable_lower == "time.zone" {
2394+
// we could introduce alias in OptionDefinition if this string matching thing grows
2395+
String::from("SELECT name, setting FROM information_schema.df_settings WHERE name = 'datafusion.execution.time_zone'")
23912396
} else {
23922397
format!(
23932398
"SELECT name, setting FROM information_schema.df_settings WHERE name = '{}'",

0 commit comments

Comments
 (0)