feat(sql): add CTE (WITH clause) support to SQL executor#353
Open
eddiethedean wants to merge 3 commits intomainfrom
Open
feat(sql): add CTE (WITH clause) support to SQL executor#353eddiethedean wants to merge 3 commits intomainfrom
eddiethedean wants to merge 3 commits intomainfrom
Conversation
added 3 commits
January 23, 2026 20:05
- Add WITH query type detection in parser (before UNION check) - Implement _parse_with_query() method with balanced parenthesis counting - Add _execute_with() method to execute CTEs and main query - Execute each CTE and store results in temp views - Replace CTE references in main query with temp view names - Clean up temp views after execution - Add comprehensive test suite with 11 test cases covering: - Simple CTE - Multiple CTEs - Chained CTEs (CTE referencing another CTE) - CTEs with aggregation, filtering, column selection - Case-insensitive WITH keyword - Whitespace handling - ORDER BY and LIMIT in main query - Parser detection verification Fixes #347
… compatibility - Replace direct SparkSession creation with spark fixture parameter - Remove manual spark.stop() calls (fixture handles cleanup) - Skip parser detection test in PySpark mode (parser not exposed) - All 10 functional tests now pass in both sparkless and PySpark modes
- Edge cases: empty results, all null values, single row, large datasets - Complex operations: DISTINCT, GROUP BY, HAVING, JOINs (inner/left/right/full outer) - Advanced features: UNION, UNION ALL, window functions, subqueries, CASE WHEN - Chained CTEs: 3-level and 4-level chains, multiple CTE references - SQL integration: CREATE TABLE AS SELECT, INSERT INTO ... SELECT - String/date/math functions: UPPER, LENGTH, YEAR, MONTH, SQRT, POWER, etc. - Error handling: non-existent tables, missing main query, circular references - Complex expressions: nested parentheses, arithmetic precedence, boolean logic - WHERE clauses: IN, LIKE, BETWEEN, IS NULL, IS NOT NULL, comparison operators - Array operations, null handling, coalesce, cast operations - All 60 functional tests pass in PySpark mode - 3 tests correctly skipped (PySpark-specific limitations: Hive support, parser access)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add support for Common Table Expressions (CTEs) in SQL queries, enabling queries like:
Changes
Parser (
sparkless/session/sql/parser.py)WITHdetection in_detect_query_type()(before UNION check, as CTE queries can contain UNION)WITHin_parse_components()_parse_with_query()method that parses CTE definitions using balanced parenthesis countingExecutor (
sparkless/session/sql/executor.py)WITHcase inexecute()method_execute_with()method that:Features Supported
WITH cte AS (...) SELECT * FROM cteWITH cte1 AS (...), cte2 AS (...) SELECT * FROM cte2WITHkeywordTest Plan
Implementation Details
The implementation uses balanced parenthesis counting to correctly parse CTE definitions, even when CTEs contain nested parentheses. Each CTE is executed independently and stored as a temporary view, which is then referenced in the main query. Temp views are automatically cleaned up after query execution.
Related
Fixes #347
This is an independent implementation, not based on PR #320.