refactor: refine timestamp expr function (#21510)

This commit is contained in:
Yongjie Zhao 2022-09-20 18:51:01 +08:00 committed by GitHub
parent 42000823be
commit 737d4dcf0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 9 deletions

View File

@ -375,9 +375,7 @@ class TableColumn(Model, BaseColumn, CertificationMixin):
col = literal_column(expression, type_=type_)
else:
col = column(self.column_name, type_=type_)
time_expr = self.db_engine_spec.get_timestamp_expr(
col, pdf, time_grain, self.type
)
time_expr = self.db_engine_spec.get_timestamp_expr(col, pdf, time_grain)
return self.table.make_sqla_column_compatible(time_expr, label)
def dttm_sql_literal(self, dttm: DateTime) -> str:
@ -1147,7 +1145,6 @@ class SqlaTable(Model, BaseDatasource): # pylint: disable=too-many-public-metho
col=sqla_column,
pdf=None,
time_grain=time_grain,
type_=str(getattr(sqla_column, "type", "")),
)
return self.make_sqla_column_compatible(sqla_column, label)

View File

@ -476,7 +476,6 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
col: ColumnClause,
pdf: Optional[str],
time_grain: Optional[str],
type_: Optional[str] = None,
) -> TimestampExpression:
"""
Construct a TimestampExpression to be used in a SQLAlchemy query.
@ -484,10 +483,10 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
:param col: Target column for the TimestampExpression
:param pdf: date format (seconds or milliseconds)
:param time_grain: time grain, e.g. P1Y for 1 year
:param type_: the source column type
:return: TimestampExpression object
"""
if time_grain:
type_ = str(getattr(col, "type", ""))
time_expr = cls.get_time_grain_expressions().get(time_grain)
if not time_expr:
raise NotImplementedError(

View File

@ -75,7 +75,6 @@ class PinotEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method
col: ColumnClause,
pdf: Optional[str],
time_grain: Optional[str],
type_: Optional[str] = None,
) -> TimestampExpression:
if not pdf:
raise NotImplementedError(f"Empty date format for '{col}'")

View File

@ -77,8 +77,9 @@ class TestBigQueryDbEngineSpec(TestDbEngineSpec):
"TIMESTAMP": "TIMESTAMP_TRUNC(temporal, HOUR)",
}
for type_, expected in test_cases.items():
col.type = type_
actual = BigQueryEngineSpec.get_timestamp_expr(
col=col, pdf=None, time_grain="PT1H", type_=type_
col=col, pdf=None, time_grain="PT1H"
)
self.assertEqual(str(actual), expected)
@ -99,8 +100,9 @@ class TestBigQueryDbEngineSpec(TestDbEngineSpec):
") AS TIMESTAMP)",
}
for type_, expected in test_cases.items():
col.type = type_
actual = BigQueryEngineSpec.get_timestamp_expr(
col=col, pdf=None, time_grain="PT5M", type_=type_
col=col, pdf=None, time_grain="PT5M"
)
assert str(actual) == expected