Skip to content

Comments

feat: implement CTEs logic in sqlglot#33518

Merged
betodealmeida merged 1 commit intomasterfrom
sqlglot-cte
May 28, 2025
Merged

feat: implement CTEs logic in sqlglot#33518
betodealmeida merged 1 commit intomasterfrom
sqlglot-cte

Conversation

@betodealmeida
Copy link
Member

@betodealmeida betodealmeida commented May 19, 2025

SUMMARY

Part of #26786, stacked on:

Reimplements the allows_cte_in_subquery logic in sqlglot for SIP-117. This is used by MS SQL and Ocient when wrapping a query in the chart builder: if the original query has CTEs they need to be moved from the subquery to the top level of the query.

That is, this virtual dataset based off of MS SQL:

WITH foo AS (SELECT * FROM bar)
SELECT * FROM foo;

When used in a chart becomes:

WITH foo AS (SELECT * FROM bar),
     __cte AS (SELECT * FROM foo)
SELECT
  metric1(), metric2(), ...
  dim1, dim2, ...
  FROM __cte
GROUP BY dim1, dim2, ...

Note that in theory we could extract just the CTEs to the top-level, building this query:

WITH foo AS (SELECT * FROM bar)
SELECT
  metric1(), metric2(), ...
  dim1, dim2, ...
  FROM (SELECT * FROM foo)
GROUP BY dim1, dim2, ...

But since I don't have access to an MS SQL database I opted to preserve the existing behavior. If we ever decide to do that we could add a method split_ctes to SQLStatement which returns CTEs and the main query.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@dosubot dosubot bot added the change:backend Requires changing the backend label May 19, 2025
Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Functionality Missing CTE format validation ▹ view 🧠 Incorrect
Files scanned
File Path Reviewed
superset/sql/parse.py
superset/db_engine_specs/base.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

@betodealmeida betodealmeida changed the title chore: implement CTEs logic in sqlglot feat: implement CTEs logic in sqlglot May 21, 2025
Base automatically changed from remove-parse_sql to master May 27, 2025 22:03
@betodealmeida betodealmeida merged commit e205846 into master May 28, 2025
50 of 51 checks passed
@betodealmeida betodealmeida deleted the sqlglot-cte branch May 28, 2025 12:38
LevisNgigi pushed a commit to LevisNgigi/superset that referenced this pull request Jun 18, 2025
@github-actions github-actions bot added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 6.0.0 First shipped in 6.0.0 labels Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels change:backend Requires changing the backend preset-io size/L 🚢 6.0.0 First shipped in 6.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants