shivamgoel opened a new pull request, #43526:
URL: https://github.com/apache/superset/pull/43526

   ### SUMMARY
   
   Superset has no REST API for querying a datasource by its semantic 
definitions. The only
   option today is `POST /api/v1/chart/data`, which is built for the Explore 
UI: callers must
   hand-construct a `query_context` including `BASE_AXIS`/`SERIES` adhoc 
columns and
   `post_processing` chains, get no validation of metric or dimension names, 
and couple
   themselves to a payload whose shape follows frontend needs. In practice 
external consumers
   reverse-engineer a `query_context` from a saved chart's `form_data`, which 
breaks on
   viz-type changes.
   
   This adds `POST /api/v1/datasource/<type>/<id>/query`, taking a name-based 
payload of
   `metrics`, `dimensions`, `filters`, `time_range`, `time_grain`, 
`limit`/`offset` and
   `order`, plus `GET /api/v1/datasource/<type>/<id>` for metadata and 
capabilities. Both
   live on the existing `DatasourceRestApi`, which already serves this URL 
shape for
   `/compatible` and `/column/<col>/values/`. No new execution machinery: the 
endpoint enters
   the pipeline at `QueryContextFactory`, so caching, RLS, post-processing and 
row-limit
   clamping are unchanged, and `Explorable.get_query_result` already dispatches 
datasets to SQL
   and semantic views to the semantic-layer mapper — so there is no 
type-dispatch layer.
   
   Two things a reviewer may want to weigh in on. First, the 
resolve/validate/build/execute
   sequence moves into `superset/common/tabular_query.py` and the two MCP tools
   (`get_table`, `query_dataset`) are refactored onto it. They had already 
drifted — only
   `query_dataset` called `set_query_context_form_data`, so identical queries 
against a
   Jinja-templated virtual dataset returned different results through the two 
tools. Their
   existing unit tests pass unmodified, which is the evidence the refactor is
   behaviour-preserving. Second, `can_query on Datasource` is added to 
`READ_ONLY_PERMISSION`
   so Gamma receives it; `Datasource` is in `GAMMA_READ_ONLY_MODEL_VIEWS`, so 
otherwise
   `_is_alpha_only` withholds it. `can_get_column_values` and `can_compatible` 
on this same
   class appear to have that pre-existing problem — happy to fix separately.
   
   ### TESTING INSTRUCTIONS
   
   Unit tests: `pytest tests/unit_tests/datasource 
tests/unit_tests/common/test_tabular_query.py`,
   plus `pytest tests/unit_tests/mcp_service tests/unit_tests/semantic_layers` 
to confirm the
   refactor and the `ILIKE` change.
   
   Manually, against the examples data:
   
   ```bash
   TOKEN=$(curl -s -X POST http://127.0.0.1:8088/api/v1/security/login \
     -H 'Content-Type: application/json' \
     -d '{"username":"admin","password":"admin","provider":"db"}' \
     | python3 -c 'import sys,json;print(json.load(sys.stdin)["access_token"])')
   
   # saved metric + dimension
   curl -s -X POST 
http://127.0.0.1:8088/api/v1/datasource/table/<birth_names_id>/query \
     -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
     -d '{"metrics":["count"],"dimensions":["gender"]}'
   
   # ad-hoc metric (datasets only)
   curl -s -X POST .../query -H "Authorization: Bearer $TOKEN" -H 
'Content-Type: application/json' \
     -d 
'{"metrics":[{"expressionType":"SQL","sqlExpression":"SUM(num_boys)*1.0/SUM(num)","label":"boy_ratio"}],"dimensions":["gender"]}'
   
   # time grain — expect DATETIME(ds,'start of year') and a matching GROUP BY
   curl -s -X POST .../query -H "Authorization: Bearer $TOKEN" -H 
'Content-Type: application/json' \
     -d '{"metrics":["count"],"dimensions":["ds"],"time_grain":"P1Y","limit":3}'
   
   # Arrow opt-in — expect application/vnd.apache.arrow.stream
   curl -s -X POST .../query -H "Authorization: Bearer $TOKEN" -H 
'Content-Type: application/json' \
     -d '{"metrics":["count"],"dimensions":["gender"],"result_format":"arrow"}' 
-o out.arrow -D -
   
   # capabilities
   curl -s http://127.0.0.1:8088/api/v1/datasource/table/<id> -H 
"Authorization: Bearer $TOKEN"
   ```
   
   Expected failure modes: unknown metric → 400 naming it with a suggestion; 
`time_grain` with
   no temporal column → 400; ad-hoc metric against a `semantic_view` → 400 
naming the metric;
   `semantic_view` with `SEMANTIC_LAYERS` off → 404; unauthenticated → 401.
   
   ### ADDITIONAL INFORMATION
   
   - [x] Has associated issue: #37535
   - [ ] Required feature flags: none for `table`; `semantic_view` requires 
`SEMANTIC_LAYERS`
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in 
[SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [x] Introduces new feature or API
   - [ ] Removes existing feature or API


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