Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(druid): Standardizing time grain transformations #17050

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions superset/db_engine_specs/druid.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ class DruidEngineSpec(BaseEngineSpec):

_time_grain_expressions = {
None: "{col}",
"PT1S": "FLOOR({col} TO SECOND)",
"PT1S": "TIME_FLOOR({col}, 'PT1S')",
"PT5S": "TIME_FLOOR({col}, 'PT5S')",
"PT30S": "TIME_FLOOR({col}, 'PT30S')",
"PT1M": "FLOOR({col} TO MINUTE)",
"PT1M": "TIME_FLOOR({col}, 'PT1M')",
"PT5M": "TIME_FLOOR({col}, 'PT5M')",
"PT10M": "TIME_FLOOR({col}, 'PT10M')",
"PT15M": "TIME_FLOOR({col}, 'PT15M')",
"PT0.5H": "TIME_FLOOR({col}, 'PT30M')",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@villebro I've never understood why this is PT0.5H ("Half hour") as opposed to PT30M ("30 minute"). I was thinking of doing a pass to change these—which will require a database migration. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've wondered about this before, as well as why P0.25Y is used in Superset instead of P3M. I tried looking at ISO-8601 to see if there's any guidance there, but I can't find anything. So I think they might be used interchangeably. However, since the decimal character varies from country to country, I think we'd be better off replacing PT0.5H and P0.25Y with PT30M and P3M respectively.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because originally it was called just "half an hour", so PT0.5H mad sense. It was a direct translation, and I was just looking for a way to standardize the intervals across DB engine specs.

"PT1H": "FLOOR({col} TO HOUR)",
"PT1H": "TIME_FLOOR({col}, 'PT1H')",
"PT6H": "TIME_FLOOR({col}, 'PT6H')",
"P1D": "FLOOR({col} TO DAY)",
"P1W": "FLOOR({col} TO WEEK)",
"P1M": "FLOOR({col} TO MONTH)",
"P0.25Y": "FLOOR({col} TO QUARTER)",
"P1Y": "FLOOR({col} TO YEAR)",
"P1D": "TIME_FLOOR({col}, 'P1D')",
"P1W": "TIME_FLOOR({col}, 'P1W')",
"P1M": "TIME_FLOOR({col}, 'P1M')",
"P0.25Y": "TIME_FLOOR({col}, 'P0.25Y')",
"P1Y": "TIME_FLOOR({col}, 'P1Y')",
"P1W/1970-01-03T00:00:00Z": (
"TIMESTAMPADD(DAY, 5, FLOOR(TIMESTAMPADD(DAY, 1, {col}) TO WEEK))"
"TIME_SHIFT(TIME_FLOOR(TIME_SHIFT({col}, 'P1D', 1), 'P1W'), 'P1D', 5)"
),
"1969-12-28T00:00:00Z/P1W": (
"TIMESTAMPADD(DAY, -1, FLOOR(TIMESTAMPADD(DAY, 1, {col}) TO WEEK))"
"TIME_SHIFT(TIME_FLOOR(TIME_SHIFT({col}, 'P1D', 1), 'P1W'), 'P1D', -1)"
),
}

Expand Down
4 changes: 3 additions & 1 deletion tests/integration_tests/db_engine_specs/druid_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ def test_timegrain_expressions(self):
col = "__time"
sqla_col = column(col)
test_cases = {
"PT1S": f"FLOOR({col} TO SECOND)",
"PT1S": f"TIME_FLOOR({col}, 'PT1S')",
"PT5M": f"TIME_FLOOR({col}, 'PT5M')",
"P1W/1970-01-03T00:00:00Z": f"TIME_SHIFT(TIME_FLOOR(TIME_SHIFT({col}, 'P1D', 1), 'P1W'), 'P1D', 5)",
"1969-12-28T00:00:00Z/P1W": f"TIME_SHIFT(TIME_FLOOR(TIME_SHIFT({col}, 'P1D', 1), 'P1W'), 'P1D', -1)",
}
for grain, expected in test_cases.items():
actual = DruidEngineSpec.get_timestamp_expr(
Expand Down