codeant-ai-for-open-source[bot] commented on code in PR #42639:
URL: https://github.com/apache/superset/pull/42639#discussion_r3690559439
##########
superset/db_engine_specs/postgres.py:
##########
@@ -266,21 +266,39 @@ def get_timestamp_expr(
time_grain: str | None,
) -> TimestampExpression:
"""
- Construct a timestamp expression while preserving pure ``DATE``
semantics.
+ Construct a timestamp expression while preserving pure ``DATE``
semantics
+ and handling string columns marked as temporal.
Applying ``DATE_TRUNC`` to a ``DATE`` column implicitly casts the
value to
``TIMESTAMP``, which can trigger unwanted timezone conversion on the
client
and shift the displayed date by a day. To avoid this, the truncated
value is
cast back to ``DATE`` when the source column is a pure ``DATE`` type.
See https://github.com/apache/superset/issues/42254.
+
+ ``DATE_TRUNC`` does not accept ``VARCHAR``/``TEXT`` arguments, so when
a
+ string column is manually marked as temporal, its value is cast to
+ ``TIMESTAMP`` before the grain is applied. Otherwise the query fails
with
+ ``function date_trunc(unknown, character varying) does not exist``.
+
+ See https://github.com/apache/superset/issues/42386.
"""
expr = super().get_timestamp_expr(col, pdf, time_grain)
col_type = getattr(col, "type", None)
# ``DateTime``/``TIMESTAMP`` are distinct SQLAlchemy types (not
subclasses
# of ``Date``), so this only matches pure ``DATE`` columns.
if time_grain and isinstance(col_type, Date):
return TimestampExpression(f"CAST({expr.name} AS DATE)", col,
type_=Date())
+ if (
+ time_grain
+ and pdf not in ("epoch_s", "epoch_ms", "%Y")
+ and isinstance(col_type, (String, Text))
Review Comment:
**Suggestion:** The cast is gated on `col.type` being a string type, but SQL
Lab and adhoc-column paths construct temporal expressions with untyped
`literal_column()` objects even after metadata identifies them as temporal.
Those callers therefore still generate `DATE_TRUNC(..., raw_string_expression)`
and PostgreSQL raises the same overload error. Propagate the discovered
SQLAlchemy type to the literal column or apply the cast using the temporal
column metadata before calling this method. [incomplete implementation]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ SQL Lab adhoc time-series queries can still fail.
- ❌ Adhoc chart columns may retain invalid DATE_TRUNC SQL.
- ⚠️ Physical-column paths remain fixed when metadata supplies types.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2eb2998d10914cf098d7107229e896bd&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=2eb2998d10914cf098d7107229e896bd&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/db_engine_specs/postgres.py
**Line:** 295:295
**Comment:**
*Incomplete Implementation: The cast is gated on `col.type` being a
string type, but SQL Lab and adhoc-column paths construct temporal expressions
with untyped `literal_column()` objects even after metadata identifies them as
temporal. Those callers therefore still generate `DATE_TRUNC(...,
raw_string_expression)` and PostgreSQL raises the same overload error.
Propagate the discovered SQLAlchemy type to the literal column or apply the
cast using the temporal column metadata before calling this method.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42639&comment_hash=479572f19338a197b904f9d2b2b8f5fcd7e4c4b69547f5787535df2401ef35ed&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42639&comment_hash=479572f19338a197b904f9d2b2b8f5fcd7e4c4b69547f5787535df2401ef35ed&reaction=dislike'>👎</a>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]