superset: [SIP-115] Parsing SQL expression block for virtual datasets into separate statements
[SIP-115] Proposal for Pre SQL block for virtual datasets
Motivation
Currently huge piece of SQL functionality couldn’t be used in virtual datasets due to the fact that dataset query is being put into from section in a pattern "from (“SQL query”) as virtual_table. This limits plenty of SQL dialects from using non SELECT statements in a dataset query including for example temp tables, variables or WITH statements. For example Postgre SQL does support WITH statements inside of a SELECT statement but NS SQL does not. Also it would be very useful to be able to actually create temporary tables in a dataset query but as far as I know there is no SQL dialect that supports CREATE statements inside of SELECT ones.
P.S. Actually the whole idea is to make it so that any possible query that can be completed in sql lab should be a valid query for a virtual dataset.
Proposed Change
The change that is being proposed is to parse SQL statement under the hood to distinguish actual final SELECT statement from all the preceding auxillary statements and put the auxillary part in the beginning of every query being built by viz plugins later.
So user should be able to use the following query as a virtual dataset query:
DROP TABLE IF EXISTS #tmp;
DECLARE @t1 TABLE (itmID INT, itmIDComp INT);
INSERT @t1 VALUES (1,10), (2,10);
DECLARE @t2 TABLE (itmID INT, itmIDComp INT);
INSERT @t2 VALUES (3,10), (4,10);
WITH vw AS
(
SELECT itmIDComp, itmID
FROM @t1
UNION ALL
SELECT itmIDComp, itmID
FROM @t2
)
, r AS
(
SELECT t.itmID AS itmIDComp
, NULL AS itmID
, CAST(0 AS BIGINT) AS N
, 1 AS Lvl
FROM (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) AS t (itmID)
UNION ALL
SELECT t.itmIDComp
, t.itmID
, ROW_NUMBER() OVER(PARTITION BY t.itmIDComp ORDER BY t.itmIDComp, t.itmID) AS N
, Lvl + 1
FROM r
JOIN vw AS t ON t.itmID = r.itmIDComp
)
CREATE TEMP TABLE #tmp
(Lvl INT, N INT);
INSERT INTO #tmp
SELECT Lvl, N FROM r;
----------------SPLIT POINT------------------
SELECT
Lvl,
MAX(N) as max_n
FROM #tmp
GROUP BY
Lvl;
And everything that is above the split point should be at least automaticcaly put at the beginning of every query generated by viz plugins.
New or Changed Public Interfaces
None should be needed.
New dependencies
To be filled.
Migration Plan and Compatibility
To be filled.
Rejected Alternatives
Rejected due to overcomplicating the UI while also making it less intuitive and userfriendly.
Proposed change is to include a new "Pre SQL" field that would contain SQL that would be put before all SELECT statements for every query on that dataset.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 4
- Comments: 29 (29 by maintainers)
We have implemented this SIP in our PR. https://github.com/apache/superset/pull/30518
Any solution that requires parsing SQL is bound to intricately fail at times. Given that another idea would be to have the user be explicit about what goes above the SELECT statement. You could imagine having two TEXTAREAs in the dataset editor, say one label
pre-queryand one labeledsub-query(we’d probably need a better explanation for this, but that’s one option).Another option would be to use hints and have the parse look for hints
Then even today you could have the
config.SQL_QUERY_MUTATORdo something like (this was GPT-generated, didn’t take time to validate)@supersetbot orglabel