This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new 331922036 add time_zone into ConfigOptions (#3485)
331922036 is described below

commit 331922036a23265084ab091fa2ab5c57da42c841
Author: Wei-Ting Kuo <[email protected]>
AuthorDate: Fri Sep 16 01:04:17 2022 +0800

    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 <[email protected]>
    
    Co-authored-by: Kun Liu <[email protected]>
---
 datafusion/core/src/config.rs                   | 34 +++++++++++++++++++++-
 datafusion/core/tests/sql/information_schema.rs | 38 +++++++++++++++++++++++++
 datafusion/sql/src/planner.rs                   |  7 ++++-
 3 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/datafusion/core/src/config.rs b/datafusion/core/src/config.rs
index 3c828079e..00ccdd523 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 c9a5c9aa9..854166155 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 0338d713a..04518d81e 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 = '{}'",

Reply via email to