codeant-ai-for-open-source[bot] commented on code in PR #38766:
URL: https://github.com/apache/superset/pull/38766#discussion_r3530277780
##########
superset/models/sql_lab.py:
##########
@@ -446,14 +446,32 @@ def adhoc_column_to_sqla(
:rtype: tuple[sqlalchemy.sql.ColumnElement, Optional[GenericDataType]]
"""
label = get_column_name(col)
+ sql_expression = col["sqlExpression"]
+ time_grain = col.get("timeGrain")
+ has_timegrain = col.get("columnType") == "BASE_AXIS" and time_grain
+ is_dttm = False
+ pdf = None
+
+ if col_in_metadata := self.get_column(sql_expression):
+ is_dttm = col_in_metadata.is_temporal
+ pdf = col_in_metadata.python_date_format
Review Comment:
**Suggestion:** The new temporal-column lookup uses the raw `sqlExpression`
string, but adhoc expressions can contain templating or extra whitespace that
is only resolved during SQL processing. In those cases metadata lookup fails,
`is_dttm` stays false, and the selected time grain is silently skipped.
Normalize/render the expression used for metadata lookup (as done in the
SqlaTable path) before calling `get_column` so time grain is applied
consistently. [incomplete implementation]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ Query-based chart time grain ignored for formatted expressions.
- ⚠️ Unsaved SQL Lab charts show ungrouped temporal data.
- ⚠️ Inconsistent time grain handling between query and dataset.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Issue a chart-data request whose datasource is a SQL Lab query, so
`DatasourceDAO.get_datasource(DatasourceType.QUERY, id)`
(superset/daos/datasource.py:50-55, 58-92) returns a `Query` object
(superset/models/sql_lab.py:116-128) as the Explore datasource.
2. Ensure that `Query.extra["columns"]` contains a temporal column (e.g.
`"column_name":
"event_time", "is_dttm": True`), so `Query.columns` builds `TableColumn`
objects with
`column_name` and `is_dttm` set (superset/models/sql_lab.py:33-47).
3. In the chart request payload, include an adhoc column in
`queries[0]["columns"]` with
`columnType: "BASE_AXIS"`, a non-null `timeGrain` (e.g. `"P1D"`), and
`sqlExpression`
referencing that temporal column but with additional formatting (e.g.
whitespace or
templating) so the string differs from the plain `column_name` stored in
`extra["columns"]`.
4. When the backend builds the query, `ExploreMixin.get_sqla_query`
(superset/models/helpers.py:3180-3439) encounters this adhoc column and calls
`self.adhoc_column_to_sqla(col=selected,
template_processor=template_processor)`
(helpers.py:90-93), which routes to `Query.adhoc_column_to_sqla`
(superset/models/sql_lab.py:433-98); there, `sql_expression` is taken as-is
from the adhoc
dict, `self.get_column(sql_expression)` (sql_lab.py:46-52) fails to match any
`TableColumn.column_name`, so `col_in_metadata` is None, `is_dttm` stays
False, and the
time-grain branch at sql_lab.py:89-93 is skipped, causing the chart to use
un-grained
timestamps despite a valid `timeGrain` being requested.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e39b2f43d1194ddea2ce7c3ac17dfb7e&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=e39b2f43d1194ddea2ce7c3ac17dfb7e&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/models/sql_lab.py
**Line:** 455:457
**Comment:**
*Incomplete Implementation: The new temporal-column lookup uses the raw
`sqlExpression` string, but adhoc expressions can contain templating or extra
whitespace that is only resolved during SQL processing. In those cases metadata
lookup fails, `is_dttm` stays false, and the selected time grain is silently
skipped. Normalize/render the expression used for metadata lookup (as done in
the SqlaTable path) before calling `get_column` so time grain is applied
consistently.
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%2F38766&comment_hash=5c0492d9e6df2cc177dc7eba5c432885cdfe4ffe3b41d2540830031ca181a688&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38766&comment_hash=5c0492d9e6df2cc177dc7eba5c432885cdfe4ffe3b41d2540830031ca181a688&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]