codeant-ai-for-open-source[bot] commented on code in PR #42639:
URL: https://github.com/apache/superset/pull/42639#discussion_r3690839370
##########
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))
+ ):
+ return TimestampExpression(
+ expr.name.replace("{col}", "CAST({col} AS TIMESTAMP)"),
+ col,
+ type_=DateTime(),
+ )
Review Comment:
**Suggestion:** The new branch applies a native PostgreSQL `CAST(... AS
TIMESTAMP)` to every temporal string column regardless of `pdf`. Columns
configured with formats such as `%d/%m/%Y` or other non-ISO layouts can contain
values that PostgreSQL cannot parse with a plain cast, so queries that apply a
grain still fail at runtime. Preserve format-aware parsing for custom date
formats, or restrict this cast to formats that PostgreSQL accepts directly.
[time/date]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ Time-series charts fail for custom-formatted temporal strings.
- ❌ Grained temporal filters can fail during SQL execution.
- ⚠️ PostgreSQL receives format-unaware timestamp casts.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=9a8e5cb304fd433c86630bfd5ab07ad2&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=9a8e5cb304fd433c86630bfd5ab07ad2&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:** 292:301
**Comment:**
*Time Date: The new branch applies a native PostgreSQL `CAST(... AS
TIMESTAMP)` to every temporal string column regardless of `pdf`. Columns
configured with formats such as `%d/%m/%Y` or other non-ISO layouts can contain
values that PostgreSQL cannot parse with a plain cast, so queries that apply a
grain still fail at runtime. Preserve format-aware parsing for custom date
formats, or restrict this cast to formats that PostgreSQL accepts directly.
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=789489f1fbf052aff57dcd94679b1944d984fe6258a6c6addd124bca7d2509f4&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42639&comment_hash=789489f1fbf052aff57dcd94679b1944d984fe6258a6c6addd124bca7d2509f4&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]