mikebridge commented on PR #42052:
URL: https://github.com/apache/superset/pull/42052#issuecomment-4974392621

   ### How this change fits into query construction
   
   `QueryContextFactory` is the backend normalization step between the 
chart-data request and dataset SQL generation:
   
   ```text
   Browser chart configuration
     → QueryObjectFactory
     → QueryContextFactory
     → dataset SQL generator
     → database
   ```
   
   `QueryObjectFactory` parses a dashboard `time_range` into `from_dttm` and 
`to_dttm`. Those values describe the bounds, but they do not identify the 
physical datetime column that belongs in the SQL `WHERE` clause.
   
   In this code path, `QueryObject.granularity` is the physical datetime column 
used as the time-filter subject; the time grain such as hour/day/month is 
represented separately. Given:
   
   ```python
   granularity = "event_time"
   from_dttm = ...
   to_dttm = ...
   ```
   
   the dataset SQL generator can produce:
   
   ```sql
   WHERE event_time >= ...
     AND event_time < ...
   ```
   
   Before this change, `_apply_granularity()` only reconciled the x-axis and 
temporal filters when `query_object.granularity` was already populated. For an 
adhoc SQL-expression x-axis, the request could contain valid 
`from_dttm`/`to_dttm` values but no granularity. The SQL generator therefore 
had bounds without a physical column and omitted the time predicate, allowing 
ClickHouse to read the full table and hit `max_rows_to_read`.
   
   This patch adds a narrow fallback. When:
   
   - the x-axis is an adhoc SQL expression;
   - the query has no explicit granularity;
   - at least one time bound exists; and
   - the dataset main datetime column is present and temporal;
   
   we set the validated main datetime column as the filter subject.
   
   The immediate `return` is intentional. The rest of `_apply_granularity()` 
handles explicitly selected granularities and may rewrite the temporal x-axis 
or remove a temporal filter that it considers replaced. An inferred granularity 
has a narrower purpose: supply the physical column needed for the time 
predicate while leaving the SQL-expression grouping axis and independent 
temporal filters unchanged.
   
   The resulting query keeps the expression in `SELECT`/`GROUP BY` and adds the 
dashboard bounds against the physical datetime column in `WHERE`.


-- 
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]

Reply via email to