Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
11 changes: 11 additions & 0 deletions parser/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Parser Development Notes

## Running Tests

Always run parser tests with a 5 second timeout:

```bash
go test ./parser/... -timeout 5s
```

The tests are very fast. If a test is timing out, it indicates a bug (likely an infinite loop in the parser).
49 changes: 14 additions & 35 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,19 @@ import (

// testMetadata holds optional metadata for a test case
type testMetadata struct {
Todo bool `json:"todo,omitempty"`
Source string `json:"source,omitempty"`
}

// astJSON represents the structure of ast.json from ClickHouse EXPLAIN AST
type astJSON struct {
Meta []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"meta"`
Data []struct {
Explain string `json:"explain"`
} `json:"data"`
Rows int `json:"rows"`
Statistics struct {
Elapsed float64 `json:"elapsed"`
RowsRead int `json:"rows_read"`
BytesRead int `json:"bytes_read"`
} `json:"statistics"`
Error bool `json:"error,omitempty"`
Todo bool `json:"todo,omitempty"`
Source string `json:"source,omitempty"`
Explain *bool `json:"explain,omitempty"`
Skip bool `json:"skip,omitempty"`
}

// TestParser tests the parser using test cases from the testdata directory.
// Each subdirectory in testdata represents a test case with:
// - query.sql: The SQL query to parse
// - ast.json: Expected AST from ClickHouse EXPLAIN AST
// - metadata.json (optional): Metadata including:
// - todo: true if the test is not yet expected to pass
// - explain: false to skip the test (e.g., when ClickHouse couldn't parse it)
// - skip: true to skip the test entirely (e.g., causes infinite loop)
func TestParser(t *testing.T) {
testdataDir := "testdata"

Expand All @@ -64,13 +49,13 @@ func TestParser(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

// Read the query (only first line, as ast.json was generated from first statement)
// Read the query (only first line)
queryPath := filepath.Join(testDir, "query.sql")
queryBytes, err := os.ReadFile(queryPath)
if err != nil {
t.Fatalf("Failed to read query.sql: %v", err)
}
// Get first line only (ast.json contains AST for first statement)
// Get first line only
lines := strings.SplitN(string(queryBytes), "\n", 2)
query := strings.TrimSpace(lines[0])

Expand All @@ -83,18 +68,14 @@ func TestParser(t *testing.T) {
}
}

// Read expected AST from ClickHouse
var expectedAST astJSON
astPath := filepath.Join(testDir, "ast.json")
if astBytes, err := os.ReadFile(astPath); err == nil {
if err := json.Unmarshal(astBytes, &expectedAST); err != nil {
t.Fatalf("Failed to parse ast.json: %v", err)
}
// Skip tests marked with skip: true
if metadata.Skip {
t.Skip("Skipping: skip is true in metadata")
}

// Skip tests where ClickHouse also couldn't parse the query
if expectedAST.Error {
t.Skipf("ClickHouse also failed to parse this query")
// Skip tests where explain is explicitly false (e.g., ClickHouse couldn't parse it)
if metadata.Explain != nil && !*metadata.Explain {
t.Skipf("Skipping: explain is false in metadata")
return
}

Expand Down Expand Up @@ -125,8 +106,6 @@ func TestParser(t *testing.T) {
}
t.Fatalf("JSON marshal error: %v\nQuery: %s", jsonErr, query)
}

// TODO: Compare parsed AST against expectedAST.Data
})
}
}
Expand Down
1 change: 0 additions & 1 deletion parser/testdata/00001_count_hits/ast.json

This file was deleted.

37 changes: 0 additions & 37 deletions parser/testdata/00001_select_1/ast.json

This file was deleted.

1 change: 0 additions & 1 deletion parser/testdata/00002_count_visits/ast.json

This file was deleted.

25 changes: 0 additions & 25 deletions parser/testdata/00002_system_numbers/ast.json

This file was deleted.

70 changes: 0 additions & 70 deletions parser/testdata/00003_reinterpret_as_string/ast.json

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion parser/testdata/00004_top_counters/ast.json

This file was deleted.

1 change: 0 additions & 1 deletion parser/testdata/00005_filtering/ast.json

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion parser/testdata/00006_agregates/ast.json

This file was deleted.

25 changes: 0 additions & 25 deletions parser/testdata/00006_extremes_and_subquery_from/ast.json

This file was deleted.

37 changes: 0 additions & 37 deletions parser/testdata/00007_array/ast.json

This file was deleted.

1 change: 0 additions & 1 deletion parser/testdata/00007_uniq/ast.json

This file was deleted.

43 changes: 0 additions & 43 deletions parser/testdata/00008_array_join/ast.json

This file was deleted.

1 change: 0 additions & 1 deletion parser/testdata/00008_uniq/ast.json

This file was deleted.

Loading