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

villebro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 90275fe  Add convert_dttm method to SnowflakeEngineSpec (#8551)
90275fe is described below

commit 90275fe991a97f328e1a24bd5aa7030326ccf85e
Author: Ville Brofeldt <[email protected]>
AuthorDate: Wed Nov 13 11:13:49 2019 +0200

    Add convert_dttm method to SnowflakeEngineSpec (#8551)
---
 superset/db_engine_specs/snowflake.py    | 13 +++++++++++
 tests/db_engine_specs/snowflake_tests.py | 39 ++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/superset/db_engine_specs/snowflake.py 
b/superset/db_engine_specs/snowflake.py
index 4d69124..dc2e248 100644
--- a/superset/db_engine_specs/snowflake.py
+++ b/superset/db_engine_specs/snowflake.py
@@ -14,6 +14,8 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+from datetime import datetime
+from typing import Optional
 from urllib import parse
 
 from superset.db_engine_specs.postgres import PostgresBaseEngineSpec
@@ -61,3 +63,14 @@ class SnowflakeEngineSpec(PostgresBaseEngineSpec):
     @classmethod
     def epoch_ms_to_dttm(cls) -> str:
         return "DATEADD(MS, {col}, '1970-01-01')"
+
+    @classmethod
+    def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
+        tt = target_type.upper()
+        if tt == "DATE":
+            return f"TO_DATE('{dttm.date().isoformat()}')"
+        if tt == "DATETIME":
+            return f"""CAST('{dttm.isoformat(timespec="microseconds")}' AS 
DATETIME)"""
+        if tt == "TIMESTAMP":
+            return 
f"""TO_TIMESTAMP('{dttm.isoformat(timespec="microseconds")}')"""
+        return None
diff --git a/tests/db_engine_specs/snowflake_tests.py 
b/tests/db_engine_specs/snowflake_tests.py
new file mode 100644
index 0000000..52d38c2
--- /dev/null
+++ b/tests/db_engine_specs/snowflake_tests.py
@@ -0,0 +1,39 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from sqlalchemy import column
+
+from superset.db_engine_specs.snowflake import SnowflakeEngineSpec
+from tests.db_engine_specs.base_tests import DbEngineSpecTestCase
+
+
+class SnowflakeTestCase(DbEngineSpecTestCase):
+    def test_convert_dttm(self):
+        dttm = self.get_dttm()
+
+        self.assertEqual(
+            SnowflakeEngineSpec.convert_dttm("DATE", dttm), 
"TO_DATE('2019-01-02')"
+        )
+
+        self.assertEqual(
+            SnowflakeEngineSpec.convert_dttm("DATETIME", dttm),
+            "CAST('2019-01-02T03:04:05.678900' AS DATETIME)",
+        )
+
+        self.assertEqual(
+            SnowflakeEngineSpec.convert_dttm("TIMESTAMP", dttm),
+            "TO_TIMESTAMP('2019-01-02T03:04:05.678900')",
+        )

Reply via email to