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

fix(sqla): replace custom dttm type with literal_column #19917

Merged
merged 1 commit into from
May 3, 2022

Conversation

villebro
Copy link
Member

@villebro villebro commented May 2, 2022

SUMMARY

Some time ago #13622 introduced support for emitting temporal filters from native/cross filters. This caused a regression that was fixed in #16634, which itself caused another regression #17143. The original feature, which relied on creating a custom temporal type that can be used to compile temporal expressions in SQLAlchemy queries proved to be problematic, in part due to the fact that it required using the DATETIME type which isn't natvely supported by Presto.

For these types of use cases, the documentation recommends creating a custom class that extends TypeDecorator and implementing process_bind_param and/or process_literal_value: https://docs.sqlalchemy.org/en/14/core/custom_types.html#typedecorator-recipes. However, during testing this was shown to not work universally, as some dialects expected the return value to be a date/datetime, while others expected it to always be a str.

A simpler and universally supported solution is to just use literal_column. This simplifies the codebase and should ensure that temporal adhoc filters always work. The temporal conversion functions for Presto, Trino and Athena are also updated to use the recommended casts from the docs (note how from_iso8601_* is no longer recommended):

Note that this change might be seen as making the filter_values_handler helper function slightly messy with the added arguments. However, the function can be streamlined once the native Druid connector is removed and the helper can be made more specific to the SQLAlchemy model.

This PR doesn't add new tests, as this test covers these changes:

@unittest.skip("Failing due to timezone difference")
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_chart_data_dttm_filter(self):
"""
Chart data API: Ensure temporal column filter converts epoch to dttm expression
"""
table = self.get_birth_names_dataset()
if table.database.backend == "presto":
# TODO: date handling on Presto not fully in line with other engine specs
return
self.query_context_payload["queries"][0]["time_range"] = ""
dttm = self.get_dttm()
ms_epoch = dttm.timestamp() * 1000
self.query_context_payload["queries"][0]["filters"][0] = {
"col": "ds",
"op": "!=",
"val": ms_epoch,
}
rv = self.post_assert_metric(CHART_DATA_URI, self.query_context_payload, "data")
response_payload = json.loads(rv.data.decode("utf-8"))
result = response_payload["result"][0]
# assert that unconverted timestamp is not present in query
assert str(ms_epoch) not in result["query"]
# assert that converted timestamp is present in query where supported
dttm_col: Optional[TableColumn] = None
for col in table.columns:
if col.column_name == table.main_dttm_col:
dttm_col = col
if dttm_col:
dttm_expression = table.database.db_engine_spec.convert_dttm(
dttm_col.type,
dttm,
)
self.assertIn(dttm_expression, result["query"])
else:
raise Exception("ds column not found")

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue: Closes Presto engine spec broken - Date column mapped to Datetime #17143
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@villebro villebro force-pushed the villebro/remove-custom-dttm-type branch 5 times, most recently from 1d34e59 to c77c5aa Compare May 2, 2022 12:51
@codecov
Copy link

codecov bot commented May 2, 2022

Codecov Report

Merging #19917 (add1492) into master (aff10a7) will decrease coverage by 0.18%.
The diff coverage is 85.71%.

❗ Current head add1492 differs from pull request most recent head b8c4c7e. Consider uploading reports for the commit b8c4c7e to get more accurate results

@@            Coverage Diff             @@
##           master   #19917      +/-   ##
==========================================
- Coverage   66.52%   66.34%   -0.19%     
==========================================
  Files        1714     1713       -1     
  Lines       65052    65035      -17     
  Branches     6722     6722              
==========================================
- Hits        43279    43148     -131     
- Misses      20061    20175     +114     
  Partials     1712     1712              
Flag Coverage Δ
hive ?
mysql 81.94% <85.71%> (+<0.01%) ⬆️
postgres 81.99% <85.71%> (+<0.01%) ⬆️
presto ?
python 82.04% <85.71%> (-0.37%) ⬇️
sqlite ?
unit ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
superset/connectors/druid/models.py 82.99% <ø> (ø)
superset/db_engine_specs/base.py 87.45% <ø> (-0.90%) ⬇️
superset/connectors/base/models.py 88.21% <70.00%> (-0.44%) ⬇️
superset/connectors/sqla/models.py 88.75% <100.00%> (-1.30%) ⬇️
superset/db_engine_specs/athena.py 89.65% <100.00%> (ø)
superset/db_engine_specs/presto.py 83.36% <100.00%> (-5.34%) ⬇️
superset/db_engine_specs/trino.py 71.42% <100.00%> (ø)
superset/db_engines/hive.py 0.00% <0.00%> (-85.19%) ⬇️
superset/db_engine_specs/hive.py 70.11% <0.00%> (-15.71%) ⬇️
superset/db_engine_specs/sqlite.py 91.89% <0.00%> (-5.41%) ⬇️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aff10a7...b8c4c7e. Read the comment docs.

@villebro villebro force-pushed the villebro/remove-custom-dttm-type branch from c77c5aa to b8c4c7e Compare May 2, 2022 14:21
@villebro villebro changed the title [WIP] fix(sqla): replace custom dttm type with literal_column fix(sqla): replace custom dttm type with literal_column May 2, 2022
Copy link
Member

@nytai nytai left a comment

Choose a reason for hiding this comment

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

Confirmed fix works using a presto connection, date column is correctly identified as Date type and time filters work now.

@villebro villebro merged commit 99f1f9e into apache:master May 3, 2022
@villebro villebro deleted the villebro/remove-custom-dttm-type branch May 3, 2022 06:59
hughhhh pushed a commit to hve-labs/superset that referenced this pull request May 11, 2022
villebro added a commit that referenced this pull request May 26, 2022
michael-s-molina pushed a commit that referenced this pull request May 26, 2022
philipher29 pushed a commit to ValtechMobility/superset that referenced this pull request Jun 9, 2022
@mistercrunch mistercrunch added 🍒 1.5.1 🍒 1.5.2 🍒 1.5.3 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 2.0.0 labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels lts-v1 preset-io size/L 🍒 1.5.1 🍒 1.5.2 🍒 1.5.3 🚢 2.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Presto engine spec broken - Date column mapped to Datetime
4 participants