@@ -12,8 +12,10 @@ import (
1212 "os"
1313 "os/exec"
1414 "path/filepath"
15+ "runtime"
1516 "strconv"
1617 "strings"
18+ "sync"
1719 "syscall"
1820 "time"
1921)
@@ -40,6 +42,7 @@ func main() {
4042 dryRun := flag .Bool ("dry-run" , false , "Print statements without executing" )
4143 serverOnly := flag .Bool ("server" , false , "Only ensure server is running, don't regenerate" )
4244 stopServer := flag .Bool ("stop" , false , "Stop the ClickHouse server" )
45+ parallel := flag .Int ("j" , runtime .NumCPU (), "Number of parallel workers (default: number of CPUs)" )
4346 flag .Parse ()
4447
4548 // Handle stop command
@@ -85,19 +88,68 @@ func main() {
8588 os .Exit (1 )
8689 }
8790
88- var errors [] string
89- var processed , skipped int
91+ // Collect test directories
92+ var testDirs [] string
9093 for _ , entry := range entries {
9194 if ! entry .IsDir () {
9295 continue
9396 }
94- testDir := filepath .Join (testdataDir , entry .Name ())
95- if err := processTest (testDir , * dryRun ); err != nil {
96- if strings .Contains (err .Error (), "no statements found" ) {
97- skipped ++
98- continue
97+ testDirs = append (testDirs , filepath .Join (testdataDir , entry .Name ()))
98+ }
99+
100+ // Process tests in parallel
101+ type result struct {
102+ name string
103+ err error
104+ skipped bool
105+ }
106+
107+ numWorkers := * parallel
108+ if numWorkers < 1 {
109+ numWorkers = 1
110+ }
111+ fmt .Printf ("Processing %d tests with %d workers...\n " , len (testDirs ), numWorkers )
112+
113+ jobs := make (chan string , len (testDirs ))
114+ results := make (chan result , len (testDirs ))
115+
116+ // Start workers
117+ var wg sync.WaitGroup
118+ for w := 0 ; w < numWorkers ; w ++ {
119+ wg .Add (1 )
120+ go func () {
121+ defer wg .Done ()
122+ for testDir := range jobs {
123+ err := processTest (testDir , * dryRun )
124+ skipped := err != nil && strings .Contains (err .Error (), "no statements found" )
125+ if skipped {
126+ err = nil
127+ }
128+ results <- result {name : filepath .Base (testDir ), err : err , skipped : skipped }
99129 }
100- errors = append (errors , fmt .Sprintf ("%s: %v" , entry .Name (), err ))
130+ }()
131+ }
132+
133+ // Send jobs
134+ for _ , testDir := range testDirs {
135+ jobs <- testDir
136+ }
137+ close (jobs )
138+
139+ // Wait for workers and close results
140+ go func () {
141+ wg .Wait ()
142+ close (results )
143+ }()
144+
145+ // Collect results
146+ var errors []string
147+ var processed , skipped int
148+ for r := range results {
149+ if r .skipped {
150+ skipped ++
151+ } else if r .err != nil {
152+ errors = append (errors , fmt .Sprintf ("%s: %v" , r .name , r .err ))
101153 } else {
102154 processed ++
103155 }
@@ -524,21 +576,26 @@ func processTest(testDir string, dryRun bool) error {
524576 return fmt .Errorf ("no statements found" )
525577 }
526578
527- fmt .Printf ("Processing %s (%d statements)\n " , filepath .Base (testDir ), len (statements ))
579+ testName := filepath .Base (testDir )
580+
581+ if dryRun {
582+ fmt .Printf ("Processing %s (%d statements)\n " , testName , len (statements ))
583+ for i , stmt := range statements {
584+ fmt .Printf (" [%d] %s\n " , i + 1 , truncate (stmt , 80 ))
585+ }
586+ return nil
587+ }
528588
529589 // Generate version header comment
530590 versionHeader := fmt .Sprintf ("-- Generated by ClickHouse %s\n " , requiredVersion )
531591
592+ var stmtErrors []string
532593 for i , stmt := range statements {
533594 stmtNum := i + 1 // 1-indexed
534- if dryRun {
535- fmt .Printf (" [%d] %s\n " , stmtNum , truncate (stmt , 80 ))
536- continue
537- }
538595
539596 explain , err := explainAST (stmt )
540597 if err != nil {
541- fmt .Printf ( " [%d] ERROR : %v\n " , stmtNum , err )
598+ stmtErrors = append ( stmtErrors , fmt .Sprintf ( "stmt %d : %v" , stmtNum , err ) )
542599 // Skip statements that fail - they might be intentionally invalid
543600 continue
544601 }
@@ -556,7 +613,13 @@ func processTest(testDir string, dryRun bool) error {
556613 if err := os .WriteFile (outputPath , []byte (content ), 0644 ); err != nil {
557614 return fmt .Errorf ("writing %s: %w" , outputPath , err )
558615 }
559- fmt .Printf (" [%d] -> %s\n " , stmtNum , filepath .Base (outputPath ))
616+ }
617+
618+ // Print summary
619+ if len (stmtErrors ) > 0 {
620+ fmt .Printf ("%s: %d stmts, %d errors\n " , testName , len (statements ), len (stmtErrors ))
621+ } else {
622+ fmt .Printf ("%s: %d stmts OK\n " , testName , len (statements ))
560623 }
561624
562625 return nil
0 commit comments