Skip to content

Apache DataFusion SQL Query Engine

add time_zone into ConfigOptions (#3485)

Wei-Ting Kuo331922036a23
diff --git a/datafusion/core/src/config.rs b/datafusion/core/src/config.rs
index 3c828079e2..00ccdd523c 100644
--- a/datafusion/core/src/config.rs
+++ b/datafusion/core/src/config.rs
@@ -47,6 +47,9 @@ pub const OPT_COALESCE_TARGET_BATCH_SIZE: &str =
pub const OPT_OPTIMIZER_SKIP_FAILED_RULES: &str =
"datafusion.optimizer.skip_failed_rules";
+/// Configuration option "datafusion.execution.time_zone"
+pub const OPT_TIME_ZONE: &str = "datafusion.execution.time_zone";
+
/// Definition of a configuration option
pub struct ConfigDefinition {
/// key used to identifier this configuration option
@@ -102,6 +105,20 @@ impl ConfigDefinition {
ScalarValue::UInt64(Some(default_value)),
)
}
+
+ /// Create a configuration option definition with a string value
+ pub fn new_string(
+ key: impl Into<String>,
+ description: impl Into<String>,
+ default_value: String,
+ ) -> Self {
+ Self::new(
+ key,
+ description,
+ DataType::Utf8,
+ ScalarValue::Utf8(Some(default_value)),
+ )
+ }
}
/// Contains definitions for all built-in configuration options
@@ -167,7 +184,14 @@ impl BuiltInConfigs {
messages if any optimization rules produce errors and then proceed to the next \
rule. When set to false, any rules that produce errors will cause the query to fail.",
true
- )],
+ ),
+ ConfigDefinition::new_string(
+ OPT_TIME_ZONE,
+ "The session time zone which some function require \
+ e.g. EXTRACT(HOUR from SOME_TIME) shift the underline datetime according to the time zone,
+ then extract the hour",
+ "UTC".into()
+ )]
}
}
@@ -279,6 +303,14 @@ impl ConfigOptions {
}
}
+ /// get a string configuration option
+ pub fn get_string(&self, key: &str) -> String {
+ match self.get(key) {
+ Some(ScalarValue::Utf8(Some(s))) => s,
+ _ => "".into(),
+ }
+ }
+
/// Access the underlying hashmap
pub fn options(&self) -> &HashMap<String, ScalarValue> {
&self.options
diff --git a/datafusion/core/tests/sql/information_schema.rs b/datafusion/core/tests/sql/information_schema.rs
index c9a5c9aa9c..8541661551 100644
--- a/datafusion/core/tests/sql/information_schema.rs
+++ b/datafusion/core/tests/sql/information_schema.rs
@@ -703,3 +703,41 @@ async fn show_all() {
.len();
assert_eq!(expected_length, results[0].num_rows());
}
+
+#[tokio::test]
+async fn show_time_zone_default_utc() {
+ // https://github.com/apache/arrow-datafusion/issues/3255
+ let ctx =
+ SessionContext::with_config(SessionConfig::new().with_information_schema(true));
+ let sql = "SHOW TIME ZONE";
+ let results = plan_and_collect(&ctx, sql).await.unwrap();
+
+ let expected = vec![
+ "+--------------------------------+---------+",
+ "| name | setting |",
+ "+--------------------------------+---------+",
+ "| datafusion.execution.time_zone | UTC |",
+ "+--------------------------------+---------+",
+ ];
+
+ assert_batches_eq!(expected, &results);
+}
+
+#[tokio::test]
+async fn show_timezone_default_utc() {
+ // https://github.com/apache/arrow-datafusion/issues/3255
+ let ctx =
+ SessionContext::with_config(SessionConfig::new().with_information_schema(true));
+ let sql = "SHOW TIMEZONE";
+ let results = plan_and_collect(&ctx, sql).await.unwrap();
+
+ let expected = vec![
+ "+--------------------------------+---------+",
+ "| name | setting |",
+ "+--------------------------------+---------+",
+ "| datafusion.execution.time_zone | UTC |",
+ "+--------------------------------+---------+",
+ ];
+
+ assert_batches_eq!(expected, &results);
+}
diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rs
index 0338d713a3..04518d81e4 100644
--- a/datafusion/sql/src/planner.rs
+++ b/datafusion/sql/src/planner.rs
@@ -2386,8 +2386,13 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
));
}
- let query = if variable.to_lowercase() == "all" {
+ let variable_lower = variable.to_lowercase();
+
+ let query = if variable_lower == "all" {
String::from("SELECT name, setting FROM information_schema.df_settings")
+ } else if variable_lower == "timezone" || variable_lower == "time.zone" {
+ // we could introduce alias in OptionDefinition if this string matching thing grows
+ String::from("SELECT name, setting FROM information_schema.df_settings WHERE name = 'datafusion.execution.time_zone'")
} else {
format!(
"SELECT name, setting FROM information_schema.df_settings WHERE name = '{}'",