You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by vi...@apache.org on 2021/01/15 11:48:42 UTC

[superset] 06/08: Fixing Pinot queries for time granularities: WEEKS/MONTHS/QUARTERS/YEARS (#12536)

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

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

commit 5f2de1df71a13866a86465c9d8c11881663b617a
Author: Xiang Fu <fx...@gmail.com>
AuthorDate: Fri Jan 15 01:05:31 2021 -0800

    Fixing Pinot queries for time granularities: WEEKS/MONTHS/QUARTERS/YEARS (#12536)
---
 superset/db_engine_specs/pinot.py    | 26 +++++++++++++++++++++-----
 tests/db_engine_specs/pinot_tests.py | 12 ++++++++++--
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/superset/db_engine_specs/pinot.py b/superset/db_engine_specs/pinot.py
index 7a11d08..1f504b8 100644
--- a/superset/db_engine_specs/pinot.py
+++ b/superset/db_engine_specs/pinot.py
@@ -35,10 +35,10 @@ class PinotEngineSpec(BaseEngineSpec):  # pylint: disable=abstract-method
         "PT1M": "1:MINUTES",
         "PT1H": "1:HOURS",
         "P1D": "1:DAYS",
-        "P1W": "1:WEEKS",
-        "P1M": "1:MONTHS",
-        "P0.25Y": "3:MONTHS",
-        "P1Y": "1:YEARS",
+        "P1W": "week",
+        "P1M": "month",
+        "P0.25Y": "quarter",
+        "P1Y": "year",
     }
 
     _python_to_java_time_patterns: Dict[str, str] = {
@@ -50,6 +50,17 @@ class PinotEngineSpec(BaseEngineSpec):  # pylint: disable=abstract-method
         "%S": "ss",
     }
 
+    _use_date_trunc_function: Dict[str, bool] = {
+        "PT1S": False,
+        "PT1M": False,
+        "PT1H": False,
+        "P1D": False,
+        "P1W": True,
+        "P1M": True,
+        "P0.25Y": True,
+        "P1Y": True,
+    }
+
     @classmethod
     def get_timestamp_expr(
         cls,
@@ -86,8 +97,13 @@ class PinotEngineSpec(BaseEngineSpec):  # pylint: disable=abstract-method
                 raise NotImplementedError("No pinot grain spec for " + str(time_grain))
         else:
             return TimestampExpression("{{col}}", col)
+
         # In pinot the output is a string since there is no timestamp column like pg
-        time_expr = f"DATETIMECONVERT({{col}}, '{tf}', '{tf}', '{granularity}')"
+        if cls._use_date_trunc_function.get(time_grain):
+            time_expr = f"DATETRUNC('{granularity}', {{col}}, '{seconds_or_ms}')"
+        else:
+            time_expr = f"DATETIMECONVERT({{col}}, '{tf}', '{tf}', '{granularity}')"
+
         return TimestampExpression(time_expr, col)
 
     @classmethod
diff --git a/tests/db_engine_specs/pinot_tests.py b/tests/db_engine_specs/pinot_tests.py
index 9732827..4dcca0c 100644
--- a/tests/db_engine_specs/pinot_tests.py
+++ b/tests/db_engine_specs/pinot_tests.py
@@ -23,11 +23,19 @@ from tests.db_engine_specs.base_tests import TestDbEngineSpec
 class TestPinotDbEngineSpec(TestDbEngineSpec):
     """ Tests pertaining to our Pinot database support """
 
+    def test_pinot_time_expression_sec_one_1d_grain(self):
+        col = column("tstamp")
+        expr = PinotEngineSpec.get_timestamp_expr(col, "epoch_s", "P1D")
+        result = str(expr.compile())
+        self.assertEqual(
+            result,
+            "DATETIMECONVERT(tstamp, '1:SECONDS:EPOCH', '1:SECONDS:EPOCH', '1:DAYS')",
+        )  # noqa
+
     def test_pinot_time_expression_sec_one_1m_grain(self):
         col = column("tstamp")
         expr = PinotEngineSpec.get_timestamp_expr(col, "epoch_s", "P1M")
         result = str(expr.compile())
         self.assertEqual(
-            result,
-            "DATETIMECONVERT(tstamp, '1:SECONDS:EPOCH', '1:SECONDS:EPOCH', '1:MONTHS')",
+            result, "DATETRUNC('month', tstamp, 'SECONDS')",
         )  # noqa