GitHub user dosubot[bot] added a comment to the discussion: REST API Endpoint 
for Downloading Superset Charts in CSV/Excel Format

<!-- Answer -->
Yes, it is possible to reconstruct the `queries` section from the `form_data` 
you receive via the Superset API. Superset's frontend uses the 
`buildQueryContext` and `buildQueryObject` functions to transform `form_data` 
into the full query context, including the `queries` array required by the 
`/api/v1/chart/data` endpoint. This process is also used internally for 
exporting data in CSV or Excel formats.

### How adhoc_filters are handled

- **adhoc_filters** from `form_data` are processed and split into the 
appropriate fields in the query object:
  - Simple adhoc filters with a `WHERE` clause become entries in the `filters` 
field.
  - SQL expression adhoc filters with a `WHERE` clause are combined into the 
`extras.where` field.
  - The same logic applies for `HAVING` clauses and `extras.having`.
- This means you do **not** copy `adhoc_filters` directly into `filters`, but 
rather process them as described above. Superset does this automatically in its 
query-building utilities 
([reference](https://github.com/apache/superset/blob/master/superset-frontend/packages/superset-ui-core/src/query/processFilters.ts)).

### Example: Reconstructing the queries section

Given your `form_data`, here is how you would reconstruct the `queries` section 
for the `/api/v1/chart/data` endpoint:

```json
{
  "datasource": "1__table",
  "queries": [
    {
      "columns": [
        { "expressionType": "SQL", "label": "YEAR", "sqlExpression": "YEAR" },
        { "expressionType": "SQL", "label": "QUARTER", "sqlExpression": 
"QUARTER" },
        { "expressionType": "SQL", "label": "MONTH", "sqlExpression": "MONTH" },
        { "expressionType": "SQL", "label": "PROMOTION_DATE", "sqlExpression": 
"PROMOTION_DATE" },
        { "expressionType": "SQL", "label": "CAMPAIGN NAME", "sqlExpression": 
"CAMPAIGN_NAME" }
      ],
      "metrics": [
        { "expressionType": "SQL", "label": "CONVERSION PERCENTAGE", 
"sqlExpression": "round((SUM(DEL_COUNT) * 100.0 / NULLIF(SUM(PUSHED_COUNT), 
0)), 2)" },
        { "expressionType": "SQL", "label": "PUSHED", "sqlExpression": 
"SUM(PUSHED_COUNT)" },
        { "expressionType": "SQL", "label": "DELIVERED", "sqlExpression": 
"SUM(DEL_COUNT)" }
      ],
      "filters": [],
      "extras": {
        "time_grain_sqla": "P1D",
        "having": "",
        "where": "(PROMOTION_DATE >= DATE_SUB(CURRENT_DATE, CAST(2 AS INTERVAL 
MONTH))) AND (PROMOTION_DATE <= CURRENT_DATE)"
      },
      "orderby": [
        [
          { "expressionType": "SQL", "label": "PUSHED", "sqlExpression": 
"SUM(PUSHED_COUNT)" },
          false
        ]
      ],
      "row_limit": 1000,
      "series_limit": 0,
      "series_limit_metric": { "expressionType": "SQL", "label": "PUSHED", 
"sqlExpression": "SUM(PUSHED_COUNT)" },
      "order_desc": true
    }
  ],
  "result_format": "csv",   // or "xlsx" for Excel
  "result_type": "full"
}
```

#### Notes:
- The `filters` field is empty here because your `adhoc_filters` are SQL 
expressions, so they are combined into the `extras.where` string.
- If you had simple (non-SQL) adhoc filters, they would appear in the `filters` 
array as objects with `col`, `op`, and `val` fields.
- The `extras.where` field is a concatenation of all SQL-type adhoc filters 
with a `WHERE` clause 
([reference](https://github.com/apache/superset/blob/master/superset-frontend/packages/superset-ui-core/src/query/processFilters.ts)).

### Summary

- **Yes, you can reconstruct the `queries` section from `form_data`** by 
following the mapping logic used in Superset's frontend utilities 
([reference](https://github.com/apache/superset/blob/master/superset-frontend/packages/superset-ui-core/src/query/buildQueryContext.ts)).
- **adhoc_filters** are not copied directly to `filters`, but are processed: 
simple filters go to `filters`, SQL expressions go to `extras.where` or 
`extras.having`.
- The example above matches your configuration and is ready for use with 
`/api/v1/chart/data`.

If you need a code example for automating this transformation or have more 
complex filter scenarios, let me know!


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://go.dosu.dev/dosubot), mention @dosu-bot.*

---
<sup>Help Dosu learn! Give it feedback: [Great 
Response](https://app.dosu.dev/response-feedback/57b9a951-4eae-4957-9494-c13d82de534b?feedback_type=great_response)
 | [Irrelevant 
Answer](https://app.dosu.dev/response-feedback/57b9a951-4eae-4957-9494-c13d82de534b?feedback_type=irrelevant_answer)
 | [Incorrect 
Sources](https://app.dosu.dev/response-feedback/57b9a951-4eae-4957-9494-c13d82de534b?feedback_type=incorrect_sources)
 | [Too 
Verbose](https://app.dosu.dev/response-feedback/57b9a951-4eae-4957-9494-c13d82de534b?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/57b9a951-4eae-4957-9494-c13d82de534b?feedback_type=hallucination)
 | [Bug 
Report](https://app.dosu.dev/response-feedback/57b9a951-4eae-4957-9494-c13d82de534b?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/57b9a951-4eae-4957-9494-c13d82de534b?feedback_type=other)</sup>


GitHub link: 
https://github.com/apache/superset/discussions/33574#discussioncomment-13256494

----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: 
notifications-unsubscr...@superset.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to