Skip to content

Commit a3107af

Browse files
authored
Merge pull request #9 from ajitpratap0/feature/phase-2-cte-implementation
feat: GoSQLX v1.2.0 - Phase 2: Complete CTE and Set Operations Implementation
2 parents 6032f49 + 4254e7c commit a3107af

File tree

5 files changed

+937
-24
lines changed

5 files changed

+937
-24
lines changed

doc.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Package gosqlx provides a high-performance SQL parsing SDK for Go with zero-copy tokenization
22
// and object pooling. It offers production-ready SQL lexing, parsing, and AST generation with
3-
// support for multiple SQL dialects.
3+
// support for multiple SQL dialects and advanced SQL features.
44
//
5-
// Features:
5+
// Core Features:
66
//
77
// - Zero-copy tokenization for optimal performance
88
// - Object pooling for 60-80% memory reduction
@@ -12,6 +12,15 @@
1212
// - Performance monitoring and metrics collection
1313
// - Visitor pattern support for AST traversal
1414
//
15+
// Advanced SQL Features (Phase 2 - v1.2.0+):
16+
//
17+
// - Common Table Expressions (CTEs) with WITH clause
18+
// - Recursive CTEs with WITH RECURSIVE support
19+
// - Multiple CTEs in single query
20+
// - Set operations: UNION, UNION ALL, EXCEPT, INTERSECT
21+
// - Complex query compositions and left-associative parsing
22+
// - ~70% SQL-92 standards compliance
23+
//
1524
// Basic Usage:
1625
//
1726
// import (
@@ -38,14 +47,40 @@
3847
// }
3948
// defer ast.ReleaseAST(astObj)
4049
//
50+
// Advanced Usage (Phase 2 Features):
51+
//
52+
// // Common Table Expression (CTE)
53+
// cteSQL := `WITH sales_summary AS (
54+
// SELECT region, SUM(amount) as total
55+
// FROM sales
56+
// GROUP BY region
57+
// ) SELECT region FROM sales_summary WHERE total > 1000`
58+
//
59+
// // Recursive CTE
60+
// recursiveSQL := `WITH RECURSIVE employee_tree AS (
61+
// SELECT employee_id, manager_id, name FROM employees WHERE manager_id IS NULL
62+
// UNION ALL
63+
// SELECT e.employee_id, e.manager_id, e.name
64+
// FROM employees e JOIN employee_tree et ON e.manager_id = et.employee_id
65+
// ) SELECT * FROM employee_tree`
66+
//
67+
// // Set Operations
68+
// unionSQL := `SELECT name FROM customers UNION SELECT name FROM suppliers`
69+
// exceptSQL := `SELECT product FROM inventory EXCEPT SELECT product FROM discontinued`
70+
// intersectSQL := `SELECT customer_id FROM orders INTERSECT SELECT customer_id FROM payments`
71+
//
4172
// Performance:
4273
//
4374
// GoSQLX achieves:
44-
// - 2.2M operations/second throughput
45-
// - 8M tokens/second processing speed
46-
// - <200ns latency for simple queries
47-
// - Linear scaling to 128 cores
48-
// - 60-80% memory reduction with pooling
75+
// - 946K+ sustained operations/second (30s load testing)
76+
// - 1.25M+ operations/second peak throughput (concurrent)
77+
// - 8M+ tokens/second processing speed
78+
// - <280ns latency for simple queries
79+
// - <1μs latency for complex queries with CTEs/set operations
80+
// - Linear scaling to 128+ cores
81+
// - 60-80% memory reduction with object pooling
82+
// - Zero memory leaks under extended load
83+
// - Race-free concurrent operation validated
4984
//
5085
// For more examples and detailed documentation, see:
5186
// https://github.com/ajitpratap0/GoSQLX

pkg/sql/ast/ast.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Package ast provides Abstract Syntax Tree (AST) node definitions for SQL statements.
2-
// It includes support for DDL and DML operations with object pooling for performance optimization.
2+
// It includes comprehensive support for DDL and DML operations, Common Table Expressions (CTEs),
3+
// and set operations, with object pooling for performance optimization.
4+
//
5+
// Phase 2 Features (v1.2.0+):
6+
// - WithClause and CommonTableExpr for CTE support
7+
// - SetOperation for UNION, EXCEPT, INTERSECT operations
8+
// - Recursive CTE support with proper AST representation
9+
// - Integration with all statement types
310
package ast
411

512
import "fmt"
@@ -22,11 +29,9 @@ type Expression interface {
2229
expressionNode()
2330
}
2431

25-
// WithClause represents a WITH clause in a SQL statement
26-
// TODO: PHASE 2 - Complete CTE implementation
27-
// Current Status: AST structures defined, parser integration incomplete
28-
// Missing: parseWithClause, parseCommonTableExpr, parseStatementWithSetOps functions
29-
// Priority: High (Phase 2 core feature)
32+
// WithClause represents a WITH clause in a SQL statement.
33+
// It supports both simple and recursive Common Table Expressions (CTEs).
34+
// Phase 2 Complete: Full parser integration with all statement types.
3035
type WithClause struct {
3136
Recursive bool
3237
CTEs []*CommonTableExpr
@@ -42,10 +47,9 @@ func (w WithClause) Children() []Node {
4247
return children
4348
}
4449

45-
// CommonTableExpr represents a single CTE in a WITH clause
46-
// TODO: PHASE 2 - Parser integration needed for CTE functionality
47-
// Current: AST structure complete, parser functions missing
48-
// Required: Integration with SELECT/INSERT/UPDATE/DELETE statement parsing
50+
// CommonTableExpr represents a single Common Table Expression in a WITH clause.
51+
// It supports optional column specifications and any statement type as the CTE query.
52+
// Phase 2 Complete: Full parser support with column specifications.
4953
type CommonTableExpr struct {
5054
Name string
5155
Columns []string
@@ -59,7 +63,9 @@ func (c CommonTableExpr) Children() []Node {
5963
return []Node{c.Statement}
6064
}
6165

62-
// SetOperation represents UNION, EXCEPT, INTERSECT operations
66+
// SetOperation represents set operations (UNION, EXCEPT, INTERSECT) between two statements.
67+
// It supports the ALL modifier (e.g., UNION ALL) and proper left-associative parsing.
68+
// Phase 2 Complete: Full parser support with left-associative precedence.
6369
type SetOperation struct {
6470
Left Statement
6571
Operator string // UNION, EXCEPT, INTERSECT

0 commit comments

Comments
 (0)