You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by mi...@apache.org on 2023/12/29 12:00:12 UTC

(superset) branch master updated: fix(databend): databend time grain expression (#26378)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3a0391bbb9 fix(databend): databend time grain expression (#26378)
3a0391bbb9 is described below

commit 3a0391bbb9138f1a3dab4d3c10d08e5520a4274d
Author: Jeremy <ha...@outlook.com>
AuthorDate: Fri Dec 29 20:00:05 2023 +0800

    fix(databend): databend time grain expression (#26378)
---
 superset/db_engine_specs/databend.py | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/superset/db_engine_specs/databend.py b/superset/db_engine_specs/databend.py
index 8ad4713024..ff28ee08a3 100644
--- a/superset/db_engine_specs/databend.py
+++ b/superset/db_engine_specs/databend.py
@@ -18,6 +18,7 @@ from sqlalchemy import types
 from sqlalchemy.engine.url import URL
 from urllib3.exceptions import NewConnectionError
 
+from superset.constants import TimeGrain
 from superset.databases.utils import make_url_safe
 from superset.db_engine_specs.base import (
     BaseEngineSpec,
@@ -45,17 +46,17 @@ class DatabendBaseEngineSpec(BaseEngineSpec):
 
     _time_grain_expressions = {
         None: "{col}",
-        "PT1M": "to_start_of_minute(TO_DATETIME({col}))",
-        "PT5M": "to_start_of_five_minutes(TO_DATETIME({col}))",
-        "PT10M": "to_start_of_ten_minutes(TO_DATETIME({col}))",
-        "PT15M": "to_start_of_fifteen_minutes(TO_DATETIME({col}))",
-        "PT30M": "TO_DATETIME(intDiv(toUInt32(TO_DATETIME({col})), 1800)*1800)",
-        "PT1H": "to_start_of_hour(TO_DATETIME({col}))",
-        "P1D": "to_start_of_day(TO_DATETIME({col}))",
-        "P1W": "to_monday(TO_DATETIME({col}))",
-        "P1M": "to_start_of_month(TO_DATETIME({col}))",
-        "P3M": "to_start_of_quarter(TO_DATETIME({col}))",
-        "P1Y": "to_start_of_year(TO_DATETIME({col}))",
+        TimeGrain.SECOND: "DATE_TRUNC('SECOND', {col})",
+        TimeGrain.MINUTE: "to_start_of_minute(TO_DATETIME({col}))",
+        TimeGrain.FIVE_MINUTES: "to_start_of_five_minutes(TO_DATETIME({col}))",
+        TimeGrain.TEN_MINUTES: "to_start_of_ten_minutes(TO_DATETIME({col}))",
+        TimeGrain.FIFTEEN_MINUTES: "to_start_of_fifteen_minutes(TO_DATETIME({col}))",
+        TimeGrain.HOUR: "to_start_of_hour(TO_DATETIME({col}))",
+        TimeGrain.DAY: "to_start_of_day(TO_DATETIME({col}))",
+        TimeGrain.WEEK: "to_monday(TO_DATETIME({col}))",
+        TimeGrain.MONTH: "to_start_of_month(TO_DATETIME({col}))",
+        TimeGrain.QUARTER: "to_start_of_quarter(TO_DATETIME({col}))",
+        TimeGrain.YEAR: "to_start_of_year(TO_DATETIME({col}))",
     }
 
     column_type_mappings = (
@@ -133,6 +134,8 @@ class DatabendBaseEngineSpec(BaseEngineSpec):
 
         if isinstance(sqla_type, types.Date):
             return f"to_date('{dttm.date().isoformat()}')"
+        if isinstance(sqla_type, types.TIMESTAMP):
+            return f"""TO_TIMESTAMP('{dttm.isoformat(timespec="microseconds")}')"""
         if isinstance(sqla_type, types.DateTime):
             return f"""to_dateTime('{dttm.isoformat(sep=" ", timespec="seconds")}')"""
         return None